/** * Helper functions font settings. * * @package Bsf_Custom_Fonts * @since 2.0.0 */ // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Return image media ID on basis of file URL. * * @param string $url file url. * @since 2.0.0 * @return string */ function bcf_get_media_image_id_by_url( $url ) { global $wpdb; $image = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ) ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder if ( ! empty( $image ) ) { return $image[0]; } return false; } /** * Create css for font-face * * @param array $font selected font from custom font list. * * @return array Font data. * @since 2.0.0 */ function bcf_prepare_backward_font_data( $font ) { $fonts = Bsf_Custom_Fonts_Taxonomy::get_links_by_name( $font ); $font_data = array(); $temp_arr = array(); $counter = 0; $font_wt_loop_count = 0; foreach ( $fonts as $font_key => $font_data_array ) { $font_data['font_name'] = $font_key; foreach ( $font_data_array as $key => $value ) { if ( 7 === $font_wt_loop_count ) { // 7 is all array font repeater keys count - weight, eot, woff, woff2, ttf, svg, otf. $font_wt_loop_count = 0; $counter++; } $temp_arr[ $counter ]['id'] = $counter + 1; $temp_arr[ $counter ]['font_style'] = 'normal'; if ( strpos( $key, 'display' ) !== false ) { $font_data['font_display'] = $value; continue; } if ( strpos( $key, 'fallback' ) !== false ) { $font_data['font_fallback'] = $value; continue; } if ( strpos( $key, 'weight' ) !== false ) { $temp_arr[ $counter ]['font_weight'] = $value; $font_wt_loop_count++; continue; } if ( strpos( $key, 'font_woff_2' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } if ( strpos( $key, 'font_woff-' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } if ( strpos( $key, 'font_ttf-' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } if ( strpos( $key, 'font_eot-' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } if ( strpos( $key, 'font_svg-' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } if ( strpos( $key, 'font_otf-' ) !== false ) { $font_wt_loop_count++; if ( $value ) { $temp_arr[ $counter ]['font_url'][] = esc_url( $value ); $temp_arr[ $counter ]['font_file'] = bcf_get_media_image_id_by_url( $value ); } continue; } } $final_font_arr = array(); if ( ! empty( $temp_arr ) ) { foreach ( $temp_arr as $index => $data ) { if ( ! empty( $data['font_weight'] ) ) { $final_font_arr[] = $data; } } } $font_data['variations'] = $final_font_arr; } return $font_data; } /** * Make font_file variation meta as array, whereas previously it was string type. * Case: 2.0.0 to 2.0.2 update. * * @param array $font_meta_data Meta font data. * * @return array Passed Font meta data. * @since 2.0.2 */ function bcf_make_font_url_meta_as_array( $font_meta_data ) { $variation_data = ! empty( $font_meta_data['variations'] ) ? $font_meta_data['variations'] : array(); if ( ! empty( $variation_data ) ) { foreach ( $variation_data as $index => $data ) { if ( is_string( $data['font_url'] ) ) { $font_meta_data['variations'][ $index ]['font_url'] = array( $data['font_url'] ); } elseif ( is_array( $data['font_url'] ) ) { $font_meta_data['variations'][ $index ]['font_url'] = $data['font_url']; } else { $font_meta_data['variations'][ $index ]['font_url'] = array(); } } } return $font_meta_data; } /** * Sanitize data recursively, eg: font-face data. * * @param array $data Data to sanitize. * * @return array * @since 2.0.0 */ function bcf_sanitize_text_field_recursive( $data ) { if ( is_array( $data ) ) { foreach ( $data as $key => $value ) { $data[ $key ] = bcf_sanitize_text_field_recursive( $value ); } return $data; } return sanitize_text_field( $data ); } /** * Get font source as per the asked type. * * @param string $font_type Font type. * @param string $font_url Font URL. * * @return string * @since 2.0.0 */ function bcf_get_font_src( $font_type, $font_url ) { $font_src = 'url(\'' . esc_url( $font_url ) . '\') '; switch ( $font_type ) { case 'woff': case 'woff2': case 'svg': $font_src .= 'format(\'' . $font_type . '\')'; break; case 'ttf': $font_src .= 'format(\'truetype\')'; break; case 'otf': $font_src .= 'format(\'OpenType\')'; break; case 'eot': $font_src = 'url(\'' . esc_url( $font_url ) . '?#iefix\') format(\'embedded-opentype\')'; break; } return $font_src; } /** * Get the file extension of font file. * * @param string $font_url Font URL. * * @return mixed * @since 2.0.0 */ function bcf_get_font_file_extension( $font_url ) { $file_last_chunk = substr( $font_url, -6 ); if ( strpos( $file_last_chunk, '.woff2' ) !== false ) { return 'woff2'; } if ( strpos( $file_last_chunk, '.woff' ) !== false ) { return 'woff'; } if ( strpos( $file_last_chunk, '.ttf' ) !== false ) { return 'ttf'; } if ( strpos( $file_last_chunk, '.eot' ) !== false ) { return 'eot'; } if ( strpos( $file_last_chunk, '.svg' ) !== false ) { return 'svg'; } if ( strpos( $file_last_chunk, '.otf' ) !== false ) { return 'otf'; } return false; } /** * Based on the font post data create font-face CSS. * * @param string $font_family Font family name. * @param array $font_data Font data. * @param array $variation_data Font variation data. * * @return string * @since 2.0.0 */ function bcf_prepare_gfont_face_css( $font_family, $font_data, $variation_data ) { $src = array(); $font_urls = $variation_data['font_url']; foreach ( $font_urls as $url ) { $file_extension = bcf_get_font_file_extension( $url ); if ( ! $file_extension ) { return ''; } foreach ( array( 'eot', 'woff2', 'woff', 'ttf', 'svg', 'otf' ) as $type ) { if ( empty( $url ) || $file_extension !== $type ) { continue; } $src[] = bcf_get_font_src( $type, $url ); } } $font_face = '@font-face {' . PHP_EOL; $font_face .= "\tfont-family: '" . $font_family . "';" . PHP_EOL; $font_face .= ! empty( $variation_data['font_style'] ) ? "\tfont-style: " . $variation_data['font_style'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $variation_data['font_weight'] ) ? "\tfont-weight: " . $variation_data['font_weight'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $font_data['font_display'] ) ? "\tfont-display: " . $font_data['font_display'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $font_data['font_fallback'] ) ? "\tfont-fallback: " . $font_data['font_fallback'] . ';' . PHP_EOL : ''; if ( empty( $font_urls ) ) { $font_face .= '}'; return $font_face; } $font_face .= "\tsrc: " . implode( ',' . PHP_EOL . "\t\t", $src ) . ';' . PHP_EOL . '}'; return $font_face; } /** * Based on the post-meta create font-face CSS. * * @param string $font_family Font family name. * @param array $font_data Font data. * @param array $variation_data Font variation data. * * @return string * @since 2.0.0 */ function bcf_prepare_lfont_face_css( $font_family, $font_data, $variation_data ) { $src = array(); $font_face = '@font-face {' . PHP_EOL; $font_face .= "\tfont-family: '" . $font_family . "';" . PHP_EOL; $font_face .= ! empty( $variation_data['font_style'] ) ? "\tfont-style: " . $variation_data['font_style'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $variation_data['font_weight'] ) ? "\tfont-weight: " . $variation_data['font_weight'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $font_data['font_display'] ) ? "\tfont-display: " . $font_data['font_display'] . ';' . PHP_EOL : ''; $font_face .= ! empty( $font_data['font_fallback'] ) ? "\tfont-fallback: " . $font_data['font_fallback'] . ';' . PHP_EOL : ''; $variation_urls = $variation_data['font_url']; if ( empty( $variation_urls ) ) { $font_face .= '}'; return $font_face; } if ( is_array( $variation_urls ) && ! empty( $variation_urls ) ) { foreach ( $variation_urls as $url ) { $file_extension = bcf_get_font_file_extension( $url ); if ( ! $file_extension ) { return ''; } foreach ( array( 'eot', 'woff2', 'woff', 'ttf', 'svg', 'otf' ) as $type ) { if ( empty( $url ) || $file_extension !== $type ) { continue; } $src[] = bcf_get_font_src( $type, $url ); } } } else { $file_extension = bcf_get_font_file_extension( $variation_urls ); if ( ! $file_extension ) { return ''; } foreach ( array( 'eot', 'woff2', 'woff', 'ttf', 'svg', 'otf' ) as $type ) { if ( empty( $variation_urls ) || $file_extension !== $type ) { continue; } $src[] = bcf_get_font_src( $type, $variation_urls ); } } $font_face .= "\tsrc: " . implode( ',' . PHP_EOL . "\t\t", $src ) . ';' . PHP_EOL . '}'; return $font_face; } /** * Retrieve font-face CSS for assigned $post_id. * * @param int $post_id Post ID. * @param array $font_data Font data. * @param bool $force_update Force update. * @param bool $is_google_font Is Google font. * * @since 2.0.0 * @return string css */ function bcf_get_font_face_css( $post_id, $font_data = array(), $force_update = false, $is_google_font = false ) { $saved = get_post_meta( $post_id, 'fonts-face', true ); if ( ! empty( $saved ) && false === $force_update ) { return $saved; } $font_face = ''; if ( true === $is_google_font ) { $font_family = ! empty( $font_data['font_name'] ) ? $font_data['font_name'] : ''; } else { $font_family = get_the_title( $post_id ); } if ( empty( $font_data ) ) { $font_data = get_post_meta( $post_id, 'fonts-data', true ); } if ( empty( $font_data ) || ( ! empty( $font_data ) && empty( $font_data['variations'] ) ) ) { return ''; } foreach ( $font_data['variations'] as $key => $variation_data ) { if ( true === $is_google_font ) { $font_face .= bcf_prepare_gfont_face_css( $font_family, $font_data, $variation_data ) . PHP_EOL; } else { $font_face .= bcf_prepare_lfont_face_css( $font_family, $font_data, $variation_data ) . PHP_EOL; } } return $font_face; } if ( ! defined( 'ABSPATH' ) ) exit; function uaf_clear_plugins_cache(){ if (function_exists('sg_cachepress_purge_cache')) {sg_cachepress_purge_cache();} // FOR SG OPTIMIZER if (function_exists('w3tc_flush_all')){w3tc_flush_all();} // FOR W3 TOTAL CACHE if (function_exists('wpfc_clear_all_cache')){wpfc_clear_all_cache(true);} // FOR WP Fastest Cache if (function_exists('wp_cache_clear_cache')){wp_cache_clear_cache();} // FOR WP Super Cache if (function_exists('rocket_clean_domain')){rocket_clean_domain();} // FOR WP ROCKET if (class_exists( 'WpeCommon' ) && method_exists( 'WpeCommon', 'purge_memcached' ) ) { // FOR WP ENGINE WpeCommon::purge_memcached(); WpeCommon::purge_varnish_cache(); } if (class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all') ) { // FOR LITE SPEED LiteSpeed_Cache_API::purge_all(); } if (class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_total_cache') ) { // FOR CACHE ENABLER Cache_Enabler::clear_total_cache(); } if (class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge','purgeAll') ) { // FOR PAGELY //PagelyCachePurge::purgeAll(); REMOVED AFTER THIS TICKET https://wordpress.org/support/topic/version-6-1-1-causes-fatal-errors-on-pagely-servers/ $uaf_purger = new PagelyCachePurge(); $uaf_purger->purgeAll(); } if (class_exists('autoptimizeCache') && method_exists( 'autoptimizeCache', 'clearall') ) { // FOR AUTOOPTIMIZSE autoptimizeCache::clearall(); } if (class_exists('comet_cache') && method_exists('comet_cache', 'clear') ) { // FOR COMET CACHE comet_cache::clear(); } if (class_exists('\Hummingbird\WP_Hummingbird') && method_exists('\Hummingbird\WP_Hummingbird', 'flush_cache')) { // FOR HUMINBIRD CACHE \Hummingbird\WP_Hummingbird::flush_cache(); } if (class_exists( '\Kinsta\Cache' ) && !empty( $kinsta_cache ) ) { // FOR KINSTA CACHE $kinsta_cache->kinsta_cache_purge->purge_complete_caches(); } if (class_exists( '\WPaaS\Cache' ) ) { // FOR GODADDY Cache if (function_exists('ccfm_godaddy_purge')){ccfm_godaddy_purge();} } if ( class_exists( 'Breeze_Admin' ) ) { // FOR BREEZE (Cloudways) do_action('breeze_clear_all_cache'); } } function uaf_check_if_cdn_plugin_in_use(){ // NOT BEING USED NOW. //if (function_exists('sg_cachepress_purge_cache')) {return true;} // IS SG OPTIMIZER ENABLED. //return false; } if ( ! defined( 'ABSPATH' ) ) exit; // DEFAULT WORDPRESS EDITOR $uaf_disbale_editor_font_list_value = get_option('uaf_disbale_editor_font_list'); if ($uaf_disbale_editor_font_list_value != 1): add_filter('mce_buttons_2', 'wp_editor_fontsize_filter'); add_filter('tiny_mce_before_init', 'uaf_mce_before_init' ); endif; function uaf_mce_before_init( $init_array ) { $theme_advanced_fonts = ''; $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $theme_advanced_fonts .= ucfirst(str_replace('_',' ', $fontName)) .'='.$fontName.';'; endforeach; endif; $init_array['font_formats'] = $theme_advanced_fonts.'Andale Mono=Andale Mono, Times;Arial=Arial, Helvetica, sans-serif;Arial Black=Arial Black, Avant Garde;Book Antiqua=Book Antiqua, Palatino;Comic Sans MS=Comic Sans MS, sans-serif;Courier New=Courier New, Courier;Georgia=Georgia, Palatino;Helvetica=Helvetica;Impact=Impact, Chicago;Symbol=Symbol;Tahoma=Tahoma, Arial, Helvetica, sans-serif;Terminal=Terminal, Monaco;Times New Roman=Times New Roman, Times;Trebuchet MS=Trebuchet MS, Geneva;Verdana=Verdana, Geneva;Webdings=Webdings;Wingdings=Wingdings'; return $init_array; } function wp_editor_fontsize_filter( $options ) { array_unshift( $options, 'fontsizeselect'); array_unshift( $options, 'fontselect'); return $options; } // DIVI CUSTOMIZER AND BUILDER (Tested with 4.0.9 and 4.0.9) add_filter('et_websafe_fonts', 'uaf_send_fonts_divi_list',10,2); function uaf_send_fonts_divi_list($fonts){ $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $variationStyles = array(); foreach ($fontData as $fontVariationKey => $fontVariationData){ if (array_key_exists('font_weight',$fontVariationData)){ $variationStyles[] = $fontVariationData['font_weight']; } else { $variationStyles[] = '400'; } } $fonts_uaf[$fontName] = array( 'styles' => join(',',$variationStyles), 'character_set' => 'cyrillic,greek,latin', 'type' => 'serif' ); endforeach; endif; return array_merge($fonts_uaf,$fonts); } // SITE ORIGIN BUILDER add_filter('siteorigin_widgets_font_families', 'uaf_send_fonts_siteorigin_list',10,2); function uaf_send_fonts_siteorigin_list($fonts){ $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $fonts_uaf[$fontName] = $fontName; endforeach; endif; return array_merge($fonts_uaf,$fonts); } // REDUX Framework if (class_exists( 'Redux' ) ) { $reduxUafObject = new Redux; $reduxArgs = $reduxUafObject::$args; $reduxOptName = array_key_first($reduxArgs); add_filter('redux/'.$reduxOptName.'/field/typography/custom_fonts', 'uaf_send_fonts_redux_list' ); } function uaf_send_fonts_redux_list( $custom_fonts ) { $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array('Use Any Fonts' => array()); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $fonts_uaf['Use Any Fonts'][$fontName] = $fontName; endforeach; endif; return $fonts_uaf; } // X Theme add_filter('x_fonts_data', 'uaf_send_fonts_x_theme_list',10,2); function uaf_send_fonts_x_theme_list($fonts){ $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $fonts_uaf[$fontName] = array( 'source' => 'Use Any Font', 'family' => $fontName, 'stack' => '"'.$fontName.'"', 'weights' => array( '400' ) ); endforeach; endif; return array_merge($fonts_uaf,$fonts); } // elementor function uaf_send_fonts_elementor_list( $controls_registry ) { $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array('Use Any Fonts' => array()); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $fonts_uaf[$fontName] = 'system'; endforeach; endif; $fonts = $controls_registry->get_control( 'font' )->get_settings( 'options' ); $new_fonts = array_merge($fonts_uaf, $fonts ); $controls_registry->get_control( 'font' )->set_settings( 'options', $new_fonts ); } add_action( 'elementor/controls/controls_registered', 'uaf_send_fonts_elementor_list', 10, 1 ); // Beaver Builder and Themes (Tested with 2.3.0.1 ) add_filter('fl_theme_system_fonts', 'uaf_send_fonts_beaver_builder_list',10,2); add_filter('fl_builder_font_families_system', 'uaf_send_fonts_beaver_builder_list',10,2); function uaf_send_fonts_beaver_builder_list($fonts){ $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $variationStyles = array(); foreach ($fontData as $fontVariationKey => $fontVariationData){ if (array_key_exists('font_weight',$fontVariationData)){ $variationStyles[] = $fontVariationData['font_weight']; } else { $variationStyles[] = '400'; } } $fonts_uaf[$fontName] = array( 'fallback' => 'Verdana, Arial, sans-serif', 'weights' => $variationStyles ); endforeach; endif; return array_merge($fonts_uaf,$fonts); } // Themify Builder add_filter('themify_get_web_safe_font_list', 'uaf_send_fonts_themify_builder_list',10,2); function uaf_send_fonts_themify_builder_list($fonts){ $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): foreach ($fontsData as $fontName=>$fontData): $fonts_uaf[] = array( 'value' => $fontName, 'name' => $fontName ); endforeach; endif; return array_merge($fonts_uaf,$fonts); } // GENERATE PRESS Tested With Version: 2.4.1 add_filter( 'generate_typography_default_fonts', function( $fonts ) { $fonts_uaf = uaf_get_font_families(); return array_merge($fonts_uaf,$fonts); }); // ASTRA THEME ver 2.2.1 add_action( 'astra_customizer_font_list', 'uaf_astra_customizer_font_list'); function uaf_astra_customizer_font_list( $value ) { $fontsData = uaf_group_fontdata_by_fontname(uaf_get_uploaded_font_data()); $fonts_uaf = array(); if (!empty($fontsData)): echo esc_html('