Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.03% |
145 / 151 |
|
77.78% |
14 / 18 |
CRAP | |
0.00% |
0 / 1 |
| Admin | |
96.03% |
145 / 151 |
|
77.78% |
14 / 18 |
45 | |
0.00% |
0 / 1 |
| init | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| add_admin_shell_member | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
| load_as_admin_shell_guest | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| handle_admin_menu | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
3 | |||
| is_modern_dashboard | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_modernization_enabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| maybe_load_wp_build | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
5 | |||
| register_wp_build | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| build_file | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dashboard_build_is_available | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| alias_screen_id_for_wp_build | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| restore_screen_id_after_wp_build | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| admin_init | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| add_enqueue_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_scripts | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
6 | |||
| plugin_page_settings_link | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| localize_api_settings | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| render_settings | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The admin-specific functionality of the plugin. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package automattic/jetpack-boost |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack_Boost\Admin; |
| 10 | |
| 11 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 12 | use Automattic\Jetpack\Admin_UI\Admin_Shell; |
| 13 | use Automattic\Jetpack\Assets; |
| 14 | use Automattic\Jetpack\Boost_Speed_Score\Speed_Score; |
| 15 | use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer; |
| 16 | use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; |
| 17 | use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync; |
| 18 | use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry; |
| 19 | use Automattic\Jetpack_Boost\Lib\Analytics; |
| 20 | use Automattic\Jetpack_Boost\Lib\Debug; |
| 21 | use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector; |
| 22 | use Automattic\Jetpack_Boost\Lib\Premium_Features; |
| 23 | use Automattic\Jetpack_Boost\Modules\Modules_Setup; |
| 24 | |
| 25 | class Admin { |
| 26 | /** |
| 27 | * Menu slug. |
| 28 | */ |
| 29 | const MENU_SLUG = 'jetpack-boost'; |
| 30 | |
| 31 | /** |
| 32 | * Filter controlling the modern dashboard; return false to restore the legacy dashboard. |
| 33 | */ |
| 34 | const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_boost'; |
| 35 | |
| 36 | /** |
| 37 | * Whether this request loaded the modern dashboard. |
| 38 | * |
| 39 | * @var bool |
| 40 | */ |
| 41 | private $modern_dashboard_loaded = false; |
| 42 | |
| 43 | /** |
| 44 | * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored. |
| 45 | * |
| 46 | * @var string|null |
| 47 | */ |
| 48 | private $wp_build_original_screen_id = null; |
| 49 | |
| 50 | public function init( Modules_Setup $modules ) { |
| 51 | Environment_Change_Detector::init(); |
| 52 | |
| 53 | // Initiate speed scores. |
| 54 | new Speed_Score( $modules->get_ready_active_optimization_modules(), 'boost-plugin' ); |
| 55 | |
| 56 | add_action( 'init', array( new Analytics(), 'init' ) ); |
| 57 | add_filter( 'plugin_action_links_' . JETPACK_BOOST_PLUGIN_BASE, array( $this, 'plugin_page_settings_link' ) ); |
| 58 | add_action( 'admin_menu', array( $this, 'handle_admin_menu' ), 1 ); // Akismet uses 4, so we use 1 to ensure both menus are added when only they exist. |
| 59 | if ( class_exists( Admin_Shell::class ) ) { |
| 60 | add_filter( 'jetpack_admin_shell_members', array( $this, 'add_admin_shell_member' ) ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Register Boost with the admin shell, which switches between Jetpack dashboards without a page load. |
| 66 | * |
| 67 | * @since 4.9.0 |
| 68 | * |
| 69 | * @param array $members Shell members keyed by admin page slug. |
| 70 | * @return array |
| 71 | */ |
| 72 | public function add_admin_shell_member( $members ) { |
| 73 | if ( ! self::is_modernization_enabled() || ! file_exists( self::build_file() ) ) { |
| 74 | return $members; |
| 75 | } |
| 76 | |
| 77 | $members[ JETPACK_BOOST_SLUG ] = array( |
| 78 | 'build_prefix' => 'jetpack_boost', |
| 79 | 'build_page' => 'jetpack-boost-dashboard', |
| 80 | 'init_modules' => array( '@jetpack-boost/init' ), |
| 81 | 'title' => 'Boost', // "Boost" is a product name, do not translate. |
| 82 | 'capability' => 'manage_options', |
| 83 | // The webpack bundle renders the dashboard's content into the wp-build page; it carries the Boost and DataSync data. |
| 84 | // My Jetpack's header controls script fills the account control in Boost's header. |
| 85 | 'handles' => array( 'jetpack-boost-admin', 'my-jetpack-header-controls', 'wp-jp-i18n-loader' ), |
| 86 | 'load' => array( $this, 'load_as_admin_shell_guest' ), |
| 87 | ); |
| 88 | // The Getting Started screen, which a bare menu link opens until it's done, has no page header. |
| 89 | $members[ JETPACK_BOOST_SLUG ]['header'] = ( new Getting_Started_Entry() )->get() ? false : array( |
| 90 | 'title' => 'Boost', |
| 91 | 'subtitle' => __( 'Improve your site speed and performance.', 'jetpack-boost' ), |
| 92 | ); |
| 93 | return $members; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Ready Boost's routes and page data while another dashboard's page is open, without the screen ID alias. |
| 98 | * |
| 99 | * Skips admin_init()'s premium feature refresh, since a guest must not write options; Boost's own page load still runs it. |
| 100 | * |
| 101 | * @since 4.9.0 |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | public function load_as_admin_shell_guest() { |
| 106 | $build_file = self::build_file(); |
| 107 | require_once $build_file; |
| 108 | if ( ! function_exists( 'jetpack_boost_register_script_modules' ) ) { |
| 109 | return; |
| 110 | } |
| 111 | $this->register_wp_build(); |
| 112 | |
| 113 | // On the host page the shell removes these again; the guest endpoint runs them. |
| 114 | $this->add_enqueue_hooks(); |
| 115 | // Boost's own page attaches the DataSync state when it renders the page body, which the guest endpoint doesn't print. |
| 116 | Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE )->attach_to_plugin( 'jetpack-boost-admin', 'admin_enqueue_scripts' ); |
| 117 | } |
| 118 | |
| 119 | public function handle_admin_menu() { |
| 120 | $this->maybe_load_wp_build(); |
| 121 | |
| 122 | /** |
| 123 | * Filters the number of problems shown in the Boost sidebar menu |
| 124 | * |
| 125 | * @param int $count the number of problems shown. |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | // Only report the count to users who can actually reach the Boost menu |
| 130 | // (added below with the 'manage_options' cap). Otherwise the central |
| 131 | // menu-badges total would include problems the current user can't see. |
| 132 | if ( current_user_can( 'manage_options' ) ) { |
| 133 | $total_problems = apply_filters( 'jetpack_boost_total_problem_count', 0 ); |
| 134 | \Automattic\Jetpack\Menu_Badges\Menu_Badges::init(); // idempotent; wires the renderer. |
| 135 | \Automattic\Jetpack\Menu_Badges\Notification_Counts::register( |
| 136 | 'jetpack-boost', |
| 137 | array( |
| 138 | 'menu_slug' => JETPACK_BOOST_SLUG, |
| 139 | 'count' => (int) $total_problems, |
| 140 | 'type' => 'count', |
| 141 | ) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | $page_suffix = Admin_Menu::add_menu( |
| 146 | __( 'Jetpack Boost - Settings', 'jetpack-boost' ), |
| 147 | 'Boost', // "Boost" is a product name, do not translate. |
| 148 | 'manage_options', |
| 149 | JETPACK_BOOST_SLUG, |
| 150 | $this->modern_dashboard_loaded ? 'jetpack_boost_jetpack_boost_dashboard_wp_admin_render_page' : array( $this, 'render_settings' ), |
| 151 | null, |
| 152 | array( |
| 153 | 'product' => 'boost', |
| 154 | 'key' => 'jetpack-boost', |
| 155 | ) |
| 156 | ); |
| 157 | add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Whether this admin request uses the modern dashboard. |
| 162 | * |
| 163 | * @return bool Whether modernization is enabled for this admin request. |
| 164 | */ |
| 165 | private static function is_modern_dashboard() { |
| 166 | return self::is_modernization_enabled() && is_admin(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Whether the modern dashboard is enabled. |
| 171 | * |
| 172 | * @return bool Whether modernization is enabled. |
| 173 | */ |
| 174 | public static function is_modernization_enabled() { |
| 175 | // Keep the call below on one line: replace-next-version-tag.sh only rewrites the version there. |
| 176 | $message = __( 'The legacy Jetpack Boost dashboard will be removed in a future release.', 'jetpack-boost' ); |
| 177 | |
| 178 | /** |
| 179 | * Filters whether to load the modern Boost dashboard. |
| 180 | * |
| 181 | * Hook `__return_false` to restore the legacy dashboard. |
| 182 | * |
| 183 | * @since 4.7.1 |
| 184 | * @since 4.8.0 Defaults to true. |
| 185 | * @deprecated 4.8.2 The legacy dashboard will be removed in a future release. |
| 186 | * @param bool $enabled Whether to enable the modern dashboard. Default true. |
| 187 | */ |
| 188 | return (bool) apply_filters_deprecated( self::MODERNIZATION_FILTER, array( true ), '4.8.2', '', $message ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Load the modern dashboard only on a Boost admin request that has not filtered it off. |
| 193 | */ |
| 194 | private function maybe_load_wp_build() { |
| 195 | // Check the page first, so a site hooking the deprecated filter is only warned on Boost's screen. |
| 196 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 197 | $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 198 | if ( JETPACK_BOOST_SLUG !== $page || ! self::is_modern_dashboard() || ! $this->dashboard_build_is_available() ) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | $this->register_wp_build(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Register the polyfills and script modules of the generated build, once it is loaded. |
| 207 | */ |
| 208 | private function register_wp_build() { |
| 209 | WP_Build_Polyfills::register( |
| 210 | 'jetpack-boost', |
| 211 | array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS ) |
| 212 | ); |
| 213 | |
| 214 | // wp_default_scripts has already fired by admin_menu, so register the init module now. |
| 215 | jetpack_boost_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Defined by the generated build; callers check it exists. |
| 216 | $this->modern_dashboard_loaded = true; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Path to the generated build's entry file. |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | private static function build_file() { |
| 225 | return JETPACK_BOOST_DIR_PATH . '/build/build.php'; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Load the generated dashboard, retaining the legacy page when assets are absent. |
| 230 | * |
| 231 | * @return bool Whether the generated dashboard is available. |
| 232 | */ |
| 233 | private function dashboard_build_is_available() { |
| 234 | $build_file = self::build_file(); |
| 235 | if ( ! file_exists( $build_file ) ) { |
| 236 | Debug::log( 'Modern dashboard build is missing; loading the legacy dashboard.' ); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | // Hooked around the require, so only the generated enqueue callback, with its init action and REST preloading, sees the alias. |
| 241 | add_action( 'admin_enqueue_scripts', array( $this, 'alias_screen_id_for_wp_build' ) ); |
| 242 | require_once $build_file; |
| 243 | add_action( 'admin_enqueue_scripts', array( $this, 'restore_screen_id_after_wp_build' ) ); |
| 244 | |
| 245 | if ( function_exists( 'jetpack_boost_register_script_modules' ) |
| 246 | && function_exists( 'jetpack_boost_jetpack_boost_dashboard_wp_admin_render_page' ) ) { |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | remove_action( 'admin_enqueue_scripts', array( $this, 'alias_screen_id_for_wp_build' ) ); |
| 251 | remove_action( 'admin_enqueue_scripts', array( $this, 'restore_screen_id_after_wp_build' ) ); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Match wp-build's enqueue screen without changing the Boost menu URL. |
| 257 | * |
| 258 | * @since 4.7.1 |
| 259 | * @since 4.8.0 Reads the current screen itself and records the ID it replaces. |
| 260 | */ |
| 261 | public function alias_screen_id_for_wp_build() { |
| 262 | $screen = get_current_screen(); |
| 263 | if ( ! $screen ) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | $this->wp_build_original_screen_id = $screen->id; |
| 268 | $screen->id = 'jetpack-boost-dashboard'; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Undo alias_screen_id_for_wp_build(), since JITM builds its message path from the screen ID. |
| 273 | * |
| 274 | * @since 4.8.0 |
| 275 | */ |
| 276 | public function restore_screen_id_after_wp_build() { |
| 277 | $screen = get_current_screen(); |
| 278 | if ( ! $screen || null === $this->wp_build_original_screen_id ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | $screen->id = $this->wp_build_original_screen_id; |
| 283 | $this->wp_build_original_screen_id = null; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Enqueue scripts and styles for the admin page. |
| 288 | */ |
| 289 | public function admin_init() { |
| 290 | // Clear premium features cache when the plugin settings page is loaded. |
| 291 | Premium_Features::clear_cache(); |
| 292 | if ( current_user_can( 'manage_options' ) ) { |
| 293 | $already_activated = get_option( Premium_Features::CLOUD_CSS_ACTIVATED_OPTION ); |
| 294 | Premium_Features::enable_cloud_css_after_upgrade(); |
| 295 | if ( $this->modern_dashboard_loaded && ! $already_activated && get_option( Premium_Features::CLOUD_CSS_ACTIVATED_OPTION ) ) { |
| 296 | jetpack_boost_ds_set( 'cloud_css_upgrade_notice', true ); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | $this->add_enqueue_hooks(); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Hook the dashboard's scripts, on Boost's own page and in the admin shell's guest endpoint. |
| 305 | */ |
| 306 | private function add_enqueue_hooks() { |
| 307 | add_action( 'admin_enqueue_scripts', array( My_Jetpack_Initializer::class, 'enqueue_scripts' ) ); |
| 308 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Register the JavaScript for the admin area. |
| 313 | * |
| 314 | * @since 1.0.0 |
| 315 | */ |
| 316 | public function enqueue_scripts() { |
| 317 | /** |
| 318 | * Filters the internal path to the distributed assets used by the plugin |
| 319 | * |
| 320 | * @param string $path the path to the assets |
| 321 | */ |
| 322 | $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' ); |
| 323 | |
| 324 | $admin_js_handle = 'jetpack-boost-admin'; |
| 325 | |
| 326 | $admin_js_dependencies = array( |
| 327 | 'wp-i18n', |
| 328 | 'wp-components', |
| 329 | 'my_jetpack_main_app', |
| 330 | ); |
| 331 | |
| 332 | Assets::register_script( |
| 333 | $admin_js_handle, |
| 334 | $internal_path . 'jetpack-boost.js', |
| 335 | JETPACK_BOOST_PATH, |
| 336 | array( |
| 337 | 'dependencies' => $admin_js_dependencies, |
| 338 | 'in_footer' => true, |
| 339 | 'textdomain' => 'jetpack-boost', |
| 340 | 'css_path' => $internal_path . 'jetpack-boost.css', |
| 341 | ) |
| 342 | ); |
| 343 | |
| 344 | wp_localize_script( |
| 345 | $admin_js_handle, |
| 346 | 'Jetpack_Boost', |
| 347 | ( new Config() )->constants() |
| 348 | ); |
| 349 | |
| 350 | Assets::enqueue_script( $admin_js_handle ); |
| 351 | |
| 352 | if ( $this->modern_dashboard_loaded ) { |
| 353 | $this->localize_api_settings( $admin_js_handle ); |
| 354 | $i18n_loader_registered = wp_script_is( 'wp-jp-i18n-loader', 'registered' ); |
| 355 | if ( $i18n_loader_registered ) { |
| 356 | wp_enqueue_script( 'wp-jp-i18n-loader' ); |
| 357 | } |
| 358 | |
| 359 | // The webpack handle carries Boost constants and DataSync bootstrap needed before modules run. |
| 360 | $prerequisites = wp_scripts()->query( 'jetpack-boost-dashboard-wp-admin-prerequisites', 'registered' ); |
| 361 | if ( $prerequisites ) { |
| 362 | $prerequisites->deps[] = $admin_js_handle; |
| 363 | if ( $i18n_loader_registered ) { |
| 364 | $prerequisites->deps[] = 'wp-jp-i18n-loader'; |
| 365 | } |
| 366 | } elseif ( ! wp_doing_ajax() ) { |
| 367 | // The admin shell's guest endpoint has no prerequisites handle: the shell runs the bundle before it boots the page. |
| 368 | Debug::log( 'Modern dashboard prerequisites are not registered; bootstrap dependencies could not be attached.' ); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Get settings link. |
| 375 | * |
| 376 | * @param array $links the array of links. |
| 377 | */ |
| 378 | public function plugin_page_settings_link( $links ) { |
| 379 | $settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-boost' ) . '">' . esc_html__( 'Settings', 'jetpack-boost' ) . '</a>'; |
| 380 | array_unshift( $links, $settings_link ); |
| 381 | |
| 382 | return $links; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Localize the REST API settings for a script. |
| 387 | * |
| 388 | * @param string $handle Script handle. |
| 389 | */ |
| 390 | private function localize_api_settings( $handle ) { |
| 391 | wp_localize_script( |
| 392 | $handle, |
| 393 | 'wpApiSettings', |
| 394 | array( |
| 395 | 'root' => esc_url_raw( rest_url() ), |
| 396 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 397 | ) |
| 398 | ); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Generate the settings page. |
| 403 | */ |
| 404 | public function render_settings() { |
| 405 | $this->localize_api_settings( 'jetpack-boost-admin' ); |
| 406 | ?> |
| 407 | <div id="jb-admin-settings"></div> |
| 408 | <?php |
| 409 | } |
| 410 | } |