Skip to content

Commit

Permalink
Add/transient to cache backup api calls (#41608)
Browse files Browse the repository at this point in the history
* Cache backup api calls in My Jetpack

* changelog

* Fix issue with return value when transient is present

* remove unused variable
  • Loading branch information
CodeyGuyDylan authored Feb 7, 2025
1 parent 263930e commit b7813eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Cache calls to backup API in My Jetpack
15 changes: 15 additions & 0 deletions projects/packages/my-jetpack/src/products/class-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class Backup extends Hybrid_Product {
*/
public static $feature_identifying_paid_plan = 'backups';

public const BACKUP_STATUS_TRANSIENT_KEY = 'my-jetpack-backup-status';

/**
* Get the product name
*
Expand Down Expand Up @@ -250,6 +252,13 @@ public static function get_latest_backups() {
* @return boolean|array
*/
public static function does_module_need_attention() {
$previous_backup_status = get_transient( self::BACKUP_STATUS_TRANSIENT_KEY );

// If we have a previous backup status, show it.
if ( ! empty( $previous_backup_status ) ) {
return $previous_backup_status === 'no_errors' ? false : $previous_backup_status;
}

$backup_failed_status = false;
// First check the status of Rewind for failure.
$rewind_state = self::get_state_from_wpcom();
Expand Down Expand Up @@ -291,6 +300,12 @@ public static function does_module_need_attention() {
}
}

if ( is_array( $backup_failed_status ) && $backup_failed_status['type'] === 'error' ) {
set_transient( self::BACKUP_STATUS_TRANSIENT_KEY, $backup_failed_status, 5 * MINUTE_IN_SECONDS );
} else {
set_transient( self::BACKUP_STATUS_TRANSIENT_KEY, 'no_errors', HOUR_IN_SECONDS );
}

return $backup_failed_status;
}

Expand Down

0 comments on commit b7813eb

Please sign in to comment.