Skip to content

Commit

Permalink
Closes #7071 Clean CSS & fonts for host fonts locally (#7110)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona authored Dec 13, 2024
1 parent ea1eee8 commit 2c51c61
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 6 deletions.
117 changes: 117 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Clean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Clean;

use WP_Rocket\Engine\Media\Fonts\Filesystem;

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

/**
* Base path for fonts
*
* @var string
*/
private $base_path;

/**
* Constructor
*
* @param Filesystem $filesystem Filesystem instance.
*/
public function __construct( $filesystem ) {
$this->filesystem = $filesystem;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
}

/**
* Clean fonts CSS files stored locally
*
* @return void
*/
public function clean_fonts_css() {
$path = $this->base_path . 'google-fonts/css/';

$this->filesystem->delete_all_files_from_directory( $path );
}

/**
* Clean fonts files stored locally
*
* @return void
*/
public function clean_fonts() {
$path = $this->base_path . 'google-fonts/fonts/';

$this->filesystem->delete_all_files_from_directory( $path );
}

/**
* Clean CSS & fonts files stored locally on option change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_option_change( $old_value, $value ) {
if ( ! $this->did_setting_change( 'host_fonts_locally', $old_value, $value ) ) {
return;
}

$this->clean_fonts_css();

/**
* Fires when the option to host fonts locally is changed
*
* @since 3.18
*/
do_action( 'rocket_host_fonts_locally_changed' );
}

/**
* Clean CSS & fonts files stored locally on CDN change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_cdn_change( $old_value, $value ) {
if ( ! $this->did_setting_change( 'cdn', $old_value, $value ) ) {
return;
}

if ( ! $this->did_setting_change( 'cdn_cnames', $old_value, $value ) ) {
return;
}

$this->clean_fonts_css();
}

/**
* Checks if the given setting's value changed.
*
* @param string $setting The settings's value to check in the old and new values.
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return bool
*/
private function did_setting_change( $setting, $old_value, $value ) {
return (
array_key_exists( $setting, $old_value )
&&
array_key_exists( $setting, $value )
&&
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
$old_value[ $setting ] != $value[ $setting ]
);
}
}
86 changes: 86 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Clean;

use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {
/**
* Clean instance
*
* @var Clean
*/
private $clean;

/**
* Constructor
*
* @param Clean $clean Clean instance.
*/
public function __construct( Clean $clean ) {
$this->clean = $clean;
}

/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'rocket_after_clean_domain' => 'clean_fonts_css',
'switch_theme' => 'clean_fonts',
'rocket_domain_options_changed' => [
[ 'clean_fonts_css' ],
[ 'clean_fonts' ],
],
'update_option_wp_rocket_settings' => [
[ 'clean_on_option_change', 10, 2 ],
[ 'clean_on_cdn_change', 11, 2 ],
],
];
}

/**
* Clean fonts CSS files stored locally
*
* @return void
*/
public function clean_fonts_css() {
$this->clean->clean_fonts_css();
}

/**
* Clean fonts files stored locally
*
* @return void
*/
public function clean_fonts() {
$this->clean->clean_fonts();
}

