diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 76620c4ff6..a31d363193 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -847,6 +847,7 @@ private function media_section() { $exclude_lazyload = $this->beacon->get_suggest( 'exclude_lazyload' ); $dimensions = $this->beacon->get_suggest( 'image_dimensions' ); $fonts = $this->beacon->get_suggest( 'host_fonts_locally' ); + $fonts_preload = $this->beacon->get_suggest( 'fonts_preload' ); $this->settings->add_page_section( 'media', @@ -936,12 +937,10 @@ private function media_section() { 'page' => 'media', ], 'font_optimization_section' => [ - 'title' => __( 'Fonts', 'rocket' ), - 'type' => 'fields_container', - // translators: %1$s = opening tag, %2$s = closing tag. - 'description' => sprintf( __( 'Download and serve fonts directly from your server. Reduces connections to external servers and minimizes font shifts. %1$sMore info%2$s', 'rocket' ), '', '' ), - 'help' => $fonts, - 'page' => 'media', + 'title' => __( 'Fonts', 'rocket' ), + 'type' => 'fields_container', + 'help' => $fonts, + 'page' => 'media', ], ] ); @@ -1041,9 +1040,21 @@ private function media_section() { 'default' => 0, 'sanitize_callback' => 'sanitize_checkbox', ], + 'auto_preload_fonts' => [ + 'type' => 'checkbox', + 'label' => __( 'Preload fonts', 'rocket' ), + // translators: %1$s = opening tag, %2$s = closing tag. + 'description' => sprintf( __( 'Preload above-the-fold fonts to enhance layout stability and optimize text-based LCP elements. %1$sMore info%2$s', 'rocket' ), '', '' ), + 'section' => 'font_optimization_section', + 'page' => 'media', + 'default' => 0, + 'sanitize_callback' => 'sanitize_checkbox', + ], 'host_fonts_locally' => [ 'type' => 'checkbox', 'label' => __( 'Self-host Google Fonts', 'rocket' ), + // translators: %1$s = opening tag, %2$s = closing tag. + 'description' => sprintf( __( 'Download and serve fonts directly from your server. Reduces connections to external servers and minimizes font shifts. %1$sMore info%2$s', 'rocket' ), '', '' ), 'section' => 'font_optimization_section', 'page' => 'media', 'default' => 0, @@ -2294,4 +2305,20 @@ public function display_update_notice() { ] ); } + + /** + * Enables the auto preload fonts option if the old preload fonts option is not empty. + * + * This function checks the value of the `rocket_preload_fonts` option. + * If it contains a non-empty value, it updates the `auto_preload_fonts` option to `true`. + * This is useful for ensuring that automatic font preloading is enabled based on legacy settings. + * + * @return void + */ + public function maybe_enable_auto_preload_fonts(): void { + $old_preload_fonts = $this->options->get( 'rocket_preload_fonts', [] ); + if ( ! empty( $old_preload_fonts ) ) { + $this->options->set( 'auto_preload_fonts', true ); + } + } } diff --git a/inc/Engine/Admin/Settings/Subscriber.php b/inc/Engine/Admin/Settings/Subscriber.php index b66fc12dd1..0f3e022a61 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -66,7 +66,10 @@ public static function get_subscribed_events() { 'rocket_after_settings_radio_options' => [ 'display_radio_options_sub_fields', 11 ], 'rocket_settings_tools_content' => 'display_mobile_cache_option', 'wp_ajax_rocket_enable_mobile_cache' => 'enable_mobile_cache', - 'wp_rocket_upgrade' => [ 'enable_separate_cache_files_mobile', 9, 2 ], + 'wp_rocket_upgrade' => [ + [ 'enable_separate_cache_files_mobile', 9, 2 ], + [ 'maybe_enable_auto_preload_fonts', 9 ], + ], 'admin_notices' => 'display_update_notice', ]; @@ -285,6 +288,19 @@ public function enable_separate_cache_files_mobile( $new_version, $old_version ) $this->page->enable_separate_cache_files_mobile(); } + /** + * Enables the auto preload fonts option if the old preload fonts option is not empty. + * + * This function checks the value of the `rocket_preload_fonts` option. + * If it contains a non-empty value, it updates the `auto_preload_fonts` option to `true`. + * This is useful for ensuring that automatic font preloading is enabled based on legacy settings. + * + * @return void + */ + public function maybe_enable_auto_preload_fonts(): void { + $this->page->maybe_enable_auto_preload_fonts(); + } + /** * Display the update notice. * diff --git a/inc/Engine/Media/Fonts/Admin/Settings.php b/inc/Engine/Media/Fonts/Admin/Settings.php index f51f2e2f51..715378ee74 100644 --- a/inc/Engine/Media/Fonts/Admin/Settings.php +++ b/inc/Engine/Media/Fonts/Admin/Settings.php @@ -9,12 +9,15 @@ class Settings { /** * Adds the host fonts locally option to WP Rocket options array * + * @since 3.19 adds auto preload fonts + * * @param array $options WP Rocket options array. * * @return array */ public function add_option( array $options ): array { $options['host_fonts_locally'] = 0; + $options['auto_preload_fonts'] = 0; return $options; } @@ -22,13 +25,16 @@ public function add_option( array $options ): array { /** * Sanitizes the option value when saving from the settings page * + * @since 3.19 adds auto preload fonts + * * @param array $input Array of sanitized values after being submitted by the form. * @param AdminSettings $settings Settings class instance. * * @return array */ public function sanitize_option_value( array $input, AdminSettings $settings ): array { - $input['host_fonts_locally'] = $settings->sanitize_checkbox( $input, 'host_fonts_locally' ); + $input['host_fonts_locally'] = $settings->sanitize_checkbox( $input, 'host_fonts_locally' ); + $intput['auto_preload_fonts'] = $settings->sanitize_checkbox( $input, 'auto_preload_fonts' ); return $input; } diff --git a/tests/Fixtures/inc/admin/rocketFirstInstall.php b/tests/Fixtures/inc/admin/rocketFirstInstall.php index 23a232d6a7..e932439f90 100644 --- a/tests/Fixtures/inc/admin/rocketFirstInstall.php +++ b/tests/Fixtures/inc/admin/rocketFirstInstall.php @@ -78,6 +78,7 @@ $integration[ 'image_dimensions' ] = 0; $integration[ 'exclude_lazyload' ] = []; $integration['host_fonts_locally'] = 0; +$integration['auto_preload_fonts'] = 0; return [ 'test_data' => [