-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: WordPressFan <[email protected]>
- Loading branch information
1 parent
a60bcda
commit a5f59e0
Showing
7 changed files
with
330 additions
and
3 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 |
---|---|---|
@@ -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 ); | ||
} | ||
} |
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
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
88 changes: 88 additions & 0 deletions
88
tests/Fixtures/inc/Engine/Media/Fonts/Admin/Data/collectData.php
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,88 @@ | ||
<?php | ||
|
||
return [ | ||
'structure' => [ | ||
'wp-content' => [ | ||
'cache' => [ | ||
'fonts' => [ | ||
'1' => [ | ||
'google-fonts' => [ | ||
'fonts' => [ | ||
's' => [ | ||
'lato' => [ | ||
'v24' => [ | ||
'S6uyw4BMUTPHjx4wXg.woff2' => '', | ||
], | ||
], | ||
'montserrat' => [ | ||
'v40' => [ | ||
'memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4gaVI.woff2' => '', | ||
], | ||
], | ||
'oswald' => [ | ||
'v53' => [ | ||
'TK3_WkUHHAIjg75cFRf3bXL8LICs1_FvsUZiZQ.woff2' => '', | ||
], | ||
], | ||
'roboto' => [ | ||
'v32' => [ | ||
'KFOmCnqEu92Fr1Mu4mxK.woff2' => '', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
'test_data' => [ | ||
'shouldDoNothingWhenOptionIsDisabled' => [ | ||
'config' => [ | ||
'options' => [ | ||
'host_fonts_locally' => 0, | ||
'analytics_enabled' => 1, | ||
], | ||
'transient' => false, | ||
], | ||
'expected' => false, | ||
], | ||
'shouldDoNothingWhenAnalyticsDisabled' => [ | ||
'config' => [ | ||
'options' => [ | ||
'host_fonts_locally' => 1, | ||
'analytics_enabled' => 0, | ||
], | ||
'transient' => false, | ||
], | ||
'expected' => false, | ||
], | ||
'shouldDoNothingWhenDataAlreadyExists' => [ | ||
'config' => [ | ||
'options' => [ | ||
'host_fonts_locally' => 1, | ||
'analytics_enabled' => 1, | ||
], | ||
'transient' => [ | ||
'fonts_total_number' => 4, | ||
'fonts_total_size' => '1.2 MB', | ||
], | ||
], | ||
'expected' => false, | ||
], | ||
'shouldCollectData' => [ | ||
'config' => [ | ||
'options' => [ | ||
'host_fonts_locally' => 1, | ||
'analytics_enabled' => 1, | ||
], | ||
'transient' => false, | ||
], | ||
'expected' => [ | ||
'fonts_total_number' => 4, | ||
'fonts_total_size' => '1.2 MB', | ||
], | ||
], | ||
], | ||
]; |
Oops, something went wrong.