From cbe336f98959392d2f090188cb3a379aa71d1683 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 15 Nov 2024 05:51:24 +0100 Subject: [PATCH] Add new filesystem structure --- inc/Engine/Common/AbstractFileSystem.php | 46 +++++++++++++++++ .../Media/Fonts/Controller/Filesystem.php | 49 +++++++++++++++++++ .../Media/Fonts/Frontend/Controller.php | 11 +++-- inc/Engine/Media/Fonts/ServiceProvider.php | 11 ++++- 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 inc/Engine/Common/AbstractFileSystem.php create mode 100644 inc/Engine/Media/Fonts/Controller/Filesystem.php diff --git a/inc/Engine/Common/AbstractFileSystem.php b/inc/Engine/Common/AbstractFileSystem.php new file mode 100644 index 0000000000..bf871bfa20 --- /dev/null +++ b/inc/Engine/Common/AbstractFileSystem.php @@ -0,0 +1,46 @@ +filesystem = $filesystem ?? rocket_direct_filesystem(); + } + + /** + * Checks if a given path exists in the filesystem. + * + * @param string $path The path to check. + * @return bool True if the path exists, false otherwise. + */ + public function exists( string $path ): bool { + return $this->filesystem->exists( $path ); + } + + /** + * Retrieves the contents of a file at the given path. + * + * @param string $path The path to the file. + * @return string|false The file contents on success, false on failure. + */ + public function get_contents( string $path ) { + return $this->filesystem->get_contents( $path ); + } +} diff --git a/inc/Engine/Media/Fonts/Controller/Filesystem.php b/inc/Engine/Media/Fonts/Controller/Filesystem.php new file mode 100644 index 0000000000..dcf568310c --- /dev/null +++ b/inc/Engine/Media/Fonts/Controller/Filesystem.php @@ -0,0 +1,49 @@ +path = $base_path . get_current_blog_id() . '/'; + } + + /** + * Hash the url + * + * @param string $font_url Font url. + * + * @return string + */ + private function hash_url( string $font_url ): string { + return md5( $font_url ); + } +} diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 1cf607ae65..86de5f83a6 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -4,6 +4,7 @@ namespace WP_Rocket\Engine\Media\Fonts\Frontend; use WP_Rocket\Engine\Media\Fonts\Context\Context; +use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem; use WP_Rocket\Engine\Optimization\RegexTrait; use WP_Rocket\Logger\Logger; use WP_Filesystem_Direct; @@ -29,7 +30,7 @@ class Controller { /** * WordPress filesystem. * - * @var WP_Filesystem_Direct + * @var WP_Rocket\Engine\Media\Fonts\Controller\Filesystem; */ private $filesystem; @@ -43,10 +44,10 @@ class Controller { /** * Constructor. * - * @param Context $context Context instance. - * @param WP_Filesystem_Direct|null $filesystem WordPress filesystem. + * @param Context $context Context instance. + * @param WP_Rocket\Engine\Media\Fonts\Controller\Filesystem|null $filesystem WordPress filesystem. */ - public function __construct( Context $context, ?WP_Filesystem_Direct $filesystem ) { + public function __construct( Context $context, ?Filesystem $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() . '/'; @@ -130,7 +131,7 @@ protected function get_optimized_markup( string $hash, string $original_url ): s * * @param bool $enable Tells if we are enabling or not the inline css output. */ - $inline_fonts_css = wpm_apply_filters_typed( 'boolean', 'rocket_host_fonts_locally_inline_css', false ); + $inline_fonts_css = wpm_apply_filters_typed( 'boolean', 'rocket_host_fonts_locally_inline_css', true ); if ( $inline_fonts_css ) { $raw_path = $this->base_path . $path . '.css'; $inline_css = $this->get_font_inline_css( $gf_parameters, $raw_path ); diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index b979fca134..1b2b095e86 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -10,6 +10,7 @@ use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController; use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber; use WP_Filesystem_Direct; +use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem; class ServiceProvider extends AbstractServiceProvider { /** @@ -27,6 +28,7 @@ class ServiceProvider extends AbstractServiceProvider { 'media_fonts_context', 'media_fonts_frontend_controller', 'media_fonts_frontend_subscriber', + 'media_fonts_filesystem', ]; /** @@ -49,6 +51,13 @@ 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()->add( 'media_fonts_filesystem', Filesystem::class ) + ->addArguments( + [ + wpm_apply_filters_typed( 'string', 'rocket_host_font_cache_root', 'fonts/' . get_current_blog_id() ), + 'wp_direct_filesystem', + ] + ); $this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class ) ->addArgument( 'media_fonts_settings' ); @@ -58,7 +67,7 @@ public function register(): void { ->addArguments( [ $this->getContainer()->get( 'media_fonts_context' ), - $this->getContainer()->get( 'wp_direct_filesystem' ), + $this->getContainer()->get( 'media_fonts_filesystem' ), ] ); $this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )