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