Skip to content

Commit

Permalink
:chore: Add service provider, fix rewrite issue with font filepath, a…
Browse files Browse the repository at this point in the history
…dd delete method #7062
  • Loading branch information
Khadreal committed Nov 1, 2024
1 parent cedbd31 commit dc911c6
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 102 deletions.
241 changes: 146 additions & 95 deletions inc/Engine/Media/Fonts/Controller/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,169 @@
namespace WP_Rocket\Engine\Media\Fonts\Controller;

class Filesystem {
/**
* WP Filesystem instance
*
* @var WP_Filesystem_Direct
*/
private $filesystem;

/**
* Path to the fonts storage
*
* @var string
*/
private $path;

/**
* Instantiate the class
*
* @param string $base_path Base path to the fonts storage.
* @param WP_Filesystem_Direct $filesystem WP Filesystem instance.
*/
public function __construct( $base_path, $filesystem = null ) {
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
$this->path = $base_path . get_current_blog_id() . '/';
}

/**
* Write font css to path
*
* @param string $url
* @param string $font_url
*
* @return bool
*/
public function write_font_css( string $url, string $font_url ): bool {
$font_provider = $this->get_font_provider_path( $font_url );
$file = $this->get_fonts_full_path( $font_provider );
/**
* WP Filesystem instance
*
* @var WP_Filesystem_Direct
*/
private $filesystem;

Check failure on line 13 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property WP_Rocket\Engine\Media\Fonts\Controller\Filesystem::$filesystem has unknown class WP_Rocket\Engine\Media\Fonts\Controller\WP_Filesystem_Direct as its type.

/**
* Path to the fonts storage
*
* @var string
*/
private $path;

/**
* Instantiate the class
*
* @param string $base_path Base path to the fonts storage.
* @param WP_Filesystem_Direct $filesystem WP Filesystem instance.
*/
public function __construct( $base_path, $filesystem = null ) {

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

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Parameter $filesystem of method WP_Rocket\Engine\Media\Fonts\Controller\Filesystem::__construct() has invalid type WP_Rocket\Engine\Media\Fonts\Controller\WP_Filesystem_Direct.
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
$this->path = $base_path . get_current_blog_id() . '/';
}

/**
* Write font css to path
*
* @param string $url The url of the page.
* @param string $font_url The font url to save locally.
*
* @return bool
*/
public function write_font_css( string $url, string $font_url ): bool {
$font_provider = $this->get_font_provider_path( $font_url );
$file = $this->get_fonts_full_path( $font_provider, $url );
$css_file_name = $file . md5( $url ) . '.css';
$relative_path = $this->get_fonts_relative_path( $font_provider, $url );

if ( ! rocket_mkdir_p( dirname( $file ) ) ) {
return false;
}
if ( ! rocket_mkdir_p( dirname( $file ) ) ) {
return false;
}

$css_content = $this->download_font( $font_url );
$css_content = $this->download_font( $font_url );

if( ! $css_content ) {
return false;
}
if ( ! $css_content ) {
return false;
}

preg_match_all('/url\((https:\/\/[^)]+)\)/', $css_content, $matches);
$font_urls = $matches[1];
$local_css = $css_content;
preg_match_all( '/url\((https:\/\/[^)]+)\)/', $css_content, $matches );
$font_urls = $matches[1];
$local_css = $css_content;

foreach ( $font_urls as $font_url ) {
$parsed_url = parse_url( $font_url );
$path_parts = explode( '/', trim( $parsed_url['path'], '/' ) );
$local_path = $file . implode('/', $path_parts );
$local_dir = dirname( $local_path );
foreach ( $font_urls as $font_url ) {
$parsed_url = wp_parse_url( $font_url );
$path_parts = explode( '/', trim( $parsed_url['path'], '/' ) );
$local_path = $file . implode( '/', $path_parts );
$local_dir = dirname( $local_path );

rocket_mkdir_p( $local_dir );
rocket_mkdir_p( $local_dir );

if ( ! file_exists( $local_path ) ) {
if ( ! file_exists( $local_path ) ) {
$font_data = wp_remote_get( $font_url );

if ( is_wp_error( $font_data ) ) {
error_log($font_data->get_error_message());
continue;
}

$font_content = wp_remote_retrieve_body( $font_data );

$this->filesystem->put_contents( $local_path, $font_content, rocket_get_filesystem_perms( 'file' ) );

Check failure on line 78 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Call to method put_contents() on an unknown class WP_Rocket\Engine\Media\Fonts\Controller\WP_Filesystem_Direct.
}
}

$local_url = content_url($this->path . implode('/', $path_parts ) );
$local_url = content_url( $relative_path . implode( '/', $path_parts ) );
$local_css = str_replace( $font_url, $local_url, $local_css );
}

return $this->filesystem->put_contents( $css_file_name , $local_css, rocket_get_filesystem_perms( 'file' ) );
}

private function download_font( string $url ) {
$content = wp_remote_retrieve_body( wp_remote_get( $url, [
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'httpversion' => '2.0',
] ) );

if ( ! $content ) {
return false;
}

return $content;
}

/**
* Get the fonts path for the css file.
*
* @param string $font_provider_path Font provider path.
*
* @return string Path for the font file.
*/
private function get_fonts_full_path( string $font_provider_path ): string {
return $this->path . $font_provider_path;
}

/**
* Get the fonts provider path
*
* @param string $font_url The url of the fonts.
*
* @return string
*/
private function get_font_provider_path( string $font_url ): string {
return 'google-fonts/';
}
}

return $this->filesystem->put_contents( $css_file_name, $local_css, rocket_get_filesystem_perms( 'file' ) );

Check failure on line 85 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Call to method put_contents() on an unknown class WP_Rocket\Engine\Media\Fonts\Controller\WP_Filesystem_Direct.
}

/**
* Download font from external url
*
* @param string $url Url of the file to download.
*
* @return bool|string
*/
private function download_font( string $url ): bool|string {
$content = wp_remote_retrieve_body(
wp_remote_get(
$url,
[
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'httpversion' => '2.0',
]
)
);

if ( ! $content ) {
return false;
}

return $content;
}

/**
* Get the fonts path for the css file.
*
* @param string $font_provider_path Font provider path.
* @param string $url Url of the page.
*
* @return string Path for the font file.
*/
private function get_fonts_full_path( string $font_provider_path, string $url ): string {
return $this->path . $font_provider_path . md5( $url ) . '/';
}


/**
* Get the fonts relative paths
*
* @param string $font_provider_path Font provider path.
* @param string $url Url of the page.
*
* @return string
*/
private function get_fonts_relative_path( string $font_provider_path, string $url ): string {
$full_path = $this->path . $font_provider_path;
$relative_path = str_replace( WP_CONTENT_DIR, '', $full_path );

return $relative_path . md5( $url ) . '/';
}

/**
* Get the fonts provider path
*
* @param string $font_url The url of the fonts.
*
* @return string
*/
private function get_font_provider_path( string $font_url ): string {

Check warning on line 148 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Filesystem.php#L148

Avoid unused parameters such as '$font_url'.

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Fonts/Controller/Filesystem.php#L148

The method parameter $font_url is never used
return 'google-fonts/';
}

/**
* Deletes the locally stored fonts for the corresponding url
*
* @since 3.11.4
*
* @param string $url The url of the page to be deleted.
*
* @return bool
*/
public function delete_font_css( string $url ): bool {
$dir = $this->get_fonts_full_path( $this->get_font_provider_path( $url ), $url );

return $this->filesystem->delete( $dir, true, 'd' );

Check failure on line 164 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Call to method delete() on an unknown class WP_Rocket\Engine\Media\Fonts\Controller\WP_Filesystem_Direct.
}

/**
* Deletes all the font CSS files
*/
public function delete_all_font_css() {
// TODO:create method to recursively delete all locally stored fonts.
}
}
35 changes: 28 additions & 7 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@
namespace WP_Rocket\Engine\Media\Fonts;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;