/**
* Clean CSS & fonts files stored locally on option change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_option_change( $old_value, $value ) {
$this->clean->clean_on_option_change( $old_value, $value );
}

/**
* Clean CSS & fonts files stored locally on CDN change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_cdn_change( $old_value, $value ) {
$this->clean->clean_on_cdn_change( $old_value, $value );
}
}
17 changes: 12 additions & 5 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use WP_Rocket\Engine\Media\Fonts\Context\SaasContext;
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;
use WP_Rocket\Engine\Media\Fonts\Clean\Subscriber as CleanSubscriber;
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;

Expand All @@ -32,6 +34,8 @@ class ServiceProvider extends AbstractServiceProvider {
'media_fonts_saas_context',
'media_fonts_frontend_controller',
'media_fonts_frontend_subscriber',
'media_fonts_clean',
'media_fonts_clean_subscriber',
];

/**
Expand Down Expand Up @@ -59,9 +63,14 @@ public function register(): void {
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );

$this->getContainer()->add( 'media_fonts_clean', Clean::class )
->addArgument( 'media_fonts_filesystem' );

$this->getContainer()->addShared( 'media_fonts_clean_subscriber', CleanSubscriber::class )
->addArgument( 'media_fonts_clean' );

$this->getContainer()->add( 'media_fonts_optimization_context', OptimizationContext::class )
->addArgument( 'options' );

$this->getContainer()->add( 'media_fonts_saas_context', SaasContext::class )
->addArgument( 'options' );

Expand All @@ -73,9 +82,7 @@ public function register(): void {
'media_fonts_filesystem',
]
);
$this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
->addArgument(
'media_fonts_frontend_controller'
);
$this->getContainer()->addShared( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
->addArgument( 'media_fonts_frontend_controller' );
}
}
1 change: 1 addition & 0 deletions inc/Engine/Optimization/Minify/CSS/AdminSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected function maybe_minify_regenerate( array $new, array $old ) { // phpcs:
'minify_css',
'exclude_css',
'cdn',
'host_fonts_locally',
];

foreach ( $settings_to_check as $setting ) {
Expand Down
3 changes: 2 additions & 1 deletion inc/Engine/Optimization/RUCSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function get_subscribed_events(): array {
'switch_theme' => 'truncate_used_css',
'permalink_structure_changed' => 'truncate_used_css',
'rocket_domain_options_changed' => 'truncate_used_css',
'rocket_host_fonts_locally_changed' => 'delete_used_css_rows',
'wp_trash_post' => 'delete_used_css_on_update_or_delete',
'delete_post' => 'delete_used_css_on_update_or_delete',
'clean_post_cache' => 'delete_used_css_on_update_or_delete',
Expand Down Expand Up @@ -206,7 +207,7 @@ public function truncate_used_css() {
*
* @return void
*/
private function delete_used_css_rows() {
public function delete_used_css_rows() {
$this->used_css->delete_all_used_css();

if ( 0 < $this->used_css->get_not_completed_count() ) {
Expand Down
2 changes: 2 additions & 0 deletions inc/Engine/WPRocketUninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class WPRocketUninstall {
'busting',
'critical-css',
'used-css',
'fonts',
'background-css',
];

/**
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ private function init_common_subscribers() {
'taxonomy_subscriber',
'media_fonts_frontend_subscriber',
'media_fonts_admin_subscriber',
'media_fonts_clean_subscriber',
];

$host_type = HostResolver::get_host_service();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

return [
'testShouldDoNothingWhenOldValueAndNewValueAreNotSet' => [
'old_value' => [],
'value' => [],
'expected' => false,
],
'testShouldDoNothingWhenOldValueAndNewValueAreTheSame' => [
'old_value' => [
'host_fonts_locally' => 0,
],
'value' => [
'host_fonts_locally' => 0,
],
'expected' => false,
],
'testShouldDeleteAllFilesWhenOldValueAndNewValueAreDifferent' => [
'old_value' => [
'host_fonts_locally' => 0,
],
'value' => [
'host_fonts_locally' => 1,
],
'expected' => true,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\Media\Fonts\Clean\Clean;

use Mockery;
use Brain\Monkey\Functions;
use WP_Rocket\Engine\Media\Fonts\Clean\Clean;
use WP_Rocket\Engine\Media\Fonts\Filesystem;
use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Engine\Media\Fonts\Clean\Clean::clean_on_option_change
* @group HostFontsLocally
*/
class Test_CleanOnOptionChange extends TestCase {
private $filesystem;
private $clean;

public function setUp(): void {
parent::setUp();

Functions\when( 'get_current_blog_id' )->justReturn( 1 );

$this->filesystem = Mockery::mock( Filesystem::class );
$this->clean = new Clean( $this->filesystem );
}

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected( $old_value, $value, $expected ) {
if ( $expected ) {
$this->filesystem->shouldReceive( 'delete_all_files_from_directory' )->once();
} else {
$this->filesystem->shouldNotReceive( 'delete_all_files_from_directory' );
}

$this->clean->clean_on_option_change( $old_value, $value );
}
}

0 comments on commit 2c51c61

Please sign in to comment.