Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7240: 3.19 - Beacon Insertion #7302

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/Engine/Common/PerformanceHints/Cron/CronTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
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( 'preload_fonts_factory' ),
];

foreach ( $factory_array as $factory ) {
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Media/PreloadFonts/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
115 changes: 115 additions & 0 deletions inc/Engine/Media/PreloadFonts/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts;

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

class Factory implements FactoryInterface {
use CronTrait;

/**
* 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 AjaxControllerInterface $ajax_controller PreloadFonts AJAX Controller instance.
* @param FrontendControllerInterface $frontend_controller PreloadFonts Frontend Controller instance.
* @param TableInterface $table PreloadFonts Table instance.
* @param QueriesInterface $queries PreloadFonts Queries instance.
* @param ContextInterface $context PreloadFonts Context instance.
*/
public function __construct( AjaxControllerInterface $ajax_controller, FrontendControllerInterface $frontend_controller, TableInterface $table, QueriesInterface $queries, ContextInterface $context ) {
$this->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;
}
}
106 changes: 106 additions & 0 deletions inc/Engine/Media/PreloadFonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts\Frontend;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface;
use WP_Rocket\Engine\Media\PreloadFonts\Database\Queries\PreloadFonts as PFQuery;
use WP_Rocket\Engine\Media\PreloadFonts\Context\Context;

class Controller implements ControllerInterface {
/**
* Options instance
*
* @var Options_Data
*/
private $options; // @phpstan-ignore-line Use of this will come later.

/**
* Queries instance
*
* @var PFQuery
*/
private $query; // @phpstan-ignore-line Use of this will come later.

/**
* Context instance.
*
* @var Context
*/
private $context; // @phpstan-ignore-line Use of this will come later.

/**
* Constructor
*
* @param Options_Data $options Options instance.
* @param PFQuery $query Queries instance.
* @param Context $context Context instance.
*/
public function __construct( Options_Data $options, PFQuery $query, Context $context ) {
$this->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;
}
}
22 changes: 22 additions & 0 deletions inc/Engine/Media/PreloadFonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
];

/**
Expand Down Expand Up @@ -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(
Expand All @@ -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' ),
]
);
}
}
Loading
Loading