diff --git a/inc/Engine/Media/Fonts/Clean/Clean.php b/inc/Engine/Media/Fonts/Clean/Clean.php new file mode 100644 index 0000000000..6b55d8522a --- /dev/null +++ b/inc/Engine/Media/Fonts/Clean/Clean.php @@ -0,0 +1,117 @@ +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 ] + ); + } +} diff --git a/inc/Engine/Media/Fonts/Clean/Subscriber.php b/inc/Engine/Media/Fonts/Clean/Subscriber.php new file mode 100644 index 0000000000..ff963e48e3 --- /dev/null +++ b/inc/Engine/Media/Fonts/Clean/Subscriber.php @@ -0,0 +1,86 @@ +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 ); + } +} diff --git a/inc/Engine/Media/Fonts/ServiceProvider.php b/inc/Engine/Media/Fonts/ServiceProvider.php index 97fe559e9c..84ae0f64a9 100644 --- a/inc/Engine/Media/Fonts/ServiceProvider.php +++ b/inc/Engine/Media/Fonts/ServiceProvider.php @@ -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; @@ -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', ]; /** @@ -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' ); @@ -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' ); } } diff --git a/inc/Engine/Optimization/Minify/CSS/AdminSubscriber.php b/inc/Engine/Optimization/Minify/CSS/AdminSubscriber.php index d4e29e6485..3fe7255c57 100755 --- a/inc/Engine/Optimization/Minify/CSS/AdminSubscriber.php +++ b/inc/Engine/Optimization/Minify/CSS/AdminSubscriber.php @@ -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 ) { diff --git a/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php index 08814e970a..2962c44b87 100644 --- a/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php +++ b/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php @@ -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', @@ -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() ) { diff --git a/inc/Engine/WPRocketUninstall.php b/inc/Engine/WPRocketUninstall.php index ae30a0ad06..c29345dd31 100644 --- a/inc/Engine/WPRocketUninstall.php +++ b/inc/Engine/WPRocketUninstall.php @@ -113,6 +113,8 @@ class WPRocketUninstall { 'busting', 'critical-css', 'used-css', + 'fonts', + 'background-css', ]; /** diff --git a/inc/Plugin.php b/inc/Plugin.php index eae9d928e4..e8ba4c5f36 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -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(); diff --git a/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php b/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php new file mode 100644 index 0000000000..8a4e8ee886 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php @@ -0,0 +1,27 @@ + [ + '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, + ], +]; diff --git a/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php b/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php new file mode 100644 index 0000000000..b5b1d84f11 --- /dev/null +++ b/tests/Unit/inc/Engine/Media/Fonts/Clean/Clean/cleanOnOptionChange.php @@ -0,0 +1,41 @@ +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 ); + } +}