Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.98% |
379 / 468 |
|
57.14% |
20 / 35 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Backup | |
81.33% |
379 / 466 |
|
57.14% |
20 / 35 |
136.25 | |
0.00% |
0 / 1 |
| initialize | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 | |||
| add_wp_admin_submenu | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| admin_init | |
100.00% |
4 / 4 |
|
100.00% |
1 / 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% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_failed_fetch_error | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| get_recent_backups | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| get_rewind_state_from_wpcom | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
3.07 | |||
| has_backup_plan | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
| 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% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| jetpack_check_user_licenses | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
| 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% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| get_site_backup_policies | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| render_connection_initial_state | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| load_wp_build | |
27.27% |
3 / 11 |
|
0.00% |
0 / 1 |
3.54 | |||
| alias_screen_id_for_wp_build | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| is_modernized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_wp_build_dashboard_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_backup_admin_request | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| 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\Assets; |
| 20 | use Automattic\Jetpack\Backup\V0005\Initial_State as Backup_Initial_State; |
| 21 | use Automattic\Jetpack\Config; |
| 22 | use Automattic\Jetpack\Connection\Client; |
| 23 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 24 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 25 | use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication; |
| 26 | use Automattic\Jetpack\My_Jetpack\Wpcom_Products; |
| 27 | use Automattic\Jetpack\Status; |
| 28 | use Automattic\Jetpack\Terms_Of_Service; |
| 29 | use Automattic\Jetpack\Tracking; |
| 30 | use Jetpack_Options; |
| 31 | use WP_Error; |
| 32 | use WP_REST_Server; |
| 33 | use function add_action; |
| 34 | use function add_filter; |
| 35 | use function did_action; |
| 36 | use function do_action; |
| 37 | use function esc_url_raw; |
| 38 | use function get_option; |
| 39 | use function is_wp_error; |
| 40 | use function rest_ensure_response; |
| 41 | use function update_option; |
| 42 | use function wp_add_inline_script; |
| 43 | use function wp_remote_get; |
| 44 | use function wp_remote_retrieve_body; |
| 45 | use function wp_remote_retrieve_response_code; |
| 46 | |
| 47 | /** |
| 48 | * Class Jetpack_Backup |
| 49 | */ |
| 50 | class Jetpack_Backup { |
| 51 | |
| 52 | /** |
| 53 | * Slug. |
| 54 | * |
| 55 | * @var string |
| 56 | */ |
| 57 | const JETPACK_BACKUP_SLUG = 'jetpack-backup'; |
| 58 | |
| 59 | /** |
| 60 | * Backup name. |
| 61 | * |
| 62 | * @var string |
| 63 | */ |
| 64 | const JETPACK_BACKUP_NAME = 'Jetpack Backup'; |
| 65 | |
| 66 | /** |
| 67 | * Backup URL. |
| 68 | * |
| 69 | * @var string |
| 70 | */ |
| 71 | const JETPACK_BACKUP_URI = 'https://jetpack.com/jetpack-backup'; |
| 72 | |
| 73 | /** |
| 74 | * Promoted product. |
| 75 | * |
| 76 | * @var string |
| 77 | */ |
| 78 | const JETPACK_BACKUP_PROMOTED_PRODUCT = 'jetpack_backup_t1_yearly'; |
| 79 | |
| 80 | /** |
| 81 | * Licenses product ID. |
| 82 | * |
| 83 | * @var string |
| 84 | */ |
| 85 | const JETPACK_BACKUP_PRODUCT_IDS = array( |
| 86 | 2014, // JETPACK_COMPLETE. |
| 87 | 2015, // JETPACK_COMPLETE_MONTHLY. |
| 88 | 2016, // JETPACK_SECURITY_TIER_1_YEARLY. |
| 89 | 2017, // JETPACK_SECURITY_TIER_1_MONTHLY. |
| 90 | 2019, // JETPACK_SECURITY_TIER_2_YEARLY. |
| 91 | 2020, // JETPACK_SECURITY_TIER_2_MONTHLY. |
| 92 | 2112, // JETPACK_BACKUP_TIER_1_YEARLY. |
| 93 | 2113, // JETPACK_BACKUP_TIER_1_MONTHLY. |
| 94 | 2114, // JETPACK_BACKUP_TIER_2_YEARLY. |
| 95 | 2115, // JETPACK_BACKUP_TIER_2_MONTHLY. |
| 96 | ); |
| 97 | |
| 98 | /** |
| 99 | * Jetpack Backup DB version. |
| 100 | * |
| 101 | * @var string |
| 102 | */ |
| 103 | const JETPACK_BACKUP_DB_VERSION = '2'; |
| 104 | |
| 105 | /** |
| 106 | * Filter name that gates the wp-build–based dashboard. |
| 107 | * |
| 108 | * When this filter returns true, "Jetpack > Backup" renders the new |
| 109 | * wp-build dashboard instead of the legacy React app. |
| 110 | */ |
| 111 | const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_backup'; |
| 112 | |
| 113 | /** |
| 114 | * Constructor. |
| 115 | */ |
| 116 | public static function initialize() { |
| 117 | if ( did_action( 'jetpack_backup_initialized' ) ) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Set up the REST authentication hooks. |
| 122 | Connection_Rest_Authentication::init(); |
| 123 | |
| 124 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 125 | add_action( 'rest_api_init', array( \Automattic\Jetpack\Backup\V0005\REST\Rest_Controller::class, 'register_routes' ) ); |
| 126 | |
| 127 | add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 ); |
| 128 | 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. |
| 129 | |
| 130 | // Init Jetpack packages. |
| 131 | add_action( |
| 132 | 'plugins_loaded', |
| 133 | function () { |
| 134 | $config = new Config(); |
| 135 | // Connection package. |
| 136 | $config->ensure( |
| 137 | 'connection', |
| 138 | array( |
| 139 | 'slug' => self::JETPACK_BACKUP_SLUG, |
| 140 | 'name' => self::JETPACK_BACKUP_NAME, |
| 141 | 'url_info' => self::JETPACK_BACKUP_URI, |
| 142 | ) |
| 143 | ); |
| 144 | // Sync package. |
| 145 | $config->ensure( 'sync' ); |
| 146 | |
| 147 | // Identity crisis package. |
| 148 | $config->ensure( 'identity_crisis' ); |
| 149 | }, |
| 150 | 1 |
| 151 | ); |
| 152 | |
| 153 | add_action( 'plugins_loaded', array( __CLASS__, 'maybe_upgrade_db' ), 20 ); |
| 154 | |
| 155 | add_filter( 'jetpack_connection_user_has_license', array( __CLASS__, 'jetpack_check_user_licenses' ), 10, 3 ); |
| 156 | |
| 157 | // Jetpack Backup abilities are registered from `actions.php` at package |
| 158 | // autoload time so the surface is available in every consumer that |
| 159 | // loads this package (both the standalone Backup plugin and the |
| 160 | // Jetpack plugin), not only when `Jetpack_Backup::initialize()` runs. |
| 161 | |
| 162 | /** |
| 163 | * Runs right after the Jetpack Backup package is initialized. |
| 164 | * |
| 165 | * @since 1.3.0 |
| 166 | */ |
| 167 | do_action( 'jetpack_backup_initialized' ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * The page to be added to submenu |
| 172 | */ |
| 173 | public static function add_wp_admin_submenu() { |
| 174 | $wp_build_active = self::is_wp_build_dashboard_active(); |
| 175 | $callback = $wp_build_active |
| 176 | ? 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' |
| 177 | : array( __CLASS__, 'plugin_settings_page' ); |
| 178 | |
| 179 | // The relabel rides the modernized dashboard rather than the filter alone, |
| 180 | // so a fallback to the legacy page also falls back to the legacy title. |
| 181 | $page_title = $wp_build_active ? 'Jetpack VaultPress Backup' : 'Jetpack Backup'; |
| 182 | $menu_title = $wp_build_active ? 'VaultPress Backup' : 'Backup'; // Product name, do not translate. |
| 183 | |
| 184 | $page_suffix = Admin_Menu::add_menu( |
| 185 | $page_title, |
| 186 | $menu_title, |
| 187 | 'manage_options', |
| 188 | self::JETPACK_BACKUP_SLUG, |
| 189 | $callback, |
| 190 | 7 |
| 191 | ); |
| 192 | |
| 193 | if ( $page_suffix ) { |
| 194 | add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Initialize the admin resources. |
| 200 | */ |
| 201 | public static function admin_init() { |
| 202 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) ); |
| 203 | |
| 204 | if ( self::is_wp_build_dashboard_active() ) { |
| 205 | // The modernized Backup overview is a focused, full-screen product |
| 206 | // surface. Suppress JITMs and other core/plugin admin notices so they |
| 207 | // don't reflow on top of the dual-pane layout. Mirrors how Jetpack |
| 208 | // Forms handles its dashboard page |
| 209 | // (`plugins/forms/src/dashboard/class-dashboard.php`). |
| 210 | remove_all_actions( 'admin_notices' ); |
| 211 | remove_all_actions( 'all_admin_notices' ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Checks current version against version in code and run upgrades if we are running a new version |
| 217 | */ |
| 218 | public static function maybe_upgrade_db() { |
| 219 | $current_db_version = get_option( 'jetpack_backup_db_version' ); |
| 220 | if ( version_compare( $current_db_version, self::JETPACK_BACKUP_DB_VERSION, '<' ) ) { |
| 221 | update_option( 'jetpack_backup_db_version', self::JETPACK_BACKUP_DB_VERSION ); |
| 222 | Jetpack_Backup_Upgrades::upgrade(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns whether we are in condition to track to use |
| 228 | * Analytics functionality like Tracks, MC, or GA. |
| 229 | */ |
| 230 | public static function can_use_analytics() { |
| 231 | $status = new Status(); |
| 232 | $connection = new Connection_Manager( 'jetpack-backup' ); |
| 233 | $tracking = new Tracking( 'jetpack', $connection ); |
| 234 | |
| 235 | return $tracking->should_enable_tracking( new Terms_Of_Service(), $status ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Enqueue plugin admin scripts and styles. |
| 240 | */ |
| 241 | public static function enqueue_admin_scripts() { |
| 242 | // This callback is registered via `load-{$page_suffix}` in `add_wp_admin_submenu()`, |
| 243 | // so it only fires on the Backup admin page — no need to re-check the page here. |
| 244 | if ( self::is_wp_build_dashboard_active() ) { |
| 245 | // The i18n loader is registered on every admin page by jetpack-assets but |
| 246 | // only enqueued when depended on; the esbuild bundles don't pull it in. |
| 247 | // Enqueue it here, before the early return, so the wp-build dashboard's |
| 248 | // init module can download its JS translation catalogs. |
| 249 | if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) { |
| 250 | wp_enqueue_script( 'wp-jp-i18n-loader' ); |
| 251 | } |
| 252 | |
| 253 | // The esbuild bundles don't declare the Tracks client as a dependency |
| 254 | // either, so it never reaches the page on its own. |
| 255 | if ( self::can_use_analytics() ) { |
| 256 | Tracking::register_tracks_functions_scripts( true ); |
| 257 | } |
| 258 | |
| 259 | // wp-build manages its own enqueue pipeline. The legacy script and |
| 260 | // its initial state are skipped for the wp-build dashboard. |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | Assets::register_script( |
| 265 | 'jetpack-backup', |
| 266 | '../build/index.js', |
| 267 | __FILE__, |
| 268 | array( |
| 269 | 'in_footer' => true, |
| 270 | 'textdomain' => 'jetpack-backup-pkg', |
| 271 | ) |
| 272 | ); |
| 273 | Assets::enqueue_script( 'jetpack-backup' ); |
| 274 | // Initial JS state including JP Connection data. |
| 275 | wp_add_inline_script( 'jetpack-backup', self::get_initial_state(), 'before' ); |
| 276 | Connection_Initial_State::render_script( 'jetpack-backup' ); |
| 277 | |
| 278 | // Load script for analytics. |
| 279 | if ( self::can_use_analytics() ) { |
| 280 | Tracking::register_tracks_functions_scripts( true ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Main plugin settings page. |
| 286 | */ |
| 287 | public static function plugin_settings_page() { |
| 288 | ?> |
| 289 | <div id="jetpack-backup-root"></div> |
| 290 | <?php |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Return the rendered initial state JavaScript code. |
| 295 | * |
| 296 | * @return string |
| 297 | */ |
| 298 | private static function get_initial_state() { |
| 299 | return ( new Backup_Initial_State() )->render(); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Register REST API |
| 304 | */ |
| 305 | public static function register_rest_routes() { |
| 306 | |
| 307 | // Get information on most recent 10 backups. |
| 308 | register_rest_route( |
| 309 | 'jetpack/v4', |
| 310 | '/backups', |
| 311 | array( |
| 312 | 'methods' => WP_REST_Server::READABLE, |
| 313 | 'callback' => __CLASS__ . '::get_recent_backups', |
| 314 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 315 | ) |
| 316 | ); |
| 317 | |
| 318 | // Get site backup/scan/anti-spam capabilities. |
| 319 | register_rest_route( |
| 320 | 'jetpack/v4', |
| 321 | '/backup-capabilities', |
| 322 | array( |
| 323 | 'methods' => WP_REST_Server::READABLE, |
| 324 | 'callback' => __CLASS__ . '::get_backup_capabilities', |
| 325 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 326 | ) |
| 327 | ); |
| 328 | |
| 329 | // Get whether the site has a backup plan |
| 330 | register_rest_route( |
| 331 | 'jetpack/v4', |
| 332 | '/has-backup-plan', |
| 333 | array( |
| 334 | 'methods' => WP_REST_Server::READABLE, |
| 335 | 'callback' => __CLASS__ . '::has_backup_plan', |
| 336 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 337 | ) |
| 338 | ); |
| 339 | |
| 340 | // Get site rewind data. |
| 341 | register_rest_route( |
| 342 | 'jetpack/v4', |
| 343 | '/restores', |
| 344 | array( |
| 345 | 'methods' => WP_REST_Server::READABLE, |
| 346 | 'callback' => __CLASS__ . '::get_recent_restores', |
| 347 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 348 | ) |
| 349 | ); |
| 350 | |
| 351 | // Get information on site products. |
| 352 | // Backup plugin version of /site/purchases from JP plugin. |
| 353 | // Revert once this route and MyPlan component are extracted to a common package. |
| 354 | register_rest_route( |
| 355 | 'jetpack/v4', |
| 356 | '/site/current-purchases', |
| 357 | array( |
| 358 | 'methods' => WP_REST_Server::READABLE, |
| 359 | 'callback' => __CLASS__ . '::get_site_current_purchases', |
| 360 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 361 | ) |
| 362 | ); |
| 363 | |
| 364 | // Get currently promoted product from the product's endpoint. |
| 365 | register_rest_route( |
| 366 | 'jetpack/v4', |
| 367 | '/backup-promoted-product-info', |
| 368 | array( |
| 369 | 'methods' => WP_REST_Server::READABLE, |
| 370 | 'callback' => __CLASS__ . '::get_backup_promoted_product_info', |
| 371 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 372 | ) |
| 373 | ); |
| 374 | |
| 375 | // Get and set value of dismissed_backup_review_request option |
| 376 | register_rest_route( |
| 377 | 'jetpack/v4', |
| 378 | '/site/dismissed-review-request', |
| 379 | array( |
| 380 | 'methods' => WP_REST_Server::EDITABLE, |
| 381 | 'callback' => __CLASS__ . '::manage_dismissed_backup_review_request', |
| 382 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 383 | 'args' => array( |
| 384 | 'option_name' => array( |
| 385 | 'required' => true, |
| 386 | 'type' => 'string', |
| 387 | // The two names `Jetpack_Options` recognises. Anything else |
| 388 | // falls through its allowlist to a `trigger_error()` and is |
| 389 | // stored nowhere, so an unlisted reason would dismiss |
| 390 | // nothing while answering as though it had — and on a site |
| 391 | // with `display_errors` on, the warning is printed ahead of |
| 392 | // the JSON and the response no longer parses. |
| 393 | 'enum' => array( 'restore', 'backups' ), |
| 394 | ), |
| 395 | 'should_dismiss' => array( |
| 396 | 'required' => true, |
| 397 | 'type' => 'boolean', |
| 398 | ), |
| 399 | ), |
| 400 | ) |
| 401 | ); |
| 402 | |
| 403 | // Get site size |
| 404 | register_rest_route( |
| 405 | 'jetpack/v4', |
| 406 | '/site/backup/size', |
| 407 | array( |
| 408 | 'methods' => WP_REST_Server::READABLE, |
| 409 | 'callback' => __CLASS__ . '::get_site_backup_size', |
| 410 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 411 | ) |
| 412 | ); |
| 413 | |
| 414 | // Get backup schedule time |
| 415 | register_rest_route( |
| 416 | 'jetpack/v4', |
| 417 | '/site/backup/schedule', |
| 418 | array( |
| 419 | 'methods' => WP_REST_Server::READABLE, |
| 420 | 'callback' => __CLASS__ . '::get_site_backup_schedule_time', |
| 421 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 422 | ) |
| 423 | ); |
| 424 | |
| 425 | // Get site policies |
| 426 | register_rest_route( |
| 427 | 'jetpack/v4', |
| 428 | '/site/backup/policies', |
| 429 | array( |
| 430 | 'methods' => WP_REST_Server::READABLE, |
| 431 | 'callback' => __CLASS__ . '::get_site_backup_policies', |
| 432 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 433 | ) |
| 434 | ); |
| 435 | |
| 436 | // Get site add-on offer |
| 437 | register_rest_route( |
| 438 | 'jetpack/v4', |
| 439 | '/site/backup/addon-offer', |
| 440 | array( |
| 441 | 'methods' => WP_REST_Server::READABLE, |
| 442 | 'callback' => __CLASS__ . '::get_site_backup_addon_offer', |
| 443 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 444 | 'args' => array( |
| 445 | 'storage_size' => array( |
| 446 | 'required' => true, |
| 447 | 'type' => 'numeric', |
| 448 | ), |
| 449 | 'storage_limit' => array( |
| 450 | 'required' => true, |
| 451 | 'type' => 'numeric', |
| 452 | ), |
| 453 | ), |
| 454 | ) |
| 455 | ); |
| 456 | |
| 457 | // Enqueue a new backup |
| 458 | register_rest_route( |
| 459 | 'jetpack/v4', |
| 460 | '/site/backup/enqueue', |
| 461 | array( |
| 462 | 'methods' => WP_REST_Server::CREATABLE, |
| 463 | 'callback' => __CLASS__ . '::enqueue_backup', |
| 464 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 465 | ) |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * The backup calls should only occur from a signed in admin user |
| 471 | * |
| 472 | * @access public |
| 473 | * @static |
| 474 | * |
| 475 | * @return true|WP_Error |
| 476 | */ |
| 477 | public static function backups_permissions_callback() { |
| 478 | return current_user_can( 'manage_options' ); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * The error a route answers with when its WordPress.com request did not come |
| 483 | * back with a 200. |
| 484 | * |
| 485 | * Returning `null` instead — which these routes used to do — is served as an |
| 486 | * HTTP 200 carrying a `null` body, so `apiFetch` resolves and nothing throws. |
| 487 | * A WordPress.com blip then reaches the dashboard as an empty success, which |
| 488 | * is how a paying customer ends up looking at the first-run screen. A |
| 489 | * WP_Error makes the REST layer answer with a status, so every caller's |
| 490 | * existing failure path runs. |
| 491 | * |
| 492 | * @param int $status The upstream response code, already cast to an int, or 0 |
| 493 | * when the request never reached WordPress.com. |
| 494 | * @return WP_Error |
| 495 | */ |
| 496 | private static function get_failed_fetch_error( $status = 0 ) { |
| 497 | return new WP_Error( |
| 498 | 'failed_to_fetch_data', |
| 499 | esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup-pkg' ), |
| 500 | array( |
| 501 | // A transport failure has no status at all, and `status_header( 0 )` |
| 502 | // emits an invalid status line — so anything falsy becomes a 500. |
| 503 | 'status' => $status ? $status : 500, |
| 504 | ) |
| 505 | ); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Get information about recent backups |
| 510 | * |
| 511 | * @access public |
| 512 | * @static |
| 513 | * |
| 514 | * @return \WP_REST_Response|WP_Error The recent backups, or a WP_Error if WordPress.com could not be reached. |
| 515 | */ |
| 516 | public static function get_recent_backups() { |
| 517 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 518 | |
| 519 | $response = Client::wpcom_json_api_request_as_blog( |
| 520 | '/sites/' . $blog_id . '/rewind/backups', |
| 521 | 'v2', |
| 522 | array(), |
| 523 | null, |
| 524 | 'wpcom' |
| 525 | ); |
| 526 | |
| 527 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 528 | |
| 529 | if ( 200 !== $response_code ) { |
| 530 | return self::get_failed_fetch_error( $response_code ); |
| 531 | } |
| 532 | |
| 533 | return rest_ensure_response( |
| 534 | json_decode( $response['body'], true ) |
| 535 | ); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Hits the wpcom api to check rewind status. |
| 540 | * |
| 541 | * @return Object|WP_Error |
| 542 | */ |
| 543 | private static function get_rewind_state_from_wpcom() { |
| 544 | static $status = null; |
| 545 | |
| 546 | if ( $status !== null ) { |
| 547 | return $status; |
| 548 | } |
| 549 | |
| 550 | $site_id = Jetpack_Options::get_option( 'id' ); |
| 551 | |
| 552 | $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/rewind', $site_id ) . '?force=wpcom', '2', array( 'timeout' => 2 ), null, 'wpcom' ); |
| 553 | |
| 554 | // Cast: `wp_remote_retrieve_response_code()` hands back whatever the |
| 555 | // transport put there, and a numeric-string `'200'` fails this |
| 556 | // strict comparison. The result is memoized in `$status` and read by |
| 557 | // `has_backup_plan()`, which answers false for a `WP_Error` — so a |
| 558 | // site that does have Backup is told, for the rest of the request, |
| 559 | // that it does not. That answer is acted on: it backs the |
| 560 | // `/has-backup-plan` route and the standalone-license upsell. |
| 561 | if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 562 | return new WP_Error( 'rewind_state_fetch_failed' ); |
| 563 | } |
| 564 | |
| 565 | $body = wp_remote_retrieve_body( $response ); |
| 566 | $status = json_decode( $body ); |
| 567 | return $status; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Checks whether the current plan (or purchases) of the site already supports the product |
| 572 | * |
| 573 | * @return boolean |
| 574 | */ |
| 575 | public static function has_backup_plan() { |
| 576 | $rewind_data = static::get_rewind_state_from_wpcom(); |
| 577 | if ( is_wp_error( $rewind_data ) ) { |
| 578 | return false; |
| 579 | } |
| 580 | return is_object( $rewind_data ) && isset( $rewind_data->state ) && 'unavailable' !== $rewind_data->state; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Get an array of backup/scan/anti-spam site capabilities |
| 585 | * |
| 586 | * @access public |
| 587 | * @static |
| 588 | * |
| 589 | * @return \WP_REST_Response|WP_Error The site capabilities, or a WP_Error if WordPress.com could not be reached. |
| 590 | */ |
| 591 | public static function get_backup_capabilities() { |
| 592 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 593 | |
| 594 | $response = Client::wpcom_json_api_request_as_user( |
| 595 | '/sites/' . $blog_id . '/rewind/capabilities', |
| 596 | 'v2', |
| 597 | array(), |
| 598 | null, |
| 599 | 'wpcom' |
| 600 | ); |
| 601 | |
| 602 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 603 | |
| 604 | if ( 200 !== $response_code ) { |
| 605 | return self::get_failed_fetch_error( $response_code ); |
| 606 | } |
| 607 | |
| 608 | return rest_ensure_response( |
| 609 | json_decode( $response['body'], true ) |
| 610 | ); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Get information about recent restores |
| 615 | * |
| 616 | * @access public |
| 617 | * @static |
| 618 | * |
| 619 | * @return \WP_REST_Response|WP_Error The recent restores, or a WP_Error if WordPress.com could not be reached. |
| 620 | */ |
| 621 | public static function get_recent_restores() { |
| 622 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 623 | $response = Client::wpcom_json_api_request_as_blog( |
| 624 | '/sites/' . $blog_id . '/rewind/restores', |
| 625 | 'v2', |
| 626 | array(), |
| 627 | null, |
| 628 | 'wpcom' |
| 629 | ); |
| 630 | |
| 631 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 632 | |
| 633 | if ( 200 !== $response_code ) { |
| 634 | return self::get_failed_fetch_error( $response_code ); |
| 635 | } |
| 636 | |
| 637 | return rest_ensure_response( |
| 638 | json_decode( $response['body'], true ) |
| 639 | ); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Query backup-completion events from the wpcom activity-log via the |
| 644 | * general `/sites/<id>/activity` endpoint with the action filter pinned |
| 645 | * to backup-completion event names. This endpoint paginates real-ly |
| 646 | * (Elasticsearch `from` offset under the hood) — the `/activity/rewindable` |
| 647 | * sibling looks like a more natural fit but hardcodes `page: 1, |
| 648 | * totalPages: 1` and ignores the `page` parameter. |
| 649 | * |
| 650 | * Auth: signs as user. The endpoint gates on the requesting WP user |
| 651 | * being an administrator of the blog (see |
| 652 | * sites-activity.php::readable_permission_check); blog-level tokens |
| 653 | * return 401. |
| 654 | * |
| 655 | * Returned shape (success): a W3C ActivityStreams envelope: |
| 656 | * { |
| 657 | * "@context": ..., "type": "OrderedCollection", "totalItems": int, |
| 658 | * "page": int, "totalPages": int, "itemsPerPage": int, |
| 659 | * "orderedItems": [ <event>, ... ] |
| 660 | * } |
| 661 | * Each event has at least `published`, `rewind_id`, `is_rewindable`, |
| 662 | * `name`, `status`, `summary`. |
| 663 | * |
| 664 | * @param array $args Query args passed through to wpcom. Supported keys: |
| 665 | * `after` (ISO 8601), `before` (ISO 8601), `on` (ISO 8601), |
| 666 | * `date_range`, `number` (max 1000), `page` (1-based), |
| 667 | * `sort_order` ('asc'|'desc'). Any `action` key is |
| 668 | * overridden with the curated backup-completion list. |
| 669 | * @return array|\WP_REST_Response|null |
| 670 | */ |
| 671 | public static function list_backup_events( array $args = array() ) { |
| 672 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 673 | |
| 674 | // Curated set of activity actions that represent "a backup completed". |
| 675 | // Mirrors `WPCOM_REST_API_V2_Endpoint_Site_Activity::$backup_action_names`. |
| 676 | // Pinned here (and overriding any caller-supplied `action`) so the |
| 677 | // helper is always scoped to backups regardless of what the caller passes. |
| 678 | $args['action'] = array( |
| 679 | 'backup_complete_full', |
| 680 | 'backup_complete_initial', |
| 681 | 'backup_only_complete_full', |
| 682 | 'backup_only_complete_initial', |
| 683 | 'rewind__backup_complete_full', |
| 684 | 'rewind__backup_complete_initial', |
| 685 | 'rewind__backup_only_complete_full', |
| 686 | 'rewind__backup_only_complete_initial', |
| 687 | ); |
| 688 | |
| 689 | $path = '/sites/' . (int) $blog_id . '/activity?' . http_build_query( $args ); |
| 690 | |
| 691 | $response = Client::wpcom_json_api_request_as_user( |
| 692 | $path, |
| 693 | 'v2', |
| 694 | array(), |
| 695 | null, |
| 696 | 'wpcom' |
| 697 | ); |
| 698 | |
| 699 | // Cast, as everywhere else this package reads a status. Uncast, a |
| 700 | // numeric-string `'200'` discards a good activity page and the |
| 701 | // `jetpack-backup/list-backup-events` ability reports that the site |
| 702 | // has completed no backups — `unwrap_response()` flattens this |
| 703 | // `null` into an empty list, which is a claim rather than an error. |
| 704 | if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 705 | return null; |
| 706 | } |
| 707 | |
| 708 | return rest_ensure_response( |
| 709 | json_decode( $response['body'], true ) |
| 710 | ); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Gets information about the currently promoted backup product. |
| 715 | * |
| 716 | * @return object|WP_Error The promoted product, or a WP_Error if it could not be read. |
| 717 | */ |
| 718 | public static function get_backup_promoted_product_info() { |
| 719 | $request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack'; |
| 720 | $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) ); |
| 721 | // Cast: the transport may report the status as a numeric string, which |
| 722 | // a strict comparison against 200 sends down the failure path. |
| 723 | $response_code = (int) wp_remote_retrieve_response_code( $wpcom_request ); |
| 724 | |
| 725 | if ( 200 !== $response_code ) { |
| 726 | return new WP_Error( |
| 727 | 'failed_to_fetch_data', |
| 728 | esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup-pkg' ), |
| 729 | array( |
| 730 | // A transport failure has no status at all; reporting 0 would |
| 731 | // leave the REST layer with nothing to serve. |
| 732 | 'status' => $response_code ? $response_code : 500, |
| 733 | 'request' => $wpcom_request, |
| 734 | ) |
| 735 | ); |
| 736 | } |
| 737 | |
| 738 | $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) ); |
| 739 | |
| 740 | // A 200 is not a promise that the promoted product is in the body. A |
| 741 | // truncated response decodes to null, and the slug is a constant here |
| 742 | // but a catalogue entry upstream — retiring it there leaves this a 200 |
| 743 | // with the key absent. Reading through either emits a PHP warning and |
| 744 | // yields null, which the route then serves as a 200 carrying `null`: |
| 745 | // indistinguishable, to a caller, from a priced answer it failed to |
| 746 | // read. Refusing says which of the two happened. |
| 747 | if ( ! is_object( $products ) || ! isset( $products->{ self::JETPACK_BACKUP_PROMOTED_PRODUCT } ) ) { |
| 748 | return new WP_Error( |
| 749 | 'promoted_product_unreadable', |
| 750 | esc_html__( 'Unable to read the promoted product information.', 'jetpack-backup-pkg' ), |
| 751 | array( 'status' => 500 ) |
| 752 | ); |
| 753 | } |
| 754 | |
| 755 | return $products->{ self::JETPACK_BACKUP_PROMOTED_PRODUCT }; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Check for user licenses. |
| 760 | * |
| 761 | * @param boolean $has_license If the user already has a license found. |
| 762 | * @param array $licenses List of unattached licenses belonging to the user. |
| 763 | * @param string $plugin_slug The plugin that initiated the flow. |
| 764 | * |
| 765 | * @return boolean |
| 766 | */ |
| 767 | public static function jetpack_check_user_licenses( $has_license, $licenses, $plugin_slug ) { |
| 768 | if ( $plugin_slug !== static::JETPACK_BACKUP_SLUG || $has_license ) { |
| 769 | return $has_license; |
| 770 | } |
| 771 | |
| 772 | $license_found = false; |
| 773 | |
| 774 | foreach ( $licenses as $license ) { |
| 775 | if ( in_array( $license->product_id, static::JETPACK_BACKUP_PRODUCT_IDS, true ) ) { |
| 776 | $license_found = true; |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Checking for existing backup plan is costly, so only check if there's an appropriate license. |
| 782 | return $license_found && ! static::has_backup_plan(); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Returns the result of `/upgrades` endpoint call. |
| 787 | * |
| 788 | * @return \WP_REST_Response|WP_Error The site purchases, or a WP_Error if WordPress.com could not be reached. |
| 789 | */ |
| 790 | public static function get_site_current_purchases() { |
| 791 | |
| 792 | $request = sprintf( '/upgrades?site=%d', Jetpack_Options::get_option( 'id' ) ); |
| 793 | $response = Client::wpcom_json_api_request_as_blog( $request, '1.2' ); |
| 794 | |
| 795 | // Bail if there was an error or malformed response. |
| 796 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
| 797 | return self::get_failed_fetch_error(); |
| 798 | } |
| 799 | |
| 800 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 801 | |
| 802 | if ( 200 !== $response_code ) { |
| 803 | return self::get_failed_fetch_error( $response_code ); |
| 804 | } |
| 805 | |
| 806 | return rest_ensure_response( |
| 807 | json_decode( $response['body'], true ) |
| 808 | ); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Set value of the dismissed_backup_review_request Jetack option. |
| 813 | * Get value if should_dismiss is false |
| 814 | * |
| 815 | * @access public |
| 816 | * @static |
| 817 | * @param array $request arguments should_dismiss and option_name. |
| 818 | * @return bool value of option if value is requested | updated or not if value is updated. |
| 819 | */ |
| 820 | public static function manage_dismissed_backup_review_request( $request ) { |
| 821 | |
| 822 | if ( ! $request['should_dismiss'] ) { |
| 823 | |
| 824 | return rest_ensure_response( |
| 825 | Jetpack_Options::get_option( 'dismissed_backup_review_' . $request['option_name'] ) |
| 826 | ); |
| 827 | } |
| 828 | |
| 829 | return Jetpack_Options::update_option( 'dismissed_backup_review_' . $request['option_name'], true ); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Get site storage size |
| 834 | * |
| 835 | * @return \WP_REST_Response|WP_Error The site storage size, or a WP_Error if WordPress.com could not be reached. |
| 836 | */ |
| 837 | public static function get_site_backup_size() { |
| 838 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 839 | |
| 840 | $response = Client::wpcom_json_api_request_as_user( |
| 841 | '/sites/' . $blog_id . '/rewind/size?force=wpcom', |
| 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_code ); |
| 852 | } |
| 853 | |
| 854 | return rest_ensure_response( |
| 855 | json_decode( $response['body'], true ) |
| 856 | ); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Get site policies from WPCOM. It includes the storage limit and activity log limit, if apply. |
| 861 | * |
| 862 | * @return \WP_REST_Response|WP_Error The site storage policies, or a WP_Error if WordPress.com could not be reached. |
| 863 | */ |
| 864 | public static function get_site_backup_policies() { |
| 865 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 866 | |
| 867 | $response = Client::wpcom_json_api_request_as_user( |
| 868 | '/sites/' . $blog_id . '/rewind/policies?force=wpcom', |
| 869 | 'v2', |
| 870 | array(), |
| 871 | null, |
| 872 | 'wpcom' |
| 873 | ); |
| 874 | |
| 875 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 876 | |
| 877 | if ( 200 !== $response_code ) { |
| 878 | return self::get_failed_fetch_error( $response_code ); |
| 879 | } |
| 880 | |
| 881 | return rest_ensure_response( |
| 882 | json_decode( $response['body'], true ) |
| 883 | ); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Get suggested storage addon based on storage usage |
| 888 | * |
| 889 | * @param int $bytes_used Storage used. |
| 890 | * @param int $bytes_available Storage limit. |
| 891 | * @return string Suggested addon storage slug |
| 892 | */ |
| 893 | public static function get_storage_addon_upsell_slug( $bytes_used, $bytes_available ) { |
| 894 | $bytes_10gb = 10 * 1024 * 1024 * 1024; // 10GB in bytes |
| 895 | $bytes_100gb = 100 * 1024 * 1024 * 1024; // 100GB in bytes |
| 896 | $bytes_1tb = 1024 * 1024 * 1024 * 1024; // 1TB in bytes |
| 897 | |
| 898 | $upsell_products = array( |
| 899 | $bytes_10gb => 'jetpack_backup_addon_storage_10gb_monthly', |
| 900 | $bytes_100gb => 'jetpack_backup_addon_storage_100gb_monthly', |
| 901 | $bytes_1tb => 'jetpack_backup_addon_storage_1tb_monthly', |
| 902 | ); |
| 903 | |
| 904 | // If usage has crossed over the storage limit, then dynamically calculate the upgrade option |
| 905 | if ( $bytes_used > $bytes_available ) { |
| 906 | $additional_bytes_used = $bytes_used - $bytes_available; |
| 907 | |
| 908 | // Add aditional 25% buffer |
| 909 | $additional_bytes_needed = $additional_bytes_used + $additional_bytes_used * 0.25; |
| 910 | |
| 911 | // Since 1TB is our max upgrade but the additional storage needed is greater than 1TB, then just return 1TB |
| 912 | if ( $additional_bytes_needed > $bytes_1tb ) { |
| 913 | return $upsell_products[ $bytes_1tb ]; |
| 914 | } |
| 915 | |
| 916 | $matched_bytes = $bytes_10gb; |
| 917 | foreach ( $upsell_products as $bytes => $product ) { |
| 918 | if ( $bytes > $additional_bytes_needed ) { |
| 919 | $matched_bytes = $bytes; |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return $upsell_products[ $matched_bytes ]; |
| 925 | } |
| 926 | |
| 927 | // For 1 TB we are going to offer 1 TB by default |
| 928 | if ( $bytes_1tb === $bytes_available ) { |
| 929 | return $upsell_products[ $bytes_1tb ]; |
| 930 | } |
| 931 | |
| 932 | // Otherwise, we are going to offer 10 GB |
| 933 | return $upsell_products[ $bytes_10gb ]; |
| 934 | } |
| 935 | |
| 936 | /** |
| 937 | * Get the best addon offer for this site, including pricing details |
| 938 | * |
| 939 | * @param \WP_REST_Request $request Object including storage usage. |
| 940 | * |
| 941 | * @return string|WP_Error A JSON object with the suggested storage addon details if the request was successful, |
| 942 | * or a WP_Error otherwise. |
| 943 | */ |
| 944 | public static function get_site_backup_addon_offer( $request ) { |
| 945 | $suggested_addon = self::get_storage_addon_upsell_slug( |
| 946 | $request['storage_size'], |
| 947 | $request['storage_limit'] |
| 948 | ); |
| 949 | |
| 950 | $addons_size_text_map = array( |
| 951 | 'jetpack_backup_addon_storage_10gb_monthly' => '10GB', |
| 952 | 'jetpack_backup_addon_storage_100gb_monthly' => '100GB', |
| 953 | 'jetpack_backup_addon_storage_1tb_monthly' => '1TB', |
| 954 | ); |
| 955 | |
| 956 | // Fetch addon storage price information |
| 957 | $pricing_info = Wpcom_Products::get_product_pricing( $suggested_addon ); |
| 958 | |
| 959 | // Response |
| 960 | $response = array( |
| 961 | 'slug' => $suggested_addon, |
| 962 | 'size_text' => $addons_size_text_map[ $suggested_addon ], |
| 963 | 'pricing' => $pricing_info, |
| 964 | ); |
| 965 | |
| 966 | return rest_ensure_response( $response ); |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Enqueue a new backup on demand |
| 971 | * |
| 972 | * @return \WP_REST_Response|WP_Error The enqueue result, or a WP_Error if WordPress.com could not be reached. |
| 973 | */ |
| 974 | public static function enqueue_backup() { |
| 975 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 976 | $endpoint = sprintf( '/sites/%d/rewind/backups/enqueue', $blog_id ); |
| 977 | |
| 978 | $response = Client::wpcom_json_api_request_as_user( |
| 979 | $endpoint, |
| 980 | 'v2', |
| 981 | array( |
| 982 | 'method' => 'POST', |
| 983 | ), |
| 984 | null, |
| 985 | 'wpcom' |
| 986 | ); |
| 987 | |
| 988 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 989 | |
| 990 | if ( 200 !== $response_code ) { |
| 991 | return self::get_failed_fetch_error( $response_code ); |
| 992 | } |
| 993 | |
| 994 | return rest_ensure_response( |
| 995 | json_decode( $response['body'], true ) |
| 996 | ); |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Get site backup schedule time |
| 1001 | * |
| 1002 | * @return \WP_REST_Response|WP_Error The backup schedule time, or a WP_Error if WordPress.com could not be reached. |
| 1003 | */ |
| 1004 | public static function get_site_backup_schedule_time() { |
| 1005 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 1006 | |
| 1007 | $response = Client::wpcom_json_api_request_as_user( |
| 1008 | '/sites/' . $blog_id . '/rewind/scheduled', |
| 1009 | 'v2', |
| 1010 | array(), |
| 1011 | null, |
| 1012 | 'wpcom' |
| 1013 | ); |
| 1014 | |
| 1015 | $response_code = (int) wp_remote_retrieve_response_code( $response ); |
| 1016 | |
| 1017 | if ( 200 !== $response_code ) { |
| 1018 | return self::get_failed_fetch_error( $response_code ); |
| 1019 | } |
| 1020 | |
| 1021 | return rest_ensure_response( |
| 1022 | json_decode( $response['body'], true ) |
| 1023 | ); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Removes plugin from the connection manager |
| 1028 | * If it's the last plugin using the connection, the site will be disconnected. |
| 1029 | * |
| 1030 | * @access public |
| 1031 | * @static |
| 1032 | */ |
| 1033 | public static function plugin_deactivation() { |
| 1034 | $manager = new Connection_Manager( 'jetpack-backup' ); |
| 1035 | $manager->remove_connection(); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Load wp-build when modernization is enabled on the Backup admin page. |
| 1040 | * |
| 1041 | * @return void |
| 1042 | */ |
| 1043 | public static function maybe_load_wp_build() { |
| 1044 | if ( ! self::is_modernized() || ! self::is_backup_admin_request() ) { |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | self::load_wp_build(); |
| 1049 | |
| 1050 | // wp-build registers standalone modules (e.g. the init module) on |
| 1051 | // wp_default_scripts, which has already fired by admin_menu. Register them |
| 1052 | // directly so the init module makes it into the import map. |
| 1053 | if ( function_exists( 'jetpack_backup_register_script_modules' ) ) { |
| 1054 | jetpack_backup_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes. |
| 1055 | } |
| 1056 | |
| 1057 | add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); |
| 1058 | add_action( 'admin_print_scripts', array( __CLASS__, 'render_connection_initial_state' ), 1 ); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Emit `window.JP_CONNECTION_INITIAL_STATE` inline on the modernized |
| 1063 | * Backup admin page. |
| 1064 | * |
| 1065 | * The modernized enqueue path short-circuits before the legacy |
| 1066 | * `Connection_Initial_State::render_script()` call, so without this |
| 1067 | * the React `<Gates>` component never sees the connection state and |
| 1068 | * sits on its loading skeleton forever. We emit the same JS payload |
| 1069 | * the legacy path emits, just outside of a registered script handle |
| 1070 | * (wp-build's handles aren't reliable here, and the global is |
| 1071 | * page-scoped — any tag setting it works). |
| 1072 | * |
| 1073 | * @return void |
| 1074 | */ |
| 1075 | public static function render_connection_initial_state() { |
| 1076 | echo '<script id="jetpack-backup-connection-initial-state">' |
| 1077 | . Connection_Initial_State::render() // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render() returns pre-escaped JSON. |
| 1078 | . '</script>'; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Load the wp-build entry file and register its polyfills. |
| 1083 | * |
| 1084 | * Only called on `?page=jetpack-backup` admin requests when the |
| 1085 | * modernization filter is enabled. Keeps wp-build off every other request. |
| 1086 | * |
| 1087 | * @return void |
| 1088 | */ |
| 1089 | private static function load_wp_build() { |
| 1090 | $build_index = dirname( __DIR__ ) . '/build/build.php'; |
| 1091 | |
| 1092 | if ( ! file_exists( $build_index ) ) { |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | require_once $build_index; |
| 1097 | |
| 1098 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register( |
| 1099 | 'jetpack-backup', |
| 1100 | array_merge( |
| 1101 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES, |
| 1102 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS |
| 1103 | ) |
| 1104 | ); |
| 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check. |
| 1109 | * |
| 1110 | * Wp-build's `<page>-wp-admin` enqueue callback enqueues only when the screen ID |
| 1111 | * matches the wp-build page slug (`jetpack-backup-dashboard`). Our WP-admin |
| 1112 | * menu slug stays `jetpack-backup`, so we mutate the screen object in place |
| 1113 | * to make the check pass without changing the user-facing URL. |
| 1114 | * |
| 1115 | * Hooked only when modernization is on AND we're on the Backup admin page, |
| 1116 | * so this never affects any other request. |
| 1117 | * |
| 1118 | * @param \WP_Screen|null $screen The current screen object (passed by WP). |
| 1119 | * @return void |
| 1120 | */ |
| 1121 | public static function alias_screen_id_for_wp_build( $screen ) { |
| 1122 | if ( ! is_object( $screen ) ) { |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | $screen->id = 'jetpack-backup-dashboard'; |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * Returns true when the wp-build modernization filter is enabled. |
| 1131 | * |
| 1132 | * @since 4.3.14 Changed from private to public; the REST bridges gate their route registration on it. |
| 1133 | * |
| 1134 | * @return bool |
| 1135 | */ |
| 1136 | public static function is_modernized() { |
| 1137 | return (bool) apply_filters( self::MODERNIZATION_FILTER, false ); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Returns true when the modernization filter is on AND the wp-build dashboard loaded. |
| 1142 | * |
| 1143 | * `build/` is gitignored, so the render function is absent in any unbuilt checkout |
| 1144 | * and in any release whose wp-build step failed. Every consumer of the modernized |
| 1145 | * surface has to agree on this, or the menu falls back to the legacy page while the |
| 1146 | * enqueue path skips the legacy script — an empty div with no JS. |
| 1147 | * |
| 1148 | * Only meaningful once `maybe_load_wp_build()` has run. It is hooked on `admin_menu` |
| 1149 | * at the same priority as `add_wp_admin_submenu()`, so registration order — not |
| 1150 | * priority — is what keeps it first. Do not reorder those two `add_action()` calls. |
| 1151 | * |
| 1152 | * @return bool |
| 1153 | */ |
| 1154 | private static function is_wp_build_dashboard_active() { |
| 1155 | return self::is_modernized() && function_exists( 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' ); |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * Returns true when the current request targets the Backup admin page. |
| 1160 | * |
| 1161 | * Used to scope wp-build loading to the one page that needs it. The |
| 1162 | * `$_GET['page']` value is populated by wp-admin/admin.php before any of |
| 1163 | * our hooks fire, so this check is reliable from `initialize()` onwards. |
| 1164 | * |
| 1165 | * @return bool |
| 1166 | */ |
| 1167 | private static function is_backup_admin_request() { |
| 1168 | if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
| 1172 | return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::JETPACK_BACKUP_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1173 | } |
| 1174 | } |