Skip to content

Commit

Permalink
Merge beacon branch PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Feb 17, 2025
2 parents f4d511e + 8febaa1 commit fa37434
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -30,6 +31,7 @@ class ServiceProvider extends AbstractServiceProvider {
'atf_activation_factory',
'lrc_context',
'lrc_activation_factory',
'preconnect_context',
];

/**
Expand Down Expand Up @@ -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' );
Expand Down
1 change: 1 addition & 0 deletions inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
46 changes: 46 additions & 0 deletions inc/Engine/Media/PreconnectExternalDomains/Context/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreconnectExternalDomains\Context;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\Context\ContextInterface;

class Context implements ContextInterface {

/**
* Options instance
*
* @var Options_Data
*/
private $options;


/**
* Constructor
*
* @param Options_Data $options Options instance.
*/
public function __construct( Options_Data $options ) {
$this->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 );
}
}
104 changes: 104 additions & 0 deletions inc/Engine/Media/PreconnectExternalDomains/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreconnectExternalDomains;

use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Common\PerformanceHints\AJAX\ControllerInterface as AjaxControllerInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\QueriesInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Table\TableInterface;
use WP_Rocket\Engine\Common\PerformanceHints\FactoryInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface as FrontendControllerInterface;

class Factory implements FactoryInterface {
/**
* Ajax Controller instance.
*
* @var AjaxControllerInterface
*/
protected $ajax_controller;

/**
* Frontend Controller instance.
*
* @var FrontendControllerInterface
*/
protected $frontend_controller;

/**
* Table instance.
*
* @var TableInterface
*/
protected $table;

/**
* Queries instance.
*
* @var QueriesInterface
*/
protected $queries;

/**
* Context instance.
*
* @var ContextInterface
*/
protected $context;

/**
* Instantiate the class.
*
* @param QueriesInterface $queries Preconnect external domains Queries instance.
* @param ContextInterface $context Preconnect external domains Context instance.
*/
public function __construct( QueriesInterface $queries, ContextInterface $context ) {
$this->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;
}
}
58 changes: 58 additions & 0 deletions inc/Engine/Media/PreconnectExternalDomains/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreconnectExternalDomains;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\PreconnectExternalDomains\Context\Context;
use WP_Rocket\Engine\Media\PreconnectExternalDomains\Database\Queries\PreconnectExternalDomains as Query;

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 = [
'preconnect_factory',
'preconnect_query',
'preconnect_context',
];

/**
* 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 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' ),
]
);
}
}
2 changes: 2 additions & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit fa37434

Please sign in to comment.