Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.38% |
77 / 443 |
|
6.06% |
2 / 33 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Backup | |
17.23% |
76 / 441 |
|
6.06% |
2 / 33 |
3708.63 | |
0.00% |
0 / 1 |
| initialize | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 | |||
| add_wp_admin_submenu | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| admin_init | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| maybe_upgrade_db | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| can_use_analytics | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_admin_scripts | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| plugin_settings_page | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_initial_state | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_routes | |
0.00% |
0 / 128 |
|
0.00% |
0 / 1 |
2 | |||
| backups_permissions_callback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_recent_backups | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
2.05 | |||
| get_rewind_state_from_wpcom | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| has_backup_plan | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| get_backup_capabilities | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
2.05 | |||
| get_recent_restores | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
2.05 | |||
| list_backup_events | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
2 | |||
| get_backup_promoted_product_info | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| jetpack_check_user_licenses | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
| get_site_current_purchases | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| manage_dismissed_backup_review_request | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_site_backup_size | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| get_site_backup_policies | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| get_site_backup_schedule_time | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| plugin_deactivation | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| maybe_load_wp_build | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| render_connection_initial_state | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| load_wp_build | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| alias_screen_id_for_wp_build | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| is_modernized | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_backup_admin_request | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | $is_modernized = self::is_modernized(); |
| 175 | $callback = $is_modernized && function_exists( 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' ) |
| 176 | ? 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' |
| 177 | : array( __CLASS__, 'plugin_settings_page' ); |
| 178 | |
| 179 | // Gate the "VaultPress Backup" relabel behind the modernization |
| 180 | // filter so the flag-off path stays byte-identical to trunk. |
| 181 | $page_title = $is_modernized ? 'Jetpack VaultPress Backup' : 'Jetpack Backup'; |
| 182 | $menu_title = $is_modernized ? '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_modernized() ) { |
| 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_modernized() ) { |
| 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 | // wp-build manages its own enqueue pipeline. The legacy script, |
| 254 | // initial state, and tracking are intentionally skipped for the |
| 255 | // wp-build dashboard. |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | Assets::register_script( |
| 260 | 'jetpack-backup', |
| 261 | '../build/index.js', |
| 262 | __FILE__, |
| 263 | array( |
| 264 | 'in_footer' => true, |
| 265 | 'textdomain' => 'jetpack-backup-pkg', |
| 266 | ) |
| 267 | ); |
| 268 | Assets::enqueue_script( 'jetpack-backup' ); |
| 269 | // Initial JS state including JP Connection data. |
| 270 | wp_add_inline_script( 'jetpack-backup', self::get_initial_state(), 'before' ); |
| 271 | Connection_Initial_State::render_script( 'jetpack-backup' ); |
| 272 | |
| 273 | // Load script for analytics. |
| 274 | if ( self::can_use_analytics() ) { |
| 275 | Tracking::register_tracks_functions_scripts( true ); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Main plugin settings page. |
| 281 | */ |
| 282 | public static function plugin_settings_page() { |
| 283 | ?> |
| 284 | <div id="jetpack-backup-root"></div> |
| 285 | <?php |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Return the rendered initial state JavaScript code. |
| 290 | * |
| 291 | * @return string |
| 292 | */ |
| 293 | private static function get_initial_state() { |
| 294 | return ( new Backup_Initial_State() )->render(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Register REST API |
| 299 | */ |
| 300 | public static function register_rest_routes() { |
| 301 | |
| 302 | // Get information on most recent 10 backups. |
| 303 | register_rest_route( |
| 304 | 'jetpack/v4', |
| 305 | '/backups', |
| 306 | array( |
| 307 | 'methods' => WP_REST_Server::READABLE, |
| 308 | 'callback' => __CLASS__ . '::get_recent_backups', |
| 309 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 310 | ) |
| 311 | ); |
| 312 | |
| 313 | // Get site backup/scan/anti-spam capabilities. |
| 314 | register_rest_route( |
| 315 | 'jetpack/v4', |
| 316 | '/backup-capabilities', |
| 317 | array( |
| 318 | 'methods' => WP_REST_Server::READABLE, |
| 319 | 'callback' => __CLASS__ . '::get_backup_capabilities', |
| 320 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 321 | ) |
| 322 | ); |
| 323 | |
| 324 | // Get whether the site has a backup plan |
| 325 | register_rest_route( |
| 326 | 'jetpack/v4', |
| 327 | '/has-backup-plan', |
| 328 | array( |
| 329 | 'methods' => WP_REST_Server::READABLE, |
| 330 | 'callback' => __CLASS__ . '::has_backup_plan', |
| 331 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 332 | ) |
| 333 | ); |
| 334 | |
| 335 | // Get site rewind data. |
| 336 | register_rest_route( |
| 337 | 'jetpack/v4', |
| 338 | '/restores', |
| 339 | array( |
| 340 | 'methods' => WP_REST_Server::READABLE, |
| 341 | 'callback' => __CLASS__ . '::get_recent_restores', |
| 342 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 343 | ) |
| 344 | ); |
| 345 | |
| 346 | // Get information on site products. |
| 347 | // Backup plugin version of /site/purchases from JP plugin. |
| 348 | // Revert once this route and MyPlan component are extracted to a common package. |
| 349 | register_rest_route( |
| 350 | 'jetpack/v4', |
| 351 | '/site/current-purchases', |
| 352 | array( |
| 353 | 'methods' => WP_REST_Server::READABLE, |
| 354 | 'callback' => __CLASS__ . '::get_site_current_purchases', |
| 355 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 356 | ) |
| 357 | ); |
| 358 | |
| 359 | // Get currently promoted product from the product's endpoint. |
| 360 | register_rest_route( |
| 361 | 'jetpack/v4', |
| 362 | '/backup-promoted-product-info', |
| 363 | array( |
| 364 | 'methods' => WP_REST_Server::READABLE, |
| 365 | 'callback' => __CLASS__ . '::get_backup_promoted_product_info', |
| 366 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 367 | ) |
| 368 | ); |
| 369 | |
| 370 | // Get and set value of dismissed_backup_review_request option |
| 371 | register_rest_route( |
| 372 | 'jetpack/v4', |
| 373 | '/site/dismissed-review-request', |
| 374 | array( |
| 375 | 'methods' => WP_REST_Server::EDITABLE, |
| 376 | 'callback' => __CLASS__ . '::manage_dismissed_backup_review_request', |
| 377 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 378 | 'args' => array( |
| 379 | 'option_name' => array( |
| 380 | 'required' => true, |
| 381 | 'type' => 'string', |
| 382 | ), |
| 383 | 'should_dismiss' => array( |
| 384 | 'required' => true, |
| 385 | 'type' => 'boolean', |
| 386 | ), |
| 387 | ), |
| 388 | ) |
| 389 | ); |
| 390 | |
| 391 | // Get site size |
| 392 | register_rest_route( |
| 393 | 'jetpack/v4', |
| 394 | '/site/backup/size', |
| 395 | array( |
| 396 | 'methods' => WP_REST_Server::READABLE, |
| 397 | 'callback' => __CLASS__ . '::get_site_backup_size', |
| 398 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 399 | ) |
| 400 | ); |
| 401 | |
| 402 | // Get backup schedule time |
| 403 | register_rest_route( |
| 404 | 'jetpack/v4', |
| 405 | '/site/backup/schedule', |
| 406 | array( |
| 407 | 'methods' => WP_REST_Server::READABLE, |
| 408 | 'callback' => __CLASS__ . '::get_site_backup_schedule_time', |
| 409 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 410 | ) |
| 411 | ); |
| 412 | |
| 413 | // Get site policies |
| 414 | register_rest_route( |
| 415 | 'jetpack/v4', |
| 416 | '/site/backup/policies', |
| 417 | array( |
| 418 | 'methods' => WP_REST_Server::READABLE, |
| 419 | 'callback' => __CLASS__ . '::get_site_backup_policies', |
| 420 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 421 | ) |
| 422 | ); |
| 423 | |
| 424 | // Get site add-on offer |
| 425 | register_rest_route( |
| 426 | 'jetpack/v4', |
| 427 | '/site/backup/addon-offer', |
| 428 | array( |
| 429 | 'methods' => WP_REST_Server::READABLE, |
| 430 | 'callback' => __CLASS__ . '::get_site_backup_addon_offer', |
| 431 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 432 | 'args' => array( |
| 433 | 'storage_size' => array( |
| 434 | 'required' => true, |
| 435 | 'type' => 'numeric', |
| 436 | ), |
| 437 | 'storage_limit' => array( |
| 438 | 'required' => true, |
| 439 | 'type' => 'numeric', |
| 440 | ), |
| 441 | ), |
| 442 | ) |
| 443 | ); |
| 444 | |
| 445 | // Enqueue a new backup |
| 446 | register_rest_route( |
| 447 | 'jetpack/v4', |
| 448 | '/site/backup/enqueue', |
| 449 | array( |
| 450 | 'methods' => WP_REST_Server::CREATABLE, |
| 451 | 'callback' => __CLASS__ . '::enqueue_backup', |
| 452 | 'permission_callback' => __CLASS__ . '::backups_permissions_callback', |
| 453 | ) |
| 454 | ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * The backup calls should only occur from a signed in admin user |
| 459 | * |
| 460 | * @access public |
| 461 | * @static |
| 462 | * |
| 463 | * @return true|WP_Error |
| 464 | */ |
| 465 | public static function backups_permissions_callback() { |
| 466 | return current_user_can( 'manage_options' ); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Get information about recent backups |
| 471 | * |
| 472 | * @access public |
| 473 | * @static |
| 474 | * |
| 475 | * @return array An array of recent backups |
| 476 | */ |
| 477 | public static function get_recent_backups() { |
| 478 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 479 | |
| 480 | $response = Client::wpcom_json_api_request_as_blog( |
| 481 | '/sites/' . $blog_id . '/rewind/backups', |
| 482 | 'v2', |
| 483 | array(), |
| 484 | null, |
| 485 | 'wpcom' |
| 486 | ); |
| 487 | |
| 488 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 489 | return null; |
| 490 | } |
| 491 | |
| 492 | return rest_ensure_response( |
| 493 | json_decode( $response['body'], true ) |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Hits the wpcom api to check rewind status. |
| 499 | * |
| 500 | * @return Object|WP_Error |
| 501 | */ |
| 502 | private static function get_rewind_state_from_wpcom() { |
| 503 | static $status = null; |
| 504 | |
| 505 | if ( $status !== null ) { |
| 506 | return $status; |
| 507 | } |
| 508 | |
| 509 | $site_id = Jetpack_Options::get_option( 'id' ); |
| 510 | |
| 511 | $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/rewind', $site_id ) . '?force=wpcom', '2', array( 'timeout' => 2 ), null, 'wpcom' ); |
| 512 | |
| 513 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 514 | return new WP_Error( 'rewind_state_fetch_failed' ); |
| 515 | } |
| 516 | |
| 517 | $body = wp_remote_retrieve_body( $response ); |
| 518 | $status = json_decode( $body ); |
| 519 | return $status; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Checks whether the current plan (or purchases) of the site already supports the product |
| 524 | * |
| 525 | * @return boolean |
| 526 | */ |
| 527 | public static function has_backup_plan() { |
| 528 | $rewind_data = static::get_rewind_state_from_wpcom(); |
| 529 | if ( is_wp_error( $rewind_data ) ) { |
| 530 | return false; |
| 531 | } |
| 532 | return is_object( $rewind_data ) && isset( $rewind_data->state ) && 'unavailable' !== $rewind_data->state; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Get an array of backup/scan/anti-spam site capabilities |
| 537 | * |
| 538 | * @access public |
| 539 | * @static |
| 540 | * |
| 541 | * @return array An array of capabilities |
| 542 | */ |
| 543 | public static function get_backup_capabilities() { |
| 544 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 545 | |
| 546 | $response = Client::wpcom_json_api_request_as_user( |
| 547 | '/sites/' . $blog_id . '/rewind/capabilities', |
| 548 | 'v2', |
| 549 | array(), |
| 550 | null, |
| 551 | 'wpcom' |
| 552 | ); |
| 553 | |
| 554 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 555 | return null; |
| 556 | } |
| 557 | |
| 558 | return rest_ensure_response( |
| 559 | json_decode( $response['body'], true ) |
| 560 | ); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Get information about recent restores |
| 565 | * |
| 566 | * @access public |
| 567 | * @static |
| 568 | * |
| 569 | * @return array An array of recent restores |
| 570 | */ |
| 571 | public static function get_recent_restores() { |
| 572 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 573 | $response = Client::wpcom_json_api_request_as_blog( |
| 574 | '/sites/' . $blog_id . '/rewind/restores', |
| 575 | 'v2', |
| 576 | array(), |
| 577 | null, |
| 578 | 'wpcom' |
| 579 | ); |
| 580 | |
| 581 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 582 | return null; |
| 583 | } |
| 584 | |
| 585 | return rest_ensure_response( |
| 586 | json_decode( $response['body'], true ) |
| 587 | ); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Query backup-completion events from the wpcom activity-log via the |
| 592 | * general `/sites/<id>/activity` endpoint with the action filter pinned |
| 593 | * to backup-completion event names. This endpoint paginates real-ly |
| 594 | * (Elasticsearch `from` offset under the hood) — the `/activity/rewindable` |
| 595 | * sibling looks like a more natural fit but hardcodes `page: 1, |
| 596 | * totalPages: 1` and ignores the `page` parameter. |
| 597 | * |
| 598 | * Auth: signs as user. The endpoint gates on the requesting WP user |
| 599 | * being an administrator of the blog (see |
| 600 | * sites-activity.php::readable_permission_check); blog-level tokens |
| 601 | * return 401. |
| 602 | * |
| 603 | * Returned shape (success): a W3C ActivityStreams envelope: |
| 604 | * { |
| 605 | * "@context": ..., "type": "OrderedCollection", "totalItems": int, |
| 606 | * "page": int, "totalPages": int, "itemsPerPage": int, |
| 607 | * "orderedItems": [ <event>, ... ] |
| 608 | * } |
| 609 | * Each event has at least `published`, `rewind_id`, `is_rewindable`, |
| 610 | * `name`, `status`, `summary`. |
| 611 | * |
| 612 | * @param array $args Query args passed through to wpcom. Supported keys: |
| 613 | * `after` (ISO 8601), `before` (ISO 8601), `on` (ISO 8601), |
| 614 | * `date_range`, `number` (max 1000), `page` (1-based), |
| 615 | * `sort_order` ('asc'|'desc'). Any `action` key is |
| 616 | * overridden with the curated backup-completion list. |
| 617 | * @return array|\WP_REST_Response|null |
| 618 | */ |
| 619 | public static function list_backup_events( array $args = array() ) { |
| 620 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 621 | |
| 622 | // Curated set of activity actions that represent "a backup completed". |
| 623 | // Mirrors `WPCOM_REST_API_V2_Endpoint_Site_Activity::$backup_action_names`. |
| 624 | // Pinned here (and overriding any caller-supplied `action`) so the |
| 625 | // helper is always scoped to backups regardless of what the caller passes. |
| 626 | $args['action'] = array( |
| 627 | 'backup_complete_full', |
| 628 | 'backup_complete_initial', |
| 629 | 'backup_only_complete_full', |
| 630 | 'backup_only_complete_initial', |
| 631 | 'rewind__backup_complete_full', |
| 632 | 'rewind__backup_complete_initial', |
| 633 | 'rewind__backup_only_complete_full', |
| 634 | 'rewind__backup_only_complete_initial', |
| 635 | ); |
| 636 | |
| 637 | $path = '/sites/' . (int) $blog_id . '/activity?' . http_build_query( $args ); |
| 638 | |
| 639 | $response = Client::wpcom_json_api_request_as_user( |
| 640 | $path, |
| 641 | 'v2', |
| 642 | array(), |
| 643 | null, |
| 644 | 'wpcom' |
| 645 | ); |
| 646 | |
| 647 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 648 | return null; |
| 649 | } |
| 650 | |
| 651 | return rest_ensure_response( |
| 652 | json_decode( $response['body'], true ) |
| 653 | ); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Gets information about the currently promoted backup product. |
| 658 | * |
| 659 | * @return string|WP_Error A JSON object of the current backup product being promoted if the request was successful, or a WP_Error otherwise. |
| 660 | */ |
| 661 | public static function get_backup_promoted_product_info() { |
| 662 | $request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack'; |
| 663 | $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) ); |
| 664 | $response_code = wp_remote_retrieve_response_code( $wpcom_request ); |
| 665 | if ( 200 === $response_code ) { |
| 666 | $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) ); |
| 667 | return $products->{self::JETPACK_BACKUP_PROMOTED_PRODUCT}; |
| 668 | } else { |
| 669 | // Something went wrong so we'll just return the response without caching. |
| 670 | return new WP_Error( |
| 671 | 'failed_to_fetch_data', |
| 672 | esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup-pkg' ), |
| 673 | array( |
| 674 | 'status' => $response_code, |
| 675 | 'request' => $wpcom_request, |
| 676 | ) |
| 677 | ); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Check for user licenses. |
| 683 | * |
| 684 | * @param boolean $has_license If the user already has a license found. |
| 685 | * @param array $licenses List of unattached licenses belonging to the user. |
| 686 | * @param string $plugin_slug The plugin that initiated the flow. |
| 687 | * |
| 688 | * @return boolean |
| 689 | */ |
| 690 | public static function jetpack_check_user_licenses( $has_license, $licenses, $plugin_slug ) { |
| 691 | if ( $plugin_slug !== static::JETPACK_BACKUP_SLUG || $has_license ) { |
| 692 | return $has_license; |
| 693 | } |
| 694 | |
| 695 | $license_found = false; |
| 696 | |
| 697 | foreach ( $licenses as $license ) { |
| 698 | if ( in_array( $license->product_id, static::JETPACK_BACKUP_PRODUCT_IDS, true ) ) { |
| 699 | $license_found = true; |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | // Checking for existing backup plan is costly, so only check if there's an appropriate license. |
| 705 | return $license_found && ! static::has_backup_plan(); |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Returns the result of `/upgrades` endpoint call. |
| 710 | * |
| 711 | * @return array of site purchases. |
| 712 | */ |
| 713 | public static function get_site_current_purchases() { |
| 714 | |
| 715 | $request = sprintf( '/upgrades?site=%d', Jetpack_Options::get_option( 'id' ) ); |
| 716 | $response = Client::wpcom_json_api_request_as_blog( $request, '1.2' ); |
| 717 | |
| 718 | // Bail if there was an error or malformed response. |
| 719 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
| 720 | return self::get_failed_fetch_error(); |
| 721 | } |
| 722 | |
| 723 | if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 724 | return self::get_failed_fetch_error(); |
| 725 | } |
| 726 | |
| 727 | return rest_ensure_response( |
| 728 | json_decode( $response['body'], true ) |
| 729 | ); |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Set value of the dismissed_backup_review_request Jetack option. |
| 734 | * Get value if should_dismiss is false |
| 735 | * |
| 736 | * @access public |
| 737 | * @static |
| 738 | * @param array $request arguments should_dismiss and option_name. |
| 739 | * @return bool value of option if value is requested | updated or not if value is updated. |
| 740 | */ |
| 741 | public static function manage_dismissed_backup_review_request( $request ) { |
| 742 | |
| 743 | if ( ! $request['should_dismiss'] ) { |
| 744 | |
| 745 | return rest_ensure_response( |
| 746 | Jetpack_Options::get_option( 'dismissed_backup_review_' . $request['option_name'] ) |
| 747 | ); |
| 748 | } |
| 749 | |
| 750 | return Jetpack_Options::update_option( 'dismissed_backup_review_' . $request['option_name'], true ); |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Get site storage size |
| 755 | * |
| 756 | * @return string|WP_Error A JSON object with the site storage size if the request was successful, or a WP_Error otherwise. |
| 757 | */ |
| 758 | public static function get_site_backup_size() { |
| 759 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 760 | |
| 761 | $response = Client::wpcom_json_api_request_as_user( |
| 762 | '/sites/' . $blog_id . '/rewind/size?force=wpcom', |
| 763 | 'v2', |
| 764 | array(), |
| 765 | null, |
| 766 | 'wpcom' |
| 767 | ); |
| 768 | |
| 769 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 770 | return null; |
| 771 | } |
| 772 | |
| 773 | return rest_ensure_response( |
| 774 | json_decode( $response['body'], true ) |
| 775 | ); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Get site policies from WPCOM. It includes the storage limit and activity log limit, if apply. |
| 780 | * |
| 781 | * @return string|WP_Error A JSON object with the site storage policies if the request was successful, |
| 782 | * or a WP_Error otherwise. |
| 783 | */ |
| 784 | public static function get_site_backup_policies() { |
| 785 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 786 | |
| 787 | $response = Client::wpcom_json_api_request_as_user( |
| 788 | '/sites/' . $blog_id . '/rewind/policies?force=wpcom', |
| 789 | 'v2', |
| 790 | array(), |
| 791 | null, |
| 792 | 'wpcom' |
| 793 | ); |
| 794 | |
| 795 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 796 | return null; |
| 797 | } |
| 798 | |
| 799 | return rest_ensure_response( |
| 800 | json_decode( $response['body'], true ) |
| 801 | ); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Get suggested storage addon based on storage usage |
| 806 | * |
| 807 | * @param int $bytes_used Storage used. |
| 808 | * @param int $bytes_available Storage limit. |
| 809 | * @return string Suggested addon storage slug |
| 810 | */ |
| 811 | public static function get_storage_addon_upsell_slug( $bytes_used, $bytes_available ) { |
| 812 | $bytes_10gb = 10 * 1024 * 1024 * 1024; // 10GB in bytes |
| 813 | $bytes_100gb = 100 * 1024 * 1024 * 1024; // 100GB in bytes |
| 814 | $bytes_1tb = 1024 * 1024 * 1024 * 1024; // 1TB in bytes |
| 815 | |
| 816 | $upsell_products = array( |
| 817 | $bytes_10gb => 'jetpack_backup_addon_storage_10gb_monthly', |
| 818 | $bytes_100gb => 'jetpack_backup_addon_storage_100gb_monthly', |
| 819 | $bytes_1tb => 'jetpack_backup_addon_storage_1tb_monthly', |
| 820 | ); |
| 821 | |
| 822 | // If usage has crossed over the storage limit, then dynamically calculate the upgrade option |
| 823 | if ( $bytes_used > $bytes_available ) { |
| 824 | $additional_bytes_used = $bytes_used - $bytes_available; |
| 825 | |
| 826 | // Add aditional 25% buffer |
| 827 | $additional_bytes_needed = $additional_bytes_used + $additional_bytes_used * 0.25; |
| 828 | |
| 829 | // Since 1TB is our max upgrade but the additional storage needed is greater than 1TB, then just return 1TB |
| 830 | if ( $additional_bytes_needed > $bytes_1tb ) { |
| 831 | return $upsell_products[ $bytes_1tb ]; |
| 832 | } |
| 833 | |
| 834 | $matched_bytes = $bytes_10gb; |
| 835 | foreach ( $upsell_products as $bytes => $product ) { |
| 836 | if ( $bytes > $additional_bytes_needed ) { |
| 837 | $matched_bytes = $bytes; |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return $upsell_products[ $matched_bytes ]; |
| 843 | } |
| 844 | |
| 845 | // For 1 TB we are going to offer 1 TB by default |
| 846 | if ( $bytes_1tb === $bytes_available ) { |
| 847 | return $upsell_products[ $bytes_1tb ]; |
| 848 | } |
| 849 | |
| 850 | // Otherwise, we are going to offer 10 GB |
| 851 | return $upsell_products[ $bytes_10gb ]; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Get the best addon offer for this site, including pricing details |
| 856 | * |
| 857 | * @param \WP_REST_Request $request Object including storage usage. |
| 858 | * |
| 859 | * @return string|WP_Error A JSON object with the suggested storage addon details if the request was successful, |
| 860 | * or a WP_Error otherwise. |
| 861 | */ |
| 862 | public static function get_site_backup_addon_offer( $request ) { |
| 863 | $suggested_addon = self::get_storage_addon_upsell_slug( |
| 864 | $request['storage_size'], |
| 865 | $request['storage_limit'] |
| 866 | ); |
| 867 | |
| 868 | $addons_size_text_map = array( |
| 869 | 'jetpack_backup_addon_storage_10gb_monthly' => '10GB', |
| 870 | 'jetpack_backup_addon_storage_100gb_monthly' => '100GB', |
| 871 | 'jetpack_backup_addon_storage_1tb_monthly' => '1TB', |
| 872 | ); |
| 873 | |
| 874 | // Fetch addon storage price information |
| 875 | $pricing_info = Wpcom_Products::get_product_pricing( $suggested_addon ); |
| 876 | |
| 877 | // Response |
| 878 | $response = array( |
| 879 | 'slug' => $suggested_addon, |
| 880 | 'size_text' => $addons_size_text_map[ $suggested_addon ], |
| 881 | 'pricing' => $pricing_info, |
| 882 | ); |
| 883 | |
| 884 | return rest_ensure_response( $response ); |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Enqueue a new backup on demand |
| 889 | * |
| 890 | * @return string|WP_Error A JSON object with `success` if the request was successful, |
| 891 | * or a WP_Error otherwise. |
| 892 | */ |
| 893 | public static function enqueue_backup() { |
| 894 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 895 | $endpoint = sprintf( '/sites/%d/rewind/backups/enqueue', $blog_id ); |
| 896 | |
| 897 | $response = Client::wpcom_json_api_request_as_user( |
| 898 | $endpoint, |
| 899 | 'v2', |
| 900 | array( |
| 901 | 'method' => 'POST', |
| 902 | ), |
| 903 | null, |
| 904 | 'wpcom' |
| 905 | ); |
| 906 | |
| 907 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 908 | return null; |
| 909 | } |
| 910 | |
| 911 | return rest_ensure_response( |
| 912 | json_decode( $response['body'], true ) |
| 913 | ); |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * Get site backup schedule time |
| 918 | * |
| 919 | * @return string|WP_Error A JSON object with the backup schedule time if the request was successful, or a WP_Error otherwise. |
| 920 | */ |
| 921 | public static function get_site_backup_schedule_time() { |
| 922 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 923 | |
| 924 | $response = Client::wpcom_json_api_request_as_user( |
| 925 | '/sites/' . $blog_id . '/rewind/scheduled', |
| 926 | 'v2', |
| 927 | array(), |
| 928 | null, |
| 929 | 'wpcom' |
| 930 | ); |
| 931 | |
| 932 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 933 | return null; |
| 934 | } |
| 935 | |
| 936 | return rest_ensure_response( |
| 937 | json_decode( $response['body'], true ) |
| 938 | ); |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Removes plugin from the connection manager |
| 943 | * If it's the last plugin using the connection, the site will be disconnected. |
| 944 | * |
| 945 | * @access public |
| 946 | * @static |
| 947 | */ |
| 948 | public static function plugin_deactivation() { |
| 949 | $manager = new Connection_Manager( 'jetpack-backup' ); |
| 950 | $manager->remove_connection(); |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * Load wp-build when modernization is enabled on the Backup admin page. |
| 955 | * |
| 956 | * @return void |
| 957 | */ |
| 958 | public static function maybe_load_wp_build() { |
| 959 | if ( ! self::is_modernized() || ! self::is_backup_admin_request() ) { |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | self::load_wp_build(); |
| 964 | |
| 965 | // wp-build registers standalone modules (e.g. the init module) on |
| 966 | // wp_default_scripts, which has already fired by admin_menu. Register them |
| 967 | // directly so the init module makes it into the import map. |
| 968 | if ( function_exists( 'jetpack_backup_register_script_modules' ) ) { |
| 969 | jetpack_backup_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes. |
| 970 | } |
| 971 | |
| 972 | add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); |
| 973 | add_action( 'admin_print_scripts', array( __CLASS__, 'render_connection_initial_state' ), 1 ); |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * Emit `window.JP_CONNECTION_INITIAL_STATE` inline on the modernized |
| 978 | * Backup admin page. |
| 979 | * |
| 980 | * The modernized enqueue path short-circuits before the legacy |
| 981 | * `Connection_Initial_State::render_script()` call, so without this |
| 982 | * the React `<Gates>` component never sees the connection state and |
| 983 | * sits on its loading skeleton forever. We emit the same JS payload |
| 984 | * the legacy path emits, just outside of a registered script handle |
| 985 | * (wp-build's handles aren't reliable here, and the global is |
| 986 | * page-scoped — any tag setting it works). |
| 987 | * |
| 988 | * @return void |
| 989 | */ |
| 990 | public static function render_connection_initial_state() { |
| 991 | echo '<script id="jetpack-backup-connection-initial-state">' |
| 992 | . Connection_Initial_State::render() // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render() returns pre-escaped JSON. |
| 993 | . '</script>'; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Load the wp-build entry file and register its polyfills. |
| 998 | * |
| 999 | * Only called on `?page=jetpack-backup` admin requests when the |
| 1000 | * modernization filter is enabled. Keeps wp-build off every other request. |
| 1001 | * |
| 1002 | * @return void |
| 1003 | */ |
| 1004 | private static function load_wp_build() { |
| 1005 | $build_index = dirname( __DIR__ ) . '/build/build.php'; |
| 1006 | |
| 1007 | if ( ! file_exists( $build_index ) ) { |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | require_once $build_index; |
| 1012 | |
| 1013 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register( |
| 1014 | 'jetpack-backup', |
| 1015 | array_merge( |
| 1016 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES, |
| 1017 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS |
| 1018 | ) |
| 1019 | ); |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check. |
| 1024 | * |
| 1025 | * Wp-build's `<page>-wp-admin` enqueue callback enqueues only when the screen ID |
| 1026 | * matches the wp-build page slug (`jetpack-backup-dashboard`). Our WP-admin |
| 1027 | * menu slug stays `jetpack-backup`, so we mutate the screen object in place |
| 1028 | * to make the check pass without changing the user-facing URL. |
| 1029 | * |
| 1030 | * Hooked only when modernization is on AND we're on the Backup admin page, |
| 1031 | * so this never affects any other request. |
| 1032 | * |
| 1033 | * @param \WP_Screen|null $screen The current screen object (passed by WP). |
| 1034 | * @return void |
| 1035 | */ |
| 1036 | public static function alias_screen_id_for_wp_build( $screen ) { |
| 1037 | if ( ! is_object( $screen ) ) { |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | $screen->id = 'jetpack-backup-dashboard'; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Returns true when the wp-build modernization filter is enabled. |
| 1046 | * |
| 1047 | * @since 4.3.14 Changed from private to public; the REST bridges gate their route registration on it. |
| 1048 | * |
| 1049 | * @return bool |
| 1050 | */ |
| 1051 | public static function is_modernized() { |
| 1052 | return (bool) apply_filters( self::MODERNIZATION_FILTER, false ); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Returns true when the current request targets the Backup admin page. |
| 1057 | * |
| 1058 | * Used to scope wp-build loading to the one page that needs it. The |
| 1059 | * `$_GET['page']` value is populated by wp-admin/admin.php before any of |
| 1060 | * our hooks fire, so this check is reliable from `initialize()` onwards. |
| 1061 | * |
| 1062 | * @return bool |
| 1063 | */ |
| 1064 | private static function is_backup_admin_request() { |
| 1065 | if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::JETPACK_BACKUP_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1070 | } |
| 1071 | } |