Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -110,8 +109,12 @@ function ( $message ) {
}
);
if ( ! $this->should_load() ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$this->setup_sites_listing();
}
return;
}
$this->setup_sites_listing();
Comment thread
girishpanchal30 marked this conversation as resolved.
$this->setup_admin();
$this->setup_api();
$this->setup_active_state();
Expand Down
23 changes: 13 additions & 10 deletions includes/Sites_Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Comment thread
girishpanchal30 marked this conversation as resolved.
set_transient(
$this->transient_key,
array(
'fetched_at' => time(),
'data' => $response,
),
$this->cache_ttl
);
}

return $response;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/sites-listing-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading