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

DRAFT: Closes #7063: Google font - Frontend part #7091

Open
wants to merge 1 commit into
base: feature/3.18
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions inc/Engine/Media/Fonts/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace WP_Rocket\Engine\Media\Fonts;

use WP_Rocket\Engine\Optimization\ContextInterface;

class Context implements ContextInterface {

Check failure on line 7 in inc/Engine/Media/Fonts/Context.php

View workflow job for this annotation

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

Class WP_Rocket\Engine\Media\Fonts\Context implements unknown interface WP_Rocket\Engine\Optimization\ContextInterface.
/**
* Checks if the feature is allowed.
*
* @param array $data Optional. Data to check against.
* @return bool
*/
public function is_allowed( array $data = [] ): bool {
if ( get_option( 'local_google_fonts' ) ) {
return false;
}

return wpm_apply_filters_typed( 'boolean', 'rocket_self_host_fonts', true );
}
}
55 changes: 55 additions & 0 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace WP_Rocket\Engine\Media\Fonts\Frontend;

use WP_Rocket\Engine\Media\Fonts\Context;

class Controller {
/**
* Context instance.
*
* @var Context
*/
private $context;

/**
* Constructor.
*
* @param Context $context Context instance.
*/
public function __construct( Context $context ) {
$this->context = $context;
}

/**
* Rewrites the Google Fonts paths to local ones.
*
* @param string $html HTML content.
* @return string
*/
public function rewrite_fonts( string $html ): string {
if ( ! $this->context->is_allowed() ) {
return $html;
}

// Use the URL hash value to get the CSS file paths.
$pattern = '/<link\s+[^>]*href=["\'](https:\/\/fonts\.googleapis\.com\/css2?[^"\']+)["\'][^>]*>/i';
$html = preg_replace_callback( $pattern, [ $this, 'replace_font_url' ], $html );

return $html;
}

/**
* Replaces the Google Fonts URL with the local one.
*
* @param array $matches Matches from the regex pattern.
* @return string
*/
private function replace_font_url( array $matches ): string {
$google_fonts_url = $matches[1];
$hash = md5( $google_fonts_url );
$local_url = WP_ROCKET_CACHE_URL . "fonts/google-fonts/{$hash}.css";

return str_replace( $google_fonts_url, $local_url, $matches[0] ) . ' data-wpr-hosted-gf-parameters="' . esc_attr( wp_parse_url( $google_fonts_url, PHP_URL_QUERY ) ) . '"';
}
}
44 changes: 44 additions & 0 deletions inc/Engine/Media/Fonts/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace WP_Rocket\Engine\Media\Fonts\Frontend;

use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {
/**
* Frontend Controller instance.
*
* @var Controller
*/
private $frontend_controller;

/**
* Constructor.
*
* @param Controller $frontend_controller Frontend Controller instance.
*/
public function __construct( Controller $frontend_controller ) {
$this->frontend_controller = $frontend_controller;
}

/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'rocket_buffer' => [ 'rewrite_fonts', 18 ],
];
}

/**
* Rewrites the Google Fonts paths to local ones.
*
* @param string $html HTML content.
* @return string
*/
public function rewrite_fonts( string $html ): string {
return $this->frontend_controller->rewrite_fonts( $html );
}
}
32 changes: 32 additions & 0 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace WP_Rocket\Engine\Media\Fonts;

use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Context;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;

class ServiceProvider extends AbstractServiceProvider {

Check failure on line 10 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

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

Non-abstract class WP_Rocket\Engine\Media\Fonts\ServiceProvider contains abstract method provides() from interface WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface.
/**
* @var array
*/
protected $provides = [
'media_fonts_context',
'media_fonts_frontend_controller',
'media_fonts_frontend_subscriber',
];

/**
* Registers the classes.
*
* @return void
*/
public function register() {

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.3 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.3 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.0 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.1 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

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

Return type mixed of method WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() is not covariant with return type void of method WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register().

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP 5.8 with PHP 7.4 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void

Check failure on line 25 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP latest with PHP 8.2 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void
$this->getContainer()->add('media_fonts_context', Context::class);
$this->getContainer()->add('media_fonts_frontend_controller', FrontendController::class)

Check failure on line 27 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

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

Call to an undefined method WP_Rocket\Dependencies\League\Container\Definition\DefinitionInterface::withArgument().
->withArgument('media_fonts_context');
$this->getContainer()->add('media_fonts_frontend_subscriber', FrontendSubscriber::class)

Check failure on line 29 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

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

Call to an undefined method WP_Rocket\Dependencies\League\Container\Definition\DefinitionInterface::withArgument().
->withArgument('media_fonts_frontend_controller');
}
}

Check failure on line 32 in inc/Engine/Media/Fonts/ServiceProvider.php

View workflow job for this annotation

GitHub Actions / WP 5.8 with PHP 7.3 on ubuntu-latest.

Declaration of WP_Rocket\Engine\Media\Fonts\ServiceProvider::register() must be compatible with WP_Rocket\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface::register(): void
3 changes: 3 additions & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use WP_Rocket\Engine\Debug\ServiceProvider as DebugServiceProvider;
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 FontsServiceProvider;

/**
* Plugin Manager.
Expand Down Expand Up @@ -308,6 +309,7 @@ private function init_common_subscribers() {
$this->container->addServiceProvider( new SaasAdminServiceProvider() );
$this->container->addServiceProvider( new PerformanceHintsServiceProvider() );
$this->container->addServiceProvider( new LRCServiceProvider() );
$this->container->addServiceProvider( new FontsServiceProvider() );

$common_subscribers = [
'license_subscriber',
Expand Down Expand Up @@ -401,6 +403,7 @@ private function init_common_subscribers() {
'performance_hints_admin_subscriber',
'lrc_frontend_subscriber',
'taxonomy_subscriber',
'media_fonts_frontend_subscriber',
];

$host_type = HostResolver::get_host_service();
Expand Down
Loading