From 8742783ef463636c1af226f105c5bdec5b5339b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 02:55:10 +0100 Subject: [PATCH 1/8] Move description --- inc/Engine/Admin/Settings/Page.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 76620c4ff6..2b522ebfdb 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -936,12 +936,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', ], ] ); @@ -1044,6 +1042,8 @@ private function media_section() { '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, From 7abfead56e9a3d10369ec54cf9359130736636e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 02:55:24 +0100 Subject: [PATCH 2/8] Adds new checkbox --- inc/Engine/Admin/Settings/Page.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 2b522ebfdb..6101eb6dda 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -1039,6 +1039,14 @@ private function media_section() { 'default' => 0, 'sanitize_callback' => 'sanitize_checkbox', ], + 'auto_preload_fonts' => [ + 'type' => 'checkbox', + 'label' => __( 'Preload Fonts', 'rocket' ), + 'section' => 'font_optimization_section', + 'page' => 'media', + 'default' => 0, + 'sanitize_callback' => 'sanitize_checkbox', + ], 'host_fonts_locally' => [ 'type' => 'checkbox', 'label' => __( 'Self-host Google Fonts', 'rocket' ), From 5ebfd352701b10e04630257f3c81362263283fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 02:57:10 +0100 Subject: [PATCH 3/8] Adds logic on upgrade --- inc/Engine/Admin/Settings/Subscriber.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Admin/Settings/Subscriber.php b/inc/Engine/Admin/Settings/Subscriber.php index b66fc12dd1..26a0736212 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,22 @@ 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 { + $old_preload_fonts = get_option( 'rocket_preload_fonts', [] ); + if ( ! empty( $old_preload_fonts ) ) { + update_option( 'auto_preload_fonts', true ); + } + } + /** * Display the update notice. * From bfc0acc16e084f7a69b73bde143480c155b9926d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 02:57:36 +0100 Subject: [PATCH 4/8] Fix linter --- inc/Engine/Admin/Settings/Subscriber.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Admin/Settings/Subscriber.php b/inc/Engine/Admin/Settings/Subscriber.php index 26a0736212..47a0c2e2ab 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -291,8 +291,8 @@ public function enable_separate_cache_files_mobile( $new_version, $old_version ) /** * 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 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 From 87ad74de3365dd81528e3c4831472b1dd3cbe386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 03:03:57 +0100 Subject: [PATCH 5/8] Enable auto preload fonts based on legacy settings --- inc/Engine/Admin/Settings/Page.php | 16 ++++++++++++++++ inc/Engine/Admin/Settings/Subscriber.php | 5 +---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 6101eb6dda..58b932775c 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -2302,4 +2302,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 47a0c2e2ab..0f3e022a61 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -298,10 +298,7 @@ public function enable_separate_cache_files_mobile( $new_version, $old_version ) * @return void */ public function maybe_enable_auto_preload_fonts(): void { - $old_preload_fonts = get_option( 'rocket_preload_fonts', [] ); - if ( ! empty( $old_preload_fonts ) ) { - update_option( 'auto_preload_fonts', true ); - } + $this->page->maybe_enable_auto_preload_fonts(); } /** From dc7ca66533ca09f2bc4e806a703fc916adc1b5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 03:08:00 +0100 Subject: [PATCH 6/8] Add auto preload fonts option to settings --- inc/Engine/Media/Fonts/Admin/Settings.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; } From d27d154d5a19d7ee60972eb634f5cf1c185b24ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 05:16:30 +0100 Subject: [PATCH 7/8] Fix tests --- tests/Fixtures/inc/admin/rocketFirstInstall.php | 1 + 1 file changed, 1 insertion(+) 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' => [ From 09142460de757366d571eaf643588bea73669aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 11:53:26 +0100 Subject: [PATCH 8/8] Add description + more info link --- inc/Engine/Admin/Settings/Page.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 58b932775c..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', @@ -1041,7 +1042,9 @@ private function media_section() { ], 'auto_preload_fonts' => [ 'type' => 'checkbox', - 'label' => __( 'Preload Fonts', 'rocket' ), + '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,