diff --git a/inc/Engine/Admin/Beacon/Beacon.php b/inc/Engine/Admin/Beacon/Beacon.php index 12c4e6e2ef..41c52604b9 100644 --- a/inc/Engine/Admin/Beacon/Beacon.php +++ b/inc/Engine/Admin/Beacon/Beacon.php @@ -831,6 +831,16 @@ public function get_suggest( $doc_id ) { 'url' => 'https://fr.docs.wp-rocket.me/article/1836-rendu-differe-automatique/?utm_source=wp_plugin&utm_medium=wp_rocket', ], ], + 'host_fonts_locally' => [ + 'en' => [ + 'id' => '', + 'url' => '', + ], + 'fr' => [ + 'id' => '', + 'url' => '', + ], + ], ]; return isset( $suggest[ $doc_id ][ $this->get_user_locale() ] ) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index ac5eb3f473..6a367df94c 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -823,12 +823,13 @@ private function media_section() { $lazyload_beacon = $this->beacon->get_suggest( 'lazyload' ); $exclude_lazyload = $this->beacon->get_suggest( 'exclude_lazyload' ); $dimensions = $this->beacon->get_suggest( 'image_dimensions' ); + $fonts = $this->beacon->get_suggest( 'host_fonts_locally' ); $this->settings->add_page_section( 'media', [ 'title' => __( 'Media', 'rocket' ), - 'menu_description' => __( 'LazyLoad, image dimensions', 'rocket' ), + 'menu_description' => __( 'LazyLoad, image dimensions, font optimization', 'rocket' ), ] ); @@ -890,7 +891,7 @@ private function media_section() { $this->settings->add_settings_sections( [ - 'lazyload_section' => [ + 'lazyload_section' => [ 'title' => __( 'LazyLoad', 'rocket' ), 'type' => 'fields_container', // translators: %1$s = opening tag, %2$s = closing tag. @@ -903,7 +904,7 @@ private function media_section() { // translators: %1$s = “WP Rocket”, %2$s = a list of plugin names. 'helper' => ! empty( $disable_lazyload ) ? sprintf( __( 'LazyLoad is currently activated in %2$s. If you want to use WP Rocket’s LazyLoad, disable this option in %2$s.', 'rocket' ), WP_ROCKET_PLUGIN_NAME, $disable_lazyload ) : '', ], - 'dimensions_section' => [ + 'dimensions_section' => [ 'title' => __( 'Image Dimensions', 'rocket' ), 'type' => 'fields_container', // translators: %1$s = opening tag, %2$s = closing tag. @@ -911,6 +912,14 @@ private function media_section() { 'help' => $dimensions, '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', + ], ] ); @@ -1009,6 +1018,14 @@ private function media_section() { 'default' => 0, 'sanitize_callback' => 'sanitize_checkbox', ], + 'host_fonts_locally' => [ + 'type' => 'checkbox', + 'label' => __( 'Host Google Fonts locally', 'rocket' ), + 'section' => 'font_optimization_section', + 'page' => 'media', + 'default' => 0, + 'sanitize_callback' => 'sanitize_checkbox', + ], ] ); } diff --git a/inc/Engine/Media/Fonts/Admin/Settings.php b/inc/Engine/Media/Fonts/Admin/Settings.php new file mode 100644 index 0000000000..f51f2e2f51 --- /dev/null +++ b/inc/Engine/Media/Fonts/Admin/Settings.php @@ -0,0 +1,35 @@ +sanitize_checkbox( $input, 'host_fonts_locally' ); + + return $input; + } +} diff --git a/inc/Engine/Media/Fonts/Admin/Subscriber.php b/inc/Engine/Media/Fonts/Admin/Subscriber.php new file mode 100644 index 0000000000..d3bf107c5c --- /dev/null +++ b/inc/Engine/Media/Fonts/Admin/Subscriber.php @@ -0,0 +1,62 @@ +settings = $settings; + } + + /** + * Returns an array of events this listens to + * + * @return array + */ + public static function get_subscribed_events(): array { + return [ + 'rocket_first_install_options' => [ 'add_option', 16 ], + 'rocket_input_sanitize' => [ 'sanitize_option', 10, 2 ], + ]; + } + + /** + * Add the images dimensions option to the WP Rocket options array + * + * @param array $options WP Rocket options array. + * + * @return array + */ + public function add_option( array $options ): array { + return $this->settings->add_option( $options ); + } + + /** + * Sanitizes the option value when saving from the settings page + * + * @param array $input Array of sanitized values after being submitted by the form. + * @param Settings $settings Settings class instance. + * + * @return array + */ + public function sanitize_option( array $input, Settings $settings ): array { + return $this->settings->sanitize_option_value( $input, $settings ); + } +} diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php new file mode 100644 index 0000000000..8c68d550f0 --- /dev/null +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -0,0 +1,46 @@ +provides, true ); + } + + /** + * Registers the classes. + * + * @return void + */ + public function register(): void { + $this->getContainer()->add( 'media_fonts_settings', Settings::class ); + $this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class ) + ->addArgument( 'media_fonts_settings' ); + } +} diff --git a/inc/Plugin.php b/inc/Plugin.php index c9733eec93..529ac3ed85 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -53,6 +53,7 @@ use WP_Rocket\Engine\Debug\ServiceProvider as DebugServiceProvider; use WP_Rocket\Engine\Common\PerformanceHints\ServiceProvider as PerformanceHintsServiceProvider; use WP_Rocket\Engine\Optimization\LazyRenderContent\ServiceProvider as LRCServiceProvider; +use WP_Rocket\Engine\Media\Fonts\ServiceProvider as MediaFontsServiceProvider; /** * Plugin Manager. @@ -308,6 +309,7 @@ private function init_common_subscribers() { $this->container->addServiceProvider( new SaasAdminServiceProvider() ); $this->container->addServiceProvider( new PerformanceHintsServiceProvider() ); $this->container->addServiceProvider( new LRCServiceProvider() ); + $this->container->addServiceProvider( new MediaFontsServiceProvider() ); $common_subscribers = [ 'license_subscriber', @@ -401,6 +403,7 @@ private function init_common_subscribers() { 'performance_hints_admin_subscriber', 'lrc_frontend_subscriber', 'taxonomy_subscriber', + 'media_fonts_admin_subscriber', ]; $host_type = HostResolver::get_host_service(); diff --git a/tests/Fixtures/inc/admin/rocketFirstInstall.php b/tests/Fixtures/inc/admin/rocketFirstInstall.php index 4e44d03413..b06e2eefde 100644 --- a/tests/Fixtures/inc/admin/rocketFirstInstall.php +++ b/tests/Fixtures/inc/admin/rocketFirstInstall.php @@ -76,6 +76,7 @@ $integration[ 'preload_links' ] = 1; $integration[ 'image_dimensions' ] = 0; $integration[ 'exclude_lazyload' ] = []; +$integration['host_fonts_locally'] = 0; return [ 'test_data' => [ diff --git a/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php b/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php index ffe184e47a..e1d19dcfcd 100644 --- a/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php +++ b/tests/Integration/inc/Engine/Media/ImageDimensions/AdminSubscriber/addOption.php @@ -18,9 +18,9 @@ public function set_up() { } public function tear_down() { - parent::tear_down(); - $this->restoreWpHook( 'rocket_first_install_options' ); + + parent::tear_down(); } /**