Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.63% |
479 / 566 |
|
68.75% |
33 / 48 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Backup | |
84.93% |
479 / 564 |
|
68.75% |
33 / 48 |
182.21 | |
0.00% |
0 / 1 |
| initialize | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| init_standalone_connection | |
45.00% |
9 / 20 |
|
0.00% |
0 / 1 |
1.17 | |||
| add_wp_admin_submenu | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
7 | |||
| admin_init | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
4.18 | |||
| add_admin_shell_member | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| load_as_admin_shell_guest | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| maybe_upgrade_db | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| can_use_analytics | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_admin_scripts | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
5 | |||
| plugin_settings_page | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_initial_state | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_routes | |
100.00% |
129 / 129 |
|
100.00% |
1 / 1 |
1 | |||
| backups_permissions_callback | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_offline_mode_error | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| get_failed_fetch_error | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| get_recent_backups | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| get_rewind_state_from_wpcom | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
5.01 | |||
| get_backup_plan_state | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| has_backup_plan | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_backup_capabilities | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| get_recent_restores | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| list_backup_events | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
2 | |||
| get_backup_promoted_product_info | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
6 | |||
| jetpack_check_user_licenses | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
6.07 | |||
| get_site_current_purchases | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| manage_dismissed_backup_review_request | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| get_site_backup_size | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_site_backup_policies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clear_storage_cache | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_cached_rewind_read | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |||
| get_storage_addon_upsell_slug | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
6 | |||
| get_site_backup_addon_offer | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_backup | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| get_site_backup_schedule_time | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| plugin_deactivation | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| maybe_load_wp_build | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| render_connection_initial_state | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| get_activity_log_url | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| activity_log_page_will_register | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| load_wp_build | |
23.08% |
3 / 13 |
|
0.00% |
0 / 1 |
7.10 | |||
| load_wp_build_with_screen_alias | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
2.06 | |||
| alias_screen_id_for_wp_build | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| restore_screen_id_after_wp_build | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| activity_log_restore_url_template | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| activity_log_backup_url_template | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| is_modernized | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| is_wp_build_dashboard_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_backup_admin_request | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Primary class file for the Jetpack Backup plugin. |
| 4 | * |
| 5 | * @package automattic/jetpack-backup-plugin |
| 6 | */ |
| 7 | |
| 8 | // After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in |
| 9 | // order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide |
| 10 | // to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin |
| 11 | // are installed, or in some other cases). |
| 12 | namespace Automattic\Jetpack\Backup\V0005; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 19 | use Automattic\Jetpack\Admin_UI\Admin_Shell; |
| 20 | use Automattic\Jetpack\Assets; |
| 21 | use Automattic\Jetpack\Backup\V0005\Initial_State as Backup_Initial_State; |
| 22 | use Automattic\Jetpack\Config; |
| 23 | use Automattic\Jetpack\Connection\Client; |
| 24 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 25 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 26 | use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication; |
| 27 | use Automattic\Jetpack\JITMS\JITM; |
| 28 | use Automattic\Jetpack\My_Jetpack\Wpcom_Products; |
| 29 | use Automattic\Jetpack\Status; |
| 30 | use Automattic\Jetpack\Status\Host; |
| 31 | use Automattic\Jetpack\Terms_Of_Service; |
| 32 | use Automattic\Jetpack\Tracking; |
| 33 | use Jetpack_Options; |
| 34 | use WP_Error; |
| 35 | use WP_REST_Server; |
| 36 | use function add_action; |
| 37 | use function add_filter; |
| 38 | use function admin_url; |
| 39 | use function did_action; |
| 40 | use function do_action; |
| 41 | use function esc_url_raw; |
| 42 | use function get_option; |
| 43 | use function is_wp_error; |
| 44 | use function menu_page_url; |
| 45 | use function rest_ensure_response; |
| 46 | use function update_option; |
| 47 | use function wp_add_inline_script; |
| 48 | use function wp_doing_ajax; |
| 49 | use function wp_remote_get; |
| 50 | use function wp_remote_retrieve_body; |
| 51 | use function wp_remote_retrieve_response_code; |
| 52 | |
| 53 | /** |
| 54 | * Class Jetpack_Backup |
| 55 | */ |
| 56 | class Jetpack_Backup { |
| 57 | |
| 58 | /** |
| 59 | * Slug. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | const JETPACK_BACKUP_SLUG = 'jetpack-backup'; |
| 64 | |
| 65 | /** |
| 66 | * Backup name. |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | const JETPACK_BACKUP_NAME = 'Jetpack Backup'; |
| 71 | |
| 72 | /** |
| 73 | * Backup URL. |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
| 77 | const JETPACK_BACKUP_URI = 'https://jetpack.com/jetpack-backup'; |
| 78 | |
| 79 | /** |
| 80 | * Promoted product. |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
| 84 | const JETPACK_BACKUP_PROMOTED_PRODUCT = 'jetpack_backup_t1_yearly'; |
| 85 | |
| 86 | /** |
| 87 | * Transient key prefix for the cached promoted product. |
| 88 | * |
| 89 | * Suffixed with the locale: it is a query arg on the catalogue request, so |
| 90 | * one shared key would serve one reader's language to another. |
| 91 | * |
| 92 | * @var string |
| 93 | */ |
| 94 | const PROMOTED_PRODUCT_TRANSIENT_PREFIX = 'jetpack_backup_promoted_product_'; |
| 95 | |
| 96 | /** |
| 97 | * How long a fetched promoted product stays cached. |
| 98 | * |
| 99 | * The catalogue turns over on a marketing schedule rather than a |
| 100 | * session's, so half a day bounds how stale a price can get. |
| 101 | * |
| 102 | * @var int |
| 103 | */ |
| 104 | const PROMOTED_PRODUCT_CACHE_TTL = 12 * HOUR_IN_SECONDS; |
| 105 | |
| 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 | |
| 148 | /** |
| 149 | * Licenses product ID. |
| 150 | * |
| 151 | * @var string |
| 152 | */ |
| 153 | const JETPACK_BACKUP_PRODUCT_IDS = array( |
| 154 | 2014, // JETPACK_COMPLETE. |
| 155 | 2015, // JETPACK_COMPLETE_MONTHLY. |
| 156 | 2024, // JETPACK_PRO_MONTHLY. |
| 157 | 2025, // JETPACK_PRO_YEARLY. |
| 158 | 2026, // JETPACK_PRO_BI_YEARLY. |
| 159 | 2016, // JETPACK_SECURITY_TIER_1_YEARLY. |
| 160 | 2017, // JETPACK_SECURITY_TIER_1_MONTHLY. |
| 161 | 2019, // JETPACK_SECURITY_TIER_2_YEARLY. |
| 162 | 2020, // JETPACK_SECURITY_TIER_2_MONTHLY. |
| 163 | 2112, // JETPACK_BACKUP_TIER_1_YEARLY. |
| 164 | 2113, // JETPACK_BACKUP_TIER_1_MONTHLY. |
| 165 | 2114, // JETPACK_BACKUP_TIER_2_YEARLY. |
| 166 | 2115, // JETPACK_BACKUP_TIER_2_MONTHLY. |
| 167 | ); |
| 168 | |
| 169 | /** |
| 170 | * Jetpack Backup DB version. |
| 171 | * |
| 172 | * @var string |
| 173 | */ |
| 174 | const JETPACK_BACKUP_DB_VERSION = '2'; |
| 175 | |
| 176 | /** |
| 177 | * Filter name that gates the wp-build–based dashboard. |
| 178 | * |
| 179 | * Returning false renders the legacy React app on "Jetpack > Backup" instead of the wp-build dashboard. |
| 180 | */ |
| 181 | const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_backup'; |
| 182 | |
| 183 | /** |
| 184 | * Blog sticker that puts a site back on the legacy dashboard. |
| 185 | * |
| 186 | * Atomic sees it only if it is on WordPress.com's `atomic_site_stickers()` allowlist. |
| 187 | */ |
| 188 | const LEGACY_DASHBOARD_STICKER = 'use-backup-legacy-dashboard'; |
| 189 | |
| 190 | /** |
| 191 | * The wp-build page id, which the screen ID is aliased to on the Backup page. |
| 192 | */ |
| 193 | const WP_BUILD_PAGE_ID = 'jetpack-backup-dashboard'; |
| 194 | |
| 195 | /** |
| 196 | * Rewind state read from WordPress.com, memoized for the request. |
| 197 | * |
| 198 | * A class property and not a function static so tests can clear it. |
| 199 | * |
| 200 | * @var object|null |
| 201 | */ |
| 202 | private static $rewind_state = null; |
| 203 | |
| 204 | /** |
| 205 | * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored. |
| 206 | * |
| 207 | * @var string|null |
| 208 | */ |
| 209 | private static $wp_build_original_screen_id = null; |
| 210 | |
| 211 | /** |
| 212 | * Initialization options. |
| 213 | * |
| 214 | * @var array |
| 215 | */ |
| 216 | const DEFAULT_INIT_OPTIONS = array( |
| 217 | // A host that already ensured a connection under its own slug must not have it |
| 218 | // re-ensured here as `jetpack-backup`, which would rename the site's connection. |
| 219 | 'manage_connection' => true, |
| 220 | ); |
| 221 | |
| 222 | /** |
| 223 | * Constructor. |
| 224 | * |
| 225 | * @param array $options Overrides for self::DEFAULT_INIT_OPTIONS. |
| 226 | */ |
| 227 | public static function initialize( array $options = array() ) { |
| 228 | if ( did_action( 'jetpack_backup_initialized' ) ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $options = array_merge( self::DEFAULT_INIT_OPTIONS, $options ); |
| 233 | |
| 234 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 235 | add_action( 'rest_api_init', array( \Automattic\Jetpack\Backup\V0005\REST\Rest_Controller::class, 'register_routes' ) ); |
| 236 | add_filter( 'jetpack_activity_log_restore_url_template', array( __CLASS__, 'activity_log_restore_url_template' ) ); |
| 237 | add_filter( 'jetpack_activity_log_backup_url_template', array( __CLASS__, 'activity_log_backup_url_template' ) ); |
| 238 | |
| 239 | add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 ); |
| 240 | add_action( 'admin_menu', array( __CLASS__, 'add_wp_admin_submenu' ), 1 ); // Akismet uses 4, so we need to use 1 to ensure both menus are added when only they exist. |
| 241 | |
| 242 | if ( class_exists( Admin_Shell::class ) ) { |
| 243 | add_filter( 'jetpack_admin_shell_members', array( __CLASS__, 'add_admin_shell_member' ) ); |
| 244 | } |
| 245 | |
| 246 | if ( $options['manage_connection'] ) { |
| 247 | self::init_standalone_connection(); |
| 248 | } |
| 249 | |
| 250 | // Jetpack Backup abilities are registered from `actions.php` at package |
| 251 | // autoload time so the surface is available in every consumer that |
| 252 | // loads this package (both the standalone Backup plugin and the |
| 253 | // Jetpack plugin), not only when `Jetpack_Backup::initialize()` runs. |
| 254 | |
| 255 | /** |
| 256 | * Runs right after the Jetpack Backup package is initialized. |
| 257 | * |
| 258 | * @since 1.3.0 |
| 259 | */ |
| 260 | do_action( 'jetpack_backup_initialized' ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Set up the connection, sync and identity-crisis packages under the standalone plugin's slug. |
| 265 | */ |
| 266 | private static function init_standalone_connection() { |
| 267 | // Set up the REST authentication hooks. |
| 268 | Connection_Rest_Authentication::init(); |
| 269 | |
| 270 | // Init Jetpack packages. |
| 271 | add_action( |
| 272 | 'plugins_loaded', |
| 273 | function () { |
| 274 | $config = new Config(); |
| 275 | // Connection package. |
| 276 | $config->ensure( |
| 277 | 'connection', |
| 278 | array( |
| 279 | 'slug' => self::JETPACK_BACKUP_SLUG, |
| 280 | 'name' => self::JETPACK_BACKUP_NAME, |
| 281 | 'url_info' => self::JETPACK_BACKUP_URI, |
| 282 | ) |
| 283 | ); |
| 284 | // Sync package. |
| 285 | $config->ensure( 'sync' ); |
| 286 | |
| 287 | // Identity crisis package. |
| 288 | $config->ensure( 'identity_crisis' ); |
| 289 | }, |
| 290 | 1 |
| 291 | ); |
| 292 | |
| 293 | add_action( 'plugins_loaded', array( __CLASS__, 'maybe_upgrade_db' ), 20 ); |
| 294 | |
| 295 | add_filter( 'jetpack_connection_user_has_license', array( __CLASS__, 'jetpack_check_user_licenses' ), 10, 3 ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * The page to be added to submenu |
| 300 | */ |
| 301 | public static function add_wp_admin_submenu() { |
| 302 | $wp_build_active = self::is_wp_build_dashboard_active(); |
| 303 | $callback = $wp_build_active |
| 304 | ? 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' |
| 305 | : array( __CLASS__, 'plugin_settings_page' ); |
| 306 | |
| 307 | /* |
| 308 | * The page title's relabel rides the modernized dashboard rather than the filter alone, |
| 309 | * so a fallback to the legacy page also falls back to the legacy title. Off the Backup |
| 310 | * page the build isn't loaded, so the admin shell's title comes from the build index. |
| 311 | */ |
| 312 | $renders_modernized = $wp_build_active |
| 313 | || ( ! self::is_backup_admin_request() && self::is_modernized() && file_exists( dirname( __DIR__ ) . '/build/build.php' ) ); |
| 314 | $page_title = $renders_modernized ? 'Jetpack VaultPress Backup' : 'Jetpack Backup'; |
| 315 | $menu_title = 'Backup'; // Product name, do not translate. |
| 316 | |
| 317 | $page_suffix = Admin_Menu::add_menu( |
| 318 | $page_title, |
| 319 | $menu_title, |
| 320 | 'manage_options', |
| 321 | self::JETPACK_BACKUP_SLUG, |
| 322 | $callback, |
| 323 | null, |
| 324 | array( |
| 325 | 'product' => 'backup', |
| 326 | 'key' => 'jetpack-backup', |
| 327 | ) |
| 328 | ); |
| 329 | |
| 330 | if ( $page_suffix ) { |
| 331 | add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Initialize the admin resources. |
| 337 | */ |
| 338 | public static function admin_init() { |
| 339 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) ); |
| 340 | |
| 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 | |
| 348 | if ( self::is_wp_build_dashboard_active() ) { |
| 349 | // Notices reflow the dual-pane layout, so clear them but keep our own. |
| 350 | // An older jetpack-jitm may predate the helper; the fallback costs the JITM. |
| 351 | if ( method_exists( JITM::class, 'suppress_foreign_admin_notices' ) ) { |
| 352 | JITM::suppress_foreign_admin_notices(); |
| 353 | } else { |
| 354 | remove_all_actions( 'admin_notices' ); |
| 355 | remove_all_actions( 'all_admin_notices' ); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Register Backup with the admin shell, which switches between Jetpack dashboards without a page load. |
| 362 | * |
| 363 | * The page is added whenever the package is initialized; only the modernized dashboard is a wp-build page. |
| 364 | * |
| 365 | * @since 5.2.5 |
| 366 | * |
| 367 | * @param array $members Shell members keyed by admin page slug. |
| 368 | * @return array |
| 369 | */ |
| 370 | public static function add_admin_shell_member( $members ) { |
| 371 | if ( |
| 372 | ( new Host() )->is_wpcom_simple() |
| 373 | || ! self::is_modernized() |
| 374 | || ! file_exists( dirname( __DIR__ ) . '/build/build.php' ) |
| 375 | ) { |
| 376 | return $members; |
| 377 | } |
| 378 | |
| 379 | $members[ self::JETPACK_BACKUP_SLUG ] = array( |
| 380 | 'build_prefix' => 'jetpack_backup', |
| 381 | 'build_page' => self::WP_BUILD_PAGE_ID, |
| 382 | 'init_modules' => array( '@jetpack-backup/init' ), |
| 383 | // "Backup" is a product name, do not translate. |
| 384 | 'title' => 'Backup', |
| 385 | 'header' => array( |
| 386 | 'title' => 'VaultPress Backup', |
| 387 | 'subtitle' => __( 'Save changes and restore quickly with one-click recovery.', 'jetpack-backup-pkg' ), |
| 388 | ), |
| 389 | 'capability' => 'manage_options', |
| 390 | 'handles' => array( 'jetpack-script-data', 'jp-tracks-functions', 'wp-jp-i18n-loader' ), |
| 391 | 'load' => array( __CLASS__, 'load_as_admin_shell_guest' ), |
| 392 | ); |
| 393 | return $members; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Ready the Backup routes and page data while another dashboard's page is open, without the screen ID alias. |
| 398 | * |
| 399 | * @since 5.2.5 |
| 400 | * |
| 401 | * @return void |
| 402 | */ |
| 403 | public static function load_as_admin_shell_guest() { |
| 404 | self::load_wp_build(); |
| 405 | add_action( 'admin_print_scripts', array( __CLASS__, 'render_connection_initial_state' ), 1 ); |
| 406 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) ); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Checks current version against version in code and run upgrades if we are running a new version |
| 411 | */ |
| 412 | public static function maybe_upgrade_db() { |
| 413 | $current_db_version = get_option( 'jetpack_backup_db_version' ); |
| 414 | if ( version_compare( $current_db_version, self::JETPACK_BACKUP_DB_VERSION, '<' ) ) { |
| 415 | update_option( 'jetpack_backup_db_version', self::JETPACK_BACKUP_DB_VERSION ); |
| 416 | Jetpack_Backup_Upgrades::upgrade(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Returns whether we are in condition to track to use |
| 422 | * Analytics functionality like Tracks, MC, or GA. |
| 423 | */ |
| 424 | public static function can_use_analytics() { |
| 425 | $status = new Status(); |
| 426 | $connection = new Connection_Manager( 'jetpack-backup' ); |
| 427 | $tracking = new Tracking( 'jetpack', $connection ); |
| 428 | |
| 429 | return $tracking->should_enable_tracking( new Terms_Of_Service(), $status ); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Enqueue plugin admin scripts and styles. |
| 434 | */ |
| 435 | public static function enqueue_admin_scripts() { |
| 436 | // Hooked only for the Backup page: via `load-{$page_suffix}` in `add_wp_admin_submenu()`, |
| 437 | // or by the admin shell's guest load, so there's no need to re-check the page here. |
| 438 | if ( self::is_wp_build_dashboard_active() ) { |
| 439 | // The i18n loader is registered on every admin page by jetpack-assets but |
| 440 | // only enqueued when depended on; the esbuild bundles don't pull it in. |
| 441 | // Enqueue it here, before the early return, so the wp-build dashboard's |
| 442 | // init module can download its JS translation catalogs. |
| 443 | if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) { |
| 444 | wp_enqueue_script( 'wp-jp-i18n-loader' ); |
| 445 | } |
| 446 | |
| 447 | // The esbuild bundles don't declare the Tracks client as a dependency |
| 448 | // either, so it never reaches the page on its own. |
| 449 | if ( self::can_use_analytics() ) { |
| 450 | Tracking::register_tracks_functions_scripts( true ); |
| 451 | } |
| 452 | |
| 453 | // wp-build manages its own enqueue pipeline. The legacy script and |
| 454 | // its initial state are skipped for the wp-build dashboard. |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | Assets::register_script( |
| 459 | 'jetpack-backup', |
| 460 | '../build/index.js', |
| 461 | __FILE__, |
| 462 | array( |
| 463 | 'in_footer' => true, |
| 464 | 'textdomain' => 'jetpack-backup-pkg', |
| 465 | ) |
| 466 | ); |
| 467 | Assets::enqueue_script( 'jetpack-backup' ); |
| 468 | // Initial JS state including JP Connection data. |
| 469 | wp_add_inline_script( 'jetpack-backup', self::get_initial_state(), 'before' ); |
| 470 | Connection_Initial_State::render_script( 'jetpack-backup' ); |
| 471 | |
| 472 | // Load script for analytics. |
| 473 | if ( self::can_use_analytics() ) { |
| 474 | Tracking::register_tracks_functions_scripts( true ); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Main plugin settings page. |
| 480 | */ |
| 481 | public static function plugin_settings_page() { |
| 482 | ?> |
| 483 | <div id="jetpack-backup-root"></div> |
| 484 | <?php |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Return the rendered initial state JavaScript code. |
| 489 | * |
| 490 | * @return string |
| 491 | */ |
| 492 | private static function get_initial_state() { |
| 493 | return ( new Backup_Initial_State() )->render(); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Register REST API |
| 498 | */ |
| 499 | public static function register_rest_routes() { |
| 500 | |
| 501 | // Get information on most recent 10 backups. |
| 502 | register_rest_route( |
| 503 | 'jetpack/v4', |
| 504 | '/backups', |
| 505 | array( |
| 506 | 'methods' => WP_REST_Server::READABLE, |
| 507 | 'callback' => __CLASS__ . '::get_recent_backups', |
| 508 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 509 | ) |
| 510 | ); |
| 511 | |
| 512 | // Get site backup/scan/anti-spam capabilities. |
| 513 | register_rest_route( |
| 514 | 'jetpack/v4', |
| 515 | '/backup-capabilities', |
| 516 | array( |
| 517 | 'methods' => WP_REST_Server::READABLE, |
| 518 | 'callback' => __CLASS__ . '::get_backup_capabilities', |
| 519 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 520 | ) |
| 521 | ); |
| 522 | |
| 523 | // Get whether the site has a backup plan |
| 524 | register_rest_route( |
| 525 | 'jetpack/v4', |
| 526 | '/has-backup-plan', |
| 527 | array( |
| 528 | 'methods' => WP_REST_Server::READABLE, |
| 529 | 'callback' => __CLASS__ . '::get_backup_plan_state', |
| 530 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 531 | ) |
| 532 | ); |
| 533 | |
| 534 | // Get site rewind data. |
| 535 | register_rest_route( |
| 536 | 'jetpack/v4', |
| 537 | '/restores', |
| 538 | array( |
| 539 | 'methods' => WP_REST_Server::READABLE, |
| 540 | 'callback' => __CLASS__ . '::get_recent_restores', |
| 541 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 542 | ) |
| 543 | ); |
| 544 | |
| 545 | // Get information on site products. |
| 546 | // Backup plugin version of /site/purchases from JP plugin. |
| 547 | // Revert once this route and MyPlan component are extracted to a common package. |
| 548 | register_rest_route( |
| 549 | 'jetpack/v4', |
| 550 | '/site/current-purchases', |
| 551 | array( |
| 552 | 'methods' => WP_REST_Server::READABLE, |
| 553 | 'callback' => __CLASS__ . '::get_site_current_purchases', |
| 554 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 555 | ) |
| 556 | ); |
| 557 | |
| 558 | // Get currently promoted product from the product's endpoint. |
| 559 | register_rest_route( |
| 560 | 'jetpack/v4', |
| 561 | '/backup-promoted-product-info', |
| 562 | array( |
| 563 | 'methods' => WP_REST_Server::READABLE, |
| 564 | 'callback' => __CLASS__ . '::get_backup_promoted_product_info', |
| 565 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 566 | ) |
| 567 | ); |
| 568 | |
| 569 | // Get and set value of dismissed_backup_review_request option |
| 570 | register_rest_route( |
| 571 | 'jetpack/v4', |
| 572 | '/site/dismissed-review-request', |
| 573 | array( |
| 574 | 'methods' => WP_REST_Server::EDITABLE, |
| 575 | 'callback' => __CLASS__ . '::manage_dismissed_backup_review_request', |
| 576 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 577 | 'args' => array( |
| 578 | 'option_name' => array( |
| 579 | 'required' => true, |
| 580 | 'type' => 'string', |
| 581 | // The two names `Jetpack_Options` recognises. Anything else |
| 582 | // falls through its allowlist to a `trigger_error()` and is |
| 583 | // stored nowhere, so an unlisted reason would dismiss |
| 584 | // nothing while answering as though it had — and on a site |
| 585 | // with `display_errors` on, the warning is printed ahead of |
| 586 | // the JSON and the response no longer parses. |
| 587 | 'enum' => array( 'restore', 'backups' ), |
| 588 | ), |
| 589 | 'should_dismiss' => array( |
| 590 | 'required' => true, |
| 591 | 'type' => 'boolean', |
| 592 | ), |
| 593 | ), |
| 594 | ) |
| 595 | ); |
| 596 | |
| 597 | // Get site size |
| 598 | register_rest_route( |
| 599 | 'jetpack/v4', |
| 600 | '/site/backup/size', |
| 601 | array( |
| 602 | 'methods' => WP_REST_Server::READABLE, |
| 603 | 'callback' => __CLASS__ . '::get_site_backup_size', |
| 604 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 605 | ) |
| 606 | ); |
| 607 | |
| 608 | // Get backup schedule time |
| 609 | register_rest_route( |
| 610 | 'jetpack/v4', |
| 611 | '/site/backup/schedule', |
| 612 | array( |
| 613 | 'methods' => WP_REST_Server::READABLE, |
| 614 | 'callback' => __CLASS__ . '::get_site_backup_schedule_time', |
| 615 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 616 | ) |
| 617 | ); |
| 618 | |
| 619 | // Get site policies |
| 620 | register_rest_route( |
| 621 | 'jetpack/v4', |
| 622 | '/site/backup/policies', |
| 623 | array( |
| 624 | 'methods' => WP_REST_Server::READABLE, |
| 625 | 'callback' => __CLASS__ . '::get_site_backup_policies', |
| 626 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 627 | ) |
| 628 | ); |
| 629 | |
| 630 | // Get site add-on offer |
| 631 | register_rest_route( |
| 632 | 'jetpack/v4', |
| 633 | '/site/backup/addon-offer', |
| 634 | array( |
| 635 | 'methods' => WP_REST_Server::READABLE, |
| 636 | 'callback' => __CLASS__ . '::get_site_backup_addon_offer', |
| 637 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 638 | 'args' => array( |
| 639 | 'storage_size' => array( |
| 640 | 'required' => true, |
| 641 | 'type' => 'numeric', |
| 642 | ), |
| 643 | 'storage_limit' => array( |
| 644 | 'required' => true, |
| 645 | 'type' => 'numeric', |
| 646 | ), |
| 647 | ), |
| 648 | ) |
| 649 | ); |
| 650 | |
| 651 | // Enqueue a new backup |
| 652 | register_rest_route( |
| 653 | 'jetpack/v4', |
| 654 | '/site/backup/enqueue', |
| 655 | array( |
| 656 | 'methods' => WP_REST_Server::CREATABLE, |
| 657 | 'callback' => __CLASS__ . '::enqueue_backup', |
| 658 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 659 | ) |
| 660 | ); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * The backup calls should only occur from a signed in admin user, outside offline mode |
| 665 | * |
| 666 | * @access public |
| 667 | * @static |
| 668 | * |
| 669 | * @return bool|WP_Error |
| 670 | */ |
| 671 | public static function backups_permissions_callback() { |
| 672 | if ( ! current_user_can( 'manage_options' ) ) { |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | return self::get_offline_mode_error() ?? true; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * The error the dashboard's routes and abilities answer with while Jetpack is in offline mode. |
| 681 | * |
| 682 | * A local copy of a live site keeps its tokens, and offline mode drops the identity-crisis |
| 683 | * check, so WordPress.com would act on the live site's backups. |
| 684 | * |
| 685 | * @since 5.2.0 |
| 686 | * |
| 687 | * @return WP_Error|null Null outside offline mode. |
| 688 | */ |
| 689 | public static function get_offline_mode_error() { |
| 690 | if ( ! ( new Status() )->is_offline_mode() ) { |
| 691 | return null; |
| 692 | } |
| 693 | |
| 694 | return new WP_Error( |
| 695 | 'offline_mode', |
| 696 | __( 'Backup is unavailable in Offline Mode.', 'jetpack-backup-pkg' ), |
| 697 | array( 'status' => 403 ) |
| 698 | ); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * The error a route answers with when its WordPress.com request did not come |
| 703 | * back with a 200. |
| 704 | * |
| 705 | * Returning `null` instead — which these routes used to do — is served as an |
| 706 | * HTTP 200 carrying a `null` body, so `apiFetch` resolves and nothing throws. |
| 707 | * A WordPress.com blip then reaches the dashboard as an empty success, which |
| 708 | * is how a paying customer ends up looking at the first-run screen. A |
| 709 | * WP_Error makes the REST layer answer with a status, so every caller's |
| 710 | * existing failure path runs. WordPress.com's reason, when its reply has one, |
| 711 | * rides along as `data.wpcom`; unlike preflight's, these routes' callers are all |
| 712 | * in this package, so the extra key is safe. |
| 713 | * |
| 714 | * @param array|\WP_Error $response The wp_remote_* response. |
| 715 | * @return WP_Error |
| 716 | */ |
| 717 | private static function get_failed_fetch_error( $response ) { |
| 718 | return REST\Rest_Controller::upstream_error( |
| 719 | $response, |
| 720 | 'failed_to_fetch_data', |
| 721 | esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup-pkg' ) |
| 722 | ); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Get information about recent backups |
| 727 | * |
| 728 | * @access public |
| 729 | * @static |
| 730 | * |
| 731 | * @return \WP_REST_Response|WP_Error The recent backups, or a WP_Error if WordPress.com could not be reached. |
| 732 | */ |
| 733 | public static function get_recent_backups() { |
| 734 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 735 | |
| 736 | $response = Client::wpcom_json_api_request_as_blog( |
| 737 | '/sites/' . $blog_id . '/rewind/backups', |
| 738 | 'v2', |
| 739 | array(), |
| 740 | null, |
| 741 | 'wpcom' |
| 742 | ); |
| 743 | |
| 744 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 745 | |
| 746 | if ( 200 !== $response_code ) { |
| 747 | return self::get_failed_fetch_error( $response ); |
| 748 | } |
| 749 | |
| 750 | return rest_ensure_response( |
| 751 | json_decode( $response['body'], true ) |
| 752 | ); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * Hits the wpcom api to check rewind status. |
| 757 | * |
| 758 | * Bounded well clear of a healthy round trip; `Client`'s 10s default is too |
| 759 | * long for the synchronous `authorize_redirect` this sits on. |
| 760 | * |
| 761 | * @return object|WP_Error The decoded rewind state, or a WP_Error if WordPress.com could not be read. |
| 762 | */ |
| 763 | private static function get_rewind_state_from_wpcom() { |
| 764 | if ( self::$rewind_state !== null ) { |
| 765 | return self::$rewind_state; |
| 766 | } |
| 767 | |
| 768 | $site_id = Jetpack_Options::get_option( 'id' ); |
| 769 | |
| 770 | $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/rewind', $site_id ) . '?force=wpcom', '2', array( 'timeout' => 5 ), null, 'wpcom' ); |
| 771 | |
| 772 | // Cast: `wp_remote_retrieve_response_code()` hands back whatever the |
| 773 | // transport put there, and a numeric-string `'200'` fails this strict |
| 774 | // comparison. |
| 775 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 776 | |
| 777 | if ( 200 !== $response_code ) { |
| 778 | return self::get_failed_fetch_error( $response ); |
| 779 | } |
| 780 | |
| 781 | $state = json_decode( wp_remote_retrieve_body( $response ) ); |
| 782 | |
| 783 | // A 200 with no `state` is a read that failed, not a site without a |
| 784 | // plan. Caching it would hold that answer for the rest of the request; |
| 785 | // refusing lets a later caller ask again and get a real one. |
| 786 | if ( ! is_object( $state ) || ! isset( $state->state ) ) { |
| 787 | return new WP_Error( |
| 788 | 'rewind_state_unreadable', |
| 789 | esc_html__( 'Unable to read the backup plan details for this site.', 'jetpack-backup-pkg' ), |
| 790 | array( 'status' => 500 ) |
| 791 | ); |
| 792 | } |
| 793 | |
| 794 | self::$rewind_state = $state; |
| 795 | return self::$rewind_state; |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * Checks whether the site supports the product, reporting an unreadable answer as an error. |
| 800 | * |
| 801 | * @since 5.0.1 |
| 802 | * |
| 803 | * @return bool|WP_Error True when the site has Backup, or a WP_Error if WordPress.com could not be read. |
| 804 | */ |
| 805 | public static function get_backup_plan_state() { |
| 806 | $rewind_data = static::get_rewind_state_from_wpcom(); |
| 807 | |
| 808 | if ( is_wp_error( $rewind_data ) ) { |
| 809 | return $rewind_data; |
| 810 | } |
| 811 | |
| 812 | return 'unavailable' !== $rewind_data->state; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Checks whether the current plan (or purchases) of the site already supports the product |
| 817 | * |
| 818 | * Answers a plan it could not read as absent, which is the one place that |
| 819 | * decision is made for every `bool` caller. |
| 820 | * |
| 821 | * @return bool |
| 822 | */ |
| 823 | public static function has_backup_plan() { |
| 824 | $state = static::get_backup_plan_state(); |
| 825 | |
| 826 | return ! is_wp_error( $state ) && $state; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Get an array of backup/scan/anti-spam site capabilities |
| 831 | * |
| 832 | * @access public |
| 833 | * @static |
| 834 | * |
| 835 | * @return \WP_REST_Response|WP_Error The site capabilities, or a WP_Error if WordPress.com could not be reached. |
| 836 | */ |
| 837 | public static function get_backup_capabilities() { |
| 838 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 839 | |
| 840 | $response = Client::wpcom_json_api_request_as_user( |
| 841 | '/sites/' . $blog_id . '/rewind/capabilities', |
| 842 | 'v2', |
| 843 | array(), |
| 844 | null, |
| 845 | 'wpcom' |
| 846 | ); |
| 847 | |
| 848 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 849 | |
| 850 | if ( 200 !== $response_code ) { |
| 851 | return self::get_failed_fetch_error( $response ); |
| 852 | } |
| 853 | |
| 854 | return rest_ensure_response( |
| 855 | json_decode( $response['body'], true ) |
| 856 | ); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Get information about recent restores |
| 861 | * |
| 862 | * @access public |
| 863 | * @static |
| 864 | * |
| 865 | * @return \WP_REST_Response|WP_Error The recent restores, or a WP_Error if WordPress.com could not be reached. |
| 866 | */ |
| 867 | public static function get_recent_restores() { |
| 868 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 869 | $response = Client::wpcom_json_api_request_as_blog( |
| 870 | '/sites/' . $blog_id . '/rewind/restores', |
| 871 | 'v2', |
| 872 | array(), |
| 873 | null, |
| 874 | 'wpcom' |
| 875 | ); |
| 876 | |
| 877 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 878 | |
| 879 | if ( 200 !== $response_code ) { |
| 880 | return self::get_failed_fetch_error( $response ); |
| 881 | } |
| 882 | |
| 883 | return rest_ensure_response( |
| 884 | json_decode( $response['body'], true ) |
| 885 | ); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Query backup-completion events from the wpcom activity-log via the |
| 890 | * general `/sites/<id>/activity` endpoint with the action filter pinned |
| 891 | * to backup-completion event names. This endpoint paginates real-ly |
| 892 | * (Elasticsearch `from` offset under the hood) — the `/activity/rewindable` |
| 893 | * sibling looks like a more natural fit but hardcodes `page: 1, |
| 894 | * totalPages: 1` and ignores the `page` parameter. |
| 895 | * |
| 896 | * Auth: signs as user. The endpoint gates on the requesting WP user |
| 897 | * being an administrator of the blog (see |
| 898 | * sites-activity.php::readable_permission_check); blog-level tokens |
| 899 | * return 401. |
| 900 | * |
| 901 | * Returned shape (success): a W3C ActivityStreams envelope: |
| 902 | * { |
| 903 | * "@context": ..., "type": "OrderedCollection", "totalItems": int, |
| 904 | * "page": int, "totalPages": int, "itemsPerPage": int, |
| 905 | * "orderedItems": [ <event>, ... ] |
| 906 | * } |
| 907 | * Each event has at least `published`, `rewind_id`, `is_rewindable`, |
| 908 | * `name`, `status`, `summary`. |
| 909 | * |
| 910 | * @param array $args Query args passed through to wpcom. Supported keys: |
| 911 | * `after` (ISO 8601), `before` (ISO 8601), `on` (ISO 8601), |
| 912 | * `date_range`, `number` (max 1000), `page` (1-based), |
| 913 | * `sort_order` ('asc'|'desc'). Any `action` key is |
| 914 | * overridden with the curated backup-completion list. |
| 915 | * @return array|\WP_REST_Response|null |
| 916 | */ |
| 917 | public static function list_backup_events( array $args = array() ) { |
| 918 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 919 | |
| 920 | // Curated set of activity actions that represent "a backup completed". |
| 921 | // Mirrors `WPCOM_REST_API_V2_Endpoint_Site_Activity::$backup_action_names`. |
| 922 | // Pinned here (and overriding any caller-supplied `action`) so the |
| 923 | // helper is always scoped to backups regardless of what the caller passes. |
| 924 | $args['action'] = array( |
| 925 | 'backup_complete_full', |
| 926 | 'backup_complete_initial', |
| 927 | 'backup_only_complete_full', |
| 928 | 'backup_only_complete_initial', |
| 929 | 'rewind__backup_complete_full', |
| 930 | 'rewind__backup_complete_initial', |
| 931 | 'rewind__backup_only_complete_full', |
| 932 | 'rewind__backup_only_complete_initial', |
| 933 | ); |
| 934 | |
| 935 | $path = '/sites/' . (int) $blog_id . '/activity?' . http_build_query( $args ); |
| 936 | |
| 937 | $response = Client::wpcom_json_api_request_as_user( |
| 938 | $path, |
| 939 | 'v2', |
| 940 | array(), |
| 941 | null, |
| 942 | 'wpcom' |
| 943 | ); |
| 944 | |
| 945 | // Cast, as everywhere else this package reads a status. Uncast, a |
| 946 | // numeric-string `'200'` discards a good activity page and the |
| 947 | // `jetpack-backup/list-backup-events` ability reports that the site |
| 948 | // has completed no backups — `unwrap_response()` flattens this |
| 949 | // `null` into an empty list, which is a claim rather than an error. |
| 950 | if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 951 | return null; |
| 952 | } |
| 953 | |
| 954 | return rest_ensure_response( |
| 955 | json_decode( $response['body'], true ) |
| 956 | ); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Gets information about the currently promoted backup product. |
| 961 | * |
| 962 | * Answers from a per-locale transient when one is warm; failures are not cached. |
| 963 | * |
| 964 | * @return object|WP_Error The promoted product, or a WP_Error if it could not be read. |
| 965 | */ |
| 966 | public static function get_backup_promoted_product_info() { |
| 967 | $locale = get_user_locale(); |
| 968 | $transient_key = self::PROMOTED_PRODUCT_TRANSIENT_PREFIX . sanitize_key( $locale ); |
| 969 | $cached = get_transient( $transient_key ); |
| 970 | |
| 971 | if ( false !== $cached ) { |
| 972 | return $cached; |
| 973 | } |
| 974 | |
| 975 | $request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . $locale . '&type=jetpack'; |
| 976 | $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) ); |
| 977 | // Cast: the transport may report the status as a numeric string, which |
| 978 | // a strict comparison against 200 sends down the failure path. |
| 979 | $response_code = (int) wp_remote_retrieve_response_code( $wpcom_request ); |
| 980 | |
| 981 | if ( 200 !== $response_code ) { |
| 982 | return new WP_Error( |
| 983 | 'failed_to_fetch_data', |
| 984 | esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup-pkg' ), |
| 985 | array( |
| 986 | // A transport failure has no status at all; reporting 0 would |
| 987 | // leave the REST layer with nothing to serve. |
| 988 | 'status' => $response_code ? $response_code : 500, |
| 989 | 'request' => $wpcom_request, |
| 990 | ) |
| 991 | ); |
| 992 | } |
| 993 | |
| 994 | $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) ); |
| 995 | |
| 996 | // A 200 is not a promise that the promoted product is in the body. A |
| 997 | // truncated response decodes to null, and the slug is a constant here |
| 998 | // but a catalogue entry upstream — retiring it there leaves this a 200 |
| 999 | // with the key absent. Reading through either emits a PHP warning and |
| 1000 | // yields null, which the route then serves as a 200 carrying `null`: |
| 1001 | // indistinguishable, to a caller, from a priced answer it failed to |
| 1002 | // read. Refusing says which of the two happened. |
| 1003 | if ( ! is_object( $products ) || ! isset( $products->{ self::JETPACK_BACKUP_PROMOTED_PRODUCT } ) ) { |
| 1004 | return new WP_Error( |
| 1005 | 'promoted_product_unreadable', |
| 1006 | esc_html__( 'Unable to read the promoted product information.', 'jetpack-backup-pkg' ), |
| 1007 | array( 'status' => 500 ) |
| 1008 | ); |
| 1009 | } |
| 1010 | |
| 1011 | $product = $products->{ self::JETPACK_BACKUP_PROMOTED_PRODUCT }; |
| 1012 | |
| 1013 | // Must stay below both guards: a cached failure would leave the no-plan |
| 1014 | // screen without a price for the whole TTL after WordPress.com recovered. |
| 1015 | set_transient( $transient_key, $product, self::PROMOTED_PRODUCT_CACHE_TTL ); |
| 1016 | |
| 1017 | return $product; |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Check for user licenses. |
| 1022 | * |
| 1023 | * @param boolean $has_license If the user already has a license found. |
| 1024 | * @param array $licenses List of unattached licenses belonging to the user. |
| 1025 | * @param string $plugin_slug The plugin that initiated the flow. |
| 1026 | * |
| 1027 | * @return boolean |
| 1028 | */ |
| 1029 | public static function jetpack_check_user_licenses( $has_license, $licenses, $plugin_slug ) { |
| 1030 | if ( $plugin_slug !== static::JETPACK_BACKUP_SLUG || $has_license ) { |
| 1031 | return $has_license; |
| 1032 | } |
| 1033 | |
| 1034 | $license_found = false; |
| 1035 | |
| 1036 | foreach ( $licenses as $license ) { |
| 1037 | if ( in_array( $license->product_id, static::JETPACK_BACKUP_PRODUCT_IDS, true ) ) { |
| 1038 | $license_found = true; |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | // Checking for existing backup plan is costly, so only check if there's an appropriate license. |
| 1044 | return $license_found && ! static::has_backup_plan(); |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Returns the result of `/upgrades` endpoint call. |
| 1049 | * |
| 1050 | * @return \WP_REST_Response|WP_Error The site purchases, or a WP_Error if WordPress.com could not be reached. |
| 1051 | */ |
| 1052 | public static function get_site_current_purchases() { |
| 1053 | |
| 1054 | $request = sprintf( '/upgrades?site=%d', Jetpack_Options::get_option( 'id' ) ); |
| 1055 | $response = Client::wpcom_json_api_request_as_blog( $request, '1.2' ); |
| 1056 | |
| 1057 | // Bail if there was an error or malformed response. |
| 1058 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
| 1059 | return self::get_failed_fetch_error( $response ); |
| 1060 | } |
| 1061 | |
| 1062 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 1063 | |
| 1064 | if ( 200 !== $response_code ) { |
| 1065 | return self::get_failed_fetch_error( $response ); |
| 1066 | } |
| 1067 | |
| 1068 | return rest_ensure_response( |
| 1069 | json_decode( $response['body'], true ) |
| 1070 | ); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * Set value of the dismissed_backup_review_request Jetack option. |
| 1075 | * Get value if should_dismiss is false |
| 1076 | * |
| 1077 | * @access public |
| 1078 | * @static |
| 1079 | * @param array $request arguments should_dismiss and option_name. |
| 1080 | * @return bool value of option if value is requested | updated or not if value is updated. |
| 1081 | */ |
| 1082 | public static function manage_dismissed_backup_review_request( $request ) { |
| 1083 | |
| 1084 | if ( ! $request['should_dismiss'] ) { |
| 1085 | |
| 1086 | return rest_ensure_response( |
| 1087 | Jetpack_Options::get_option( 'dismissed_backup_review_' . $request['option_name'] ) |
| 1088 | ); |
| 1089 | } |
| 1090 | |
| 1091 | return Jetpack_Options::update_option( 'dismissed_backup_review_' . $request['option_name'], true ); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * Get site storage size |
| 1096 | * |
| 1097 | * @return \WP_REST_Response|WP_Error The site storage size, or a WP_Error if WordPress.com could not be reached. |
| 1098 | */ |
| 1099 | public static function get_site_backup_size() { |
| 1100 | return self::get_cached_rewind_read( 'size', self::SITE_SIZE_TRANSIENT ); |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Get site policies from WPCOM. It includes the storage limit and activity log limit, if apply. |
| 1105 | * |
| 1106 | * @return \WP_REST_Response|WP_Error The site storage policies, or a WP_Error if WordPress.com could not be reached. |
| 1107 | */ |
| 1108 | 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 | |
| 1139 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 1140 | |
| 1141 | $response = Client::wpcom_json_api_request_as_user( |
| 1142 | '/sites/' . $blog_id . '/rewind/' . $endpoint . '?force=wpcom', |
| 1143 | 'v2', |
| 1144 | array(), |
| 1145 | null, |
| 1146 | 'wpcom' |
| 1147 | ); |
| 1148 | |
| 1149 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 1150 | |
| 1151 | if ( 200 !== $response_code ) { |
| 1152 | return self::get_failed_fetch_error( $response ); |
| 1153 | } |
| 1154 | |
| 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 ); |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * Get suggested storage addon based on storage usage |
| 1167 | * |
| 1168 | * @param int $bytes_used Storage used. |
| 1169 | * @param int $bytes_available Storage limit. |
| 1170 | * @return string Suggested addon storage slug |
| 1171 | */ |
| 1172 | public static function get_storage_addon_upsell_slug( $bytes_used, $bytes_available ) { |
| 1173 | $bytes_10gb = 10 * 1024 * 1024 * 1024; // 10GB in bytes |
| 1174 | $bytes_100gb = 100 * 1024 * 1024 * 1024; // 100GB in bytes |
| 1175 | $bytes_1tb = 1024 * 1024 * 1024 * 1024; // 1TB in bytes |
| 1176 | |
| 1177 | $upsell_products = array( |
| 1178 | $bytes_10gb => 'jetpack_backup_addon_storage_10gb_monthly', |
| 1179 | $bytes_100gb => 'jetpack_backup_addon_storage_100gb_monthly', |
| 1180 | $bytes_1tb => 'jetpack_backup_addon_storage_1tb_monthly', |
| 1181 | ); |
| 1182 | |
| 1183 | // If usage has crossed over the storage limit, then dynamically calculate the upgrade option |
| 1184 | if ( $bytes_used > $bytes_available ) { |
| 1185 | $additional_bytes_used = $bytes_used - $bytes_available; |
| 1186 | |
| 1187 | // Add aditional 25% buffer |
| 1188 | $additional_bytes_needed = $additional_bytes_used + $additional_bytes_used * 0.25; |
| 1189 | |
| 1190 | // Since 1TB is our max upgrade but the additional storage needed is greater than 1TB, then just return 1TB |
| 1191 | if ( $additional_bytes_needed > $bytes_1tb ) { |
| 1192 | return $upsell_products[ $bytes_1tb ]; |
| 1193 | } |
| 1194 | |
| 1195 | $matched_bytes = $bytes_10gb; |
| 1196 | foreach ( $upsell_products as $bytes => $product ) { |
| 1197 | if ( $bytes > $additional_bytes_needed ) { |
| 1198 | $matched_bytes = $bytes; |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return $upsell_products[ $matched_bytes ]; |
| 1204 | } |
| 1205 | |
| 1206 | // For 1 TB we are going to offer 1 TB by default |
| 1207 | if ( $bytes_1tb === $bytes_available ) { |
| 1208 | return $upsell_products[ $bytes_1tb ]; |
| 1209 | } |
| 1210 | |
| 1211 | // Otherwise, we are going to offer 10 GB |
| 1212 | return $upsell_products[ $bytes_10gb ]; |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Get the best addon offer for this site, including pricing details |
| 1217 | * |
| 1218 | * @param \WP_REST_Request $request Object including storage usage. |
| 1219 | * |
| 1220 | * @return string|WP_Error A JSON object with the suggested storage addon details if the request was successful, |
| 1221 | * or a WP_Error otherwise. |
| 1222 | */ |
| 1223 | public static function get_site_backup_addon_offer( $request ) { |
| 1224 | $suggested_addon = self::get_storage_addon_upsell_slug( |
| 1225 | $request['storage_size'], |
| 1226 | $request['storage_limit'] |
| 1227 | ); |
| 1228 | |
| 1229 | $addons_size_text_map = array( |
| 1230 | 'jetpack_backup_addon_storage_10gb_monthly' => '10GB', |
| 1231 | 'jetpack_backup_addon_storage_100gb_monthly' => '100GB', |
| 1232 | 'jetpack_backup_addon_storage_1tb_monthly' => '1TB', |
| 1233 | ); |
| 1234 | |
| 1235 | // Fetch addon storage price information |
| 1236 | $pricing_info = Wpcom_Products::get_product_pricing( $suggested_addon ); |
| 1237 | |
| 1238 | // Response |
| 1239 | $response = array( |
| 1240 | 'slug' => $suggested_addon, |
| 1241 | 'size_text' => $addons_size_text_map[ $suggested_addon ], |
| 1242 | 'pricing' => $pricing_info, |
| 1243 | ); |
| 1244 | |
| 1245 | return rest_ensure_response( $response ); |
| 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * Enqueue a new backup on demand |
| 1250 | * |
| 1251 | * @return \WP_REST_Response|WP_Error The enqueue result, or a WP_Error if WordPress.com could not be reached. |
| 1252 | */ |
| 1253 | public static function enqueue_backup() { |
| 1254 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 1255 | $endpoint = sprintf( '/sites/%d/rewind/backups/enqueue', $blog_id ); |
| 1256 | |
| 1257 | $response = Client::wpcom_json_api_request_as_user( |
| 1258 | $endpoint, |
| 1259 | 'v2', |
| 1260 | array( |
| 1261 | 'method' => 'POST', |
| 1262 | ), |
| 1263 | null, |
| 1264 | 'wpcom' |
| 1265 | ); |
| 1266 | |
| 1267 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 1268 | |
| 1269 | if ( 200 !== $response_code ) { |
| 1270 | return self::get_failed_fetch_error( $response ); |
| 1271 | } |
| 1272 | |
| 1273 | return rest_ensure_response( |
| 1274 | json_decode( $response['body'], true ) |
| 1275 | ); |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * Get site backup schedule time |
| 1280 | * |
| 1281 | * @return \WP_REST_Response|WP_Error The backup schedule time, or a WP_Error if WordPress.com could not be reached. |
| 1282 | */ |
| 1283 | public static function get_site_backup_schedule_time() { |
| 1284 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 1285 | |
| 1286 | $response = Client::wpcom_json_api_request_as_user( |
| 1287 | '/sites/' . $blog_id . '/rewind/scheduled', |
| 1288 | 'v2', |
| 1289 | array(), |
| 1290 | null, |
| 1291 | 'wpcom' |
| 1292 | ); |
| 1293 | |
| 1294 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 1295 | |
| 1296 | if ( 200 !== $response_code ) { |
| 1297 | return self::get_failed_fetch_error( $response ); |
| 1298 | } |
| 1299 | |
| 1300 | return rest_ensure_response( |
| 1301 | json_decode( $response['body'], true ) |
| 1302 | ); |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Removes plugin from the connection manager |
| 1307 | * If it's the last plugin using the connection, the site will be disconnected. |
| 1308 | * |
| 1309 | * @access public |
| 1310 | * @static |
| 1311 | */ |
| 1312 | public static function plugin_deactivation() { |
| 1313 | $manager = new Connection_Manager( 'jetpack-backup' ); |
| 1314 | $manager->remove_connection(); |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * Load wp-build when modernization is enabled on the Backup admin page. |
| 1319 | * |
| 1320 | * @return void |
| 1321 | */ |
| 1322 | public static function maybe_load_wp_build() { |
| 1323 | if ( ! self::is_modernized() || ! self::is_backup_admin_request() ) { |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | self::load_wp_build_with_screen_alias(); |
| 1328 | |
| 1329 | add_action( 'admin_print_scripts', array( __CLASS__, 'render_connection_initial_state' ), 1 ); |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * Emit `window.JP_CONNECTION_INITIAL_STATE` inline on the modernized |
| 1334 | * Backup admin page. |
| 1335 | * |
| 1336 | * The modernized enqueue path short-circuits before the legacy |
| 1337 | * `Connection_Initial_State::render_script()` call, so without this |
| 1338 | * the React `<Gates>` component never sees the connection state and |
| 1339 | * sits on its loading skeleton forever. We emit the same JS payload |
| 1340 | * the legacy path emits, just outside of a registered script handle |
| 1341 | * (wp-build's handles aren't reliable here, and the global is |
| 1342 | * page-scoped — any tag setting it works). |
| 1343 | * |
| 1344 | * @return void |
| 1345 | */ |
| 1346 | public static function render_connection_initial_state() { |
| 1347 | echo '<script id="jetpack-backup-connection-initial-state">' |
| 1348 | . Connection_Initial_State::render() // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render() returns pre-escaped JSON. |
| 1349 | . '</script>'; |
| 1350 | |
| 1351 | echo '<script id="jetpack-backup-dashboard-state">window.JPBACKUP_DASHBOARD_STATE=' |
| 1352 | . wp_json_encode( |
| 1353 | array( |
| 1354 | 'activityLogUrl' => self::get_activity_log_url(), |
| 1355 | 'knownThreatsAvailable' => \Automattic\Jetpack\Backup\V0005\REST\Restore_Bridge::known_threats_available(), |
| 1356 | 'addLicense' => Backup_Initial_State::is_add_license_screen_enabled(), |
| 1357 | ), |
| 1358 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 1359 | ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_json_encode() with JSON_HEX_* flags is safe in a script tag. |
| 1360 | . ';</script>'; |
| 1361 | } |
| 1362 | |
| 1363 | /** |
| 1364 | * The Activity Log admin page URL, or null when that page is not registered. |
| 1365 | * |
| 1366 | * Asks the admin menu rather than building the URL, so a site where the |
| 1367 | * Activity Log module is off or unavailable gets no link. |
| 1368 | * |
| 1369 | * @return string|null |
| 1370 | */ |
| 1371 | public static function get_activity_log_url() { |
| 1372 | // No admin menu here: see Admin_Shell::guest_assets(). |
| 1373 | if ( wp_doing_ajax() ) { |
| 1374 | return self::activity_log_page_will_register() ? admin_url( 'admin.php?page=jetpack-activity-log' ) : null; |
| 1375 | } |
| 1376 | |
| 1377 | $url = menu_page_url( 'jetpack-activity-log', false ); |
| 1378 | return $url ? $url : null; |
| 1379 | } |
| 1380 | |
| 1381 | /** |
| 1382 | * Whether the Activity Log page registers for this user, for requests that never build the menu. |
| 1383 | * |
| 1384 | * @return bool |
| 1385 | */ |
| 1386 | private static function activity_log_page_will_register() { |
| 1387 | return did_action( 'jetpack_activity_log_initialized' ) |
| 1388 | && class_exists( 'Automattic\Jetpack\Activity_Log\Jetpack_Activity_Log' ) |
| 1389 | && \Automattic\Jetpack\Activity_Log\Jetpack_Activity_Log::is_available(); |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * Load the wp-build entry file and register its polyfills. |
| 1394 | * |
| 1395 | * Only called on `?page=jetpack-backup` admin requests when `is_modernized()` |
| 1396 | * is true. Keeps wp-build off every other request. |
| 1397 | * |
| 1398 | * @return void |
| 1399 | */ |
| 1400 | private static function load_wp_build() { |
| 1401 | $build_index = dirname( __DIR__ ) . '/build/build.php'; |
| 1402 | |
| 1403 | if ( ! file_exists( $build_index ) ) { |
| 1404 | return; |
| 1405 | } |
| 1406 | |
| 1407 | require_once $build_index; |
| 1408 | |
| 1409 | // wp-build registers standalone modules (e.g. the init module) on |
| 1410 | // wp_default_scripts, which has already fired by admin_menu. Register them |
| 1411 | // directly so the init module makes it into the import map. |
| 1412 | if ( function_exists( 'jetpack_backup_register_script_modules' ) ) { |
| 1413 | jetpack_backup_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes. |
| 1414 | } |
| 1415 | |
| 1416 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register( |
| 1417 | 'jetpack-backup', |
| 1418 | array_merge( |
| 1419 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES, |
| 1420 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS |
| 1421 | ) |
| 1422 | ); |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * Load wp-build with the screen ID aliased across its generated enqueue check. |
| 1427 | * |
| 1428 | * @see WP_Build_Screen_Id::load_with_alias() |
| 1429 | * @return void |
| 1430 | */ |
| 1431 | private static function load_wp_build_with_screen_alias() { |
| 1432 | // Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias(). |
| 1433 | if ( method_exists( \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::class, 'load_with_alias' ) ) { |
| 1434 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::load_with_alias( |
| 1435 | array( __CLASS__, 'alias_screen_id_for_wp_build' ), |
| 1436 | array( __CLASS__, 'restore_screen_id_after_wp_build' ), |
| 1437 | function () { |
| 1438 | self::load_wp_build(); |
| 1439 | } |
| 1440 | ); |
| 1441 | return; |
| 1442 | } |
| 1443 | |
| 1444 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); |
| 1445 | self::load_wp_build(); |
| 1446 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) ); |
| 1447 | } |
| 1448 | |
| 1449 | /** |
| 1450 | * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check. |
| 1451 | * |
| 1452 | * Wp-build's `<page>-wp-admin` enqueue callback enqueues only when the screen ID |
| 1453 | * matches the wp-build page slug (`jetpack-backup-dashboard`). Our WP-admin |
| 1454 | * menu slug stays `jetpack-backup`, so we mutate the screen object in place |
| 1455 | * to make the check pass without changing the user-facing URL. |
| 1456 | * |
| 1457 | * Hooked only when modernization is on AND we're on the Backup admin page, |
| 1458 | * so this never affects any other request. |
| 1459 | * |
| 1460 | * @since 5.0.4 Takes no argument; hooked on `admin_enqueue_scripts`. |
| 1461 | * |
| 1462 | * @return void |
| 1463 | */ |
| 1464 | public static function alias_screen_id_for_wp_build() { |
| 1465 | $screen = get_current_screen(); |
| 1466 | if ( ! $screen ) { |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | self::$wp_build_original_screen_id = $screen->id; |
| 1471 | $screen->id = self::WP_BUILD_PAGE_ID; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID. |
| 1476 | * |
| 1477 | * @since 5.0.4 |
| 1478 | * |
| 1479 | * @return void |
| 1480 | */ |
| 1481 | public static function restore_screen_id_after_wp_build() { |
| 1482 | $screen = get_current_screen(); |
| 1483 | if ( ! $screen || null === self::$wp_build_original_screen_id ) { |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | $screen->id = self::$wp_build_original_screen_id; |
| 1488 | self::$wp_build_original_screen_id = null; |
| 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * Sends the Activity Log's "Restore backup" to this dashboard's restore screen when it is on. |
| 1493 | * |
| 1494 | * @param string $template Restore URL template, with `{rewindId}` as the placeholder. |
| 1495 | * @return string |
| 1496 | */ |
| 1497 | public static function activity_log_restore_url_template( $template ) { |
| 1498 | if ( ! self::is_modernized() ) { |
| 1499 | return $template; |
| 1500 | } |
| 1501 | |
| 1502 | return admin_url( 'admin.php?page=' . self::JETPACK_BACKUP_SLUG . '&p=%2Frestore%2F{rewindId}' ); |
| 1503 | } |
| 1504 | |
| 1505 | /** |
| 1506 | * Sends the Activity Log's links to a backup to this dashboard's Overview, with that backup selected, when it is on. |
| 1507 | * |
| 1508 | * The router reads search values as JSON, so the rewind id is quoted to stay a string. |
| 1509 | * |
| 1510 | * @param string $template Backup URL template, with `{rewindId}` as the placeholder. |
| 1511 | * @return string |
| 1512 | */ |
| 1513 | public static function activity_log_backup_url_template( $template ) { |
| 1514 | if ( ! self::is_modernized() ) { |
| 1515 | return $template; |
| 1516 | } |
| 1517 | |
| 1518 | return admin_url( 'admin.php?page=' . self::JETPACK_BACKUP_SLUG . '&p=%2F%3Fselected%3D%2522{rewindId}%2522' ); |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * Returns the modernization filter's value, which defaults to true unless the site has the legacy dashboard sticker. |
| 1523 | * |
| 1524 | * @since 4.3.14 Changed from private to public; the REST bridges gate their route registration on it. |
| 1525 | * @since 5.2.0 Defaults to true. |
| 1526 | * |
| 1527 | * @return bool |
| 1528 | */ |
| 1529 | public static function is_modernized() { |
| 1530 | $has_legacy_sticker = function_exists( 'wpcomsh_is_site_sticker_active' ) && wpcomsh_is_site_sticker_active( self::LEGACY_DASHBOARD_STICKER ); |
| 1531 | |
| 1532 | return (bool) apply_filters( self::MODERNIZATION_FILTER, ! $has_legacy_sticker ); |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Returns true when `is_modernized()` is true AND the wp-build dashboard loaded. |
| 1537 | * |
| 1538 | * `build/` is gitignored, so the render function is absent in any unbuilt checkout |
| 1539 | * and in any release whose wp-build step failed. Every consumer of the modernized |
| 1540 | * surface has to agree on this, or the menu falls back to the legacy page while the |
| 1541 | * enqueue path skips the legacy script — an empty div with no JS. |
| 1542 | * |
| 1543 | * Only meaningful once `maybe_load_wp_build()` has run. It is hooked on `admin_menu` |
| 1544 | * at the same priority as `add_wp_admin_submenu()`, so registration order — not |
| 1545 | * priority — is what keeps it first. Do not reorder those two `add_action()` calls. |
| 1546 | * |
| 1547 | * @return bool |
| 1548 | */ |
| 1549 | private static function is_wp_build_dashboard_active() { |
| 1550 | return self::is_modernized() && function_exists( 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' ); |
| 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * Returns true when the current request targets the Backup admin page. |
| 1555 | * |
| 1556 | * Used to scope wp-build loading to the one page that needs it. The |
| 1557 | * `$_GET['page']` value is populated by wp-admin/admin.php before any of |
| 1558 | * our hooks fire, so this check is reliable from `initialize()` onwards. |
| 1559 | * |
| 1560 | * @return bool |
| 1561 | */ |
| 1562 | private static function is_backup_admin_request() { |
| 1563 | if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1564 | return false; |
| 1565 | } |
| 1566 | |
| 1567 | return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::JETPACK_BACKUP_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1568 | } |
| 1569 | } |