← All changes
|
jetpack_vendor/automattic/jetpack-backup/src/class-jetpack-backup.php
+75
-29
16.3-a.3
→
16.3
View file →
| @@ -23,8 +23,9 @@ | ||
| 23 | 23 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 24 | 24 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 25 | 25 | use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication; |
| 26 | 26 | use Automattic\Jetpack\Constants; |
| 27 | +use Automattic\Jetpack\JITMS\JITM; | |
| 27 | 28 | use Automattic\Jetpack\My_Jetpack\Wpcom_Products; |
| 28 | 29 | use Automattic\Jetpack\Status; |
| 29 | 30 | use Automattic\Jetpack\Terms_Of_Service; |
| 30 | 31 | use Automattic\Jetpack\Tracking; |
| @@ -154,17 +155,29 @@ | ||
| 154 | 155 | */ |
| 155 | 156 | private static $wp_build_original_screen_id = null; |
| 156 | 157 | |
| 157 | 158 | /** |
| 159 | + * Initialization options. | |
| 160 | + * | |
| 161 | + * @var array | |
| 162 | + */ | |
| 163 | + const DEFAULT_INIT_OPTIONS = array( | |
| 164 | + // A host that already ensured a connection under its own slug must not have it | |
| 165 | + // re-ensured here as `jetpack-backup`, which would rename the site's connection. | |
| 166 | + 'manage_connection' => true, | |
| 167 | + ); | |
| 168 | + | |
| 169 | + /** | |
| 158 | 170 | * Constructor. |
| 171 | + * | |
| 172 | + * @param array $options Overrides for self::DEFAULT_INIT_OPTIONS. | |
| 159 | 173 | */ |
| 160 | - public static function initialize() { | |
| 174 | + public static function initialize( array $options = array() ) { | |
| 161 | 175 | if ( did_action( 'jetpack_backup_initialized' ) ) { |
| 162 | 176 | return; |
| 163 | 177 | } |
| 164 | 178 | |
| 165 | - // Set up the REST authentication hooks. | |
| 166 | - Connection_Rest_Authentication::init(); | |
| 179 | + $options = array_merge( self::DEFAULT_INIT_OPTIONS, $options ); | |
| 167 | 180 | |
| 168 | 181 | add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) ); |
| 169 | 182 | add_action( 'rest_api_init', array( \Automattic\Jetpack\Backup\V0005\REST\Rest_Controller::class, 'register_routes' ) ); |
| 170 | 183 | |
| @@ -170,8 +183,32 @@ | ||
| 170 | 183 | |
| 171 | 184 | add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 ); |
| 172 | 185 | 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. |
| 173 | 186 | |
| 187 | + if ( $options['manage_connection'] ) { | |
| 188 | + self::init_standalone_connection(); | |
| 189 | + } | |
| 190 | + | |
| 191 | + // Jetpack Backup abilities are registered from `actions.php` at package | |
| 192 | + // autoload time so the surface is available in every consumer that | |
| 193 | + // loads this package (both the standalone Backup plugin and the | |
| 194 | + // Jetpack plugin), not only when `Jetpack_Backup::initialize()` runs. | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Runs right after the Jetpack Backup package is initialized. | |
| 198 | + * | |
| 199 | + * @since 1.3.0 | |
| 200 | + */ | |
| 201 | + do_action( 'jetpack_backup_initialized' ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Set up the connection, sync and identity-crisis packages under the standalone plugin's slug. | |
| 206 | + */ | |
| 207 | + private static function init_standalone_connection() { | |
| 208 | + // Set up the REST authentication hooks. | |
| 209 | + Connection_Rest_Authentication::init(); | |
| 210 | + | |
| 174 | 211 | // Init Jetpack packages. |
| 175 | 212 | add_action( |
| 176 | 213 | 'plugins_loaded', |
| 177 | 214 | function () { |
| @@ -196,20 +233,8 @@ | ||
| 196 | 233 | |
| 197 | 234 | add_action( 'plugins_loaded', array( __CLASS__, 'maybe_upgrade_db' ), 20 ); |
| 198 | 235 | |
| 199 | 236 | add_filter( 'jetpack_connection_user_has_license', array( __CLASS__, 'jetpack_check_user_licenses' ), 10, 3 ); |
| 200 | - | |
| 201 | - // Jetpack Backup abilities are registered from `actions.php` at package | |
| 202 | - // autoload time so the surface is available in every consumer that | |
| 203 | - // loads this package (both the standalone Backup plugin and the | |
| 204 | - // Jetpack plugin), not only when `Jetpack_Backup::initialize()` runs. | |
| 205 | - | |
| 206 | - /** | |
| 207 | - * Runs right after the Jetpack Backup package is initialized. | |
| 208 | - * | |
| 209 | - * @since 1.3.0 | |
| 210 | - */ | |
| 211 | - do_action( 'jetpack_backup_initialized' ); | |
| 212 | 237 | } |
| 213 | 238 | |
| 214 | 239 | /** |
| 215 | 240 | * The page to be added to submenu |
| @@ -219,12 +244,12 @@ | ||
| 219 | 244 | $callback = $wp_build_active |
| 220 | 245 | ? 'jetpack_backup_jetpack_backup_dashboard_wp_admin_render_page' |
| 221 | 246 | : array( __CLASS__, 'plugin_settings_page' ); |
| 222 | 247 | |
| 223 | - // The relabel rides the modernized dashboard rather than the filter alone, | |
| 248 | + // The page title's relabel rides the modernized dashboard rather than the filter alone, | |
| 224 | 249 | // so a fallback to the legacy page also falls back to the legacy title. |
| 225 | 250 | $page_title = $wp_build_active ? 'Jetpack VaultPress Backup' : 'Jetpack Backup'; |
| 226 | - $menu_title = $wp_build_active ? 'VaultPress Backup' : 'Backup'; // Product name, do not translate. | |
| 251 | + $menu_title = 'Backup'; // Product name, do not translate. | |
| 227 | 252 | |
| 228 | 253 | $page_suffix = Admin_Menu::add_menu( |
| 229 | 254 | $page_title, |
| 230 | 255 | $menu_title, |
| @@ -249,15 +274,16 @@ | ||
| 249 | 274 | public static function admin_init() { |
| 250 | 275 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) ); |
| 251 | 276 | |
| 252 | 277 | if ( self::is_wp_build_dashboard_active() ) { |
| 253 | - // The modernized Backup overview is a focused, full-screen product | |
| 254 | - // surface. Suppress JITMs and other core/plugin admin notices so they | |
| 255 | - // don't reflow on top of the dual-pane layout. Mirrors how Jetpack | |
| 256 | - // Forms handles its dashboard page | |
| 257 | - // (`plugins/forms/src/dashboard/class-dashboard.php`). | |
| 258 | - remove_all_actions( 'admin_notices' ); | |
| 259 | - remove_all_actions( 'all_admin_notices' ); | |
| 278 | + // Notices reflow the dual-pane layout, so clear them but keep our own. | |
| 279 | + // An older jetpack-jitm may predate the helper; the fallback costs the JITM. | |
| 280 | + if ( method_exists( JITM::class, 'suppress_foreign_admin_notices' ) ) { | |
| 281 | + JITM::suppress_foreign_admin_notices(); | |
| 282 | + } else { | |
| 283 | + remove_all_actions( 'admin_notices' ); | |
| 284 | + remove_all_actions( 'all_admin_notices' ); | |
| 285 | + } | |
| 260 | 286 | } |
| 261 | 287 | } |
| 262 | 288 | |
| 263 | 289 | /** |
| @@ -1137,13 +1163,9 @@ | ||
| 1137 | 1163 | if ( ! self::is_modernized() || ! self::is_backup_admin_request() ) { |
| 1138 | 1164 | return; |
| 1139 | 1165 | } |
| 1140 | 1166 | |
| 1141 | - // Hooked either side of load_wp_build(), so the alias holds only for the generated | |
| 1142 | - // enqueue check it registers at the same priority. | |
| 1143 | - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); | |
| 1144 | - self::load_wp_build(); | |
| 1145 | - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) ); | |
| 1167 | + self::load_wp_build_with_screen_alias(); | |
| 1146 | 1168 | |
| 1147 | 1169 | // wp-build registers standalone modules (e.g. the init module) on |
| 1148 | 1170 | // wp_default_scripts, which has already fired by admin_menu. Register them |
| 1149 | 1171 | // directly so the init module makes it into the import map. |
| @@ -1197,8 +1219,32 @@ | ||
| 1197 | 1219 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES, |
| 1198 | 1220 | \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS |
| 1199 | 1221 | ) |
| 1200 | 1222 | ); |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + /** | |
| 1226 | + * Load wp-build with the screen ID aliased across its generated enqueue check. | |
| 1227 | + * | |
| 1228 | + * @see WP_Build_Screen_Id::load_with_alias() | |
| 1229 | + * @return void | |
| 1230 | + */ | |
| 1231 | + private static function load_wp_build_with_screen_alias() { | |
| 1232 | + // Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias(). | |
| 1233 | + if ( method_exists( \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::class, 'load_with_alias' ) ) { | |
| 1234 | + \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::load_with_alias( | |
| 1235 | + array( __CLASS__, 'alias_screen_id_for_wp_build' ), | |
| 1236 | + array( __CLASS__, 'restore_screen_id_after_wp_build' ), | |
| 1237 | + function () { | |
| 1238 | + self::load_wp_build(); | |
| 1239 | + } | |
| 1240 | + ); | |
| 1241 | + return; | |
| 1242 | + } | |
| 1243 | + | |
| 1244 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); | |
| 1245 | + self::load_wp_build(); | |
| 1246 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) ); | |
| 1201 | 1247 | } |
| 1202 | 1248 | |
| 1203 | 1249 | /** |
| 1204 | 1250 | * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check. |