Skip to content

Commit

Permalink
Closes #7065: Add a filter to change the output
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Nov 14, 2024
1 parent 6fcdd7a commit aef0d3b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
54 changes: 50 additions & 4 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;
use WP_Filesystem_Direct;

class Controller {
use RegexTrait;
Expand All @@ -24,14 +25,27 @@ class Controller {
*/
private $base_url;


/**
* WordPress filesystem.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;

private $base_path;

/**
* Constructor.
*
* @param Context $context Context instance.
* @param Context $context Context instance.
* @param WP_Filesystem_Direct $filesystem WordPress filesystem.
*/
public function __construct( Context $context ) {
$this->context = $context;
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
public function __construct( Context $context, WP_Filesystem_Direct $filesystem ) {
$this->context = $context;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->filesystem = $filesystem;
}

/**
Expand Down Expand Up @@ -104,6 +118,15 @@ protected function get_optimized_markup( string $hash, string $original_url ): s

$gf_parameters = wp_parse_url( $original_url, PHP_URL_QUERY );

$internal_styling = wpm_apply_filters_typed( 'boolean', 'rocket_internal_fonts_styling', false );
if ( $internal_styling ) {
$raw_path = $this->base_path . $path . '.css';
$internal_style = $this->set_font_internal_style( $gf_parameters, $raw_path );
if ( $internal_style ) {
return $internal_style;
}
}

return sprintf(
'<link rel="stylesheet" href="%1$s" data-wpr-hosted-gf-parameters="%2$s"/>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$url,
Expand All @@ -125,4 +148,27 @@ public function disable_google_fonts_preload( $disable ): bool {

return true;
}

/**
* Sets the font internal style.
*
* @param string $css_path CSS file path.
*
* @return string|bool
*/
private function set_font_internal_style( string $gf_parameters, string $css_path ) {
if ( ! $css_path ) {
return false;
}

if ( ! $this->filesystem->exists( $css_path ) ) {
return false;
}

return sprintf(
'<style data-wpr-hosted-gf-parameters="%1$s">%2$s</style>',
$gf_parameters,
$this->filesystem->get_contents( $css_path )
);
}
}
4 changes: 4 additions & 0 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;
use WP_Filesystem_Direct;

class ServiceProvider extends AbstractServiceProvider {
/**
Expand Down Expand Up @@ -46,6 +47,8 @@ public function provides( string $id ): bool {
*/
public function register(): void {
$this->getContainer()->add( 'media_fonts_settings', Settings::class );
$this->getContainer()->add( 'wp_direct_filesystem', WP_Filesystem_Direct::class )
->addArgument( [] );
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );

Expand All @@ -55,6 +58,7 @@ public function register(): void {
->addArguments(
[
$this->getContainer()->get( 'media_fonts_context' ),
$this->getContainer()->get( 'wp_direct_filesystem' ),
]
);
$this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
Expand Down

0 comments on commit aef0d3b

Please sign in to comment.