/**
* Service provider for the WP Rocket Font Optimization
*/
class ServiceProvider extends AbstractServiceProvider{
class ServiceProvider extends AbstractServiceProvider {
/**
* Array of services provided by this service provider
*
* @var array
*/
protected $provides = [];

public function provides(string $id): bool {
// TODO: Implement provides() method.
}
/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

public function register(): void {
// TODO: Implement register() method.
}
/**
* Registers the option array in the container
*
* @return void
*/
public function register(): void {
$this->getContainer()->add( 'fonts_filesystem', Filesystem::class )
->addArgument( rocket_get_constant( 'WP_ROCKET_FONT_CSS_PATH' ) )
->addArgument( rocket_direct_filesystem() );
}
}
2 changes: 2 additions & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use WP_Rocket\Engine\Debug\ServiceProvider as DebugServiceProvider;
use WP_Rocket\Engine\Common\PerformanceHints\ServiceProvider as PerformanceHintsServiceProvider;
use WP_Rocket\Engine\Optimization\LazyRenderContent\ServiceProvider as LRCServiceProvider;
use WP_Rocket\Engine\Media\Fonts\ServiceProvider as FontOptimisationServiceProvider;

/**
* Plugin Manager.
Expand Down Expand Up @@ -308,6 +309,7 @@ private function init_common_subscribers() {
$this->container->addServiceProvider( new SaasAdminServiceProvider() );
$this->container->addServiceProvider( new PerformanceHintsServiceProvider() );
$this->container->addServiceProvider( new LRCServiceProvider() );
$this->container->addServiceProvider( new FontOptimisationServiceProvider() );

$common_subscribers = [
'license_subscriber',
Expand Down

0 comments on commit dc911c6

Please sign in to comment.