diff --git a/inc/Engine/Common/PerformanceHints/Cron/CronTrait.php b/inc/Engine/Common/PerformanceHints/Cron/CronTrait.php index 2ec6db52a6..c19576b795 100644 --- a/inc/Engine/Common/PerformanceHints/Cron/CronTrait.php +++ b/inc/Engine/Common/PerformanceHints/Cron/CronTrait.php @@ -24,6 +24,6 @@ public function deletion_interval( string $filter_name ): object { return $this->queries; } - return $this->queries->set_cleanup_interval( $delete_interval ); // @phpstan-ignore-line + return $this->queries->set_cleanup_interval( $delete_interval ); } } diff --git a/inc/Engine/Common/PerformanceHints/Database/Queries/QueriesInterface.php b/inc/Engine/Common/PerformanceHints/Database/Queries/QueriesInterface.php index dd362f1b52..5371013848 100644 --- a/inc/Engine/Common/PerformanceHints/Database/Queries/QueriesInterface.php +++ b/inc/Engine/Common/PerformanceHints/Database/Queries/QueriesInterface.php @@ -14,4 +14,13 @@ interface QueriesInterface { * @return bool|int Returns a boolean or integer value. The exact return value depends on the implementation. */ public function delete_old_rows(); + + /** + * Sets the cleanup interval. + * + * This method sets the interval at which the cleanup process should run. + * + * @param int $interval The interval in seconds. + */ + public function set_cleanup_interval( int $interval ); } diff --git a/inc/Engine/Common/PerformanceHints/ServiceProvider.php b/inc/Engine/Common/PerformanceHints/ServiceProvider.php index 0a74c07e63..9e7528d974 100644 --- a/inc/Engine/Common/PerformanceHints/ServiceProvider.php +++ b/inc/Engine/Common/PerformanceHints/ServiceProvider.php @@ -75,6 +75,7 @@ public function register(): void { $factory_array = [ $this->getContainer()->get( 'atf_factory' ), $this->getContainer()->get( 'lrc_factory' ), + $this->getContainer()->get( 'preload_fonts_factory' ), ]; foreach ( $factory_array as $factory ) { diff --git a/inc/Engine/Media/PreloadFonts/Context/Context.php b/inc/Engine/Media/PreloadFonts/Context/Context.php index c09a26e004..ed3618f38a 100644 --- a/inc/Engine/Media/PreloadFonts/Context/Context.php +++ b/inc/Engine/Media/PreloadFonts/Context/Context.php @@ -34,6 +34,6 @@ public function is_allowed( array $data = [] ): bool { return false; } - return (bool) $this->options->get( 'rocket_preload_fonts', 1 ); + return wpm_apply_filters_typed( 'boolean', 'rocket_preload_fonts_optimization', (bool) $this->options->get( 'rocket_preload_fonts', 1 ) ); } } diff --git a/inc/Engine/Media/PreloadFonts/Factory.php b/inc/Engine/Media/PreloadFonts/Factory.php new file mode 100644 index 0000000000..41ece3069a --- /dev/null +++ b/inc/Engine/Media/PreloadFonts/Factory.php @@ -0,0 +1,115 @@ +ajax_controller = $ajax_controller; + $this->frontend_controller = $frontend_controller; + $this->table = $table; + $this->queries = $queries; + $this->context = $context; + } + + /** + * Provides an Ajax controller object. + * + * @return AjaxControllerInterface + */ + public function get_ajax_controller(): AjaxControllerInterface { + return $this->ajax_controller; + } + + /** + * Provides a Frontend object. + * + * @return FrontendControllerInterface + */ + public function get_frontend_controller(): FrontendControllerInterface { + return $this->frontend_controller; + } + + /** + * Provides a Table object. + * + * @return TableInterface + */ + public function table(): TableInterface { + return $this->table; + } + + /** + * Provides a Queries object. + * + * @return QueriesInterface + */ + public function queries(): QueriesInterface { + // Defines the interval for deletion and returns Queries object. + return $this->deletion_interval( 'rocket_pf_cleanup_interval' ); + } + + /** + * Provides a Context object. + * + * @return ContextInterface + */ + public function get_context(): ContextInterface { + return $this->context; + } +} diff --git a/inc/Engine/Media/PreloadFonts/Frontend/Controller.php b/inc/Engine/Media/PreloadFonts/Frontend/Controller.php new file mode 100644 index 0000000000..fe6fc7c632 --- /dev/null +++ b/inc/Engine/Media/PreloadFonts/Frontend/Controller.php @@ -0,0 +1,106 @@ +options = $options; + $this->query = $query; + $this->context = $context; + } + + /** + * Applies optimization. + * + * @param string $html HTML content. + * @param object $row Database Row. + * + * @return string + */ + public function optimize( string $html, $row ): string { + // Implement the optimization logic here. + return $html; + } + + /** + * Add custom data like the List of elements to be considered for optimization. + * + * @param array $data Array of data passed in beacon. + * + * @return array + */ + public function add_custom_data( array $data ): array { + $system_fonts = [ + 'serif', + 'sans-serif', + 'monospace', + 'cursive', + 'fantasy', + 'system-ui', + 'ui-serif', + 'ui-sans-serif', + 'ui-monospace', + 'ui-rounded', + 'Arial', + 'Helvetica', + 'Times New Roman', + 'Times', + 'Courier New', + 'Courier', + 'Georgia', + 'Palatino', + 'Garamond', + 'Bookman', + 'Tahoma', + 'Trebuchet MS', + 'Arial Black', + 'Impact', + 'Comic Sans MS', + ]; + + /** + * Filters the list of system fonts to be excluded from optimization. + * + * @param array $system_fonts Array of system fonts. + */ + $system_fonts = wpm_apply_filters_typed( 'array', 'rocket_preload_fonts_system_fonts', $system_fonts ); + + $data['system_fonts'] = $system_fonts; + + return $data; + } +} diff --git a/inc/Engine/Media/PreloadFonts/ServiceProvider.php b/inc/Engine/Media/PreloadFonts/ServiceProvider.php index 3b316d97e1..fabd2fa10a 100644 --- a/inc/Engine/Media/PreloadFonts/ServiceProvider.php +++ b/inc/Engine/Media/PreloadFonts/ServiceProvider.php @@ -8,6 +8,7 @@ use WP_Rocket\Engine\Media\PreloadFonts\Database\Queries\PreloadFonts as PreloadFontsQuery; use WP_Rocket\Engine\Media\PreloadFonts\AJAX\Controller as AJAXController; use WP_Rocket\Engine\Media\PreloadFonts\Context\Context; +use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Controller as FrontendController; use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Subscriber as FrontendSubscriber; class ServiceProvider extends AbstractServiceProvider { @@ -26,6 +27,8 @@ class ServiceProvider extends AbstractServiceProvider { 'preload_fonts_ajax_controller', 'preload_fonts_context', 'preload_fonts_frontend_subscriber', + 'preload_fonts_front_controller', + 'preload_fonts_factory', ]; /** @@ -53,6 +56,14 @@ public function register(): void { $this->getContainer()->add( 'preload_fonts_query', PreloadFontsQuery::class ); $this->getContainer()->add( 'preload_fonts_context', Context::class ) ->addArgument( $options ); + $this->getContainer()->add( 'preload_fonts_front_controller', FrontendController::class ) + ->addArguments( + [ + $this->getContainer()->get( 'options' ), + $this->getContainer()->get( 'preload_fonts_query' ), + $this->getContainer()->get( 'preload_fonts_context' ), + ] + ); $this->getContainer()->add( 'preload_fonts_ajax_controller', AJAXController::class ) ->addArguments( @@ -63,5 +74,16 @@ public function register(): void { ); $this->getContainer()->addShared( 'preload_fonts_frontend_subscriber', FrontendSubscriber::class ); + + $this->getContainer()->addShared( 'preload_fonts_factory', Factory::class ) + ->addArguments( + [ + $this->getContainer()->get( 'preload_fonts_ajax_controller' ), + $this->getContainer()->get( 'preload_fonts_front_controller' ), + $this->getContainer()->get( 'preload_fonts_table' ), + $this->getContainer()->get( 'preload_fonts_query' ), + $this->getContainer()->get( 'preload_fonts_context' ), + ] + ); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6e027d3e20..df0692e14c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -300,16 +300,6 @@ parameters: count: 1 path: inc/Engine/Media/AboveTheFold/Context/Context.php - - - message: "#^Hooks should not be used in ORM classes\\: WP_Rocket\\\\Engine\\\\Media\\\\AboveTheFold\\\\Database\\\\Queries\\\\AboveTheFold\\:\\:apply_filters$#" - count: 1 - path: inc/Engine/Media/AboveTheFold/Database/Queries/AboveTheFold.php - - - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 1 - path: inc/Engine/Media/AboveTheFold/Database/Queries/AboveTheFold.php - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" count: 1 @@ -335,11 +325,6 @@ parameters: count: 3 path: inc/Engine/Media/Lazyload/CSS/Front/Extractor.php - - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 1 - path: inc/Engine/Media/Lazyload/CSS/ServiceProvider.php - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" count: 3 @@ -415,16 +400,6 @@ parameters: count: 1 path: inc/Engine/Optimization/LazyRenderContent/Context/Context.php - - - message: "#^Hooks should not be used in ORM classes\\: WP_Rocket\\\\Engine\\\\Optimization\\\\LazyRenderContent\\\\Database\\\\Queries\\\\LazyRenderContent\\:\\:apply_filters$#" - count: 1 - path: inc/Engine/Optimization/LazyRenderContent/Database/Queries/LazyRenderContent.php - - - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 1 - path: inc/Engine/Optimization/LazyRenderContent/Database/Queries/LazyRenderContent.php - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" count: 2 @@ -742,27 +717,27 @@ parameters: - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 18 + count: 21 path: tests/Integration/AjaxTestCase.php - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 18 + count: 21 path: tests/Integration/ApiTestCase.php - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 18 + count: 21 path: tests/Integration/FilesystemTestCase.php - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 18 + count: 21 path: tests/Integration/RESTVfsTestCase.php - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" - count: 18 + count: 21 path: tests/Integration/TestCase.php - @@ -2185,11 +2160,6 @@ parameters: count: 1 path: tests/Integration/inc/functions/rocketIsPluginActive.php - - - message: "#^Path in include_once\\(\\) \"vfs\\://public/wp\\-content/plugins/wp\\-rocket/inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect\\.php\" is not a file or it does not exist\\.$#" - count: 1 - path: tests/Integration/vfs:/public/wp-content/advanced-cache.php - - message: "#^Usage of apply_filters\\(\\) is discouraged\\. Use wpm_apply_filters_typed\\(\\) instead\\.$#" count: 5 diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html index ac76359cdb..e038a7e2a4 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html @@ -4,4 +4,5 @@ - + + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_double_body_tag.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_double_body_tag.html index 44ba1df4c0..c3515006ff 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_double_body_tag.html +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_double_body_tag.html @@ -18,5 +18,5 @@

