diff --git a/inc/Engine/Media/Fonts/Context/Context.php b/inc/Engine/Media/Fonts/Context/Context.php
new file mode 100644
index 0000000000..82673b4611
--- /dev/null
+++ b/inc/Engine/Media/Fonts/Context/Context.php
@@ -0,0 +1,27 @@
+run_common_checks(
+ [
+ 'do_not_optimize' => false,
+ 'bypass' => false,
+ 'option' => 'host_fonts_locally',
+ ]
+ );
+
+ return $is_allowed;
+ }
+}
diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php
new file mode 100644
index 0000000000..82bdc47644
--- /dev/null
+++ b/inc/Engine/Media/Fonts/Frontend/Controller.php
@@ -0,0 +1,128 @@
+context = $context;
+ $this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
+ }
+
+ /**
+ * 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;
+ }
+
+ $html_nocomments = $this->hide_comments( $html );
+
+ $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 Fonts Locally' ] );
+ return $html;
+ }
+
+ foreach ( $v1_fonts as $font ) {
+ $html = $this->replace_font( $font, $html );
+ }
+
+ foreach ( $v2_fonts as $font ) {
+ $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
+ *
+ * @since 3.18
+ *
+ * @param string $hash Font Url has.
+ * @param string $original_url Fonts Url.
+ *
+ * @return string
+ */
+ protected function get_optimized_markup( string $hash, string $original_url ): string {
+ $levels = 3;
+ $base = substr( $hash, 0, $levels );
+ $remain = substr( $hash, $levels );
+
+ $path_array = str_split( $base );
+ $path_array[] = $remain;
+
+ $path = implode( '/', $path_array );
+ $url = $this->base_url . $path . '.css';
+
+ $gf_parameters = wp_parse_url( $original_url, PHP_URL_QUERY );
+
+ return sprintf(
+ '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
+ $url,
+ $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
new file mode 100644
index 0000000000..61c437b25e
--- /dev/null
+++ b/inc/Engine/Media/Fonts/Frontend/Subscriber.php
@@ -0,0 +1,57 @@
+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 ],
+ 'rocket_disable_google_fonts_preload' => 'disable_google_fonts_preload',
+ ];
+ }
+
+ /**
+ * 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 );
+ }
+
+ /**
+ * 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 );
+ }
+}
diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php
index 8c68d550f0..746ddf61f9 100644
--- a/inc/Engine/Media/Fonts/ServiceProvider.php
+++ b/inc/Engine/Media/Fonts/ServiceProvider.php
@@ -6,6 +6,9 @@
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\Fonts\Admin\Settings;
use WP_Rocket\Engine\Media\Fonts\Admin\Subscriber as AdminSubscriber;
+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;
class ServiceProvider extends AbstractServiceProvider {
/**
@@ -20,6 +23,9 @@ class ServiceProvider extends AbstractServiceProvider {
protected $provides = [
'media_fonts_settings',
'media_fonts_admin_subscriber',
+ 'media_fonts_context',
+ 'media_fonts_frontend_controller',
+ 'media_fonts_frontend_subscriber',
];
/**
@@ -42,5 +48,18 @@ public function register(): void {
$this->getContainer()->add( 'media_fonts_settings', Settings::class );
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );
+
+ $this->getContainer()->add( 'media_fonts_context', Context::class )
+ ->addArgument( $this->getContainer()->get( 'options' ) );
+ $this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class )
+ ->addArguments(
+ [
+ $this->getContainer()->get( 'media_fonts_context' ),
+ ]
+ );
+ $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
+ ->addArgument(
+ $this->getContainer()->get( 'media_fonts_frontend_controller' )
+ );
}
}
diff --git a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php
index e3b112402b..5d5ec3633e 100644
--- a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php
+++ b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php
@@ -111,6 +111,20 @@ protected function get_font_display_value(): string {
* @return string
*/
protected function get_optimized_markup( string $url ): string {
+ /**
+ * 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(
+ '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
+ $url
+ );
+ }
+
return sprintf(
'', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$url
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 ],
];
}
diff --git a/inc/Plugin.php b/inc/Plugin.php
index 529ac3ed85..01908d8ec0 100644
--- a/inc/Plugin.php
+++ b/inc/Plugin.php
@@ -403,6 +403,7 @@ private function init_common_subscribers() {
'performance_hints_admin_subscriber',
'lrc_frontend_subscriber',
'taxonomy_subscriber',
+ 'media_fonts_frontend_subscriber',
'media_fonts_admin_subscriber',
];
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..d70a9f3055
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Context/Context/isAllowed.php
@@ -0,0 +1,36 @@
+ [
+ 'config' => [
+ 'bypass' => true,
+ 'do_not_optimize' => false,
+ 'option' => true,
+ ],
+ 'expected' => false,
+ ],
+ 'testShouldReturnFalseWhenDoNotOptimize' => [
+ 'config' => [
+ 'bypass' => false,
+ 'do_not_optimize' => true,
+ 'option' => true,
+ ],
+ 'expected' => false,
+ ],
+ 'testShouldReturnFalseWhenOptionDisabled' => [
+ 'config' => [
+ '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/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..2662753c5a
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_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/expected_v1_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v1_v2.php
new file mode 100644
index 0000000000..d88d7dfe75
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_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/expected_v2.php b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/HTML/expected_v2.php
new file mode 100644
index 0000000000..8dcf11ad68
--- /dev/null
+++ 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/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..1b9307af2c
--- /dev/null
+++ b/tests/Fixtures/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php
@@ -0,0 +1,41 @@
+ [
+ 'testShouldReturnOriginalWhenNotAllowed' => [
+ 'config' => [
+ 'is_allowed' => false,
+ ],
+ 'original' => '',
+ 'expected' => '',
+ ],
+ 'testShouldReturnOriginalWhenNoGoogleFonts' => [
+ 'config' => [
+ 'is_allowed' => true,
+ ],
+ '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' ),
+ ],
+ ],
+];
diff --git a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index 7e5f9b8993..6e5fe2b8b8 100644
--- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
+++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
@@ -2,6 +2,10 @@
return [
'testShouldCombineGoogleFontsWithoutSubsets' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -21,6 +25,10 @@
',
],
'testShouldUseFilteredDisplayValue' => [
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -38,9 +46,12 @@
',
- 'filtered' => 'optional',
],
'testShouldCombineGoogleFontsWithSubsets' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -60,6 +71,10 @@
',
],
'testShouldCombineGoogleFontsWithoutSubsetsAndNoEnding|' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -79,6 +94,10 @@
',
],
'testShouldCombineGoogleFontsWithoutSubsetsWhenMalformedURL' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -98,6 +117,10 @@
',
],
'testShouldCombineGoogleFontsWithSubsetsWhenMalformedURL' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -119,6 +142,10 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenNoParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -138,14 +165,18 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
-
- ',
+
+ Sample Page
+
+
+
+
+ ',
'expected' => '
Sample Page
@@ -156,6 +187,10 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenInvalidParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -174,14 +209,18 @@
',
],
'testShouldOptimizeSingleGoogleFontsWhenEncodedParam' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
-
- ',
+
+ Sample Page
+
+
+
+
+ ',
'expected' => '
Sample Page
@@ -192,17 +231,21 @@
',
],
'testShouldCombineGoogleFontsWhenMultipleTitleTags' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
-
- Sample Page
-
-
-
- Sample Title 2
-
-
-
- ',
+
+ Sample Page
+
+
+
+ Sample Title 2
+
+
+
+ ',
'expected' => '
Sample Page
@@ -213,6 +256,10 @@
',
],
'testShouldCombineGoogleFontsWhenTitleTagInsideBody' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
Sample Page
@@ -234,25 +281,51 @@
',
],
'testShouldCombineGoogleFontsWhenTitleTagInsideSvgTag' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
'html' => '
+
+ Sample Page
+
+
+
+
+
+
+
+
+ ',
+ 'expected' => '
- Sample Page
-
-
-
+ Sample Page
',
+ ],
+ 'testShouldOptimizeSingleGoogleFontsNoPreload' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => true,
+ ],
+ 'html' => '
+
+ Sample Page
+
+
+
+
+ ',
+ // Expected: Combined HTML.
'expected' => '
- Sample Page
+ Sample Page
-
-
',
],
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/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
index 1436ce069d..da2689ed93 100644
--- a/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
+++ b/tests/Fixtures/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
@@ -2,8 +2,12 @@
return [
'shouldReturnGivenHTMLWhenNoRelevantTags' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -11,8 +15,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -25,8 +28,12 @@
'
],
'shouldReturnTagWithFontDisplayWhenSingleTagGiven' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -34,8 +41,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -49,8 +55,11 @@
'
],
'shouldNotCombineMultipleTagsWithTextParam' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' => '
Sample Page
@@ -59,8 +68,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -74,8 +82,12 @@
'
],
'shouldCombineMultipleTags' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -84,8 +96,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -98,8 +109,12 @@
'
],
'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -109,8 +124,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -124,8 +138,12 @@
'
],
'shouldReplaceAnotherFontDisplayValueWithSwap' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -135,8 +153,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -150,8 +167,12 @@
'
],
'shouldReplaceDisplayValueWithFilteredValue' => [
- 'given' =>
- '
+ 'config' => [
+ 'swap' => 'optional',
+ 'disable_preload' => false,
+ ],
+ 'html' =>
+ '
Sample Page
@@ -161,8 +182,7 @@
- '
- ,
+ ',
'expected' =>
'
@@ -175,6 +195,32 @@
'
,
- 'filtered' => 'optional'
+ ],
+ 'shouldCombineMultipleTagsNoPreload' => [
+ 'config' => [
+ 'swap' => false,
+ 'disable_preload' => true,
+ ],
+ 'html' =>
+ '
+
+
+ Sample Page
+
+
+
+
+
+ ',
+ 'expected' =>
+ '
+
+
+ Sample Page
+
+
+
+
+ '
],
];
diff --git a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index 1a97e8b243..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', 1001 );
+
+ $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;
}
}
diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php
index d2651ce533..6a3a4ad4d7 100644
--- a/tests/Unit/bootstrap.php
+++ b/tests/Unit/bootstrap.php
@@ -9,7 +9,7 @@
// Set the path and URL to our virtual filesystem.
define( 'WP_ROCKET_CACHE_ROOT_PATH', 'vfs://public/wp-content/cache/' );
-define( 'WP_ROCKET_CACHE_ROOT_URL', 'vfs://public/wp-content/cache/' );
+define( 'WP_ROCKET_CACHE_ROOT_URL', 'http://example.org/wp-content/cache/' );
define( 'OBJECT', 'OBJECT' );
/**
* The original files need to loaded into memory before we mock them with Patchwork. Add files here before the unit
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..abbbb5bd40
--- /dev/null
+++ b/tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php
@@ -0,0 +1,36 @@
+donotrocketoptimize = $config['do_not_optimize'];
+
+ $options = Mockery::mock( Options_Data::class );
+ $context = new Context( $options );
+
+ Functions\when( 'rocket_bypass' )->justReturn( $config['bypass'] );
+
+ $options->shouldReceive( 'get' )
+ ->with( 'host_fonts_locally', 0 )
+ ->andReturn( $config['option'] );
+
+ $this->assertSame(
+ $expected,
+ $context->is_allowed( $config )
+ );
+ }
+}
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..d2bf5940c9
--- /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 ) )
+ );
+ }
+}
diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php
index b2c8e05ff4..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,12 +37,17 @@ public function testShouldCombineGoogleFonts( $html, $expected, $filtered = fals
return str_replace( [ '&', '&' ], '&', $url );
} );
- 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(
diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/CombineV2/optimize.php
index 5fda6ddfc4..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,10 +36,15 @@ public function testShouldCombineV2GoogleFonts( $html, $expected, $filtered = fa
return str_replace( [ '&', '&' ], '&', $url );
} );
- 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'] );
+ }
+
+ if ( false !== $config['disable_preload'] ) {
+ Filters\expectApplied( 'rocket_disable_google_fonts_preload' )
+ ->andReturn( $config['disable_preload'] );
}
$combiner = new CombineV2();