@@ -103,6 +103,48 @@ class Jetpack_Backup {
103103 */
104104 const PROMOTED_PRODUCT_CACHE_TTL = 12 * HOUR_IN_SECONDS ;
105105
106+ /**
107+ * Transient holding the last good `/rewind/size` answer.
108+ *
109+ * @var string
110+ */
111+ const SITE_SIZE_TRANSIENT = 'jetpack_backup_site_size ' ;
112+
113+ /**
114+ * Transient holding the last good `/rewind/policies` answer.
115+ *
116+ * @var string
117+ */
118+ const SITE_POLICIES_TRANSIENT = 'jetpack_backup_site_policies ' ;
119+
120+ /**
121+ * Transient that, while set, keeps the storage reads out of the cache.
122+ *
123+ * @var string
124+ */
125+ const STORAGE_CACHE_BYPASS_TRANSIENT = 'jetpack_backup_storage_cache_bypass ' ;
126+
127+ /**
128+ * Transient holding the dashboard's capabilities, kept only while they include Backup.
129+ *
130+ * @var string
131+ */
132+ const CAPABILITIES_TRANSIENT = 'jetpack_backup_capabilities ' ;
133+
134+ /**
135+ * How long a rewind read stays cached: the same window the dashboard already treats these reads as fresh.
136+ *
137+ * @var int
138+ */
139+ const REWIND_READ_CACHE_TTL = 5 * MINUTE_IN_SECONDS ;
140+
141+ /**
142+ * Query arg the dashboard's checkout links return with, so the storage figures are read again.
143+ *
144+ * @var string
145+ */
146+ const STORAGE_PURCHASED_ARG = 'storage_purchased ' ;
147+
106148 /**
107149 * Licenses product ID.
108150 *
@@ -296,6 +338,13 @@ public static function add_wp_admin_submenu() {
296338 public static function admin_init () {
297339 add_action ( 'admin_enqueue_scripts ' , array ( __CLASS__ , 'enqueue_admin_scripts ' ) );
298340
341+ // Only bypasses a cache, so it needs no nonce.
342+ if ( isset ( $ _GET [ self ::STORAGE_PURCHASED_ARG ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
343+ self ::clear_storage_cache ();
344+ // A purchase can take a moment to reach `/rewind/policies`, so a read made right after checkout is not kept.
345+ set_transient ( self ::STORAGE_CACHE_BYPASS_TRANSIENT , 1 , self ::REWIND_READ_CACHE_TTL );
346+ }
347+
299348 if ( self ::is_wp_build_dashboard_active () ) {
300349 // Notices reflow the dual-pane layout, so clear them but keep our own.
301350 // An older jetpack-jitm may predate the helper; the fallback costs the JITM.
@@ -1048,25 +1097,7 @@ public static function manage_dismissed_backup_review_request( $request ) {
10481097 * @return \WP_REST_Response|WP_Error The site storage size, or a WP_Error if WordPress.com could not be reached.
10491098 */
10501099 public static function get_site_backup_size () {
1051- $ blog_id = Jetpack_Options ::get_option ( 'id ' );
1052-
1053- $ response = Client ::wpcom_json_api_request_as_user (
1054- '/sites/ ' . $ blog_id . '/rewind/size?force=wpcom ' ,
1055- 'v2 ' ,
1056- array (),
1057- null ,
1058- 'wpcom '
1059- );
1060-
1061- $ response_code = (int ) wp_remote_retrieve_response_code ( $ response );
1062-
1063- if ( 200 !== $ response_code ) {
1064- return self ::get_failed_fetch_error ( $ response );
1065- }
1066-
1067- return rest_ensure_response (
1068- json_decode ( $ response ['body ' ], true )
1069- );
1100+ return self ::get_cached_rewind_read ( 'size ' , self ::SITE_SIZE_TRANSIENT );
10701101 }
10711102
10721103 /**
@@ -1075,10 +1106,40 @@ public static function get_site_backup_size() {
10751106 * @return \WP_REST_Response|WP_Error The site storage policies, or a WP_Error if WordPress.com could not be reached.
10761107 */
10771108 public static function get_site_backup_policies () {
1109+ return self ::get_cached_rewind_read ( 'policies ' , self ::SITE_POLICIES_TRANSIENT );
1110+ }
1111+
1112+ /**
1113+ * Forget the cached storage figures, after anything that changes them.
1114+ *
1115+ * @since $$next-version$$
1116+ *
1117+ * @return void
1118+ */
1119+ public static function clear_storage_cache () {
1120+ delete_transient ( self ::SITE_SIZE_TRANSIENT );
1121+ delete_transient ( self ::SITE_POLICIES_TRANSIENT );
1122+ }
1123+
1124+ /**
1125+ * Read a `/rewind/<endpoint>` route as the user, answering from a transient while one is warm.
1126+ *
1127+ * @param string $endpoint The route under `/sites/<id>/rewind/`.
1128+ * @param string $transient The transient that holds a good answer.
1129+ * @return \WP_REST_Response|WP_Error The decoded answer, or a WP_Error if WordPress.com could not be reached.
1130+ */
1131+ private static function get_cached_rewind_read ( $ endpoint , $ transient ) {
1132+ $ use_cache = false === get_transient ( self ::STORAGE_CACHE_BYPASS_TRANSIENT );
1133+ $ cached = $ use_cache ? get_transient ( $ transient ) : false ;
1134+
1135+ if ( is_array ( $ cached ) ) {
1136+ return rest_ensure_response ( $ cached );
1137+ }
1138+
10781139 $ blog_id = Jetpack_Options ::get_option ( 'id ' );
10791140
10801141 $ response = Client ::wpcom_json_api_request_as_user (
1081- '/sites/ ' . $ blog_id . '/rewind/policies ?force=wpcom ' ,
1142+ '/sites/ ' . $ blog_id . '/rewind/ ' . $ endpoint . ' ?force=wpcom ' ,
10821143 'v2 ' ,
10831144 array (),
10841145 null ,
@@ -1091,9 +1152,14 @@ public static function get_site_backup_policies() {
10911152 return self ::get_failed_fetch_error ( $ response );
10921153 }
10931154
1094- return rest_ensure_response (
1095- json_decode ( $ response ['body ' ], true )
1096- );
1155+ $ body = json_decode ( $ response ['body ' ], true );
1156+
1157+ // `ok: false` is WordPress.com failing to measure; caching it would hide the figures once it recovers.
1158+ if ( $ use_cache && is_array ( $ body ) && false !== ( $ body ['ok ' ] ?? true ) ) {
1159+ set_transient ( $ transient , $ body , self ::REWIND_READ_CACHE_TTL );
1160+ }
1161+
1162+ return rest_ensure_response ( $ body );
10971163 }
10981164
10991165 /**
0 commit comments