Skip to content

Commit

Permalink
Add Preload Fonts feature with service provider and context integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Feb 12, 2025
1 parent 5b018e4 commit 5e7d40c
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ServiceProvider extends AbstractServiceProvider {
'performance_hints_warmup_subscriber',
'performance_hints_admin_bar',
'performance_hints_clean',
'preload_fonts_factory',
];

/**
Expand All @@ -74,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
28 changes: 28 additions & 0 deletions inc/Engine/Media/PreloadFonts/Context/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts\Context;

use WP_Rocket\Engine\Common\Context\ContextInterface;

class Context implements ContextInterface {
/**
* 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 ( get_option( 'wp_rocket_no_licence' ) ) {

Check failure on line 17 in inc/Engine/Media/PreloadFonts/Context/Context.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.4 on ubuntu-latest.

Usage of get_option() is discouraged. Use the Option object instead.
return false;
}

/**
* Filters to manage above the fold optimization
*
* @param bool $allow True to allow, false otherwise.
*/
return wpm_apply_filters_typed( 'boolean', 'rocket_preload_fonts_optimization', true );
}
}
112 changes: 112 additions & 0 deletions inc/Engine/Media/PreloadFonts/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts;

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 {

/**
* 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 {
return $this->queries;
}

/**
* Provides a Context object.
*
* @return ContextInterface
*/
public function get_context(): ContextInterface {
return $this->context;
}
}
69 changes: 69 additions & 0 deletions inc/Engine/Media/PreloadFonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts\Frontend;

use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface;

class Controller implements ControllerInterface {
/**
* 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;
}
}
85 changes: 85 additions & 0 deletions inc/Engine/Media/PreloadFonts/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreloadFonts;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\PreloadFonts\Context\Context;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Table\AbstractTable as Table;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\AbstractQueries as Queries;
use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Controller as FrontController;
use WP_Rocket\Engine\Common\PerformanceHints\AJAX\AJAXControllerTrait;



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 = [
'pf_table',
'pf_query',
'pf_ajax_controller',
'pf_controller',
'pf_factory',
'pf_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()->addShared( 'pf_table', Table::class );
$this->getContainer()->add( 'pf_query', Queries::class );
$this->getContainer()->add( 'pf_context', Context::class );

$this->getContainer()->get( 'pf_table' );

$this->getContainer()->add( 'pf_controller', FrontController::class )
->addArguments(
[
$this->getContainer()->get( 'options' ),
$this->getContainer()->get( 'pf_query' ),
$this->getContainer()->get( 'pf_context' ),
]
);

$this->getContainer()->add( 'atf_ajax_controller', AJAXControllerTrait::class )
->addArguments(
[
$this->getContainer()->get( 'pf_query' ),
$this->getContainer()->get( 'pf_context' ),
]
);

$this->getContainer()->addShared( 'pf_factory', Factory::class )
->addArguments(
[
$this->getContainer()->get( 'pf_ajax_controller' ),
$this->getContainer()->get( 'pf_controller' ),
$this->getContainer()->get( 'pf_table' ),
$this->getContainer()->get( 'pf_query' ),
$this->getContainer()->get( 'pf_context' ),
]
);
}
}

0 comments on commit 5e7d40c

Please sign in to comment.