diff --git a/inc/Engine/Common/PerformanceHints/Activation/ServiceProvider.php b/inc/Engine/Common/PerformanceHints/Activation/ServiceProvider.php index 03a2e10c4d7..8715bc27850 100644 --- a/inc/Engine/Common/PerformanceHints/Activation/ServiceProvider.php +++ b/inc/Engine/Common/PerformanceHints/Activation/ServiceProvider.php @@ -9,6 +9,7 @@ use WP_Rocket\Engine\Media\AboveTheFold\Activation\ActivationFactory as ATFActivationFactory; use WP_Rocket\Engine\Optimization\LazyRenderContent\Activation\ActivationFactory as LRCActivationFactory; use WP_Rocket\Engine\Optimization\LazyRenderContent\Context\Context as LRCContext; +use WP_Rocket\Engine\Media\PreconnectExternalDomains\Context\Context as PreconnectContext; class ServiceProvider extends AbstractServiceProvider { /** @@ -30,6 +31,7 @@ class ServiceProvider extends AbstractServiceProvider { 'atf_activation_factory', 'lrc_context', 'lrc_activation_factory', + 'preconnect_context', ]; /** @@ -68,6 +70,8 @@ public function register(): void { ] ); + $this->getContainer()->add( 'preconnect_context', PreconnectContext::class ); + $factories = []; $atf_activation_factory = $this->getContainer()->get( 'atf_activation_factory' ); diff --git a/inc/Engine/Common/PerformanceHints/ServiceProvider.php b/inc/Engine/Common/PerformanceHints/ServiceProvider.php index 0a74c07e63b..ca1ea0d157c 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( 'preconnect_factory' ), ]; foreach ( $factory_array as $factory ) { diff --git a/inc/Engine/Media/PreconnectExternalDomains/Context/Context.php b/inc/Engine/Media/PreconnectExternalDomains/Context/Context.php new file mode 100644 index 00000000000..7acd7089cc9 --- /dev/null +++ b/inc/Engine/Media/PreconnectExternalDomains/Context/Context.php @@ -0,0 +1,46 @@ +options = $options; + } + + /** + * Determine if the action is allowed. + * + * @param array $data Data to pass to the context. + * @return bool + */ + public function is_allowed( array $data = [] ): bool { + if ( $this->options->get( 'wp_rocket_no_licence', 0 ) ) { + return false; + } + + /** + * Filters to manage above the fold optimization + * + * @param bool $allow True to allow, false otherwise. + */ + return wpm_apply_filters_typed( 'boolean', 'rocket_preconnect_external_domains_optimization', true ); + } +} diff --git a/inc/Engine/Media/PreconnectExternalDomains/Factory.php b/inc/Engine/Media/PreconnectExternalDomains/Factory.php new file mode 100644 index 00000000000..1668e725dc0 --- /dev/null +++ b/inc/Engine/Media/PreconnectExternalDomains/Factory.php @@ -0,0 +1,104 @@ +context = $context; + $this->queries = $queries; + } + + /** + * Provides an Ajax controller object. + * + * @return AjaxControllerInterface + */ + public function get_ajax_controller(): AjaxControllerInterface { + return $this->ajax_controller; + } + + /** + * Provides a Frontend controller object. + * + * @return FrontendControllerInterface + */ + public function get_frontend_controller(): FrontendControllerInterface { + return $this->frontend_controller; + } + + /** + * Provides a Table interface object. + * + * @return TableInterface + */ + public function table(): TableInterface { + return $this->table; + } + + /** + * Provides a Queries object. + * + * @return QueriesInterface + */ + public function queries(): QueriesInterface { + return $this->queries; + } + + /** + * Provides Context object. + * + * @return ContextInterface + */ + public function get_context(): ContextInterface { + return $this->context; + } +} diff --git a/inc/Engine/Media/PreconnectExternalDomains/ServiceProvider.php b/inc/Engine/Media/PreconnectExternalDomains/ServiceProvider.php new file mode 100644 index 00000000000..7e1157eb9a7 --- /dev/null +++ b/inc/Engine/Media/PreconnectExternalDomains/ServiceProvider.php @@ -0,0 +1,58 @@ +provides, true ); + } + + + /** + * Registers the classes in the container + * + * @return void + */ + public function register(): void { + $this->getContainer()->add( 'preconnect_query', Query::class ); + $this->getContainer()->add( 'preconnect_context', Context::class ) + ->addArgument( + $this->getContainer()->get( 'options' ) + ); + + $this->getContainer()->addShared( 'preconnect_factory', Factory::class ) + ->addArguments( + [ + $this->getContainer()->get( 'preconnect_query' ), + $this->getContainer()->get( 'preconnect_context' ), + ] + ); + } +} diff --git a/inc/Plugin.php b/inc/Plugin.php index 7c7ec851a25..d5c261e62c8 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -55,6 +55,7 @@ use WP_Rocket\Engine\Common\PerformanceHints\ServiceProvider as PerformanceHintsServiceProvider; use WP_Rocket\Engine\Optimization\LazyRenderContent\ServiceProvider as LRCServiceProvider; use WP_Rocket\Engine\Media\Fonts\ServiceProvider as MediaFontsServiceProvider; +use WP_Rocket\Engine\Media\PreconnectExternalDomains\ServiceProvider as PreconnectDomainsServiceProvider; /** * Plugin Manager. @@ -291,6 +292,7 @@ private function init_common_subscribers() { $this->container->addServiceProvider( new PerformanceHintsServiceProvider() ); $this->container->addServiceProvider( new LRCServiceProvider() ); $this->container->addServiceProvider( new MediaFontsServiceProvider() ); + $this->container->addServiceProvider( new PreconnectDomainsServiceProvider() ); $this->container->addServiceProvider( new ThirdPartyServiceProvider() ); $common_subscribers = [