Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7071 Clean CSS & fonts for host fonts locally #7110

Merged
merged 23 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3a4ece1
add Clean class
remyperona Nov 14, 2024
5301e98
add Subscriber
remyperona Nov 14, 2024
56859de
Add new callback
remyperona Nov 14, 2024
7eb91ad
Merge branch 'feature/host-google-fonts' into task/7071-clear-data
remyperona Nov 14, 2024
76b24dd
fix code styling
remyperona Nov 14, 2024
ae3309e
Merge branch 'feature/host-google-fonts' into task/7071-clear-data
remyperona Nov 15, 2024
fd9f39b
Merge branch 'feature/host-google-fonts' into task/7071-clear-data
remyperona Dec 4, 2024
6121ff4
update filter name
remyperona Dec 4, 2024
895bfe4
instantiate clean classes
remyperona Dec 4, 2024
088af97
update clean path
remyperona Dec 4, 2024
e28b971
update hook name
remyperona Dec 4, 2024
8c6ddcc
load clean subscriber
remyperona Dec 4, 2024
71051de
add tests for clean_on_option_change()
remyperona Dec 4, 2024
7f2f3ae
clean minify files when enabling/disabling host fonts locally
remyperona Dec 6, 2024
1ca09d9
clean host fonts CSS when enabling/disabling CDN
remyperona Dec 6, 2024
cee21a4
fix styling
remyperona Dec 6, 2024
c17b04d
add missing comma
remyperona Dec 6, 2024
b92dfcc
Merge branch 'feature/host-google-fonts' into task/7071-clear-data
remyperona Dec 10, 2024
aa97cfe
add deletion of fonts & background css folders on uninstall
remyperona Dec 10, 2024
3cc17c2
separate methods to clean CSS & fonts
remyperona Dec 10, 2024
6c27613
use different callback
remyperona Dec 10, 2024
efe6c4e
also clean CSS
remyperona Dec 12, 2024
1757e20
Merge branch 'feature/host-google-fonts' into task/7071-clear-data
remyperona Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Clean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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 CSS & fonts files stored locally
*
* @return void
*/
public function clean_css_fonts() {
$path = $this->base_path . 'google-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 ( ! isset( $old_value['host_fonts_locally'], $value['host_fonts_locally'] ) ) {
return;
}

if ( $old_value['host_fonts_locally'] === $value['host_fonts_locally'] ) {
return;
}

$this->clean_css_fonts();

/**
* Fires when the option to host fonts locally is changed
*
* @since 3.18
*/
do_action( 'rocket_host_fonts_locally_changed' );
}
}
57 changes: 57 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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_css_fonts',
'update_option_wp_rocket_settings' => [ 'clean_on_option_change', 10, 2 ],
];
}

/**
* Clean CSS & fonts files stored locally
*
* @return void
*/
public function clean_css_fonts() {
$this->clean->clean_css_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 );
}
}
22 changes: 15 additions & 7 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use WP_Rocket\Engine\Media\Fonts\Filesystem;
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\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;
Expand All @@ -31,6 +33,8 @@ class ServiceProvider extends AbstractServiceProvider {
'media_fonts_context',
'media_fonts_frontend_controller',
'media_fonts_frontend_subscriber',
'media_fonts_clean',
'media_fonts_clean_subscriber',
];

/**
Expand Down Expand Up @@ -58,19 +62,23 @@ 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_context', Context::class )
->addArgument( $this->getContainer()->get( 'options' ) );
->addArgument( 'options' );

$this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class )
->addArguments(
[
$this->getContainer()->get( 'media_fonts_context' ),
$this->getContainer()->get( 'media_fonts_filesystem' ),
'media_fonts_context',
'media_fonts_filesystem',
]
);
$this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
->addArgument(
$this->getContainer()->get( '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/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' => 'truncate_used_css',
'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
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 );
}
}
Loading