Skip to content

Commit

Permalink
Closes #7181 fonts data collection (PR #7199)
Browse files Browse the repository at this point in the history
Co-authored-by: WordPressFan <[email protected]>
  • Loading branch information
remyperona and wordpressfan authored Jan 1, 2025
1 parent a60bcda commit a5f59e0
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 3 deletions.
119 changes: 119 additions & 0 deletions inc/Engine/Media/Fonts/Admin/Data.php
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 );
}
}
43 changes: 41 additions & 2 deletions inc/Engine/Media/Fonts/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace WP_Rocket\Engine\Media\Fonts\Admin;

use WP_Rocket\Engine\Admin\Settings\Settings;
use WP_Rocket\Engine\Media\Fonts\Admin\Data;
use WP_Rocket\Engine\Media\Fonts\Admin\Settings as FontsSettings;
use WP_Rocket\Event_Management\Subscriber_Interface;


class Subscriber implements Subscriber_Interface {
/**
* Fonts Settings instance
Expand All @@ -16,13 +16,22 @@ class Subscriber implements Subscriber_Interface {
*/
private $settings;

/**
* Fonts Data instance
*
* @var Data
*/
private $data;

/**
* Instantiate the class
*
* @param FontsSettings $settings Fonts Settings instance.
* @param Data $data Fonts Data instance.
*/
public function __construct( FontsSettings $settings ) {
public function __construct( FontsSettings $settings, Data $data ) {
$this->settings = $settings;
$this->data = $data;
}

/**
Expand All @@ -34,6 +43,9 @@ public static function get_subscribed_events(): array {
return [
'rocket_first_install_options' => [ 'add_option', 16 ],
'rocket_input_sanitize' => [ 'sanitize_option', 10, 2 ],
'admin_init' => 'schedule_data_collection',
'rocket_fonts_data_collection' => 'collect_data',
'rocket_deactivation' => 'unschedule_data_collection',
];
}

Expand All @@ -59,4 +71,31 @@ public function add_option( array $options ): array {
public function sanitize_option( array $input, Settings $settings ): array {
return $this->settings->sanitize_option_value( $input, $settings );
}

/**
* Schedule data collection
*
* @return void
*/
public function schedule_data_collection() {
$this->data->schedule_data_collection();
}

/**
* Unschedule data collection
*
* @return void
*/
public function unschedule_data_collection() {
$this->data->unschedule_data_collection();
}

/**
* Collect data
*
* @return void
*/
public function collect_data() {
$this->data->collect_data();
}
}
11 changes: 10 additions & 1 deletion inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\Fonts\Context\OptimizationContext;
use WP_Rocket\Engine\Media\Fonts\Context\SaasContext;
use WP_Rocket\Engine\Media\Fonts\Admin\Data;
use WP_Rocket\Engine\Media\Fonts\Admin\Settings;
use WP_Rocket\Engine\Media\Fonts\Admin\Subscriber as AdminSubscriber;
use WP_Rocket\Engine\Media\Fonts\Clean\Clean;
Expand All @@ -29,6 +30,7 @@ class ServiceProvider extends AbstractServiceProvider {
protected $provides = [
'media_fonts_filesystem',
'media_fonts_settings',
'media_fonts_data',
'media_fonts_admin_subscriber',
'media_fonts_optimization_context',
'media_fonts_saas_context',
Expand Down Expand Up @@ -60,8 +62,15 @@ public function register(): void {
->addArgument( rocket_direct_filesystem() );

$this->getContainer()->add( 'media_fonts_settings', Settings::class );
$this->getContainer()->add( 'media_fonts_data', Data::class )
->addArgument( 'options' );
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );
->addArguments(
[
'media_fonts_settings',
'media_fonts_data',
]
);

$this->getContainer()->add( 'media_fonts_clean', Clean::class )
->addArgument( 'media_fonts_filesystem' );
Expand Down
6 changes: 6 additions & 0 deletions inc/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ function rocket_analytics_data() {
$data['license_type'] = rocket_get_license_type( $customer_data );
}

$media_font_data = get_transient( 'rocket_fonts_data_collection' );

if ( false !== $media_font_data ) {
$data = array_merge( $data, $media_font_data );
}

return $data;
}

Expand Down
9 changes: 9 additions & 0 deletions inc/functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ function rocket_data_collection_preview_table() {
$html .= '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="column-primary">';
$html .= sprintf( '<strong>%s</strong>', __( 'Anonymized WP Rocket statistics:', 'rocket' ) );
$html .= '</td>';
$html .= '<td>';
$html .= sprintf( '<em>%s</em>', __( 'How WP Rocket features function and perform.', 'rocket' ) );
$html .= '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="column-primary">';
$html .= sprintf( '<strong>%s</strong>', __( 'WP Rocket license type', 'rocket' ) );
Expand Down
88 changes: 88 additions & 0 deletions tests/Fixtures/inc/Engine/Media/Fonts/Admin/Data/collectData.php
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',
],
],
],
];
Loading

0 comments on commit a5f59e0

Please sign in to comment.