-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
- Loading branch information
There are no files selected for viewing
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
|
||
* @param Provider $provider Provider Instance. | ||
* @param Filesystem $filesystem Filesystem Instance. | ||
*/ | ||
Check notice on line 29 in inc/Engine/Media/Fonts/Controller/Fonts.php
|
||
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; | ||
} | ||
} |
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() { | ||
return [ | ||
'rocket_buffer' => [ 'process', 15 ], | ||
]; | ||
} | ||
|
||
public function process( $html ): string { | ||
return $this->fonts->process( $html ); | ||
} | ||
} |
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 { | ||
$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; | ||
} | ||
} |
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 { | ||
$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; | ||
} | ||
} |
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; | ||
|
||
private $css2_handler; | ||
|
||
private $provider_methods = [ | ||
'google_font' => 'google_font_provider', | ||
'font_awesome' => 'font_awesome_provider', | ||
]; | ||
|
||
public function __construct( | ||
array $providers, | ||
CSSHandler $css_handler, | ||
CSS2Handler $css2_handler | ||
) { | ||
$this->css2_handler = $css2_handler; | ||
$this->css_handler = $css_handler; | ||
$this->providers = $providers; | ||
} | ||
|
||
/** | ||
* Process font link | ||
* @param string $html | ||
Check notice on line 38 in inc/Engine/Media/Fonts/Provider/Provider.php
|
||
* | ||
* @return array | ||
*/ | ||
Check notice on line 41 in inc/Engine/Media/Fonts/Provider/Provider.php
|
||
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 | ||
* | ||
* @param string $html | ||
* | ||
* @return array | ||
*/ | ||
private function google_font_provider( string $html ): array { | ||
return array_merge( | ||
$this->css_handler->get_font_from_html( $html ), | ||
$this->css2_handler->get_font_from_html( $html ) | ||
); | ||
} | ||
} |