diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index a31d363193..23081d2d64 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -2305,20 +2305,4 @@ 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 0f3e022a61..b66fc12dd1 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -66,10 +66,7 @@ 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 ], - [ 'maybe_enable_auto_preload_fonts', 9 ], - ], + 'wp_rocket_upgrade' => [ 'enable_separate_cache_files_mobile', 9, 2 ], 'admin_notices' => 'display_update_notice', ]; @@ -288,19 +285,6 @@ 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/PreloadFonts/Admin/Settings.php b/inc/Engine/Media/PreloadFonts/Admin/Settings.php new file mode 100644 index 0000000000..1a63c0e191 --- /dev/null +++ b/inc/Engine/Media/PreloadFonts/Admin/Settings.php @@ -0,0 +1,49 @@ +options = $option_data; + $this->options_api = $options_api; + } + + /** + * 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 { + if ( ! empty( $this->options->get( 'preload_fonts', [] ) ) ) { + $this->options->set( 'auto_preload_fonts', true ); + $this->options_api->set( 'auto_preload_fonts', true ); + } + } +} diff --git a/inc/Engine/Media/PreloadFonts/Admin/Subscriber.php b/inc/Engine/Media/PreloadFonts/Admin/Subscriber.php new file mode 100644 index 0000000000..ca5590faf5 --- /dev/null +++ b/inc/Engine/Media/PreloadFonts/Admin/Subscriber.php @@ -0,0 +1,58 @@ +controller = $controller; + } + + /** + * Returns an array of events this subscriber listens to + * + * @return array[] + */ + public static function get_subscribed_events(): array { + return [ + 'wp_rocket_upgrade' => [ 'maybe_enable_auto_preload_fonts', 9, 2 ], + ]; + } + + /** + * 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. + * + * @param string $new_version New plugin version. + * @param string $old_version Previous plugin version. + * + * @return void + */ + public function maybe_enable_auto_preload_fonts( $new_version, $old_version ): void { + if ( version_compare( $old_version, '3.19', '<' ) ) { + return; + } + $this->controller->maybe_enable_auto_preload_fonts(); + } +} diff --git a/inc/Engine/Media/PreloadFonts/ServiceProvider.php b/inc/Engine/Media/PreloadFonts/ServiceProvider.php index dce294cc2c..44c98815c7 100644 --- a/inc/Engine/Media/PreloadFonts/ServiceProvider.php +++ b/inc/Engine/Media/PreloadFonts/ServiceProvider.php @@ -10,6 +10,8 @@ use WP_Rocket\Engine\Media\PreloadFonts\Context\Context; use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Controller as FrontendController; use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Subscriber as FrontendSubscriber; +use WP_Rocket\Engine\Media\PreloadFonts\Admin\Subscriber as AdminSubscriber; +use WP_Rocket\Engine\Media\PreloadFonts\Admin\Settings as AdminSettings; class ServiceProvider extends AbstractServiceProvider { /** @@ -29,6 +31,8 @@ class ServiceProvider extends AbstractServiceProvider { 'preload_fonts_frontend_subscriber', 'preload_fonts_front_controller', 'preload_fonts_factory', + 'preload_fonts_admin_subscriber', + 'preload_fonts_admin_settings', ]; /** @@ -51,7 +55,8 @@ public function register(): void { $this->getContainer()->addShared( 'preload_fonts_table', PreloadFontsTable::class ); $this->getContainer()->get( 'preload_fonts_table' ); - $options = $this->getContainer()->get( 'options' ); + $options = $this->getContainer()->get( 'options' ); + $options_api = $this->getContainer()->get( 'options_api' ); $this->getContainer()->add( 'preload_fonts_query', PreloadFontsQuery::class ); $this->getContainer()->add( 'preload_fonts_context', Context::class ) @@ -90,5 +95,19 @@ public function register(): void { $this->getContainer()->get( 'preload_fonts_context' ), ] ); + + $this->getContainer()->addShared( 'preload_fonts_admin_settings', AdminSettings::class ) + ->addArguments( + [ + $this->getContainer()->get( 'options' ), + $this->getContainer()->get( 'options_api' ), + ] + ); + $this->getContainer()->add( 'preload_fonts_admin_subscriber', AdminSubscriber::class ) + ->addArguments( + [ + $this->getContainer()->get( 'preload_fonts_admin_settings' ), + ] + ); } } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index 564ee944d2..1150942083 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -805,7 +805,6 @@ ], ], 'expected' => $html_output_with_beacon_and_only_lrc_opt, - ], 'shouldNotDuplicateBeaconOnAPage' => [ 'config' => [