-
Notifications
You must be signed in to change notification settings - Fork 226
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
- Loading branch information
1 parent
ea1eee8
commit 2c51c61
Showing
9 changed files
with
289 additions
and
6 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,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 ] | ||
); | ||
} | ||
} |
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,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 ); | ||
} | ||
} |
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
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
27 changes: 27 additions & 0 deletions
27
tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.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,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, | ||
], | ||
]; |
41 changes: 41 additions & 0 deletions
41
tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.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,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 ); | ||
} | ||
} |