'host_link', 'desc' => __( 'Download Google Fonts from <link> and host them locally.', 'easyfonts' ) ] ); add_settings_field( 'host_import', __( 'Process @import inline style', 'easyfonts' ), [ $this, 'checkbox_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'host_import', 'desc' => __( 'Process @import rules from inline <style> tags.', 'easyfonts' ) ] ); add_settings_field( 'process_fontface', __( 'Process @font-face statement', 'easyfonts' ), [ $this, 'checkbox_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'process_fontface', 'desc' => __( 'Process @font-face from inline <style> tags.', 'easyfonts' ) ] ); add_settings_field( 'remove_hints', __( 'Remove Resource Hints', 'easyfonts' ), [ $this, 'checkbox_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'remove_hints', 'desc' => __( 'Remove resource hints like preconnect, prefetch.', 'easyfonts' ) ] ); add_settings_field( 'remove_scripts', __( 'Remove webfont.js fonts', 'easyfonts' ), [ $this, 'checkbox_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'remove_scripts', 'desc' => __( 'Remove Google Fonts loading from webfont.js inline scripts.', 'easyfonts' ) ] ); add_settings_field( 'combine_fonts', __( 'Combine Font Stylesheets', 'easyfonts' ), [ $this, 'checkbox_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'combine_fonts', 'desc' => __( 'Merge all locally hosted font CSS files into a single file and remove duplicates.', 'easyfonts' ) ] ); add_settings_field( 'font_display', __( 'Font Display', 'easyfonts' ), [ $this, 'select_field' ], 'easyfonts', 'easyfonts_main', [ 'label_for' => 'font_display', 'desc' => __( 'Set font-display on all @font-face declarations for better loading control.', 'easyfonts' ) ] ); } public function checkbox_field( $args ) { $options = get_option( 'easyfonts_options', [] ); $id = $args['label_for']; $checked = ! empty( $options[ $id ] ) ? 'checked' : ''; echo '
'; if ( ! empty( $args['desc'] ) ) { echo '

' . wp_kses_post( $args['desc'] ) . '

'; } } public function select_field( $args ) { $options = get_option( 'easyfonts_options', [] ); $id = $args['label_for']; $current = ! empty( $options[ $id ] ) ? $options[ $id ] : 'none'; $choices = [ 'none' => __( 'Disabled', 'easyfonts' ), 'auto' => 'auto', 'block' => 'block', 'swap' => 'swap', 'fallback' => 'fallback', 'optional' => 'optional', ]; echo ''; if ( ! empty( $args['desc'] ) ) { echo '

' . wp_kses_post( $args['desc'] ) . '

'; } } public function options_page_html() { if ( ! current_user_can( 'manage_options' ) ) { return; } $this->handle_actions(); $options = get_option( 'easyfonts_options', [] ); ?>

Support Us with 5⭐ Rating', 'easyfonts' ), 'https://wordpress.org/support/plugin/easyfonts/reviews/#new-post' ); ?>

'easyfonts_submit' ) ); ?>
list_styles(); } ?>

Is Your Hosting Slowing You Down?

Your hosting plays a key role in website speed and user experience. Analyze your server’s performance now, and explore better solutions if needed.

