From aef0d3be591ad27493281956fa7315d2b05fcd99 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 14 Nov 2024 06:00:21 +0100 Subject: [PATCH] Closes #7065: Add a filter to change the output --- .../Media/Fonts/Frontend/Controller.php | 54 +++++++++++++++++-- inc/Engine/Media/Fonts/ServiceProvider.php | 4 ++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 82bdc47644..adf4bfed9a 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -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; @@ -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; } /** @@ -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( '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet $url, @@ -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( + '', + $gf_parameters, + $this->filesystem->get_contents( $css_path ) + ); + } } diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 746ddf61f9..b979fca134 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -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 { /** @@ -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' ); @@ -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 )