From be9e3b94294592fe66fb693b36116c13f0a7abb3 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 6 Nov 2024 14:10:05 +0100 Subject: [PATCH 01/37] Base structure --- inc/Engine/Media/Fonts/Context.php | 21 ++++++++ .../Media/Fonts/Frontend/Controller.php | 52 +++++++++++++++++++ .../Media/Fonts/Frontend/Subscriber.php | 44 ++++++++++++++++ inc/Engine/Media/Fonts/ServiceProvider.php | 32 ++++++++++++ inc/Plugin.php | 3 ++ 5 files changed, 152 insertions(+) create mode 100644 inc/Engine/Media/Fonts/Context.php create mode 100644 inc/Engine/Media/Fonts/Frontend/Controller.php create mode 100644 inc/Engine/Media/Fonts/Frontend/Subscriber.php create mode 100644 inc/Engine/Media/Fonts/ServiceProvider.php diff --git a/inc/Engine/Media/Fonts/Context.php b/inc/Engine/Media/Fonts/Context.php new file mode 100644 index 0000000000..b3c50c5048 --- /dev/null +++ b/inc/Engine/Media/Fonts/Context.php @@ -0,0 +1,21 @@ +context = $context; + } + + /** + * Rewrites the Google Fonts paths to local ones. + * + * @param string $html HTML content. + * @return string + */ + public function rewrite_fonts( string $html ): string { + if ( ! $this->context->is_allowed() ) { + return $html; + } + + // Use the URL hash value to get the CSS file paths. + $pattern = '/]*href=["\'](https:\/\/fonts\.googleapis\.com\/css2?[^"\']+)["\'][^>]*>/i'; + $html = preg_replace_callback( $pattern, function( $matches ) { + $google_fonts_url = $matches[1]; + $font_instance = GoogleFontFactory::create( $google_fonts_url ); + + if ( ! $font_instance ) { + return $matches[0]; + } + + $local_url = $font_instance->get_local_url(); + return str_replace( $google_fonts_url, $local_url, $matches[0] ) . ' data-wpr-hosted-gf-parameters="' . esc_attr( wp_parse_url( $google_fonts_url, PHP_URL_QUERY ) ) . '"'; + }, $html ); + + return $html; + } +} diff --git a/inc/Engine/Media/Fonts/Frontend/Subscriber.php b/inc/Engine/Media/Fonts/Frontend/Subscriber.php new file mode 100644 index 0000000000..46956dc82b --- /dev/null +++ b/inc/Engine/Media/Fonts/Frontend/Subscriber.php @@ -0,0 +1,44 @@ +frontend_controller = $frontend_controller; + } + + /** + * Returns an array of events that this subscriber wants to listen to. + * + * @return array + */ + public static function get_subscribed_events(): array { + return [ + 'rocket_buffer' => [ 'rewrite_fonts', 18 ], + ]; + } + + /** + * Rewrites the Google Fonts paths to local ones. + * + * @param string $html HTML content. + * @return string + */ + public function rewrite_fonts( string $html ): string { + return $this->frontend_controller->rewrite_fonts( $html ); + } +} diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php new file mode 100644 index 0000000000..8f6b37a8c8 --- /dev/null +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -0,0 +1,32 @@ +getContainer()->add('media_fonts_context', Context::class); + $this->getContainer()->add('media_fonts_frontend_controller', FrontendController::class) + ->withArgument('media_fonts_context'); + $this->getContainer()->add('media_fonts_frontend_subscriber', FrontendSubscriber::class) + ->withArgument('media_fonts_frontend_controller'); + } +} diff --git a/inc/Plugin.php b/inc/Plugin.php index c9733eec93..30125100a0 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 FontsServiceProvider; /** * 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 FontsServiceProvider() ); $common_subscribers = [ 'license_subscriber', @@ -401,6 +403,7 @@ private function init_common_subscribers() { 'performance_hints_admin_subscriber', 'lrc_frontend_subscriber', 'taxonomy_subscriber', + 'media_fonts_frontend_subscriber', ]; $host_type = HostResolver::get_host_service(); From b33302c44786d1748beadc30a94f24a4b813a21c Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 6 Nov 2024 14:10:19 +0100 Subject: [PATCH 02/37] Google font version management --- inc/Engine/Media/Fonts/GoogleFontFactory.php | 28 ++++++++++++++++++++ inc/Engine/Media/Fonts/GoogleFontV1.php | 23 ++++++++++++++++ inc/Engine/Media/Fonts/GoogleFontV2.php | 23 ++++++++++++++++ inc/Engine/Media/Fonts/GoogleFontVersion.php | 20 ++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 inc/Engine/Media/Fonts/GoogleFontFactory.php create mode 100644 inc/Engine/Media/Fonts/GoogleFontV1.php create mode 100644 inc/Engine/Media/Fonts/GoogleFontV2.php create mode 100644 inc/Engine/Media/Fonts/GoogleFontVersion.php diff --git a/inc/Engine/Media/Fonts/GoogleFontFactory.php b/inc/Engine/Media/Fonts/GoogleFontFactory.php new file mode 100644 index 0000000000..96e07ced5f --- /dev/null +++ b/inc/Engine/Media/Fonts/GoogleFontFactory.php @@ -0,0 +1,28 @@ +url ); + return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/1/{$hash}.css" ); + } +} diff --git a/inc/Engine/Media/Fonts/GoogleFontV2.php b/inc/Engine/Media/Fonts/GoogleFontV2.php new file mode 100644 index 0000000000..9f72c412c1 --- /dev/null +++ b/inc/Engine/Media/Fonts/GoogleFontV2.php @@ -0,0 +1,23 @@ +url ); + return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/2/{$hash}.css" ); + } +} diff --git a/inc/Engine/Media/Fonts/GoogleFontVersion.php b/inc/Engine/Media/Fonts/GoogleFontVersion.php new file mode 100644 index 0000000000..395496cfd8 --- /dev/null +++ b/inc/Engine/Media/Fonts/GoogleFontVersion.php @@ -0,0 +1,20 @@ + Date: Wed, 6 Nov 2024 14:26:10 +0100 Subject: [PATCH 03/37] Change of structure + fixes linters --- .../Media/Fonts/{ => Context}/Context.php | 5 ++- .../{ => Factory/Fonts}/GoogleFontV1.php | 15 +++++++- .../{ => Factory/Fonts}/GoogleFontV2.php | 15 +++++++- .../Fonts/{ => Factory}/GoogleFontFactory.php | 4 +- .../Fonts/{ => Factory}/GoogleFontVersion.php | 3 +- .../Media/Fonts/Frontend/Controller.php | 30 ++++++++------- inc/Engine/Media/Fonts/ServiceProvider.php | 38 ++++++++++++++----- 7 files changed, 79 insertions(+), 31 deletions(-) rename inc/Engine/Media/Fonts/{ => Context}/Context.php (78%) rename inc/Engine/Media/Fonts/{ => Factory/Fonts}/GoogleFontV1.php (57%) rename inc/Engine/Media/Fonts/{ => Factory/Fonts}/GoogleFontV2.php (57%) rename inc/Engine/Media/Fonts/{ => Factory}/GoogleFontFactory.php (78%) rename inc/Engine/Media/Fonts/{ => Factory}/GoogleFontVersion.php (84%) diff --git a/inc/Engine/Media/Fonts/Context.php b/inc/Engine/Media/Fonts/Context/Context.php similarity index 78% rename from inc/Engine/Media/Fonts/Context.php rename to inc/Engine/Media/Fonts/Context/Context.php index b3c50c5048..9785fcfd45 100644 --- a/inc/Engine/Media/Fonts/Context.php +++ b/inc/Engine/Media/Fonts/Context/Context.php @@ -1,8 +1,9 @@ url = $url; + } + /** * Get the local URL for the Google Font V1. * * @return string */ public function get_local_url(): string { - // To be replaced when developing the backend + // To be replaced when developing the backend. $hash = md5( $this->url ); return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/1/{$hash}.css" ); } diff --git a/inc/Engine/Media/Fonts/GoogleFontV2.php b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php similarity index 57% rename from inc/Engine/Media/Fonts/GoogleFontV2.php rename to inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php index 9f72c412c1..5f9cd115ee 100644 --- a/inc/Engine/Media/Fonts/GoogleFontV2.php +++ b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php @@ -1,6 +1,8 @@ url = $url; + } + /** * Get the local URL for the Google Font V2. * * @return string */ public function get_local_url(): string { - // To be replaced when developing the backend + // To be replaced when developing the backend. $hash = md5( $this->url ); return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/2/{$hash}.css" ); } diff --git a/inc/Engine/Media/Fonts/GoogleFontFactory.php b/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php similarity index 78% rename from inc/Engine/Media/Fonts/GoogleFontFactory.php rename to inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php index 96e07ced5f..4312170add 100644 --- a/inc/Engine/Media/Fonts/GoogleFontFactory.php +++ b/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php @@ -1,7 +1,9 @@ ]*href=["\'](https:\/\/fonts\.googleapis\.com\/css2?[^"\']+)["\'][^>]*>/i'; - $html = preg_replace_callback( $pattern, function( $matches ) { - $google_fonts_url = $matches[1]; - $font_instance = GoogleFontFactory::create( $google_fonts_url ); - - if ( ! $font_instance ) { - return $matches[0]; - } - - $local_url = $font_instance->get_local_url(); - return str_replace( $google_fonts_url, $local_url, $matches[0] ) . ' data-wpr-hosted-gf-parameters="' . esc_attr( wp_parse_url( $google_fonts_url, PHP_URL_QUERY ) ) . '"'; - }, $html ); + $html = preg_replace_callback( + $pattern, + function ( $matches ) { + $google_fonts_url = $matches[1]; + $font_instance = GoogleFontFactory::create( $google_fonts_url ); + + if ( ! $font_instance ) { + return $matches[0]; + } + + $local_url = $font_instance->get_local_url(); + return str_replace( $google_fonts_url, $local_url, $matches[0] ) . ' data-wpr-hosted-gf-parameters="' . esc_attr( wp_parse_url( $google_fonts_url, PHP_URL_QUERY ) ) . '"'; + }, + $html + ); return $html; } diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 8f6b37a8c8..9a54aee8e1 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -2,31 +2,51 @@ namespace WP_Rocket\Engine\Media\Fonts; -use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber; -use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController; -use WP_Rocket\Engine\Media\Fonts\Context; use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider; +use WP_Rocket\Engine\Media\Fonts\Context\Context; +use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController; +use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber; +use WP_Rocket\Engine\Media\Fonts\Factory\GoogleFontFactory; class ServiceProvider extends AbstractServiceProvider { /** + * The provides array is a way to let the container + * know that a service is provided by this service + * provider. Every service that is registered via + * this service provider must have an alias added + * to this array or it will be ignored. + * * @var array */ protected $provides = [ 'media_fonts_context', 'media_fonts_frontend_controller', 'media_fonts_frontend_subscriber', + 'media_fonts_factory', ]; + /** + * Check if the service provider provides a specific service. + * + * @param string $id The id of the service. + * + * @return bool + */ + public function provides( string $id ): bool { + return in_array( $id, $this->provides, true ); + } + /** * Registers the classes. * * @return void */ - public function register() { - $this->getContainer()->add('media_fonts_context', Context::class); - $this->getContainer()->add('media_fonts_frontend_controller', FrontendController::class) - ->withArgument('media_fonts_context'); - $this->getContainer()->add('media_fonts_frontend_subscriber', FrontendSubscriber::class) - ->withArgument('media_fonts_frontend_controller'); + public function register(): void { + $this->getContainer()->add( 'media_fonts_context', Context::class ); + $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class ) + ->addArgument( 'media_fonts_context' ); + $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class ) + ->addArgument( 'media_fonts_frontend_controller' ); + $this->getContainer()->addShared( 'media_fonts_factory', GoogleFontFactory::class ); } } From 5fac3841e12740327f2403b234d0189f412fd022 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 6 Nov 2024 15:21:24 +0100 Subject: [PATCH 04/37] Change use of `home_url` to WPR constant --- inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php index f74815eea3..fa0ce71986 100644 --- a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php +++ b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php @@ -29,6 +29,6 @@ public function __construct( string $url ) { public function get_local_url(): string { // To be replaced when developing the backend. $hash = md5( $this->url ); - return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/1/{$hash}.css" ); + return rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . "fonts/google-fonts/1/{$hash}.css"; } } From 968eefde0c671b191824ad30ee74db680fb4fac0 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 6 Nov 2024 15:22:50 +0100 Subject: [PATCH 05/37] Change use of `home_url` to WPR constant --- inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php index 5f9cd115ee..f759c58583 100644 --- a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php +++ b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php @@ -29,6 +29,6 @@ public function __construct( string $url ) { public function get_local_url(): string { // To be replaced when developing the backend. $hash = md5( $this->url ); - return home_url( "/wp-content/cache/wp-rocket/fonts/google-fonts/2/{$hash}.css" ); + return rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . "fonts/google-fonts/2/{$hash}.css"; } } From 359f1758bb6974351414ee2f5122fd9e094460be Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 7 Nov 2024 06:04:46 +0100 Subject: [PATCH 06/37] Changes the build of the factory --- .../Media/Fonts/Factory/GoogleFontFactory.php | 35 ++++++++++++++++++- .../Media/Fonts/Frontend/Controller.php | 3 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php b/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php index 4312170add..2cc9deffc5 100644 --- a/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php +++ b/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php @@ -7,13 +7,37 @@ use WP_Rocket\Logger\Logger; class GoogleFontFactory { + /** + * The Google Fonts URL. + * + * @var string + */ + protected $url; + + /** + * The Google Font version instance. + * + * @var GoogleFontVersion|null + */ + protected $font_version; + + /** + * Constructor. + * + * @param string $url Google Fonts URL. + */ + public function __construct( string $url ) { + $this->url = $url; + $this->font_version = $this->create_font_version( $url ); + } + /** * Creates an instance of GoogleFontV1 or GoogleFontV2 based on the URL provided. * * @param string $url Google Fonts URL. * @return GoogleFontVersion|null */ - public static function create( string $url ): ?GoogleFontVersion { + protected function create_font_version( string $url ): ?GoogleFontVersion { if ( strpos( $url, 'css2' ) !== false ) { return new GoogleFontV2( $url ); } @@ -27,4 +51,13 @@ public static function create( string $url ): ?GoogleFontVersion { return null; } + + /** + * Get the Google Font version instance. + * + * @return GoogleFontVersion|null + */ + public function get_font_version(): ?GoogleFontVersion { + return $this->font_version; + } } diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 2e9c3f54c5..3c5f9a38ef 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -39,7 +39,8 @@ public function rewrite_fonts( string $html ): string { $pattern, function ( $matches ) { $google_fonts_url = $matches[1]; - $font_instance = GoogleFontFactory::create( $google_fonts_url ); + $font_factory = new GoogleFontFactory( $google_fonts_url ); + $font_instance = $font_factory->get_font_version(); if ( ! $font_instance ) { return $matches[0]; From 6f044c001bd4ae96494d4b682b218e54029e9e9a Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 7 Nov 2024 06:33:35 +0100 Subject: [PATCH 07/37] Add unit tests --- .../Media/Fonts/Context/Context/isAllowed.php | 18 ++++++++++ .../Fonts/GoogleFontV2/getLocalUrl.php | 34 +++++++++++++++++++ .../Media/Fonts/Context/Context/isAllowed.php | 26 ++++++++++++++ .../Fonts/GoogleFontV2/getLocalUrl.php | 27 +++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php create mode 100644 tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php create mode 100644 tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php b/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php new file mode 100644 index 0000000000..27932a1790 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php @@ -0,0 +1,18 @@ + [ + 'config' => [ + 'local_google_fonts' => false, + 'rocket_self_host_fonts' => true, + ], + 'expected' => true, + ], + 'testShouldReturnFalseWhenFeatureDisabled' => [ + 'config' => [ + 'local_google_fonts' => true, + 'rocket_self_host_fonts' => false, + ], + 'expected' => false, + ], +]; diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php new file mode 100644 index 0000000000..ccae42b1d3 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php @@ -0,0 +1,34 @@ + [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b7488900b01c2ffa45c4faf5876bd050.css', + ], + 'testShouldReturnLocalUrlWithVersion' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto&ver=1.0', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/bd7d7cead547593f5d4c8a439f32e209.css', + ], + 'testShouldReturnLocalUrlWithSubset' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto:300,400,500&subset=latin,latin-ext', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b6eed853c9ca4324a5fd627ea3465c48.css', + ], + 'testShouldReturnLocalUrlWithMultipleFonts' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3d7fe3a1467a88318d3fb6e4ced0b127.css', + ], + 'testShouldReturnLocalUrlWithMultipleFontsAndVersion' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans&ver=1.0', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3fbdd05b1bf26933207aeb7d3d910d30.css', + ], +]; diff --git a/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php new file mode 100644 index 0000000000..033140deda --- /dev/null +++ b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php @@ -0,0 +1,26 @@ +once() + ->with('local_google_fonts') + ->andReturn($config['local_google_fonts']); + + $this->assertSame($expected, $context->is_allowed($config)); + } +} diff --git a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php new file mode 100644 index 0000000000..50de9eac86 --- /dev/null +++ b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php @@ -0,0 +1,27 @@ +once() + ->with('WP_ROCKET_CACHE_PATH') + ->andReturn('/wp-content/cache/wp-rocket/'); + + $result = $googleFontV1->get_local_url(); + $this->assertEquals($expected, $result); + } +} From 4882bbf84fe8bf031bbfeba4c8c6ab59caa7d3f0 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 7 Nov 2024 07:08:50 +0100 Subject: [PATCH 08/37] Fix ServiceProvider --- inc/Engine/Media/Fonts/ServiceProvider.php | 8 +++-- .../Fonts/GoogleFontV1/getLocalUrl.php | 34 +++++++++++++++++++ .../Fonts/GoogleFontV1/getLocalUrl.php | 27 +++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php create mode 100644 tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 9a54aee8e1..92e3f3495e 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -44,9 +44,13 @@ public function provides( string $id ): bool { public function register(): void { $this->getContainer()->add( 'media_fonts_context', Context::class ); $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class ) - ->addArgument( 'media_fonts_context' ); + ->addArgument( + $this->getContainer()->get('media_fonts_context') + ); $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class ) - ->addArgument( 'media_fonts_frontend_controller' ); + ->addArgument( + $this->getContainer()->get('media_fonts_frontend_controller' ) + ); $this->getContainer()->addShared( 'media_fonts_factory', GoogleFontFactory::class ); } } diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php new file mode 100644 index 0000000000..6691199aff --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php @@ -0,0 +1,34 @@ + [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/ebc173c0fc97eef86a6e51ada56c5a9a.css', + ], + 'testShouldReturnLocalUrlWithVersion' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto&ver=1.0', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/130f702787108e593481b804d049b815.css', + ], + 'testShouldReturnLocalUrlWithSubset' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto:300,400,500&subset=latin,latin-ext', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/4942a9bb0eeef783d471f016bb8eba76.css', + ], + 'testShouldReturnLocalUrlWithMultipleFonts' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/624f2c2b9858423d0688793189f6e6cb.css', + ], + 'testShouldReturnLocalUrlWithMultipleFontsAndVersion' => [ + 'config' => [ + 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans&ver=1.0', + ], + 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/6447d15db96450edb62158b00cabac8f.css', + ], +]; diff --git a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php new file mode 100644 index 0000000000..fa06a160a7 --- /dev/null +++ b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php @@ -0,0 +1,27 @@ +once() + ->with('WP_ROCKET_CACHE_PATH') + ->andReturn('/wp-content/cache/wp-rocket/'); + + $result = $googleFontV1->get_local_url(); + $this->assertEquals($expected, $result); + } +} From 833baaf340957c6818fa1d4b573949b01987d8bc Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 11:10:47 +0100 Subject: [PATCH 09/37] Works up to combining URL --- composer.json | 1 + .../Fonts/Factory/Fonts/GoogleFontV1.php | 34 --------- .../Fonts/Factory/Fonts/GoogleFontV2.php | 34 --------- .../Media/Fonts/Factory/GoogleFontFactory.php | 63 ---------------- .../Media/Fonts/Factory/GoogleFontVersion.php | 19 ----- .../Media/Fonts/Frontend/Controller.php | 71 ++++++++++++++----- inc/Engine/Media/Fonts/ServiceProvider.php | 19 +++-- .../Optimization/GoogleFonts/Combine.php | 4 +- .../Optimization/GoogleFonts/CombineV2.php | 4 +- .../Fonts/GoogleFontV1/getLocalUrl.php | 34 --------- .../Fonts/GoogleFontV2/getLocalUrl.php | 34 --------- .../Frontend/Controller/HTML/expected_v1.php | 32 +++++++++ .../Controller/HTML/expected_v1_v2.php | 0 .../Frontend/Controller/HTML/expected_v2.php | 0 .../Frontend/Controller/HTML/input_v1.php | 33 +++++++++ .../Frontend/Controller/HTML/input_v1_v2.php | 41 +++++++++++ .../Frontend/Controller/HTML/input_v2.php | 33 +++++++++ .../Frontend/Controller/rewriteFonts.php | 34 +++++++++ .../Frontend/Controller/rewriteFonts.php | 34 +++++++++ .../Fonts/GoogleFontV1/getLocalUrl.php | 27 ------- .../Fonts/GoogleFontV2/getLocalUrl.php | 27 ------- 21 files changed, 279 insertions(+), 299 deletions(-) delete mode 100644 inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php delete mode 100644 inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php delete mode 100644 inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php delete mode 100644 inc/Engine/Media/Fonts/Factory/GoogleFontVersion.php delete mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php delete mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php create mode 100644 tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php create mode 100644 tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php delete mode 100644 tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php delete mode 100644 tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php diff --git a/composer.json b/composer.json index 5c474f3135..e2822a1d74 100644 --- a/composer.json +++ b/composer.json @@ -178,6 +178,7 @@ "test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress", "test-integration-weglot": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Weglot", "test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium", + "test-integration-googlefonthost": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group GoogleFontHost", "run-tests": [ "@test-unit", "@test-integration", diff --git a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php deleted file mode 100644 index fa0ce71986..0000000000 --- a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1.php +++ /dev/null @@ -1,34 +0,0 @@ -url = $url; - } - - /** - * Get the local URL for the Google Font V1. - * - * @return string - */ - public function get_local_url(): string { - // To be replaced when developing the backend. - $hash = md5( $this->url ); - return rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . "fonts/google-fonts/1/{$hash}.css"; - } -} diff --git a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php b/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php deleted file mode 100644 index f759c58583..0000000000 --- a/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2.php +++ /dev/null @@ -1,34 +0,0 @@ -url = $url; - } - - /** - * Get the local URL for the Google Font V2. - * - * @return string - */ - public function get_local_url(): string { - // To be replaced when developing the backend. - $hash = md5( $this->url ); - return rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) . "fonts/google-fonts/2/{$hash}.css"; - } -} diff --git a/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php b/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php deleted file mode 100644 index 2cc9deffc5..0000000000 --- a/inc/Engine/Media/Fonts/Factory/GoogleFontFactory.php +++ /dev/null @@ -1,63 +0,0 @@ -url = $url; - $this->font_version = $this->create_font_version( $url ); - } - - /** - * Creates an instance of GoogleFontV1 or GoogleFontV2 based on the URL provided. - * - * @param string $url Google Fonts URL. - * @return GoogleFontVersion|null - */ - protected function create_font_version( string $url ): ?GoogleFontVersion { - if ( strpos( $url, 'css2' ) !== false ) { - return new GoogleFontV2( $url ); - } - - if ( strpos( $url, 'fonts.googleapis.com' ) !== false ) { - return new GoogleFontV1( $url ); - } - - // Log an error message for invalid or malformed URLs. - Logger::error( 'Invalid or malformed Google Fonts URL: ' . $url ); - - return null; - } - - /** - * Get the Google Font version instance. - * - * @return GoogleFontVersion|null - */ - public function get_font_version(): ?GoogleFontVersion { - return $this->font_version; - } -} diff --git a/inc/Engine/Media/Fonts/Factory/GoogleFontVersion.php b/inc/Engine/Media/Fonts/Factory/GoogleFontVersion.php deleted file mode 100644 index 835ac2b1fb..0000000000 --- a/inc/Engine/Media/Fonts/Factory/GoogleFontVersion.php +++ /dev/null @@ -1,19 +0,0 @@ -context = $context; + $this->combineV1 = $combineV1; + $this->combineV2 = $combineV2; } /** @@ -32,25 +42,50 @@ public function rewrite_fonts( string $html ): string { if ( ! $this->context->is_allowed() ) { return $html; } + $html_nocomments = $this->hide_comments( $html ); + $v1_url = $v2_url = ''; + + + $v1_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css[^\d](?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); + ; + $v2_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css2(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); + + if ( ! $v1_fonts && ! $v2_fonts ) { + Logger::debug( 'No Google Fonts found.', [ 'Host Google Font' ] ); + return $html; + } + + if ( $v1_fonts ) { + $this->combineV1->parse( $v1_fonts ); + } - // Use the URL hash value to get the CSS file paths. - $pattern = '/]*href=["\'](https:\/\/fonts\.googleapis\.com\/css2?[^"\']+)["\'][^>]*>/i'; - $html = preg_replace_callback( - $pattern, - function ( $matches ) { - $google_fonts_url = $matches[1]; - $font_factory = new GoogleFontFactory( $google_fonts_url ); - $font_instance = $font_factory->get_font_version(); - - if ( ! $font_instance ) { - return $matches[0]; + $families = []; + if ( $v2_fonts ) { + foreach ( $v2_fonts as $tag ) { + $parsed_families = $this->combineV2->parse( $tag ); + if ( ! empty( $parsed_families ) ) { + $families = array_merge( $families, $parsed_families ); } + } + + $families = array_unique( $families ); + $this->combineV2->parse( $v2_fonts ); + } + + + + + $v1_url = $this->combineV1->get_combined_url(); + if ( ! empty( $families ) ) { + $v2_url = $this->combineV2->get_combined_url( $families ); + } + + // debug print + return print_r($v1_url, true) . ' | '. print_r( md5( $v1_url ), true) . ' ||| ' . print_r($v2_url, true) . ' | '. print_r( md5( $v2_url ), true); + + // Need to add check if font file is existing depending on the URL path. + // If it exists => change HTML to that path. - $local_url = $font_instance->get_local_url(); - return str_replace( $google_fonts_url, $local_url, $matches[0] ) . ' data-wpr-hosted-gf-parameters="' . esc_attr( wp_parse_url( $google_fonts_url, PHP_URL_QUERY ) ) . '"'; - }, - $html - ); return $html; } diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 92e3f3495e..8963dc9841 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -6,7 +6,8 @@ use WP_Rocket\Engine\Media\Fonts\Context\Context; use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController; use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber; -use WP_Rocket\Engine\Media\Fonts\Factory\GoogleFontFactory; +use WP_Rocket\Engine\Optimization\GoogleFonts\Combine as GoogleFontCombinerV1; +use WP_Rocket\Engine\Optimization\GoogleFonts\CombineV2 as GoogleFontCombinerV2; class ServiceProvider extends AbstractServiceProvider { /** @@ -22,7 +23,8 @@ class ServiceProvider extends AbstractServiceProvider { 'media_fonts_context', 'media_fonts_frontend_controller', 'media_fonts_frontend_subscriber', - 'media_fonts_factory', + 'google_font_combiner_v1', + 'google_font_combiner_v2', ]; /** @@ -43,14 +45,21 @@ public function provides( string $id ): bool { */ public function register(): void { $this->getContainer()->add( 'media_fonts_context', Context::class ); + + $this->getContainer()->addShared('google_font_combiner_v1', GoogleFontCombinerV1::class); + $this->getContainer()->addShared('google_font_combiner_v2', GoogleFontCombinerV2::class); + $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class ) - ->addArgument( - $this->getContainer()->get('media_fonts_context') + ->addArguments( + [ + $this->getContainer()->get('media_fonts_context'), + $this->getContainer()->get('google_font_combiner_v1'), + $this->getContainer()->get('google_font_combiner_v2'), + ] ); $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class ) ->addArgument( $this->getContainer()->get('media_fonts_frontend_controller' ) ); - $this->getContainer()->addShared( 'media_fonts_factory', GoogleFontFactory::class ); } } diff --git a/inc/Engine/Optimization/GoogleFonts/Combine.php b/inc/Engine/Optimization/GoogleFonts/Combine.php index 1e96d636b3..4a4db2075a 100644 --- a/inc/Engine/Optimization/GoogleFonts/Combine.php +++ b/inc/Engine/Optimization/GoogleFonts/Combine.php @@ -101,7 +101,7 @@ public function optimize( $html ): string { * * @return void */ - private function parse( array $matches ) { + public function parse(array $matches ) { $fonts_array = []; $subsets_array = []; foreach ( $matches as $match ) { @@ -138,7 +138,7 @@ private function parse( array $matches ) { * * @return string */ - private function get_combined_url(): string { + public function get_combined_url(): string { $display = $this->get_font_display_value(); return esc_url( "https://fonts.googleapis.com/css?family={$this->fonts}{$this->subsets}&display={$display}" ); diff --git a/inc/Engine/Optimization/GoogleFonts/CombineV2.php b/inc/Engine/Optimization/GoogleFonts/CombineV2.php index faec84ebca..2d71ed2dc8 100644 --- a/inc/Engine/Optimization/GoogleFonts/CombineV2.php +++ b/inc/Engine/Optimization/GoogleFonts/CombineV2.php @@ -92,7 +92,7 @@ public function optimize( $html ): string { * * @return array */ - private function parse( array $tag ): array { + public function parse( array $tag ): array { if ( false !== strpos( $tag['url'], 'text=' ) ) { Logger::debug( 'GOOGLEFONTS V2 COMBINE: ' . $tag['url'] . ' SKIPPED TO PRESERVE "text" ATTRIBUTE.' ); return []; @@ -129,7 +129,7 @@ private function parse( array $tag ): array { * * @return string */ - private function get_combined_url( array $families ): string { + public function get_combined_url( array $families ): string { $display = $this->get_font_display_value(); return esc_url( "https://fonts.googleapis.com/css2{$this->get_concatenated_families( $families )}&display={$display}" ); diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php deleted file mode 100644 index 6691199aff..0000000000 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php +++ /dev/null @@ -1,34 +0,0 @@ - [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/ebc173c0fc97eef86a6e51ada56c5a9a.css', - ], - 'testShouldReturnLocalUrlWithVersion' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto&ver=1.0', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/130f702787108e593481b804d049b815.css', - ], - 'testShouldReturnLocalUrlWithSubset' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto:300,400,500&subset=latin,latin-ext', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/4942a9bb0eeef783d471f016bb8eba76.css', - ], - 'testShouldReturnLocalUrlWithMultipleFonts' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/624f2c2b9858423d0688793189f6e6cb.css', - ], - 'testShouldReturnLocalUrlWithMultipleFontsAndVersion' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css?family=Roboto|Open+Sans&ver=1.0', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/1/6447d15db96450edb62158b00cabac8f.css', - ], -]; diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php b/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php deleted file mode 100644 index ccae42b1d3..0000000000 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php +++ /dev/null @@ -1,34 +0,0 @@ - [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b7488900b01c2ffa45c4faf5876bd050.css', - ], - 'testShouldReturnLocalUrlWithVersion' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto&ver=1.0', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/bd7d7cead547593f5d4c8a439f32e209.css', - ], - 'testShouldReturnLocalUrlWithSubset' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto:300,400,500&subset=latin,latin-ext', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b6eed853c9ca4324a5fd627ea3465c48.css', - ], - 'testShouldReturnLocalUrlWithMultipleFonts' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3d7fe3a1467a88318d3fb6e4ced0b127.css', - ], - 'testShouldReturnLocalUrlWithMultipleFontsAndVersion' => [ - 'config' => [ - 'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans&ver=1.0', - ], - 'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3fbdd05b1bf26933207aeb7d3d910d30.css', - ], -]; diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php new file mode 100644 index 0000000000..55efff88cf --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php @@ -0,0 +1,32 @@ + + + + + + + + Google Font V1 Template + + + + +
+
+
+

Hello World

+

Welcome to the world

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php new file mode 100644 index 0000000000..e49961654e --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1.php @@ -0,0 +1,33 @@ + + + + + + + + Google Font V1 Template + + + + + +
+
+
+

Hello World

+

Welcome to the world

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php new file mode 100644 index 0000000000..34be74bed6 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v1_v2.php @@ -0,0 +1,41 @@ + + + + + + + + Google Font V1 and V2 Template + + + + + +
+
+
+

Hello World

+

Welcome to the world

+

This is a subtitle

+

Enjoy your stay

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php new file mode 100644 index 0000000000..2abf18db1b --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/input_v2.php @@ -0,0 +1,33 @@ + + + + + + + + Google Font V2 Template + + + + + +
+
+
+

Hello World

+

Welcome to the world

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php new file mode 100644 index 0000000000..1168587b37 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php @@ -0,0 +1,34 @@ + [ + 'config' => [ + 'url' => 'https://example.com/input_v1', + 'html' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ), + 'is_allowed' => true, + ], + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ), + ], + 'testShouldCombineV2' => [ + 'config' => [ + 'html' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ), + 'is_allowed' => true, + ], + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ), + ], + 'testShouldCombineV1AndV2' => [ + 'config' => [ + 'html' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ), + 'is_allowed' => true, + ], + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ), + ], + 'testShouldBailOutAsNoGoogleFontIncluded' => [ + 'config' => [ + 'html' => '', + 'is_allowed' => true, + ], + 'expected' => '', + ] +]; diff --git a/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php new file mode 100644 index 0000000000..5ce21cd637 --- /dev/null +++ b/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php @@ -0,0 +1,34 @@ +shouldReceive('is_allowed') + ->once() + ->andReturn( $config['is_allowed'] ); + + $controller = new Controller($context, new Combine(), new CombineV2()); + + $result = $controller->rewrite_fonts( $config['html'] ); + + $this->assertEquals( $expected, $result ); + } + +} diff --git a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php deleted file mode 100644 index fa06a160a7..0000000000 --- a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV1/getLocalUrl.php +++ /dev/null @@ -1,27 +0,0 @@ -once() - ->with('WP_ROCKET_CACHE_PATH') - ->andReturn('/wp-content/cache/wp-rocket/'); - - $result = $googleFontV1->get_local_url(); - $this->assertEquals($expected, $result); - } -} diff --git a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php b/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php deleted file mode 100644 index 50de9eac86..0000000000 --- a/tests/Unit/inc/Engine/Media/Fonts/Factory/Fonts/GoogleFontV2/getLocalUrl.php +++ /dev/null @@ -1,27 +0,0 @@ -once() - ->with('WP_ROCKET_CACHE_PATH') - ->andReturn('/wp-content/cache/wp-rocket/'); - - $result = $googleFontV1->get_local_url(); - $this->assertEquals($expected, $result); - } -} From 98d8fdc1bec99e2754afc21ff80fe205a19d43fa Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 11:11:09 +0100 Subject: [PATCH 10/37] Apply linter --- inc/Engine/Media/Fonts/Frontend/Controller.php | 17 ++++++----------- inc/Engine/Media/Fonts/ServiceProvider.php | 12 ++++++------ inc/Engine/Optimization/GoogleFonts/Combine.php | 2 +- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 6e86dfde48..7a7c533c2d 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -10,6 +10,7 @@ class Controller { use RegexTrait; + /** * Context instance. * @@ -27,7 +28,7 @@ class Controller { * @param Context $context Context instance. */ public function __construct( Context $context, Combine $combineV1, CombineV2 $combineV2 ) { - $this->context = $context; + $this->context = $context; $this->combineV1 = $combineV1; $this->combineV2 = $combineV2; } @@ -43,11 +44,9 @@ public function rewrite_fonts( string $html ): string { return $html; } $html_nocomments = $this->hide_comments( $html ); - $v1_url = $v2_url = ''; - + $v1_url = $v2_url = ''; $v1_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css[^\d](?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); - ; $v2_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css2(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); if ( ! $v1_fonts && ! $v2_fonts ) { @@ -64,29 +63,25 @@ public function rewrite_fonts( string $html ): string { foreach ( $v2_fonts as $tag ) { $parsed_families = $this->combineV2->parse( $tag ); if ( ! empty( $parsed_families ) ) { - $families = array_merge( $families, $parsed_families ); + $families = array_merge( $families, $parsed_families ); } } - $families = array_unique( $families ); + $families = array_unique( $families ); $this->combineV2->parse( $v2_fonts ); } - - - $v1_url = $this->combineV1->get_combined_url(); if ( ! empty( $families ) ) { $v2_url = $this->combineV2->get_combined_url( $families ); } // debug print - return print_r($v1_url, true) . ' | '. print_r( md5( $v1_url ), true) . ' ||| ' . print_r($v2_url, true) . ' | '. print_r( md5( $v2_url ), true); + return print_r( $v1_url, true ) . ' | ' . print_r( md5( $v1_url ), true ) . ' ||| ' . print_r( $v2_url, true ) . ' | ' . print_r( md5( $v2_url ), true ); // Need to add check if font file is existing depending on the URL path. // If it exists => change HTML to that path. - return $html; } } diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 8963dc9841..7663f85be6 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -46,20 +46,20 @@ public function provides( string $id ): bool { public function register(): void { $this->getContainer()->add( 'media_fonts_context', Context::class ); - $this->getContainer()->addShared('google_font_combiner_v1', GoogleFontCombinerV1::class); - $this->getContainer()->addShared('google_font_combiner_v2', GoogleFontCombinerV2::class); + $this->getContainer()->addShared( 'google_font_combiner_v1', GoogleFontCombinerV1::class ); + $this->getContainer()->addShared( 'google_font_combiner_v2', GoogleFontCombinerV2::class ); $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class ) ->addArguments( [ - $this->getContainer()->get('media_fonts_context'), - $this->getContainer()->get('google_font_combiner_v1'), - $this->getContainer()->get('google_font_combiner_v2'), + $this->getContainer()->get( 'media_fonts_context' ), + $this->getContainer()->get( 'google_font_combiner_v1' ), + $this->getContainer()->get( 'google_font_combiner_v2' ), ] ); $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class ) ->addArgument( - $this->getContainer()->get('media_fonts_frontend_controller' ) + $this->getContainer()->get( 'media_fonts_frontend_controller' ) ); } } diff --git a/inc/Engine/Optimization/GoogleFonts/Combine.php b/inc/Engine/Optimization/GoogleFonts/Combine.php index 4a4db2075a..d9383213e7 100644 --- a/inc/Engine/Optimization/GoogleFonts/Combine.php +++ b/inc/Engine/Optimization/GoogleFonts/Combine.php @@ -101,7 +101,7 @@ public function optimize( $html ): string { * * @return void */ - public function parse(array $matches ) { + public function parse( array $matches ) { $fonts_array = []; $subsets_array = []; foreach ( $matches as $match ) { From 0f2c05b16691c4f4ee82c42a1aa02e19a894ca9c Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 14:56:40 +0100 Subject: [PATCH 11/37] First working prototype --- .../Media/Fonts/Frontend/Controller.php | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 7a7c533c2d..d55ad19fee 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -45,6 +45,7 @@ public function rewrite_fonts( string $html ): string { } $html_nocomments = $this->hide_comments( $html ); $v1_url = $v2_url = ''; + $v1_path = $v2_path = ''; $v1_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css[^\d](?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); $v2_fonts = $this->find( '])+)?(?:\s+href\s*=\s*([\'"])(?(?:https?:)?\/\/fonts\.googleapis\.com\/css2(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments ); @@ -56,6 +57,8 @@ public function rewrite_fonts( string $html ): string { if ( $v1_fonts ) { $this->combineV1->parse( $v1_fonts ); + $v1_url = $this->combineV1->get_combined_url(); + $v1_path = $this->get_local_path( $this->get_current_url(), md5( $v1_url ) ); } $families = []; @@ -71,17 +74,64 @@ public function rewrite_fonts( string $html ): string { $this->combineV2->parse( $v2_fonts ); } - $v1_url = $this->combineV1->get_combined_url(); if ( ! empty( $families ) ) { - $v2_url = $this->combineV2->get_combined_url( $families ); + $v2_url = $this->combineV2->get_combined_url( $families ); + $v2_path = $this->get_local_path( $this->get_current_url(), md5( $v2_url ) ); } - // debug print - return print_r( $v1_url, true ) . ' | ' . print_r( md5( $v1_url ), true ) . ' ||| ' . print_r( $v2_url, true ) . ' | ' . print_r( md5( $v2_url ), true ); + if ( file_exists( $v1_path ) ) { + $html = preg_replace( '@<\/title>@i', '$0' . $this->get_optimized_markup( $v1_path ), $html, 1 ); + } - // Need to add check if font file is existing depending on the URL path. - // If it exists => change HTML to that path. + if ( file_exists( $v2_path ) ) { + $html = preg_replace( '@<\/title>@i', '$0' . $this->get_optimized_markup( $v2_path ), $html, 1 ); + } return $html; } + + /** + * Get the local path for the combined URL. + * + * @param string $url The combined URL. + * @param string $filename The filename. + * @return string The local path. + */ + private function get_local_path( string $url, string $filename ): string { + $parsed_url = parse_url( $url ); + + $path = ( ! empty( $parsed_url['path'] ) ) ? $parsed_url['path'] : ''; + $protocol = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) ? 'https://' : 'http://'; + $host = $_SERVER['HTTP_HOST']; + $local_path = $protocol . $host . '/wp-content/cache/wp-rocket/fonts' . $path . '/' . $filename . '.css'; + + return $local_path; + } + + /** + * Get the current URL. + * + * @return string The current URL. + */ + private function get_current_url(): string { + $protocol = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) ? 'https://' : 'http://'; + $current_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + return $current_url; + } + + /** + * Returns the optimized markup for Google Fonts + * + * @since 3.18 + * + * @param string $url Google Fonts URL. + * + * @return string + */ + protected function get_optimized_markup( string $url ): string { + return sprintf( + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet + $url + ); + } } From 32fa78757516d04ffedad0d841e6ea79d70b58eb Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 15:44:12 +0100 Subject: [PATCH 12/37] WIP: Integration test --- .../Frontend/Controller/HTML/expected_v1.php | 2 +- .../Frontend/Controller/rewriteFonts.php | 48 ++++++++++--------- .../Frontend/Controller/rewriteFonts.php | 32 ++++++++++--- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php index 55efff88cf..7be6585937 100644 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1.php @@ -9,7 +9,7 @@ Google Font V1 Template - + + + +
+
+
+

Hello World

+

Welcome to the world

+

This is a subtitle

+

Enjoy your stay

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php index e69de29bb2..8dcf11ad68 100644 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php @@ -0,0 +1,33 @@ + + + + + + + + Google Font V2 Template + + + + + +
+
+
+

Hello World

+

Welcome to the world

+
+
+
+ + diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php index afd32109ab..1b9307af2c 100644 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php @@ -1,38 +1,41 @@ [ + 'test_data' => [ + 'testShouldReturnOriginalWhenNotAllowed' => [ + 'config' => [ + 'is_allowed' => false, + ], + 'original' => '', + 'expected' => '', + ], + 'testShouldReturnOriginalWhenNoGoogleFonts' => [ 'config' => [ - 'html' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ), 'is_allowed' => true, - 'files' => [ - [ - 'path' => '/wp-content/cache/wp-rocket/fonts/example.org/70d57d4fc2ebfc2c090d3a76133094d2.css' - ] - ] ], - 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ), + 'original' => '', + 'expected' => '', + ], + 'testShouldRewriteV1Font' => [ + 'config' => [ + 'is_allowed' => true, + ], + 'original' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ), + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ), + ], + 'testShouldRewriteV2' => [ + 'config' => [ + 'is_allowed' => true, + ], + 'original' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ), + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ), + ], + 'testShouldRewriteV1AndV2' => [ + 'config' => [ + 'is_allowed' => true, + ], + 'original' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ), + 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ), + ], ], -// 'testShouldCombineV2' => [ -// 'config' => [ -// 'html' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ), -// 'is_allowed' => true, -// ], -// 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ), -// ], -// 'testShouldCombineV1AndV2' => [ -// 'config' => [ -// 'html' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ), -// 'is_allowed' => true, -// ], -// 'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ), -// ], -// 'testShouldBailOutAsNoGoogleFontIncluded' => [ -// 'config' => [ -// 'html' => '', -// 'is_allowed' => true, -// ], -// 'expected' => '', -// ] ]; diff --git a/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php new file mode 100644 index 0000000000..07c1f9fcfb --- /dev/null +++ b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php @@ -0,0 +1,44 @@ +justReturn( 1 ); + + $this->context = Mockery::mock( Context::class ); + $this->controller = new Controller( $this->context ); + + $this->stubWpParseUrl(); + } + + /** + * @dataProvider providerTestData + */ + public function testShouldDoExpected( $config, $original, $expected ) { + $this->context->shouldReceive('is_allowed') + ->once() + ->andReturn( $config['is_allowed'] ); + + $this->assertSame( + $this->format_the_html( $expected ), + $this->format_the_html( $this->controller->rewrite_fonts( $original ) ) + ); + } +} From e4934afb63b414ec7e8260fc3da8c3160de2710a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:00:15 -0500 Subject: [PATCH 20/37] remove group --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index e2822a1d74..5c474f3135 100644 --- a/composer.json +++ b/composer.json @@ -178,7 +178,6 @@ "test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress", "test-integration-weglot": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Weglot", "test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium", - "test-integration-googlefonthost": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group GoogleFontHost", "run-tests": [ "@test-unit", "@test-integration", From 29b218c72df41b7b37ae5c3d718ea448f996337c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:04:27 -0500 Subject: [PATCH 21/37] update name --- inc/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Plugin.php b/inc/Plugin.php index 43c963f457..01908d8ec0 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -309,7 +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 FontsServiceProvider() ); + $this->container->addServiceProvider( new MediaFontsServiceProvider() ); $common_subscribers = [ 'license_subscriber', From 8ed9d563aeb024b3a2960e3336b07f23c7eabb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:14:35 -0500 Subject: [PATCH 22/37] update config --- inc/Engine/Media/Fonts/Context/Context.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Media/Fonts/Context/Context.php b/inc/Engine/Media/Fonts/Context/Context.php index f84452d2fe..82673b4611 100644 --- a/inc/Engine/Media/Fonts/Context/Context.php +++ b/inc/Engine/Media/Fonts/Context/Context.php @@ -16,10 +16,9 @@ class Context extends AbstractContext { public function is_allowed( array $data = [] ): bool { $is_allowed = $this->run_common_checks( [ - 'do_not_optimize' => false, - 'bypass' => false, - 'option' => 'host_fonts_locally', - 'logged_in' => false, + 'do_not_optimize' => false, + 'bypass' => false, + 'option' => 'host_fonts_locally', ] ); From 1322df33ec7afdd2345813f2ab13885500261ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:14:40 -0500 Subject: [PATCH 23/37] update test --- .../Media/Fonts/Context/Context/isAllowed.php | 32 +++++++++++++---- .../Media/Fonts/Context/Context/isAllowed.php | 36 ++++++++++++------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php b/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php index 27932a1790..d70a9f3055 100644 --- a/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php @@ -1,18 +1,36 @@ [ + 'testShouldReturnFalseWhenBypass' => [ 'config' => [ - 'local_google_fonts' => false, - 'rocket_self_host_fonts' => true, + 'bypass' => true, + 'do_not_optimize' => false, + 'option' => true, ], - 'expected' => true, + 'expected' => false, + ], + 'testShouldReturnFalseWhenDoNotOptimize' => [ + 'config' => [ + 'bypass' => false, + 'do_not_optimize' => true, + 'option' => true, + ], + 'expected' => false, ], - 'testShouldReturnFalseWhenFeatureDisabled' => [ + 'testShouldReturnFalseWhenOptionDisabled' => [ 'config' => [ - 'local_google_fonts' => true, - 'rocket_self_host_fonts' => false, + 'bypass' => false, + 'do_not_optimize' => true, + 'option' => false, ], 'expected' => false, ], + 'testShouldReturnTrueWhenOptionEnabled' => [ + 'config' => [ + 'bypass' => false, + 'do_not_optimize' => false, + 'option' => true, + ], + 'expected' => true, + ], ]; diff --git a/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php index 033140deda..abbbb5bd40 100644 --- a/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php +++ b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php @@ -1,26 +1,36 @@ donotrocketoptimize = $config['do_not_optimize']; + + $options = Mockery::mock( Options_Data::class ); + $context = new Context( $options ); + + Functions\when( 'rocket_bypass' )->justReturn( $config['bypass'] ); - Functions\expect('get_option') - ->once() - ->with('local_google_fonts') - ->andReturn($config['local_google_fonts']); + $options->shouldReceive( 'get' ) + ->with( 'host_fonts_locally', 0 ) + ->andReturn( $config['option'] ); - $this->assertSame($expected, $context->is_allowed($config)); + $this->assertSame( + $expected, + $context->is_allowed( $config ) + ); } } From 32930b5738e0c5f1ed8f89f519fdf1367d48eb4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:17:19 -0500 Subject: [PATCH 24/37] update instantiation --- inc/Engine/Media/Fonts/ServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 398510eae5..746ddf61f9 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -49,7 +49,8 @@ public function register(): void { $this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class ) ->addArgument( 'media_fonts_settings' ); - $this->getContainer()->add( 'media_fonts_context', Context::class ); + $this->getContainer()->add( 'media_fonts_context', Context::class ) + ->addArgument( $this->getContainer()->get( 'options' ) ); $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class ) ->addArguments( [ From 46e6a8b80efbf58b2f8ac26cddf1dfef8a5bd589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 8 Nov 2024 17:19:21 -0500 Subject: [PATCH 25/37] remove file --- .../Frontend/Controller/rewriteFonts.php | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php diff --git a/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php deleted file mode 100644 index e052bb54c0..0000000000 --- a/tests/Integration/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php +++ /dev/null @@ -1,54 +0,0 @@ -context = Mockery::mock( Context::class ); - - $this->combineV1 = Mockery::mock( Combine::class ); - $this->combineV2 = Mockery::mock( CombineV2::class ); - } - /** - * @dataProvider providerTestData - */ - public function testShouldDoExpected( $config, $expected ) { - $this->context->shouldReceive('is_allowed') - ->once() - ->andReturn( $config['is_allowed'] ); - - $controller = new Controller( $this->context, $this->combineV1, $this->combineV2 ); -// foreach ($config['files'] as $file) { -// $directory = dirname($file['path']); -// if (!is_dir($directory)) { -// mkdir($directory, 0777, true); -// } -// file_put_contents($file['path'], ''); -// } - - $result = $controller->rewrite_fonts( $config['html'] ); - - $this->assertEquals( $expected, $result ); - } - -} From 04dcaedab513be2fafaaff52a46c7001c2e7d1e0 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 11 Nov 2024 13:41:23 +0100 Subject: [PATCH 26/37] Change the position of google font combined url process --- inc/Engine/Optimization/GoogleFonts/Subscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Optimization/GoogleFonts/Subscriber.php b/inc/Engine/Optimization/GoogleFonts/Subscriber.php index 78b79864b6..19232d36e8 100644 --- a/inc/Engine/Optimization/GoogleFonts/Subscriber.php +++ b/inc/Engine/Optimization/GoogleFonts/Subscriber.php @@ -54,7 +54,7 @@ public function __construct( AbstractGFOptimization $combine, AbstractGFOptimiza public static function get_subscribed_events() { return [ 'wp_resource_hints' => [ 'preconnect', 10, 2 ], - 'rocket_buffer' => [ 'process', 1001 ], + 'rocket_buffer' => [ 'process', 17 ], ]; } From 489c0498398964882818d84dd28379649908dac1 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 11 Nov 2024 15:10:25 +0100 Subject: [PATCH 27/37] Fixed linter error --- inc/Engine/Media/Fonts/Frontend/Controller.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 03757cf734..961d185e26 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -17,6 +17,11 @@ class Controller { */ private $context; + /** + * Base url. + * + * @var string + */ private $base_url; /** @@ -25,7 +30,7 @@ class Controller { * @param Context $context Context instance. */ public function __construct( Context $context ) { - $this->context = $context; + $this->context = $context; $this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/'; } @@ -74,7 +79,8 @@ public function rewrite_fonts( string $html ): string { * * @since 3.18 * - * @param string $url Google Fonts URL. + * @param string $hash Font Url has. + * @param string $original_url Fonts Url. * * @return string */ @@ -87,7 +93,7 @@ protected function get_optimized_markup( string $hash, string $original_url ): s $path_array[] = $remain; $path = implode( '/', $path_array ); - $url = $this->base_url . $path . '.css'; + $url = $this->base_url . $path . '.css'; $gf_parameters = wp_parse_url( $original_url, PHP_URL_QUERY ); From 81eaa9c6fc182b8f6fda07aa11a25ccb16b59e75 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 11 Nov 2024 15:18:57 +0100 Subject: [PATCH 28/37] update test --- .../inc/Engine/Optimization/GoogleFonts/Combine/optimize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index 1a97e8b243..82703fa312 100644 --- a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -25,7 +25,7 @@ public function set_up() { 'embed', ], ]; - $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 1001 ); + $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 ); } public function tear_down() { From cab4e867ab4c8b0212d748ecf2b6409f69258490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 12 Nov 2024 10:12:28 -0500 Subject: [PATCH 29/37] refactor duplicate code --- .../Media/Fonts/Frontend/Controller.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 961d185e26..abef504f34 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -56,24 +56,31 @@ public function rewrite_fonts( string $html ): string { } foreach ( $v1_fonts as $font ) { - $hash = md5( $font['url'] ); - - $local = $this->get_optimized_markup( $hash, $font['url'] ); - - $html = str_replace( $font[0], $local, $html ); + $html = $this->replace_font( $font, $html ); } foreach ( $v2_fonts as $font ) { - $hash = md5( $font['url'] ); - - $local = $this->get_optimized_markup( $hash, $font['url'] ); - - $html = str_replace( $font[0], $local, $html ); + $html = $this->replace_font( $font, $html ); } return $html; } + /** + * Replaces the Google Fonts URL with the local one. + * + * @param array $font Font data. + * @param string $html HTML content. + * + * @return string + */ + private function replace_font( $font, $html ): string { + $hash = md5( $font['url'] ); + $local = $this->get_optimized_markup( $hash, $font['url'] ); + + return str_replace( $font[0], $local, $html ); + } + /** * Returns the optimized markup for Google Fonts * From 810538b57bbf01c1b9569bc9b4c0a8e80a8ee9a3 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 13 Nov 2024 09:43:30 +0100 Subject: [PATCH 30/37] Updated namespace --- .../inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php index 07c1f9fcfb..d2bf5940c9 100644 --- a/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php +++ b/tests/Unit/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php @@ -1,7 +1,7 @@ Date: Wed, 13 Nov 2024 14:56:17 +0100 Subject: [PATCH 31/37] Closes #7104: Remove preload tag if host_fonts_locally is enabled --- .../GoogleFonts/AbstractGFOptimization.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php index e3b112402b..b61be9f38a 100644 --- a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php +++ b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php @@ -111,9 +111,19 @@ protected function get_font_display_value(): string { * @return string */ protected function get_optimized_markup( string $url ): string { - return sprintf( - '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet + $markup = sprintf( + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet $url ); + + if ( ! get_rocket_option( 'host_fonts_locally', false ) ) { + $markup = sprintf( + '%2$s', + $url, + $markup + ); + } + + return $markup; } } From a6632103ace81af06fb6bffb85da9f6de035650c Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 13 Nov 2024 15:24:53 +0100 Subject: [PATCH 32/37] Add tests --- .../GoogleFonts/Combine/optimize.php | 146 +++++++++++++----- .../GoogleFonts/CombineV2/optimize.php | 90 ++++++++--- .../GoogleFonts/Combine/optimize.php | 10 +- .../GoogleFonts/CombineV2/optimize.php | 8 +- 4 files changed, 187 insertions(+), 67 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index 7e5f9b8993..4227489c0f 100644 --- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -2,7 +2,8 @@ return [ 'testShouldCombineGoogleFontsWithoutSubsets' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -12,6 +13,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -21,7 +24,8 @@ ', ], 'testShouldUseFilteredDisplayValue' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -31,6 +35,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -41,7 +47,8 @@ 'filtered' => 'optional', ], 'testShouldCombineGoogleFontsWithSubsets' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -51,6 +58,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -60,7 +69,8 @@ ', ], 'testShouldCombineGoogleFontsWithoutSubsetsAndNoEnding|' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -70,6 +80,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -79,7 +91,8 @@ ', ], 'testShouldCombineGoogleFontsWithoutSubsetsWhenMalformedURL' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -89,6 +102,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -98,7 +113,8 @@ ', ], 'testShouldCombineGoogleFontsWithSubsetsWhenMalformedURL' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -108,6 +124,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -119,7 +137,8 @@ ', ], 'testShouldOptimizeSingleGoogleFontsWhenNoParam' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -127,6 +146,8 @@ ', + 'host_local_font' => false, + ], // Expected: Combined HTML. 'expected' => ' @@ -138,14 +159,17 @@ ', ], 'testShouldOptimizeSingleGoogleFontsWhenParam' => [ - 'html' => ' - - Sample Page - - - - - ', + 'config' => [ + 'html' => ' + + Sample Page + + + + + ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -156,7 +180,8 @@ ', ], 'testShouldOptimizeSingleGoogleFontsWhenInvalidParam' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -164,6 +189,8 @@ ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -174,14 +201,17 @@ ', ], 'testShouldOptimizeSingleGoogleFontsWhenEncodedParam' => [ - 'html' => ' - - Sample Page - - - - - ', + 'config' => [ + 'html' => ' + + Sample Page + + + + + ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -192,17 +222,20 @@ ', ], 'testShouldCombineGoogleFontsWhenMultipleTitleTags' => [ - 'html' => ' - - Sample Page - - - - Sample Title 2 - - - - ', + 'config' => [ + 'html' => ' + + Sample Page + + + + Sample Title 2 + + + + ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -213,7 +246,8 @@ ', ], 'testShouldCombineGoogleFontsWhenTitleTagInsideBody' => [ - 'html' => ' + 'config' => [ + 'html' => ' Sample Page @@ -224,6 +258,8 @@ Sample Title 2 ', + 'host_local_font' => false, + ], 'expected' => ' Sample Page @@ -234,7 +270,34 @@ ', ], 'testShouldCombineGoogleFontsWhenTitleTagInsideSvgTag' => [ - 'html' => ' + 'config' => [ + 'html' => ' + + Sample Page + + + + + + +
logo-cacahuete","toggleOpenedIcon":"","closeIcon":"","backIcon":"","dropdownIcon":"","useBreadcrumb":true,"breadcrumbIcon":"","toggleText":"MENU","toggleLoader":true,"backText":"RETOUR","itemIconVisible":"true","itemBadgeVisible":"true","itemDescVisible":"false","loaderColor":"#FCC800","subTrigger":"item"}\'>
+ + ', + 'host_local_font' => false, + ], + 'expected' => ' + + Sample Page + + + +
logo-cacahuete","toggleOpenedIcon":"","closeIcon":"","backIcon":"","dropdownIcon":"","useBreadcrumb":true,"breadcrumbIcon":"","toggleText":"MENU","toggleLoader":true,"backText":"RETOUR","itemIconVisible":"true","itemBadgeVisible":"true","itemDescVisible":"false","loaderColor":"#FCC800","subTrigger":"item"}\'>
+ + ', + ], + 'testShouldCombineGoogleFontsWithoutSubsets' => [ + 'config' => [ + 'html' => ' Sample Page @@ -242,18 +305,17 @@ - -
logo-cacahuete","toggleOpenedIcon":"","closeIcon":"","backIcon":"","dropdownIcon":"","useBreadcrumb":true,"breadcrumbIcon":"","toggleText":"MENU","toggleLoader":true,"backText":"RETOUR","itemIconVisible":"true","itemBadgeVisible":"true","itemDescVisible":"false","loaderColor":"#FCC800","subTrigger":"item"}\'>
', + 'host_local_font' => true, + ], 'expected' => ' - Sample Page + Sample Page - -
logo-cacahuete","toggleOpenedIcon":"","closeIcon":"","backIcon":"","dropdownIcon":"","useBreadcrumb":true,"breadcrumbIcon":"","toggleText":"MENU","toggleLoader":true,"backText":"RETOUR","itemIconVisible":"true","itemBadgeVisible":"true","itemDescVisible":"false","loaderColor":"#FCC800","subTrigger":"item"}\'>
', ], + ]; diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php index 1436ce069d..8c6c5fab43 100644 --- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php @@ -2,8 +2,9 @@ return [ 'shouldReturnGivenHTMLWhenNoRelevantTags' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -12,7 +13,9 @@ ' - , + , + 'host_local_font'=> false + ], 'expected' => ' @@ -25,8 +28,9 @@ ' ], 'shouldReturnTagWithFontDisplayWhenSingleTagGiven' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -35,7 +39,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -49,8 +55,8 @@ ' ], 'shouldNotCombineMultipleTagsWithTextParam' => [ - 'given' => - ' + 'config' => [ + 'html' => ' Sample Page @@ -60,7 +66,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -74,8 +82,9 @@ ' ], 'shouldCombineMultipleTags' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -85,7 +94,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -98,8 +109,9 @@ ' ], 'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -110,7 +122,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -124,8 +138,9 @@ ' ], 'shouldReplaceAnotherFontDisplayValueWithSwap' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -136,7 +151,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -150,8 +167,9 @@ ' ], 'shouldReplaceDisplayValueWithFilteredValue' => [ - 'given' => - ' + 'config' => [ + 'html' => + ' Sample Page @@ -162,7 +180,9 @@ ' - , + , + 'host_local_font' => false, + ], 'expected' => ' @@ -177,4 +197,32 @@ , 'filtered' => 'optional' ], + 'shoudNotAddPreloadTagWhenHostLocalFontEnabled' => [ + 'config' => [ + 'html' => + ' + + + Sample Page + + + + + ' + , + 'host_local_font' => true, + ], + 'expected' => + ' + + + Sample Page + + + + + + ' + ], + ]; diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index b2c8e05ff4..a278be0920 100644 --- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -25,7 +25,7 @@ class Test_Optimize extends TestCase { /** * @dataProvider configTestData */ - public function testShouldCombineGoogleFonts( $html, $expected, $filtered = false ) { + public function testShouldCombineGoogleFonts( $config, $expected, $filtered = false ) { Functions\when( 'wp_parse_url' )->alias( function( $url, $component ) { return parse_url( $url, $component ); } ); @@ -40,17 +40,23 @@ public function testShouldCombineGoogleFonts( $html, $expected, $filtered = fals return str_replace( [ '&', '&' ], '&', $url ); } ); + Functions\expect( 'get_rocket_option' ) + ->once() + ->with( 'host_fonts_locally', false ) + ->andReturn( $config['host_local_font'] ); + if ( false !== $filtered ) { Filters\expectApplied('rocket_combined_google_fonts_display') ->with('swap', Mockery::type(AbstractGFOptimization::class)) ->andReturn( $filtered ); } + $combine = new Combine(); $this->assertSame( $this->format_the_html( $expected ), - $this->format_the_html( $combine->optimize( $html ) ) + $this->format_the_html( $combine->optimize( $config['html'] ) ) ); } } diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php index 5fda6ddfc4..d5210adee2 100644 --- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php @@ -25,7 +25,7 @@ class Test_OptimizeV2 extends TestCase { /** * @dataProvider configTestData */ - public function testShouldCombineV2GoogleFonts( $html, $expected, $filtered = false ) { + public function testShouldCombineV2GoogleFonts( $config, $expected, $filtered = false ) { Functions\when( 'wp_parse_url' )->alias( function ( $url, $component ) { return parse_url( $url, $component ); } ); @@ -39,6 +39,10 @@ public function testShouldCombineV2GoogleFonts( $html, $expected, $filtered = fa return str_replace( [ '&', '&' ], '&', $url ); } ); + Functions\expect( 'get_rocket_option' ) + ->with( 'host_fonts_locally', false ) + ->andReturn( $config['host_local_font'] ); + if ( false !== $filtered ) { Filters\expectApplied('rocket_combined_google_fonts_display') ->with('swap', Mockery::type(AbstractGFOptimization::class)) @@ -49,7 +53,7 @@ public function testShouldCombineV2GoogleFonts( $html, $expected, $filtered = fa $this->assertSame( $this->format_the_html( $expected ), - $this->format_the_html( $combiner->optimize( $html ) ) + $this->format_the_html( $combiner->optimize( $config['html'] ) ) ); } } From 6890cfdc428c67282417baaf2749ed4ed4bd5e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 Nov 2024 16:24:59 -0500 Subject: [PATCH 33/37] filter preload of google fonts --- .../GoogleFonts/AbstractGFOptimization.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php index b61be9f38a..3a4f9399e3 100644 --- a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php +++ b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php @@ -111,19 +111,23 @@ protected function get_font_display_value(): string { * @return string */ protected function get_optimized_markup( string $url ): string { - $markup = sprintf( - '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet - $url - ); - - if ( ! get_rocket_option( 'host_fonts_locally', false ) ) { - $markup = sprintf( - '%2$s', - $url, - $markup + /** + * Filters whether to disable Google Fonts preloading. + * + * @since 3.18 + * + * @param bool $disable_google_fonts_preload Whether to disable Google Fonts preloading. Default false. + */ + if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_google_fonts_preload', false ) ) { + return sprintf( + '', + $url ); } - return $markup; + return sprintf( + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet + $url + ); } } From 9cf845244ffbca0635ec29d01d8db320bf4fa36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 Nov 2024 16:25:13 -0500 Subject: [PATCH 34/37] disable google fonts preload when host fonts locally --- inc/Engine/Media/Fonts/Frontend/Controller.php | 15 +++++++++++++++ inc/Engine/Media/Fonts/Frontend/Subscriber.php | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index abef504f34..82bdc47644 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -110,4 +110,19 @@ protected function get_optimized_markup( string $hash, string $original_url ): s $gf_parameters ); } + + /** + * Disables the preload of Google Fonts. + * + * @param bool $disable Whether to disable the preload of Google Fonts. + * + * @return bool + */ + public function disable_google_fonts_preload( $disable ): bool { + if ( ! $this->context->is_allowed() ) { + return $disable; + } + + return true; + } } diff --git a/inc/Engine/Media/Fonts/Frontend/Subscriber.php b/inc/Engine/Media/Fonts/Frontend/Subscriber.php index 8470df3982..5c0a3faf25 100644 --- a/inc/Engine/Media/Fonts/Frontend/Subscriber.php +++ b/inc/Engine/Media/Fonts/Frontend/Subscriber.php @@ -30,6 +30,7 @@ public function __construct( Controller $frontend_controller ) { public static function get_subscribed_events(): array { return [ 'rocket_buffer' => [ 'rewrite_fonts', 18 ], + 'rocket_disable_google_fonts_preload' => 'disable_google_fonts_preload', ]; } @@ -42,4 +43,15 @@ public static function get_subscribed_events(): array { public function rewrite_fonts( string $html ): string { return $this->frontend_controller->rewrite_fonts( $html ); } + + /** + * Disables the preload of Google Fonts. + * + * @param bool $disable Whether to disable the preload of Google Fonts. + * + * @return bool + */ + public function disable_google_fonts_preload( $disable ): bool { + return $this->frontend_controller->disable_google_fonts_preload( $disable ); + } } From 37fa59646ef61aa3c92f87534b6d0a9e8271922b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 Nov 2024 16:25:18 -0500 Subject: [PATCH 35/37] update tests --- .../GoogleFonts/Combine/optimize.php | 107 ++++++++++-------- .../GoogleFonts/CombineV2/optimize.php | 88 +++++++------- .../GoogleFonts/Combine/optimize.php | 32 +++--- .../GoogleFonts/CombineV2/optimize.php | 30 +++-- 4 files changed, 130 insertions(+), 127 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index 4227489c0f..6e5fe2b8b8 100644 --- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -3,7 +3,10 @@ return [ 'testShouldCombineGoogleFontsWithoutSubsets' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -13,8 +16,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -25,7 +26,10 @@ ], 'testShouldUseFilteredDisplayValue' => [ 'config' => [ - 'html' => ' + 'swap' => 'optional', + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -35,8 +39,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -44,11 +46,13 @@ ', - 'filtered' => 'optional', ], 'testShouldCombineGoogleFontsWithSubsets' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -58,8 +62,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -70,7 +72,10 @@ ], 'testShouldCombineGoogleFontsWithoutSubsetsAndNoEnding|' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -80,8 +85,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -92,7 +95,10 @@ ], 'testShouldCombineGoogleFontsWithoutSubsetsWhenMalformedURL' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -102,8 +108,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -114,7 +118,10 @@ ], 'testShouldCombineGoogleFontsWithSubsetsWhenMalformedURL' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -124,8 +131,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -138,7 +143,10 @@ ], 'testShouldOptimizeSingleGoogleFontsWhenNoParam' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -146,8 +154,6 @@ ', - 'host_local_font' => false, - ], // Expected: Combined HTML. 'expected' => ' @@ -160,7 +166,10 @@ ], 'testShouldOptimizeSingleGoogleFontsWhenParam' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -168,8 +177,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -181,7 +188,10 @@ ], 'testShouldOptimizeSingleGoogleFontsWhenInvalidParam' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -189,8 +199,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -202,7 +210,10 @@ ], 'testShouldOptimizeSingleGoogleFontsWhenEncodedParam' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -210,8 +221,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -223,7 +232,10 @@ ], 'testShouldCombineGoogleFontsWhenMultipleTitleTags' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -234,8 +246,6 @@ ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -247,7 +257,10 @@ ], 'testShouldCombineGoogleFontsWhenTitleTagInsideBody' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -258,8 +271,6 @@ Sample Title 2 ', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -271,7 +282,10 @@ ], 'testShouldCombineGoogleFontsWhenTitleTagInsideSvgTag' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -283,8 +297,6 @@
logo-cacahuete","toggleOpenedIcon":"","closeIcon":"","backIcon":"","dropdownIcon":"","useBreadcrumb":true,"breadcrumbIcon":"","toggleText":"MENU","toggleLoader":true,"backText":"RETOUR","itemIconVisible":"true","itemBadgeVisible":"true","itemDescVisible":"false","loaderColor":"#FCC800","subTrigger":"item"}\'>
', - 'host_local_font' => false, - ], 'expected' => ' Sample Page @@ -295,27 +307,26 @@ ', ], - 'testShouldCombineGoogleFontsWithoutSubsets' => [ + 'testShouldOptimizeSingleGoogleFontsNoPreload' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => true, + ], + 'html' => ' Sample Page - - ', - 'host_local_font' => true, - ], + // Expected: Combined HTML. 'expected' => ' - Sample Page + Sample Page ', ], - ]; diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php index 8c6c5fab43..da2689ed93 100644 --- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php @@ -3,7 +3,10 @@ return [ 'shouldReturnGivenHTMLWhenNoRelevantTags' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -12,10 +15,7 @@ - ' - , - 'host_local_font'=> false - ], + ', 'expected' => ' @@ -29,7 +29,10 @@ ], 'shouldReturnTagWithFontDisplayWhenSingleTagGiven' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -38,10 +41,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -56,7 +56,10 @@ ], 'shouldNotCombineMultipleTagsWithTextParam' => [ 'config' => [ - 'html' => ' + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' Sample Page @@ -65,10 +68,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -83,7 +83,10 @@ ], 'shouldCombineMultipleTags' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -93,10 +96,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -110,7 +110,10 @@ ], 'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -121,10 +124,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -139,7 +139,10 @@ ], 'shouldReplaceAnotherFontDisplayValueWithSwap' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -150,10 +153,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -168,7 +168,10 @@ ], 'shouldReplaceDisplayValueWithFilteredValue' => [ 'config' => [ - 'html' => + 'swap' => 'optional', + 'disable_preload' => false, + ], + 'html' => ' @@ -179,10 +182,7 @@ - ' - , - 'host_local_font' => false, - ], + ', 'expected' => ' @@ -195,34 +195,32 @@ ' , - 'filtered' => 'optional' ], - 'shoudNotAddPreloadTagWhenHostLocalFontEnabled' => [ + 'shouldCombineMultipleTagsNoPreload' => [ 'config' => [ - 'html' => + 'swap' => false, + 'disable_preload' => true, + ], + 'html' => ' Sample Page + - ' - , - 'host_local_font' => true, - ], + ', 'expected' => ' Sample Page - - + ' ], - ]; diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index a278be0920..130c11df5c 100644 --- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -1,12 +1,11 @@ alias( function( $url, $component ) { - return parse_url( $url, $component ); - } ); + public function testShouldCombineGoogleFonts( $config, $html, $expected ) { + $this->stubWpParseUrl(); Functions\when( 'wp_parse_args' )->alias( function( $value ) { parse_str( $value, $r ); @@ -40,23 +37,22 @@ public function testShouldCombineGoogleFonts( $config, $expected, $filtered = fa return str_replace( [ '&', '&' ], '&', $url ); } ); - Functions\expect( 'get_rocket_option' ) - ->once() - ->with( 'host_fonts_locally', false ) - ->andReturn( $config['host_local_font'] ); - - if ( false !== $filtered ) { - Filters\expectApplied('rocket_combined_google_fonts_display') - ->with('swap', Mockery::type(AbstractGFOptimization::class)) - ->andReturn( $filtered ); + if ( false !== $config['swap'] ) { + Filters\expectApplied( 'rocket_combined_google_fonts_display' ) + ->with('swap', Mockery::type( AbstractGFOptimization::class ) ) + ->andReturn( $config['swap'] ); } + Filters\expectApplied( 'rocket_disable_google_fonts_preload' ) + ->andReturn( $config['disable_preload'] ); + + $combine = new Combine(); $this->assertSame( $this->format_the_html( $expected ), - $this->format_the_html( $combine->optimize( $config['html'] ) ) + $this->format_the_html( $combine->optimize( $html ) ) ); } } diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php index d5210adee2..85e5e80ff7 100644 --- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php @@ -1,12 +1,11 @@ alias( function ( $url, $component ) { - return parse_url( $url, $component ); - } ); + public function testShouldCombineV2GoogleFonts( $config, $html, $expected ) { + $this->stubWpParseUrl(); Functions\when( 'wp_parse_args' )->alias( function ( $value ) { parse_str( $value, $r ); @@ -39,21 +36,22 @@ public function testShouldCombineV2GoogleFonts( $config, $expected, $filtered = return str_replace( [ '&', '&' ], '&', $url ); } ); - Functions\expect( 'get_rocket_option' ) - ->with( 'host_fonts_locally', false ) - ->andReturn( $config['host_local_font'] ); + if ( false !== $config['swap'] ) { + Filters\expectApplied( 'rocket_combined_google_fonts_display' ) + ->with('swap', Mockery::type( AbstractGFOptimization::class ) ) + ->andReturn( $config['swap'] ); + } - if ( false !== $filtered ) { - Filters\expectApplied('rocket_combined_google_fonts_display') - ->with('swap', Mockery::type(AbstractGFOptimization::class)) - ->andReturn( $filtered ); + if ( false !== $config['disable_preload'] ) { + Filters\expectApplied( 'rocket_disable_google_fonts_preload' ) + ->andReturn( $config['disable_preload'] ); } $combiner = new CombineV2(); $this->assertSame( $this->format_the_html( $expected ), - $this->format_the_html( $combiner->optimize( $config['html'] ) ) + $this->format_the_html( $combiner->optimize( $html ) ) ); } } From 22e9f618bdae8baf1eebe0cc538c3c61771a2f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 Nov 2024 16:29:42 -0500 Subject: [PATCH 36/37] fix code styling --- inc/Engine/Media/Fonts/Frontend/Subscriber.php | 2 +- inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Subscriber.php b/inc/Engine/Media/Fonts/Frontend/Subscriber.php index 5c0a3faf25..61c437b25e 100644 --- a/inc/Engine/Media/Fonts/Frontend/Subscriber.php +++ b/inc/Engine/Media/Fonts/Frontend/Subscriber.php @@ -29,7 +29,7 @@ public function __construct( Controller $frontend_controller ) { */ public static function get_subscribed_events(): array { return [ - 'rocket_buffer' => [ 'rewrite_fonts', 18 ], + 'rocket_buffer' => [ 'rewrite_fonts', 18 ], 'rocket_disable_google_fonts_preload' => 'disable_google_fonts_preload', ]; } diff --git a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php index 3a4f9399e3..5d5ec3633e 100644 --- a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php +++ b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php @@ -120,7 +120,7 @@ protected function get_optimized_markup( string $url ): string { */ if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_google_fonts_preload', false ) ) { return sprintf( - '', + '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet $url ); } From 85b6385b739a05f8338fe5220683b48c495aa97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 13 Nov 2024 16:50:50 -0500 Subject: [PATCH 37/37] update tests --- .../GoogleFonts/CombineV1V2/optimize.php | 37 ++++++++++++++---- .../GoogleFonts/Combine/optimize.php | 39 ++++++++++++------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php index 620e6f798c..88120d742f 100644 --- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV1V2/optimize.php @@ -2,7 +2,11 @@ return [ 'shouldReturnOptimizedTagWhenSingleTagGiven' => [ - 'given' => + 'config' => [ + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -29,7 +33,11 @@ ' ], 'shouldUseFilteredDisplayValue' => [ - 'given' => + 'config' => [ + 'swap' => 'optional', + 'disable_preload' => false, + ], + 'html' => ' @@ -55,10 +63,13 @@ ' , - 'filtered' => 'optional', ], 'shouldNotCombineMultipleTagsWithTextParam' => [ - 'given' => + 'config' => [ + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -87,7 +98,11 @@ ' ], 'shouldCombineMultipleTags' => [ - 'given' => + 'config' => [ + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -115,7 +130,11 @@ ' ], 'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [ - 'given' => + 'config' => [ + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' @@ -145,7 +164,11 @@ ' ], 'shouldRemovePreconnectWhenNoGoogleFontsPresentOnPage' => [ - 'given' => + 'config' => [ + 'swap' => false, + 'disable_preload' => false, + ], + 'html' => ' diff --git a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index 82703fa312..e4af953874 100644 --- a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -13,11 +13,12 @@ * @group GoogleFonts */ class Test_Optimize extends TestCase { - - private $filter_value; + private $display; + private $disable_preload; public function set_up() { parent::set_up(); + $GLOBALS['wp'] = (object) [ 'query_vars' => [], 'request' => 'http://example.org', @@ -25,47 +26,53 @@ public function set_up() { 'embed', ], ]; + $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 ); } public function tear_down() { remove_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] ); remove_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] ); + remove_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] ); - unset( $this->filter_value ); $this->restoreWpHook('rocket_buffer'); + parent::tear_down(); } /** * @dataProvider addDataProviderV1 */ - public function testShouldCombineGoogleFontsV1( $original, $combined, $filtered = false ) { - $this->doTest( $original, $combined, $filtered ); + public function testShouldCombineGoogleFontsV1( $config, $original, $combined ) { + $this->doTest( $config, $original, $combined ); } /** * @dataProvider addDataProviderV2 */ - public function testShouldCombineGoogleFontsV2( $original, $combined, $filtered = false ) { - $this->doTest( $original, $combined, $filtered ); + public function testShouldCombineGoogleFontsV2( $config, $original, $combined ) { + $this->doTest( $config, $original, $combined ); } /** * @dataProvider addDataProviderV1V2 */ - public function testShouldCombineGoogleFontsV1V2( $original, $combined, $filtered = false ) { - $this->doTest( $original, $combined, $filtered ); + public function testShouldCombineGoogleFontsV1V2( $config, $original, $combined ) { + $this->doTest( $config, $original, $combined ); } - private function doTest( $original, $expected, $filtered ) { + private function doTest( $config, $original, $expected ) { + $this->display = $config['swap']; + $this->disable_preload = $config['disable_preload']; + add_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] ); - if ( $filtered ) { - $this->filter_value = $filtered; + if ( false !== $config['swap'] ) { add_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] ); } + add_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] ); + $actual = apply_filters( 'rocket_buffer', $original ); $this->assertSame( @@ -86,7 +93,11 @@ public function addDataProviderV1V2() { return $this->getTestData( __DIR__ . 'V1V2', 'optimize' ); } - public function set_display_value( $filtered ) { - return $this->filter_value; + public function set_display_value() { + return $this->display; + } + + public function set_disable_preload() { + return $this->disable_preload; } }