From f493e145b4e8b976a10fa5827d8fc6afbf755f80 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Wed, 22 Jul 2026 13:12:11 +0530 Subject: [PATCH 1/3] fix: optimize sites listing caching --- includes/Main.php | 2 +- includes/Sites_Listing.php | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/includes/Main.php b/includes/Main.php index 49a7f864..cb893709 100755 --- a/includes/Main.php +++ b/includes/Main.php @@ -99,7 +99,6 @@ private function init() { $this->setup_beaver(); $this->setup_elementor(); } - $this->setup_sites_listing(); add_filter( 'themeisle_sdk_hide_dashboard_widget', '__return_true' ); add_filter( 'templates_patterns_collection_feedback_review_message', @@ -112,6 +111,7 @@ function ( $message ) { if ( ! $this->should_load() ) { return; } + $this->setup_sites_listing(); $this->setup_admin(); $this->setup_api(); $this->setup_active_state(); diff --git a/includes/Sites_Listing.php b/includes/Sites_Listing.php index e5858d73..cc68ce52 100755 --- a/includes/Sites_Listing.php +++ b/includes/Sites_Listing.php @@ -118,9 +118,10 @@ public function add_sites_library_support() { * @return array */ private function get_sites() { - $response = $this->get_cached_sites(); + $response = $this->get_cached_sites(); + $is_cached = ( $response !== false ); - if ( $response === false ) { + if ( ! $is_cached ) { $response = wp_remote_get( esc_url( self::get_api_path() ) ); if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { @@ -168,14 +169,16 @@ private function get_sites() { } } - set_transient( - $this->transient_key, - array( - 'fetched_at' => time(), - 'data' => $response, - ), - $this->cache_ttl - ); + if ( ! $is_cached ) { + set_transient( + $this->transient_key, + array( + 'fetched_at' => time(), + 'data' => $response, + ), + $this->cache_ttl + ); + } return $response; } From 23cb0bbfc91ef35e004bba5e2a412ce84e7d1eed Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Wed, 22 Jul 2026 14:12:24 +0530 Subject: [PATCH 2/3] fix: ensure sites listing is executed in WP-CLI context --- includes/Main.php | 3 +++ tests/sites-listing-test.php | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/includes/Main.php b/includes/Main.php index cb893709..563eaa4f 100755 --- a/includes/Main.php +++ b/includes/Main.php @@ -109,6 +109,9 @@ function ( $message ) { } ); if ( ! $this->should_load() ) { + if ( defined( 'WP_CLI' ) && WP_CLI ) { + $this->setup_sites_listing(); + } return; } $this->setup_sites_listing(); diff --git a/tests/sites-listing-test.php b/tests/sites-listing-test.php index 1641d009..1f0f3fc3 100644 --- a/tests/sites-listing-test.php +++ b/tests/sites-listing-test.php @@ -124,6 +124,29 @@ public function test_fresh_cache_is_served_without_remote_call() { $this->assertSame( 0, $this->remote_calls ); } + /** + * A fresh cache hit must not rewrite the transient value or reset its expiration. + */ + public function test_fresh_cache_hit_leaves_transient_and_expiration_unchanged() { + $original = array( + 'fetched_at' => time() - HOUR_IN_SECONDS, + 'data' => array( 'gutenberg' => array( 'cached-site' => array() ) ), + ); + + set_transient( $this->get_transient_key(), $original, 12 * HOUR_IN_SECONDS ); + + $timeout_option = '_transient_timeout_' . $this->get_transient_key(); + $original_expiration = get_option( $timeout_option ); + + $this->mock_api( array( 'gutenberg' => array( 'fresh-site' => array() ) ) ); + + $this->get_sites(); + + $this->assertSame( 0, $this->remote_calls ); + $this->assertSame( $original, get_transient( $this->get_transient_key() ), 'Cache-hit response must not rewrite the wrapped transient payload.' ); + $this->assertSame( $original_expiration, get_option( $timeout_option ), 'Cache-hit response must not reset the transient expiration.' ); + } + /** * An orphaned-timeout transient (value present but our own fetched_at is * past the TTL) must trigger a re-fetch instead of being served forever. From 1af41e8bfa6a28ad56fd13bba8e75a7d065e0d23 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 28 Jul 2026 18:09:31 +0530 Subject: [PATCH 3/3] fix: phpcs --- includes/Main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Main.php b/includes/Main.php index 563eaa4f..ee82f197 100755 --- a/includes/Main.php +++ b/includes/Main.php @@ -110,8 +110,8 @@ function ( $message ) { ); if ( ! $this->should_load() ) { if ( defined( 'WP_CLI' ) && WP_CLI ) { - $this->setup_sites_listing(); - } + $this->setup_sites_listing(); + } return; } $this->setup_sites_listing();