-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Host google fonts locally (PR #7164)
Co-authored-by: Gael Robin <[email protected]> Co-authored-by: Rémy Perona <[email protected]> Co-authored-by: Rémy Perona <[email protected]> Co-authored-by: Opeyemi Ibrahim <[email protected]> Co-authored-by: Michael Lee <[email protected]> Co-authored-by: hanna-meda <[email protected]>
- Loading branch information
1 parent
afb8cc2
commit 45d1e3a
Showing
78 changed files
with
3,271 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
- branch-* | ||
- feature/* | ||
- enhancement/* | ||
- fix/* | ||
|
||
jobs: | ||
run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WP_Rocket\Engine\Common; | ||
|
||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use WP_Filesystem_Direct; | ||
|
||
abstract class AbstractFileSystem { | ||
/** | ||
* WP Filesystem instance. | ||
* | ||
* @var WP_Filesystem_Direct | ||
*/ | ||
protected $filesystem; | ||
|
||
/** | ||
* Constructor method. | ||
* Initializes a new instance of the Controller class. | ||
* | ||
* @param WP_Filesystem_Direct $filesystem Filesystem class. | ||
*/ | ||
public function __construct( $filesystem = null ) { | ||
$this->filesystem = $filesystem ?? rocket_direct_filesystem(); | ||
} | ||
|
||
/** | ||
* Write to file. | ||
* | ||
* @param string $file_path File path to store the file. | ||
* @param string $content File content(data). | ||
* | ||
* @return bool | ||
*/ | ||
protected function write_file( string $file_path, string $content ): bool { | ||
return $this->filesystem->put_contents( $file_path, $content, rocket_get_filesystem_perms( 'file' ) ); | ||
} | ||
|
||
/** | ||
* Get the content of a file | ||
* | ||
* @param string $file The file content to get. | ||
* | ||
* @return string | ||
*/ | ||
public function get_file_content( string $file ): string { | ||
if ( ! $this->filesystem->exists( $file ) ) { | ||
return ''; | ||
} | ||
|
||
return $this->filesystem->get_contents( $file ); | ||
} | ||
|
||
/** | ||
* Delete file from a directory | ||
* | ||
* @param string $file_path Path to file that would be deleted. | ||
* | ||
* @return bool | ||
*/ | ||
protected function delete_file( string $file_path ): bool { | ||
return $this->filesystem->delete( $file_path, false, 'f' ); | ||
} | ||
|
||
/** | ||
* Checks if the dir path is writable and create dir if it doesn't exist. | ||
* | ||
* @param string $dir_path The directory to check. | ||
* | ||
* @return bool | ||
*/ | ||
protected function is_folder_writable( string $dir_path ): bool { | ||
if ( ! $this->filesystem->exists( $dir_path ) ) { | ||
rocket_mkdir_p( $dir_path ); | ||
} | ||
|
||
return $this->filesystem->is_writable( $dir_path ); | ||
} | ||
|
||
/** | ||
* Deletes all files in a given directory | ||
* | ||
* @param string $dir_path The directory path. | ||
* | ||
* @return void | ||
*/ | ||
public function delete_all_files_from_directory( $dir_path ): void { | ||
try { | ||
$dir = new RecursiveDirectoryIterator( $dir_path, \FilesystemIterator::SKIP_DOTS ); | ||
|
||
$items = new RecursiveIteratorIterator( $dir, RecursiveIteratorIterator::CHILD_FIRST ); | ||
|
||
foreach ( $items as $item ) { | ||
$this->filesystem->delete( $item ); | ||
} | ||
} catch ( \Exception $e ) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Converts hash to path with filtered number of levels | ||
* | ||
* @since 3.11.4 | ||
* | ||
* @param string $hash md5 hash string. | ||
* | ||
* @return string | ||
*/ | ||
public function hash_to_path( string $hash ): string { | ||
/** | ||
* Filters the number of sub-folders level to create for used CSS storage | ||
* | ||
* @since 3.11.4 | ||
* | ||
* @param int $levels Number of levels. | ||
*/ | ||
$levels = wpm_apply_filters_typed( 'integer', 'rocket_used_css_dir_level', 3 ); | ||
|
||
$base = substr( $hash, 0, $levels ); | ||
$remain = substr( $hash, $levels ); | ||
|
||
$path_array = str_split( $base ); | ||
$path_array[] = $remain; | ||
|
||
return implode( '/', $path_array ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace WP_Rocket\Engine\Media\Fonts\Admin; | ||
|
||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use WP_Rocket\Admin\Options_Data; | ||
use WP_Rocket\Engine\Common\Queue\AbstractASQueue; | ||
|
||
class Data extends AbstractASQueue { | ||
/** | ||
* Options data instance. | ||
* | ||
* @var Options_Data | ||
*/ | ||
private $options; | ||
|
||
/** | ||
* Base path. | ||
* | ||
* @var string | ||
*/ | ||
private $base_path; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Options_Data $options Options data instance. | ||
*/ | ||
public function __construct( Options_Data $options ) { | ||
$this->options = $options; | ||
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/'; | ||
} | ||
|
||
/** | ||
* Schedule data collection. | ||
* | ||
* @return void | ||
*/ | ||
public function schedule_data_collection() { | ||
if ( ! $this->is_enabled() ) { | ||
return; | ||
} | ||
|
||
$this->schedule_recurring( time(), WEEK_IN_SECONDS, 'rocket_fonts_data_collection' ); | ||
} | ||
|
||
/** | ||
* Unschedule data collection. | ||
* | ||
* @return void | ||
*/ | ||
public function unschedule_data_collection() { | ||
$this->cancel( 'rocket_fonts_data_collection' ); | ||
} | ||
|
||
/** | ||
* Collect data. | ||
* | ||
* @return void | ||
*/ | ||
public function collect_data() { | ||
if ( ! $this->is_enabled() ) { | ||
return; | ||
} | ||
|
||
$fonts_data = get_transient( 'rocket_fonts_data_collection' ); | ||
|
||
// If data has been populated, bail out early. | ||
if ( false !== $fonts_data ) { | ||
return; | ||
} | ||
|
||
$fonts = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->base_path . 'google-fonts/fonts/' ) ); | ||
|
||
$allowed_extensions = [ | ||
'woff', | ||
'woff2', | ||
'ttf', | ||
'otf', | ||
]; | ||
|
||
$total_font_count = 0; | ||
$total_font_size = 0; | ||
|
||
foreach ( $fonts as $file ) { | ||
// check file is not a directory. | ||
if ( $file->isDir() ) { | ||
continue; | ||
} | ||
|
||
$extension = strtolower( pathinfo( $file->getFilename(), PATHINFO_EXTENSION ) ); | ||
|
||
if ( in_array( $extension, $allowed_extensions, true ) ) { | ||
++$total_font_count; | ||
$total_font_size += $file->getSize(); | ||
} | ||
} | ||
|
||
set_transient( | ||
'rocket_fonts_data_collection', | ||
[ | ||
'fonts_total_number' => $total_font_count, | ||
'fonts_total_size' => size_format( $total_font_size ), | ||
], | ||
WEEK_IN_SECONDS | ||
); | ||
} | ||
|
||
/** | ||
* Check if the feature & analytics are enabled. | ||
* | ||
* @return bool | ||
*/ | ||
private function is_enabled(): bool { | ||
return $this->options->get( 'host_fonts_locally', 0 ) && $this->options->get( 'analytics_enabled', 0 ); | ||
} | ||
} |
Oops, something went wrong.