Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 42 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,24 @@ public function check_license( $license = '' ) {
/**
* Fetch available updates from the remote API.
*
* Uses a short-lived site transient cache to avoid redundant HTTP calls
* when WordPress sets the update_plugins transient multiple times.
*
* @since 1.1
*
* @param int $cache_period Cache duration in seconds. Pass 0 to skip caching.
* @return array|WP_Error Response data or WP_Error on failure.
*/
public function updates() {
public function updates( $cache_period = 600 ) {
if ( $cache_period > 0 ) {
$cache_key = $this->integration->get_updates_cache_key();
$cached = get_site_transient( $cache_key );

if ( false !== $cached ) {
return $cached;
}
}

$args = [];
if ( $this->integration->license_enabled ) {
$license = $this->integration->get_license_code();
Expand All @@ -92,17 +105,36 @@ public function updates() {
}
}

return $this->request( 'updates', $args );
$response = $this->request( 'updates', $args );

if ( $cache_period > 0 && ! is_wp_error( $response ) ) {
set_site_transient( $cache_key, $response, $cache_period );
}

return $response;
}

/**
* Fetch plugin details from the remote API.
*
* Uses a short-lived site transient cache to avoid redundant HTTP calls
* from plugins_api and the license admin page.
*
* @since 1.1
*
* @param int $cache_period Cache duration in seconds. Pass 0 to skip caching.
* @return array|WP_Error Response data or WP_Error on failure.
*/
public function details() {
public function details( $cache_period = 600 ) {
if ( $cache_period > 0 ) {
$cache_key = $this->integration->get_details_cache_key();
$cached = get_site_transient( $cache_key );

if ( false !== $cached ) {
return $cached;
}
}

$args = [];
if ( $this->integration->license_enabled ) {
$license = $this->integration->get_license_code();
Expand All @@ -111,7 +143,13 @@ public function details() {
}
}

return $this->request( 'details', $args );
$response = $this->request( 'details', $args );

if ( $cache_period > 0 && ! is_wp_error( $response ) ) {
set_site_transient( $cache_key, $response, $cache_period );
}

return $response;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ public function get_license_data_key() {
return "{$this->license_name}_data";
}

/**
* Retrieves the transient key for caching updates API responses.
*
* @since 1.3
*
* @return string
*/
public function get_updates_cache_key() {
return "{$this->license_name}_updates_cache";
}

/**
* Retrieves the transient key for caching details API responses.
*
* @since 1.3
*
* @return string
*/
public function get_details_cache_key() {
return "{$this->license_name}_details_cache";
}


/**
* Get license status.
Expand Down Expand Up @@ -353,6 +375,8 @@ public function is_license_active() {
* @return void
*/
public function refresh_updates_transient() {
delete_site_transient( $this->get_updates_cache_key() );
delete_site_transient( $this->get_details_cache_key() );
set_site_transient( 'update_plugins', get_site_transient( 'update_plugins' ) );
}

Expand All @@ -363,6 +387,9 @@ public function refresh_updates_transient() {
* @return void
*/
public function clear_updates_transient() {
delete_site_transient( $this->get_updates_cache_key() );
delete_site_transient( $this->get_details_cache_key() );

$transient = get_site_transient( 'update_plugins' );

// Initialize transient if it doesn't exist
Expand Down
4 changes: 2 additions & 2 deletions src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public function upgrader_process_complete( $upgrader, $args ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$plugin = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->integration->product_file );

$this->integration->clear_updates_transient();

$this->integration->product_version = $plugin['Version'];

$this->integration->clear_updates_transient();
$this->integration->client->ping();
}
}
Expand Down
Loading