Skip to content

Commit

Permalink
Add google font providers, subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Nov 4, 2024
1 parent 9f5690d commit d65847c
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 1 deletion.
57 changes: 57 additions & 0 deletions inc/Engine/Media/Fonts/Controller/Fonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Controller;

use WP_Rocket\Engine\Media\Fonts\Provider\Provider;
use WP_Rocket\Engine\Optimization\RegexTrait;

class Fonts {
use RegexTrait;

/**
* Filesystem instance.
*
* @var Filesystem
*/
private $filesystem;

/**
* Provider instance.
*
* @var Provider
*/
private $provider;

/**

Check notice on line 26 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L26

Missing short description in doc comment

Check notice on line 26 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L26

Tabs must be used to indent lines; spaces are not allowed
* @param Provider $provider Provider Instance.

Check notice on line 27 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L27

Tabs must be used to indent lines; spaces are not allowed
* @param Filesystem $filesystem Filesystem Instance.

Check notice on line 28 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L28

Tabs must be used to indent lines; spaces are not allowed
*/

Check notice on line 29 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L29

Expected 5 spaces before asterisk; 4 found

Check notice on line 29 in inc/Engine/Media/Fonts/Controller/Fonts.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Fonts.php#L29

Tabs must be used to indent lines; spaces are not allowed
public function __construct(
Provider $provider,
Filesystem $filesystem
) {
$this->filesystem = $filesystem;
$this->provider = $provider;
}

/**
* Process
*/
public function process( $html ): string {
global $wp;
$clean_html = $this->hide_comments( $html );
$font_links = $this->provider->process( $clean_html );

if ( empty( $font_links ) ) {
return $html;
}
$url = untrailingslashit( home_url( add_query_arg( [], $wp->request ) ) );

foreach ( $font_links as $link ) {
$this->filesystem->write_font_css( $url, $link );
}

return $html;
}
}
35 changes: 35 additions & 0 deletions inc/Engine/Media/Fonts/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Frontend;

use WP_Rocket\Engine\Media\Fonts\Controller\Fonts;
use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {
/**
* Fonts instance
*
* @var Fonts
*/
private $fonts;

/**
* Instantiate the class
*
* @param Fonts $fonts Fonts instance.
*/
public function __construct( Fonts $fonts ) {
$this->fonts = $fonts;
}

public static function get_subscribed_events() {

Check notice on line 26 in inc/Engine/Media/Fonts/Frontend/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Frontend/Subscriber.php#L26

Missing doc comment for function get_subscribed_events()
return [
'rocket_buffer' => [ 'process', 15 ],
];
}

public function process( $html ): string {

Check notice on line 32 in inc/Engine/Media/Fonts/Frontend/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Frontend/Subscriber.php#L32

Missing doc comment for function process()
return $this->fonts->process( $html );
}
}
28 changes: 28 additions & 0 deletions inc/Engine/Media/Fonts/Provider/GoogleFont/CSS2Handler.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\Fonts\Provider\GoogleFont;

use WP_Rocket\Engine\Optimization\RegexTrait;

class CSS2Handler {
use RegexTrait;

public function get_font_from_html( $html ): array {

Check notice on line 11 in inc/Engine/Media/Fonts/Provider/GoogleFont/CSS2Handler.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/GoogleFont/CSS2Handler.php#L11

Missing doc comment for function get_font_from_html()
$font_urls = [];
$clean_html = $this->hide_comments( $html );
$fonts = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])(?<url>(?:https?:)?\/\/fonts\.googleapis\.com\/css2(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $clean_html );

if ( empty( $fonts ) ) {
return [];
}

foreach ( $fonts as $font ) {
if ( isset( $font['url'] ) ) {
$font_urls[] = $font['url'];
}
}

return $font_urls;
}
}
28 changes: 28 additions & 0 deletions inc/Engine/Media/Fonts/Provider/GoogleFont/CSSHandler.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\Fonts\Provider\GoogleFont;

use WP_Rocket\Engine\Optimization\RegexTrait;

class CSSHandler {
use RegexTrait;

public function get_font_from_html( $html ): array {

Check notice on line 11 in inc/Engine/Media/Fonts/Provider/GoogleFont/CSSHandler.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/GoogleFont/CSSHandler.php#L11

Missing doc comment for function get_font_from_html()
$font_urls = [];
$clean_html = $this->hide_comments( $html );
$fonts = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])(?<url>(?:https?:)?\/\/fonts\.googleapis\.com\/css[^\d](?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $clean_html );

if ( empty( $fonts ) ) {
return [];
}

foreach ( $fonts as $font ) {
if ( isset( $font['url'] ) ) {
$font_urls[] = $font['url'];
}
}

return $font_urls;
}
}
67 changes: 67 additions & 0 deletions inc/Engine/Media/Fonts/Provider/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Provider;