Iframe Header

" width="600" height="400" style="border:none;"> - + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_w_beacon.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_w_beacon.html index a4fe6228a8..f2d5eece95 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_w_beacon.html +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_w_beacon.html @@ -3,5 +3,5 @@ Test - + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_atf_opt.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_atf_opt.html index 8e8bf4b6da..9f5b519d52 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_atf_opt.html +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_atf_opt.html @@ -4,5 +4,5 @@ Sample alt - + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_only_lrc_opt.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_only_lrc_opt.html index 7016432590..b8d92ac663 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_only_lrc_opt.html +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/output_with_beacon_and_only_lrc_opt.html @@ -8,5 +8,5 @@
- + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index 05c71f65b5..eba7c2a15a 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -33,6 +33,17 @@ ], ]; +$preload_fonts = [ + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'fonts' => json_encode( [ + 'font1', + 'font2', + ] ), + ], +]; + return [ 'test_data' => [ 'shouldReturnOriginalWhenBypassAndRow' => [ @@ -56,6 +67,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_input, ], @@ -69,6 +81,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_input, ], @@ -83,6 +98,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_input, ], @@ -97,6 +115,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_output_with_beacon, ], @@ -109,6 +130,37 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], + ], + 'expected' => $html_output_with_beacon, + ], + 'shouldAddBeaconWhenOnlyMissingPreloadFontsData' => [ + 'config' => [ + 'html' => $html_input, + 'atf' => [ + 'row' => [ + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/image.jpg', + ] ), + 'viewport' => json_encode( [ + 0 => (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/image2.jpg', + ], + ] ), + ], + ], + ], + 'lrc' => $lrc, + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_output_with_beacon, ], @@ -122,6 +174,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_output_with_beacon, ], @@ -145,6 +200,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output_with_preload, ], @@ -170,6 +226,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output, ], @@ -191,6 +248,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_bg_responsive_imgset_template.php'), ], @@ -212,6 +270,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_bg_responsive_webkit_template.php'), ], @@ -233,6 +292,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_layered_bg.php'), ], @@ -253,6 +313,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_single_bg.php'), ], @@ -273,6 +334,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_responsive.php'), ], @@ -291,6 +353,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_with_relative_img_lcp.php'), ], @@ -309,6 +372,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_with_absolute_img_lcp.php'), ], @@ -327,6 +391,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_image.php'), ], @@ -345,6 +410,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_with_fetchpriority.html'), ], @@ -363,6 +429,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_with_markup_comment.html'), ], @@ -390,6 +457,7 @@ ], ], 'lrc' =>$lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output_with_bg_image_lcp, ], @@ -417,6 +485,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output_with_picture_img_lcp, ], @@ -444,6 +513,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output_with_img_lcp, ], @@ -460,6 +530,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => $html_output, ], @@ -492,6 +563,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_picture.php'), ], @@ -524,6 +596,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_picture_2.php'), ], @@ -556,6 +629,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_picture_3.php'), ], @@ -586,6 +660,7 @@ ], ], 'lrc' => $lrc, + 'preload_fonts' => $preload_fonts, ], 'expected' => file_get_contents(__DIR__ . '/HTML/output_lcp_picture_4.php'), ], @@ -606,6 +681,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_output_with_beacon_and_lcp_opt, ], @@ -616,9 +694,49 @@ 'row' => null, ], 'lrc' => $lrc, + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_output_with_beacon_and_only_lrc_opt, ], + 'ShouldAddBeaconToPageWhenOnlyLrcMissing' => [ + 'config' => [ + 'html' => $html_input_with_domain_img_lcp, + 'atf' => [ + 'row' => [ + 'status' => 'completed', + 'url' => 'http://example.org', + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => 'http://example.org/wp-content/uploads/sample_url_image.png', + ] ), + 'viewport' => json_encode ( [] ), + ], + ], + 'lrc' => [ + 'row' => null, + ], + 'preload_fonts' => [ + 'row' => $preload_fonts, + ], + ], + 'expected' => $html_output_with_beacon_and_lcp_opt, + ], + 'ShouldAddBeauconToPageWhenOnlyLcpIsMissing' => [ + 'config' => [ + 'html' => $html_input_with_only_lrc_opt, + 'atf' => [ + 'row' => null, + ], + 'lrc' => $lrc, + 'preload_fonts' => [ + 'row' => $preload_fonts, + ], + ], + 'expected' => $html_output_with_beacon_and_only_lrc_opt, + + ], 'shouldNotDuplicateBeaconOnAPage' => [ 'config' => [ 'html' => $html_with_double_body, @@ -628,6 +746,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_with_double_body_output, ], @@ -640,6 +761,9 @@ 'lrc' => [ 'row' => null, ], + 'preload_fonts' => [ + 'row' => null, + ], ], 'expected' => $html_input_without_closing_body_tag_output, ], diff --git a/tests/Fixtures/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php b/tests/Fixtures/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php new file mode 100644 index 0000000000..eca9f7b596 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php @@ -0,0 +1,53 @@ + [ + 'config' => [ + 'url' => 'http://example.org', + 'is_mobile' => false, + 'filter' => false, + 'row' => (object) [ + 'preload_fonts' => json_encode( [ + (object) [ + 'db47c7d69edcf4565baa182deb470091', + 'db47c7d69edcf4565baa182deb470092', + ], + ] ), + ], + ], + 'expected' => [ + 'result' => true, + 'message' => true + ], + ], + 'testShouldReturnSuccess' => [ + 'config' => [ + 'url' => 'http://example.org', + 'is_mobile' => false, + 'filter' => true, + 'row' => (object) [ + 'preload_fonts' => json_encode( [ + (object) [ + 'db47c7d69edcf4565baa182deb470091', + 'db47c7d69edcf4565baa182deb470092', + ], + ] ), + ], + ], + 'expected' => [ + 'result' => true, + 'message' => true + ], + ], + 'testShouldReturnError' => [ + 'config' => [ + 'url' => 'http://example.org', + 'is_mobile' => false, + 'row' => false, + 'filter' => true, + ], + 'expected' => [ + 'result' => false, + 'message' => false + ], + ], +]; diff --git a/tests/Fixtures/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php b/tests/Fixtures/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php new file mode 100644 index 0000000000..3167cfca24 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php @@ -0,0 +1,32 @@ + [ + 'config' => [ + 'status' => 'in-progress', + 'fonts' => '["font1", "font2"]', + ], + 'expected' => false, + ], + 'shouldReturnFalseWhenFontsIsEmpty' => [ + 'config' => [ + 'status' => 'completed', + 'fonts' => '', + ], + 'expected' => false, + ], + 'shouldReturnFalseWhenFontsIsEmptyArray' => [ + 'config' => [ + 'status' => 'completed', + 'fonts' => '[]', + ], + 'expected' => false, + ], + 'shouldReturnTrueWhenStatusIsCompletedAndFontsIsNotEmpty' => [ + 'config' => [ + 'status' => 'completed', + 'fonts' => '["font1", "font2"]', + ], + 'expected' => true, + ], +]; diff --git a/tests/Integration/DBTrait.php b/tests/Integration/DBTrait.php index 20c80aed50..424f198187 100644 --- a/tests/Integration/DBTrait.php +++ b/tests/Integration/DBTrait.php @@ -59,6 +59,12 @@ public static function addLrc( array $resource ) { return $lrc_query->add_item( $resource ); } + public static function addPreloadFonts(array $resource) { + $container = apply_filters( 'rocket_container', null ); + $preload_fonts_query = $container->get( 'preload_fonts_query' ); + return $preload_fonts_query->add_item( $resource ); + } + public static function installFresh() { $container = apply_filters( 'rocket_container', null ); @@ -76,6 +82,9 @@ public static function installFresh() { $lrc_table = $container->get( 'lrc_table' ); $lrc_table->install(); + + $preload_fonts_table = $container->get( 'preload_fonts_table' ); + $preload_fonts_table->install(); } public static function installUsedCssTable() { @@ -114,6 +123,15 @@ public static function installLrcTable() { } } + public static function installPreloadFontsTable() { + $container = apply_filters( 'rocket_container', null ); + $preload_fonts_table = $container->get( 'preload_fonts_table' ); + + if ( ! $preload_fonts_table->exists() ) { + $preload_fonts_table->install(); + } + } + public static function uninstallAll() { $container = apply_filters( 'rocket_container', null ); $rucss_usedcss_table = $container->get( 'rucss_usedcss_table' ); @@ -136,6 +154,11 @@ public static function uninstallAll() { if ( $lrc_table->exists() ) { $lrc_table->uninstall(); } + + $preload_fonts_table = $container->get( 'preload_fonts_table' ); + if ( $preload_fonts_table->exists() ) { + $preload_fonts_table->uninstall(); + } } public static function uninstallUsedCssTable() { @@ -170,6 +193,15 @@ public static function uninstallLrcTable() { } } + public static function uninstallPreloadFontsTable() { + $container = apply_filters( 'rocket_container', null ); + $preload_fonts_table = $container->get( 'preload_fonts_table' ); + + if ( $preload_fonts_table->exists() ) { + $preload_fonts_table->uninstall(); + } + } + public static function removeDBHooks() { $container = apply_filters( 'rocket_container', null ); @@ -178,6 +210,7 @@ public static function removeDBHooks() { $container->get( 'preload_caches_table' ), $container->get( 'atf_table' ), $container->get( 'lrc_table' ), + $container->get( 'preload_fonts_table' ), ]; foreach ( $tables as $table ) { diff --git a/tests/Integration/bootstrap.php b/tests/Integration/bootstrap.php index a105c30749..5f499c3baf 100644 --- a/tests/Integration/bootstrap.php +++ b/tests/Integration/bootstrap.php @@ -17,9 +17,10 @@ 'muplugins_loaded', function () { - // Disable ATF & LRC optimizations to prevent DB requests (unrelated to other tests). + // Disable ATF, LRC & Preload fonts optimizations to prevent DB requests (unrelated to other tests). add_filter( 'rocket_above_the_fold_optimization', '__return_false' ); add_filter( 'rocket_lrc_optimization', '__return_false' ); + add_filter( 'rocket_preload_fonts_optimization', '__return_false' ); if ( BootstrapManager::isGroup( 'TranslatePress' ) ) { require WP_ROCKET_TESTS_FIXTURES_DIR . '/classes/TRP_Translate_Press.php'; @@ -266,6 +267,7 @@ function () { if ( BootstrapManager::isGroup( 'PerformanceHints' ) ) { add_filter( 'rocket_above_the_fold_optimization', '__return_true' ); add_filter( 'rocket_lrc_optimization', '__return_true' ); + add_filter( 'rocket_preload_fonts_optimization', '__return_true' ); } // Load the plugin. @@ -286,6 +288,9 @@ function() { $lrc_table = $container->get( 'lrc_table' ); $lrc_table->uninstall(); + + $preload_fonts_table = $container->get( 'preload_fonts_table' ); + $preload_fonts_table->uninstall(); } ); diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index 882b5bb8ff..23ce585afa 100644 --- a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -22,11 +22,13 @@ public static function set_up_before_class() { // Install in set_up_before_class because of exists(). self::installAtfTable(); self::installLrcTable(); + self::installPreloadFontsTable(); } public static function tear_down_after_class() { self::uninstallAtfTable(); self::uninstallLrcTable(); + self::uninstallPreloadFontsTable(); parent::tear_down_after_class(); } @@ -76,6 +78,10 @@ public function testShouldReturnAsExpected( $config, $expected ) { self::addLrc( $config['lrc']['row'] ); } + if ( ! empty( $config['preload_fonts']['row'] ) ) { + self::addPreloadFonts( $config['preload_fonts']['row'] ); + } + if ( isset( $config['filter_delay'] ) ) { add_filter( 'rocket_performance_hints_optimization_delay', [ $this, 'add_delay' ] ); } diff --git a/tests/Integration/inc/Engine/Support/Data/getSupportData.php b/tests/Integration/inc/Engine/Support/Data/getSupportData.php index c97d505485..b831f5fd40 100644 --- a/tests/Integration/inc/Engine/Support/Data/getSupportData.php +++ b/tests/Integration/inc/Engine/Support/Data/getSupportData.php @@ -21,6 +21,7 @@ public function set_up() { add_filter( 'rocket_above_the_fold_optimization', '__return_true' ); add_filter( 'rocket_lrc_optimization', '__return_true' ); + add_filter( 'rocket_preload_fonts_optimization', '__return_true' ); $this->wp_version = $wp_version; } @@ -30,6 +31,8 @@ public function tear_down() { remove_filter( 'rocket_above_the_fold_optimization', '__return_true' ); remove_filter( 'rocket_lrc_optimization', '__return_true' ); + remove_filter( 'rocket_preload_fonts_optimization', '__return_true' ); + $wp_version = $this->wp_version; diff --git a/tests/Unit/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php b/tests/Unit/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php new file mode 100644 index 0000000000..76548c11dd --- /dev/null +++ b/tests/Unit/inc/Engine/Media/PreloadFonts/AJAX/Controller/checkData.php @@ -0,0 +1,81 @@ +query = $this->createPartialMock( PreloadFonts::class, [ 'get_row' ] ); + $this->context = Mockery::mock( Context::class ); + $this->controller = new Controller( $this->query, $this->context ); + $this->temp_post = $_POST; + + $this->stubEscapeFunctions(); + } + + protected function tearDown(): void { + unset( $_POST ); + $_POST = $this->temp_post; + + parent::tearDown(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnExpected( $config, $expected ) { + $this->stubEscapeFunctions(); + $this->stubTranslationFunctions(); + + $_POST = [ + 'url' => addslashes( $config['url'] ), + 'is_mobile' => addslashes( $config['is_mobile'] ), + ]; + + $this->context->shouldReceive( 'is_allowed' ) + ->atMost() + ->once() + ->andReturn( $config['filter'] ); + + Functions\expect( 'check_ajax_referer' ) + ->once() + ->with( 'rocket_beacon', 'rocket_beacon_nonce' ) + ->andReturn( true ); + + Functions\when('esc_url_raw')->alias( + function( $url ){ + return $url; + } + ); + + Functions\when( 'wp_unslash' )->alias( + function ( $value ) { + return is_string( $value ) ? stripslashes( $value ) : $value; + } + ); + + $this->query->method( 'get_row' ) + ->with( $config['url'], $config['is_mobile'] ) + ->willReturn( $config['row'] ); + + $this->assertSame( [ 'preload_fonts' => $expected['message'] ], $this->controller->check_data() ); + } +} diff --git a/tests/Unit/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php b/tests/Unit/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php new file mode 100644 index 0000000000..a0343a3233 --- /dev/null +++ b/tests/Unit/inc/Engine/Media/PreloadFonts/Database/Rows/PreloadFonts/hasPreloadFonts.php @@ -0,0 +1,22 @@ +assertSame($expected, $preloadFonts->has_preload_fonts()); + } +}