clear_font_cache(); } if ( isset( $_POST['easyfonts_preload'] ) ) { $this->preload_fonts(); } } private function clear_font_cache() { $dir = EASYFONTS_UPLOAD_DIR; if ( is_dir( $dir ) ) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ); foreach ( $files as $fileinfo ) { $todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' ); $todo( $fileinfo->getRealPath() ); } rmdir( $dir ); } add_settings_error( 'easyfonts_messages', 'easyfonts_message', __( 'The fonts have been removed.', 'easyfonts' ), 'success' ); } private function preload_fonts() { $current_user = wp_get_current_user(); if ( ! user_can( $current_user, 'manage_options' ) ) { return; } $tokens = $current_user->get_session_tokens(); $options = [ 'cookies' => $tokens, 'headers' => [ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', ], ]; $home_url = home_url( '?easyfonts_preload=1' ); $response = wp_remote_get( $home_url, $options ); if ( is_wp_error( $response ) ) { add_settings_error( 'easyfonts_messages', 'easyfonts_message', __( 'Preload failed: ', 'easyfonts' ) . $response->get_error_message(), 'error' ); } else { add_settings_error( 'easyfonts_messages', 'easyfonts_message', __( 'The fonts have been preloaded.', 'easyfonts' ), 'success' ); } } private function list_styles() { $options = get_option( 'easyfonts_options', [] ); $combine_enabled = ! empty( $options['combine_fonts'] ); $css_files = []; $style_data = []; if ( ! is_dir( EASYFONTS_UPLOAD_DIR ) ) { echo '

' . esc_html__( 'Fonts styles are not found. Preload the font first or visit the homepage.', 'easyfonts' ) . '

'; return; } $dir = new DirectoryIterator( EASYFONTS_UPLOAD_DIR ); foreach ( $dir as $file ) { if ( ! $file->isFile() || $file->getExtension() !== 'css' ) { continue; } $fname = $file->getFilename(); // When combine is enabled, only show combined file if ( $combine_enabled && strpos( $fname, '_combined.css' ) === false ) { continue; } // When combine is disabled, skip combined files if ( ! $combine_enabled && strpos( $fname, '_combined.css' ) !== false ) { continue; } $css_files[] = $fname; } if ( empty( $css_files ) ) { echo '

' . esc_html__( 'Fonts styles are not found. Preload the font first or visit the homepage.', 'easyfonts' ) . '

'; return; } foreach ( $css_files as $file_name ) { if ( strpos( $file_name, '..' ) !== false ) { continue; } $file_path = EASYFONTS_UPLOAD_DIR . '/' . $file_name; if ( ! is_readable( $file_path ) ) { continue; } $file_content = file_get_contents( $file_path ); $font_family = []; $variant_italic = []; $variant_normal = []; preg_match_all( '/@font-face\s*{[^}]+}/', $file_content, $matches ); if ( empty( $matches[0] ) ) { continue; } foreach ( $matches[0] as $font_face ) { if ( preg_match( '/font-family:\s*[\'"]?([^;\'"]+)[\'"]?;/', $font_face, $family_match ) ) { $font_family[] = trim( $family_match[1] ); } if ( preg_match( '/font-style:\s*([^;]+);/', $font_face, $style_match ) ) { $style = trim( $style_match[1] ); if ( preg_match( '/font-weight:\s*([^;]+);/', $font_face, $weight_match ) ) { $weight = trim( $weight_match[1] ); if ( $style === 'italic' ) { $variant_italic[] = $weight; } elseif ( $style === 'normal' ) { $variant_normal[] = $weight; } } } } $font_family = array_unique( $font_family ); $variant_italic = array_unique( $variant_italic ); $variant_normal = array_unique( $variant_normal ); $style_data[] = [ 'file_url' => esc_url( EASYFONTS_UPLOAD_URL . '/' . $file_name ), 'font_families' => esc_html( implode( ', ', $font_family ) ), 'variant' => esc_html( 'italic: ' . implode( ', ', $variant_italic ) . ' | normal: ' . implode( ', ', $variant_normal ) ), ]; } if ( empty( $style_data ) ) { return; } echo ''; foreach ( $style_data as $style ) { echo ''; } echo '
' . esc_html__( 'Hosted Fonts CSS URL', 'easyfonts' ) . '' . esc_html__( 'Font Families', 'easyfonts' ) . '' . esc_html__( 'Variants', 'easyfonts' ) . '
' . $style['file_url'] . '' . $style['font_families'] . '' . $style['variant'] . '
'; } public function enqueue_speed_check_assets( $hook ) { if ( 'settings_page_easyfonts' !== $hook ) { return; } wp_enqueue_script( 'jquery' ); // Ensure jQuery. // Localize PHP values into JS safely wp_localize_script( 'jquery', 'speedCheckData', array( 'apiKey' => 'AIzaSyA3CHybGfa0lOaXRwjc42lJDxAZsG3Rwos', 'pageUrl' => home_url(), 'phpVersion' => phpversion(), 'affiliateLink' => 'https://unified.cloudways.com/signup?id=662383&coupon=EASYWPSTUFF', ) ); $script = <<

⏳Fetching Performance Data…... Please wait.

'); $.when( $.get(mobileApiUrl), $.get(desktopApiUrl) ).done(function(mobileResponse, desktopResponse) { var mobileScore = Math.round(mobileResponse[0].lighthouseResult.categories.performance.score * 100); var desktopScore = Math.round(desktopResponse[0].lighthouseResult.categories.performance.score * 100); var mobileSpeed = (mobileResponse[0].lighthouseResult.audits['speed-index'].numericValue / 1000).toFixed(2); var desktopSpeed = (desktopResponse[0].lighthouseResult.audits['speed-index'].numericValue / 1000).toFixed(2); var resultHtml = '
📱 Mobile Score: ' + mobileScore + '
'; resultHtml += '
💻 Desktop Score: ' + desktopScore + '
'; resultHtml += '
📱 Mobile Speed: ' + mobileSpeed + ' sec
'; resultHtml += '
💻 Desktop Speed: ' + desktopSpeed + ' sec
'; resultHtml += '
🐘 PHP Version: ' + phpVersion + '
'; var shouldRecommend = false; if (mobileScore < 50 || desktopScore < 50) { shouldRecommend = true; resultHtml += '
⚠️ Low PageSpeed score detected!
'; } if (mobileSpeed > 3 || desktopSpeed > 2) { shouldRecommend = true; resultHtml += '
⚠️ Your server is slow!
'; } if (parseFloat(phpVersion) < 7.4) { shouldRecommend = true; resultHtml += '
⚠️ Your PHP version is outdated!
'; } if (shouldRecommend) { resultHtml += '
Switch to Faster Hosting
'; } else { resultHtml += '
✅ Your server is well-optimized!
'; } $('#speed-results').html(resultHtml); $('#check-speed-btn').hide(); }).fail(function() { $('#speed-results').html('

⚠️ Error fetching speed test results.

'); }); }); }); JS; wp_add_inline_script( 'jquery', $script ); } } new EasyFonts_Options();