From ea5ae60c0b2dd2f9b7cb3ae97c23b4db330ba4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 22 Nov 2023 14:16:38 +0100 Subject: [PATCH] Closes #4226: Importing data should not clear cache (#6274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: mostafa-hisham Co-authored-by: Rémy Perona --- inc/Engine/Cache/PurgeActionsSubscriber.php | 5 +++++ inc/common/purge.php | 16 ++++++++++++++++ inc/functions/api.php | 15 +++++++++++++++ inc/functions/files.php | 5 ++++- .../inc/common/rocketCleanCacheThemeUpdate.php | 12 ++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Cache/PurgeActionsSubscriber.php b/inc/Engine/Cache/PurgeActionsSubscriber.php index 820b75ec2a..e3df5a823c 100644 --- a/inc/Engine/Cache/PurgeActionsSubscriber.php +++ b/inc/Engine/Cache/PurgeActionsSubscriber.php @@ -1,6 +1,7 @@ is_taxonomy_public( $taxonomy ) ) { return; } diff --git a/inc/common/purge.php b/inc/common/purge.php index 7ee0d2c08c..7874c5aa54 100755 --- a/inc/common/purge.php +++ b/inc/common/purge.php @@ -172,6 +172,10 @@ function rocket_get_purge_urls( $post_id, $post ) { * @param WP_Post $post WP_Post object. */ function rocket_clean_post( $post_id, $post = null ) { + if ( rocket_is_importing() ) { + return; + } + static $done = []; if ( isset( $done[ $post_id ] ) ) { @@ -288,6 +292,10 @@ function rocket_clean_post( $post_id, $post = null ) { * @param array $post_data Array of unslashed post data. */ function rocket_clean_post_cache_on_status_change( $post_id, $post_data ) { + if ( rocket_is_importing() ) { + return; + } + if ( 'publish' !== get_post_field( 'post_status', $post_id ) || 'draft' !== $post_data['post_status'] ) { return; } @@ -575,6 +583,10 @@ function do_admin_post_rocket_purge_cache() { // phpcs:ignore WordPress.NamingCo * @param array $hook_extra Array of bulk item update data. */ function rocket_clean_cache_theme_update( $wp_upgrader, $hook_extra ) { + if ( rocket_is_importing() ) { + return; + } + if ( ! isset( $hook_extra['action'] ) || 'update' !== $hook_extra['action'] ) { return; } @@ -611,6 +623,10 @@ function rocket_clean_cache_theme_update( $wp_upgrader, $hook_extra ) { * @param array $post_data Array of unslashed post data. */ function rocket_clean_post_cache_on_slug_change( $post_id, $post_data ) { + if ( rocket_is_importing() ) { + return; + } + // Bail out if the post status is draft, pending or auto-draft. if ( in_array( get_post_field( 'post_status', $post_id ), [ 'draft', 'pending', 'auto-draft', 'trash' ], true ) ) { return; diff --git a/inc/functions/api.php b/inc/functions/api.php index b7dafe69b9..1eff86ecec 100644 --- a/inc/functions/api.php +++ b/inc/functions/api.php @@ -136,3 +136,18 @@ function rocket_is_live_site() { return true; } + +/** + * Checks if importing + * + * @return bool + */ +function rocket_is_importing() { + /** + * Filter use to determine if we are currently importing data into the WordPress. + * Bails out if this filter returns true. + * + * @param boolean Tells if we are importing or not. + */ + return (bool) apply_filters( 'rocket_is_importing', rocket_get_constant( 'WP_IMPORTING' ) ); +} diff --git a/inc/functions/files.php b/inc/functions/files.php index cc0eede9a1..46b5c89d39 100755 --- a/inc/functions/files.php +++ b/inc/functions/files.php @@ -803,10 +803,13 @@ function rocket_clean_home_feeds() { * @param WP_Filesystem_Direct|null $filesystem Optional. Instance of filesystem handler. */ function rocket_clean_domain( $lang = '', $filesystem = null ) { + if ( rocket_is_importing() ) { + return; + } + $urls = ( ! $lang || is_object( $lang ) || is_array( $lang ) || is_int( $lang ) ) ? (array) get_rocket_i18n_uri() : (array) get_rocket_i18n_home_url( $lang ); - /** * Filter URLs to delete all caching files from a domain. * diff --git a/tests/Fixtures/inc/common/rocketCleanCacheThemeUpdate.php b/tests/Fixtures/inc/common/rocketCleanCacheThemeUpdate.php index 317fe61289..92c98e6de8 100644 --- a/tests/Fixtures/inc/common/rocketCleanCacheThemeUpdate.php +++ b/tests/Fixtures/inc/common/rocketCleanCacheThemeUpdate.php @@ -36,6 +36,18 @@ 'wp_get_theme' => null, ], ], + 'shouldBailOutWhenImporting' => [ + 'hook_extra' => [ + 'action' => 'update', + 'type' => 'plugin', + 'themes' => [ 'default' ], + 'importing' => true, + ], + 'expected' => [ + 'cleaned' => [], + 'wp_get_theme' => null, + ], + ], 'shouldCleanDomain' => [ 'hook_extra' => [ 'action' => 'update',