From fee96d7c1cc6d18ee07994f0c48ee92b58646660 Mon Sep 17 00:00:00 2001 From: Cyrille C <18537428+CrochetFeve0251@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:00:41 +0200 Subject: [PATCH] Added logic to clean root files (#5761) Co-authored-by: Vasilis Manthos --- inc/Engine/Preload/Subscriber.php | 3 +- inc/ThirdParty/Plugins/I18n/WPML.php | 105 +++++++++++++++++- ...eDirectoryForDefaultLanguageCleanCache.php | 58 ++++++++++ .../I18n/WPML/removeRootCachedFiles.php | 15 +++ .../Preload/Subscriber/onPermalinkChanged.php | 2 +- ...eDirectoryForDefaultLanguageCleanCache.php | 48 ++++++++ .../I18n/WPML/removeRootCachedFiles.php | 36 ++++++ 7 files changed, 259 insertions(+), 8 deletions(-) create mode 100644 tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php create mode 100644 tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php create mode 100644 tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php create mode 100644 tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index bce6e74640..1b8fe4a201 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -100,6 +100,7 @@ public static function get_subscribed_events() { ], 'rocket_after_process_buffer' => 'update_cache_row', 'rocket_deactivation' => 'on_deactivation', + 'rocket_reset_preload' => 'on_permalink_changed', 'permalink_structure_changed' => 'on_permalink_changed', 'wp_rocket_upgrade' => [ 'on_update', 16, 2 ], 'rocket_rucss_complete_job_status' => 'clean_url', @@ -236,7 +237,7 @@ public function delete_url_on_not_found() { public function on_permalink_changed() { $this->query->remove_all(); $this->queue->cancel_pending_jobs(); - $this->controller->load_initial_sitemap(); + $this->queue->add_job_preload_job_load_initial_sitemap_async(); } /** diff --git a/inc/ThirdParty/Plugins/I18n/WPML.php b/inc/ThirdParty/Plugins/I18n/WPML.php index dfc458fd48..6c60886a72 100644 --- a/inc/ThirdParty/Plugins/I18n/WPML.php +++ b/inc/ThirdParty/Plugins/I18n/WPML.php @@ -3,6 +3,7 @@ namespace WP_Rocket\ThirdParty\Plugins\I18n; use WP_Rocket\Event_Management\Subscriber_Interface; +use WP_Filesystem_Direct; use WP_Rocket\ThirdParty\ReturnTypesTrait; /** @@ -11,22 +12,46 @@ class WPML implements Subscriber_Interface { use ReturnTypesTrait; + /** + * Filesystem instance. + * + * @var WP_Filesystem_Direct + */ + protected $filesystem; + + /** + * Instantiate class. + * + * @param WP_Filesystem_Direct $filesystem Filesystem instance. + */ + public function __construct( WP_Filesystem_Direct $filesystem = null ) { + $this->filesystem = $filesystem ?: rocket_direct_filesystem(); + } + + /** * Events for subscriber to listen to. * * @return array */ public static function get_subscribed_events() { - if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) { - return []; - } $events = [ - 'rocket_rucss_is_home_url' => [ 'is_secondary_home', 10, 2 ], - 'rocket_preload_all_to_pending_condition' => 'clean_only_right_domain', - 'rocket_preload_sitemap_before_queue' => 'add_languages_sitemaps', + 'activate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable', + 'deactivate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable', ]; + if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) { + return $events; + } + + $events['rocket_rucss_is_home_url'] = [ 'is_secondary_home', 10, 2 ]; + $events['rocket_preload_all_to_pending_condition'] = 'clean_only_right_domain'; + $events['rocket_preload_sitemap_before_queue'] = 'add_languages_sitemaps'; + $events['after_rocket_clean_home'] = 'remove_root_cached_files'; + $events['after_rocket_clean_domain'] = 'remove_root_cached_files'; + $events['pre_update_option_icl_sitepress_settings'] = [ 'on_change_directory_for_default_language_clean_cache', 10, 2 ]; + return $events; } @@ -100,4 +125,72 @@ public function add_languages_sitemaps( $sitemaps ) { } return array_unique( $new_sitemaps ); } + + /** + * Remove root files when WPML is active. + * + * @return void + */ + public function remove_root_cached_files() { + $site_url = home_url(); + $host_name = wp_parse_url( $site_url, PHP_URL_HOST ); + $cache_folder_path = _rocket_get_wp_rocket_cache_path() . $host_name . '/'; + $cache_folder_directory = $this->filesystem->dirlist( $cache_folder_path ); + + if ( ! is_array( $cache_folder_directory ) || ! is_array( array_keys( $cache_folder_directory ) ) ) { + return; + } + + foreach ( array_keys( $cache_folder_directory ) as $entry ) { + if ( $this->filesystem->is_dir( $cache_folder_path . $entry ) ) { + continue; + } + $this->filesystem->delete( $cache_folder_path . $entry ); + } + } + + /** + * Reset cache when changing the option. + * + * @param array $new new configurations. + * @param array $old old configurations. + * + * @return array + */ + public function on_change_directory_for_default_language_clean_cache( $new, $old ) { + if ( ! is_array( $old ) || ! is_array( $new ) ) { + return $new; + } + + if ( ! key_exists( 'urls', $old ) || ! key_exists( 'directory_for_default_language', $old['urls'] ) || ! key_exists( 'urls', $new ) || ! key_exists( 'directory_for_default_language', $new['urls'] ) || $new['urls']['directory_for_default_language'] === $old['urls']['directory_for_default_language'] ) { + return $new; + } + + /** + * Reset WP Rocket Preload. + */ + do_action( 'rocket_reset_preload' ); + rocket_clean_domain(); + + return $new; + } + + /** + * Clear the cache when the option language directory is enabled. + * + * @return void + */ + public function maybe_clear_on_disable() { + $option = get_option( 'icl_sitepress_settings' ); + if ( ! $option || ! is_array( $option ) || ! key_exists( 'urls', $option ) || ! key_exists( 'directory_for_default_language', $option['urls'] ) || false === $option['urls']['directory_for_default_language'] ) { + return; + } + + /** + * Reset WP Rocket Preload. + */ + do_action( 'rocket_reset_preload' ); + rocket_clean_cache_dir(); + rocket_clean_domain(); + } } diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php b/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php new file mode 100644 index 0000000000..325167be7a --- /dev/null +++ b/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php @@ -0,0 +1,58 @@ + [ + 'config' => [ + 'old' => [ + + ], + 'new' => [ + + ], + 'should_clean' => false, + ], + 'expected' => [ + + ] + ], + 'SameFieldsShouldNotClean' => [ + 'config' => [ + 'old' => [ + 'urls' => [ + 'directory_for_default_language' => false, + ] + ], + 'new' => [ + 'urls' => [ + 'directory_for_default_language' => false, + ] + ], + 'should_clean' => false, + ], + 'expected' => [ + 'urls' => [ + 'directory_for_default_language' => false, + ] + ] + ], + 'differentFieldsShouldClean' => [ + 'config' => [ + 'old' => [ + 'urls' => [ + 'directory_for_default_language' => false, + ] + ], + 'new' => [ + 'urls' => [ + 'directory_for_default_language' => true, + ] + ], + 'should_clean' => true, + ], + 'expected' => [ + 'urls' => [ + 'directory_for_default_language' => true, + ] + ] + ], + +]; diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php b/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php new file mode 100644 index 0000000000..ffa8120c58 --- /dev/null +++ b/tests/Fixtures/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php @@ -0,0 +1,15 @@ + [ + 'config' => [ + 'home_url' => 'https/example.org', + 'host' => 'example.org', + 'entries' => [ + 'entry_path' => [], + ], + 'path' => 'path', + 'cache_path' => 'pathexample.org/', + 'entry_path' => 'pathexample.org/entry_path', + ], + ], +]; diff --git a/tests/Unit/inc/Engine/Preload/Subscriber/onPermalinkChanged.php b/tests/Unit/inc/Engine/Preload/Subscriber/onPermalinkChanged.php index 8368dd8831..fe31bb7c02 100644 --- a/tests/Unit/inc/Engine/Preload/Subscriber/onPermalinkChanged.php +++ b/tests/Unit/inc/Engine/Preload/Subscriber/onPermalinkChanged.php @@ -38,7 +38,7 @@ protected function setUp(): void } public function testShouldDoAsExpected() { - $this->controller->expects()->load_initial_sitemap(); + $this->queue->expects()->add_job_preload_job_load_initial_sitemap_async(); $this->query->expects(self::once())->method('remove_all'); $this->queue->expects()->cancel_pending_jobs(); $this->subscriber->on_permalink_changed(); diff --git a/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php b/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php new file mode 100644 index 0000000000..4173b7dd5e --- /dev/null +++ b/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/onChangeDirectoryForDefaultLanguageCleanCache.php @@ -0,0 +1,48 @@ +filesystem = Mockery::mock(WP_Filesystem_Direct::class); + + $this->wpml = new WPML($this->filesystem); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnAsExpected( $config, $expected ) + { + if($config['should_clean']) { + Functions\expect('rocket_clean_domain'); + } else { + Functions\expect('rocket_clean_domain')->never(); + } + $this->assertSame($expected, $this->wpml->on_change_directory_for_default_language_clean_cache($config['new'], $config['old'])); + + } +} diff --git a/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php b/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php new file mode 100644 index 0000000000..e4ac8e2466 --- /dev/null +++ b/tests/Unit/inc/ThirdParty/Plugins/I18n/WPML/removeRootCachedFiles.php @@ -0,0 +1,36 @@ +filesystem = Mockery::mock( WP_Filesystem_Direct::class ); + $this->subscriber = new WPML($this->filesystem); + } + + /** + * @dataProvider configTestData + */ + public function testShouldAsExpected( $config ) { + Functions\expect('home_url')->andReturn($config['home_url']); + Functions\expect('wp_parse_url')->with($config['home_url'], PHP_URL_HOST)->andReturn($config['host']); + Functions\expect('_rocket_get_wp_rocket_cache_path')->with($config['home_url'], PHP_URL_HOST)->andReturn($config['path']); + $this->filesystem->expects()->dirlist($config['cache_path'])->andReturn($config['entries']); + $this->filesystem->expects()->is_dir($config['entry_path'])->andReturn(false); + $this->filesystem->expects()->delete($config['entry_path']); + $this->subscriber->remove_root_cached_files(); + } +}