Skip to content

Commit

Permalink
Closes #4226: Importing data should not clear cache (#6274)
Browse files Browse the repository at this point in the history
Co-authored-by: mostafa-hisham <[email protected]>
Co-authored-by: Rémy Perona <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2023
1 parent cfc26a3 commit ea5ae60
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions inc/Engine/Cache/PurgeActionsSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace WP_Rocket\Engine\Cache;

use WP_Post;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Logger\Logger;
Expand Down Expand Up @@ -88,6 +89,10 @@ public function purge_user_cache( $user_id ) {
* @return void
*/
public function maybe_purge_cache_on_term_change( $term_id, $tt_id, $taxonomy ) {
if ( rocket_is_importing() ) {
return;
}

if ( ! $this->is_taxonomy_public( $taxonomy ) ) {
return;
}
Expand Down
16 changes: 16 additions & 0 deletions inc/common/purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] ) ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions inc/functions/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
5 changes: 4 additions & 1 deletion inc/functions/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/inc/common/rocketCleanCacheThemeUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ea5ae60

Please sign in to comment.