use WP_Rocket\Engine\Media\Fonts\Provider\GoogleFont\CSS2Handler;
use WP_Rocket\Engine\Media\Fonts\Provider\GoogleFont\CSSHandler;

class Provider {
/**
* Array of providers
*
* @var array
*/
private $providers;

private $css_handler;

Check notice on line 17 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L17

Missing member variable doc comment

private $css2_handler;

private $provider_methods = [

Check notice on line 21 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L21

Missing member variable doc comment
'google_font' => 'google_font_provider',
'font_awesome' => 'font_awesome_provider',
];

public function __construct(

Check notice on line 26 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L26

Missing doc comment for function __construct()
array $providers,
CSSHandler $css_handler,
CSS2Handler $css2_handler
) {
$this->css2_handler = $css2_handler;
$this->css_handler = $css_handler;
$this->providers = $providers;
}

/**

Check notice on line 36 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L36

Tabs must be used to indent lines; spaces are not allowed
* Process font link

Check notice on line 37 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L37

Tabs must be used to indent lines; spaces are not allowed
* @param string $html

Check notice on line 38 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L38

Missing parameter comment

Check notice on line 38 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L38

Tabs must be used to indent lines; spaces are not allowed

Check notice on line 38 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L38

There must be exactly one blank line before the tags in a doc comment
*

Check notice on line 39 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L39

Tabs must be used to indent lines; spaces are not allowed
* @return array

Check notice on line 40 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L40

Tabs must be used to indent lines; spaces are not allowed
*/

Check notice on line 41 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L41

Expected 5 spaces before asterisk; 4 found

Check notice on line 41 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L41

Tabs must be used to indent lines; spaces are not allowed
public function process( string $html ): array {
$fonts = [];
foreach ( $this->providers as $provider ) {
if ( isset( $this->provider_methods[ $provider ] ) ) {
$font_provider = $this->provider_methods[ $provider ];
$fonts[] = $this->$font_provider( $html );
}
}

return array_merge( ...$fonts );
}

/**
* Google font provider

Check notice on line 55 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L55

Tabs must be used to indent lines; spaces are not allowed
*
* @param string $html
*
* @return array
*/
private function google_font_provider( string $html ): array {

Check warning on line 61 in inc/Engine/Media/Fonts/Provider/Provider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Provider/Provider.php#L61

Avoid unused private methods such as 'google_font_provider'.
return array_merge(
$this->css_handler->get_font_from_html( $html ),
$this->css2_handler->get_font_from_html( $html )
);
}
}
35 changes: 34 additions & 1 deletion inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
namespace WP_Rocket\Engine\Media\Fonts;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;
use WP_Rocket\Engine\Media\Fonts\Provider\GoogleFont\CSS2Handler;
use WP_Rocket\Engine\Media\Fonts\Provider\GoogleFont\CSSHandler;
use WP_Rocket\Engine\Media\Fonts\Provider\Provider as HostFontProvider;
use WP_Rocket\Engine\Media\Fonts\Controller\Fonts as FontsController;
use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;

/**
Expand All @@ -16,7 +21,11 @@ class ServiceProvider extends AbstractServiceProvider {
*
* @var array
*/
protected $provides = [];
protected $provides = [
'fonts_frontend_subscriber',
'fonts_filesystem',
'host_font_provider',
];

/**
* Check if the service provider provides a specific service.
Expand All @@ -35,8 +44,32 @@ public function provides( string $id ): bool {
* @return void
*/
public function register(): void {

$provider_array = [
'google_font',
];

$this->getContainer()->add( 'css2_handler', CSS2Handler::class );
$this->getContainer()->add( 'css_handler', CSSHandler::class );

$this->getContainer()->add( 'fonts_filesystem', Filesystem::class )
->addArgument( rocket_get_constant( 'WP_ROCKET_FONT_CSS_PATH' ) )
->addArgument( rocket_direct_filesystem() );

$this->getContainer()->add( 'host_font_provider', HostFontProvider::class )
->addArguments(
[
$provider_array,
'css_handler',
'css2_handler',
]
);

$this->getContainer()->add( 'fonts_controller', FontsController::class )
->addArgument( $this->getContainer()->get( 'host_font_provider' ) )
->addArgument( $this->getContainer()->get( 'fonts_filesystem' ) );

$this->getContainer()->addShared( 'fonts_frontend_subscriber', FrontendSubscriber::class )
->addArgument( $this->getContainer()->get( 'fonts_controller' ) );
}
}
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ private function init_common_subscribers() {
'performance_hints_admin_subscriber',
'lrc_frontend_subscriber',
'taxonomy_subscriber',
'fonts_frontend_subscriber',
];

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

0 comments on commit d65847c

Please sign in to comment.