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 @@