Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
96 / 128 |
|
62.50% |
10 / 16 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Settings_React_Page | |
74.40% |
93 / 125 |
|
62.50% |
10 / 16 |
114.44 | |
0.00% |
0 / 1 |
| should_load_wp_build | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| maybe_load_wp_build | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| add_admin_shell_member | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
| load_as_admin_shell_guest | |
91.30% |
21 / 23 |
|
0.00% |
0 / 1 |
7.03 | |||
| is_admin_shell_guest_request | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| should_render_wp_build | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| get_wp_build_script_dependencies | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_page_hook | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| hide_menu_item | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| add_page_actions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_fallback_redirects | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| can_access_settings | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
12.67 | |||
| is_post_by_email_on_profile_only | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| page_render | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
2.06 | |||
| additional_styles | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| page_admin_scripts | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The Jetpack Settings admin page. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 9 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | |
| 15 | require_once __DIR__ . '/class.jetpack-admin-page.php'; |
| 16 | require_once __DIR__ . '/class-jetpack-redux-state-helper.php'; |
| 17 | require_once __DIR__ . '/class-jetpack-wp-build-page.php'; |
| 18 | |
| 19 | /** |
| 20 | * Renders the Settings app, whose connection screens also serve unconnected sites. |
| 21 | * |
| 22 | * @since 16.3 |
| 23 | */ |
| 24 | class Jetpack_Settings_React_Page extends Jetpack_Admin_Page { |
| 25 | /** |
| 26 | * Register the page before the site connects, for the connection screen. |
| 27 | * |
| 28 | * @var bool |
| 29 | */ |
| 30 | protected $dont_show_if_not_active = false; |
| 31 | |
| 32 | /** |
| 33 | * Whether the REST API is off and the page falls back to the modules list. |
| 34 | * |
| 35 | * @var bool |
| 36 | */ |
| 37 | protected $is_redirecting = false; |
| 38 | |
| 39 | /** |
| 40 | * The wp-build route's page id, which must not be the `jetpack-settings` menu slug. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | const WP_BUILD_PAGE_ID = 'jetpack-settings-dashboard'; |
| 45 | |
| 46 | /** |
| 47 | * Whether this request loaded the wp-build route. |
| 48 | * |
| 49 | * @var bool |
| 50 | */ |
| 51 | private $is_wp_build_loaded = false; |
| 52 | |
| 53 | /** |
| 54 | * Whether this request should load wp-build. |
| 55 | * |
| 56 | * An IDC-blocked page shows only the IDC banner, which the wp-build template would hide. |
| 57 | * |
| 58 | * @since 16.3 |
| 59 | * |
| 60 | * @return bool |
| 61 | */ |
| 62 | public function should_load_wp_build() { |
| 63 | if ( ! is_admin() ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the page slug only. |
| 68 | if ( ! isset( $_GET['page'] ) || 'jetpack-settings' !== sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return ! $this->block_page_rendering_for_idc(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Load wp-build before the admin menu is built, on the Settings request only. |
| 77 | * |
| 78 | * @since 16.3 |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function maybe_load_wp_build() { |
| 83 | if ( $this->should_load_wp_build() ) { |
| 84 | $this->is_wp_build_loaded = Jetpack_WP_Build_Page::load( self::WP_BUILD_PAGE_ID ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Register Settings with the admin shell, which switches between Jetpack dashboards without a page load. |
| 90 | * |
| 91 | * @since 16.5 |
| 92 | * |
| 93 | * @param array $members Shell members keyed by admin page slug. |
| 94 | * @return array |
| 95 | */ |
| 96 | public function add_admin_shell_member( $members ) { |
| 97 | // During IDC the page shows only the IDC banner. |
| 98 | if ( |
| 99 | ( new Host() )->is_wpcom_simple() |
| 100 | || ! file_exists( JETPACK__PLUGIN_DIR . 'build/build.php' ) |
| 101 | || ! $this->can_access_settings() |
| 102 | || $this->block_page_rendering_for_idc() |
| 103 | ) { |
| 104 | return $members; |
| 105 | } |
| 106 | |
| 107 | $members['jetpack-settings'] = array( |
| 108 | 'build_prefix' => 'jetpack_plugin', |
| 109 | 'build_page' => self::WP_BUILD_PAGE_ID, |
| 110 | 'init_modules' => array( '@jetpack-plugin/init' ), |
| 111 | 'title' => __( 'Settings', 'jetpack' ), |
| 112 | // The landing tab, and so its subtitle, is picked in the browser from modules, plan and pages. |
| 113 | 'header' => array( |
| 114 | 'title' => __( 'Settings', 'jetpack' ), |
| 115 | 'subtitle' => null, |
| 116 | ), |
| 117 | 'capability' => 'jetpack_admin_page', |
| 118 | 'handles' => array( 'react-plugin', 'jetpack-script-data', 'jp-tracks', 'wp-jp-i18n-loader' ), |
| 119 | 'load' => array( $this, 'load_as_admin_shell_guest' ), |
| 120 | ); |
| 121 | return $members; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Ready the Settings routes and page data while another dashboard's page is open, without the screen ID alias. |
| 126 | * |
| 127 | * @since 16.5 |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public function load_as_admin_shell_guest() { |
| 132 | // A menu hover builds the page state, which saves module defaults and copies of per-user values; the page's own load saves them. |
| 133 | if ( self::is_admin_shell_guest_request() ) { |
| 134 | $user_id = get_current_user_id(); |
| 135 | $keep_stored = static function ( $value, $old_value ) { |
| 136 | return $old_value; |
| 137 | }; |
| 138 | foreach ( array( 'sharing-options', 'monitor_receive_notifications' . $user_id, 'post_by_email_address' . $user_id ) as $option ) { |
| 139 | add_filter( 'pre_update_option_' . $option, $keep_stored, 10, 2 ); |
| 140 | } |
| 141 | // The purchase token lives among the connection tokens, so hold back only that key. |
| 142 | add_filter( |
| 143 | 'pre_update_option_jetpack_private_options', |
| 144 | static function ( $value, $old_value ) { |
| 145 | if ( ! is_array( $value ) ) { |
| 146 | return $value; |
| 147 | } |
| 148 | if ( is_array( $old_value ) && isset( $old_value['purchase_token'] ) ) { |
| 149 | $value['purchase_token'] = $old_value['purchase_token']; |
| 150 | } else { |
| 151 | unset( $value['purchase_token'] ); |
| 152 | } |
| 153 | return $value; |
| 154 | }, |
| 155 | 10, |
| 156 | 2 |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | $this->is_wp_build_loaded = Jetpack_WP_Build_Page::load( self::WP_BUILD_PAGE_ID, false ); |
| 161 | if ( $this->is_wp_build_loaded ) { |
| 162 | add_action( 'admin_enqueue_scripts', array( $this, 'page_admin_scripts' ) ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Whether this is the admin shell's hover request, not another dashboard's page readying Settings for a switch. |
| 168 | * |
| 169 | * @return bool |
| 170 | */ |
| 171 | private static function is_admin_shell_guest_request() { |
| 172 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Compared only; the shell checks the nonce. |
| 173 | return wp_doing_ajax() && isset( $_GET['action'] ) && \Automattic\Jetpack\Admin_UI\Admin_Shell::GUEST_ACTION === wp_unslash( $_GET['action'] ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Whether this request renders through wp-build. |
| 178 | * |
| 179 | * @since 16.3 |
| 180 | * |
| 181 | * @return bool |
| 182 | */ |
| 183 | public function should_render_wp_build() { |
| 184 | return $this->is_wp_build_loaded && function_exists( 'jetpack_plugin_jetpack_settings_dashboard_wp_admin_render_page' ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * The route bundle's classic script dependencies (e.g. `lodash`), which wp-build registers |
| 189 | * as a script module without them, leaving globals like `window.lodash` undefined. |
| 190 | * |
| 191 | * @since 16.3 |
| 192 | * |
| 193 | * @return string[] |
| 194 | */ |
| 195 | protected function get_wp_build_script_dependencies() { |
| 196 | $asset_path = JETPACK__PLUGIN_DIR . 'build/routes/settings/content.min.asset.php'; |
| 197 | if ( ! file_exists( $asset_path ) ) { |
| 198 | return array(); |
| 199 | } |
| 200 | |
| 201 | $asset = include $asset_path; |
| 202 | return $asset['dependencies'] ?? array(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Register the page; only its sidebar entry keeps the Settings access gate. |
| 207 | * |
| 208 | * Shares the bottom tier with Beta Tester so it lands below the alphabetical run; |
| 209 | * the upsell still renders underneath because Admin_Menu appends it after sorting. |
| 210 | * |
| 211 | * @return string The page hook. |
| 212 | */ |
| 213 | public function get_page_hook() { |
| 214 | if ( ! $this->can_access_settings() ) { |
| 215 | add_filter( 'jetpack_admin_menu_visibility', array( __CLASS__, 'hide_menu_item' ) ); |
| 216 | } |
| 217 | |
| 218 | $position = defined( Admin_Menu::class . '::POSITION_LAST' ) ? Admin_Menu::POSITION_LAST : 998; |
| 219 | |
| 220 | $hook = Admin_Menu::add_menu( |
| 221 | __( 'Settings', 'jetpack' ), |
| 222 | __( 'Settings', 'jetpack' ), |
| 223 | 'jetpack_admin_page', |
| 224 | 'jetpack-settings', |
| 225 | array( $this, 'render' ), |
| 226 | $position, |
| 227 | array( 'key' => 'jetpack-settings' ) |
| 228 | ); |
| 229 | |
| 230 | // The IDC banner is a core-style notice, and during IDC it is all this page shows. |
| 231 | remove_action( "load-$hook", array( Admin_Menu::class, 'hide_core_admin_notices' ) ); |
| 232 | |
| 233 | return $hook; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Hide the sidebar entry from users who cannot use Settings. |
| 238 | * |
| 239 | * @param array $states Menu item key to visibility state. |
| 240 | * @return array |
| 241 | */ |
| 242 | public static function hide_menu_item( $states ) { |
| 243 | $states['jetpack-settings'] = 'hidden'; |
| 244 | return $states; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Add page actions. |
| 249 | * |
| 250 | * @param string $hook Hook of current page. |
| 251 | * @return void |
| 252 | */ |
| 253 | public function add_page_actions( $hook ) { |
| 254 | add_action( "load-$hook", array( $this, 'add_fallback_redirects' ) ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Send browsers that cannot run the app to the modules list. |
| 259 | * |
| 260 | * @return void |
| 261 | */ |
| 262 | public function add_fallback_redirects() { |
| 263 | if ( ! $this->is_rest_api_enabled() ) { |
| 264 | $this->is_redirecting = true; |
| 265 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
| 266 | } |
| 267 | |
| 268 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Determine whether a user can access the Jetpack Settings page. |
| 273 | * |
| 274 | * Rules are: |
| 275 | * - user is allowed to see the Jetpack Admin |
| 276 | * - site is connected or in offline mode |
| 277 | * - non-admins only need access to the settings when there are modules they can manage. |
| 278 | * |
| 279 | * @return bool $can_access_settings Can the user access settings. |
| 280 | */ |
| 281 | private function can_access_settings() { |
| 282 | $connection = new Connection_Manager( 'jetpack' ); |
| 283 | $status = new Status(); |
| 284 | |
| 285 | // User must have the necessary permissions to see the Jetpack settings pages. |
| 286 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // In offline mode, allow access to admins. |
| 291 | if ( $status->is_offline_mode() && current_user_can( 'manage_options' ) ) { |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | // If not in offline mode but site is not connected, bail. |
| 296 | if ( ! Jetpack::is_connection_ready() ) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Additional checks for non-admins. |
| 302 | */ |
| 303 | if ( ! current_user_can( 'manage_options' ) ) { |
| 304 | // If the site isn't connected at all, bail. |
| 305 | if ( ! $connection->has_connected_owner() ) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * If they haven't connected their own account yet, |
| 311 | * they have no use for the settings page. |
| 312 | * They will not be able to manage any settings. |
| 313 | */ |
| 314 | if ( ! $connection->is_user_connected() ) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Non-admins only have access to settings |
| 320 | * for the following modules: |
| 321 | * - Publicize |
| 322 | * - Post By Email, until it lives only on their Profile page |
| 323 | * If those modules are not available, bail. |
| 324 | */ |
| 325 | if ( |
| 326 | ( ! Jetpack::is_module_active( 'post-by-email' ) || self::is_post_by_email_on_profile_only() ) |
| 327 | && ( |
| 328 | ! Jetpack::is_module_active( 'publicize' ) || |
| 329 | ! current_user_can( 'publish_posts' ) |
| 330 | ) |
| 331 | ) { |
| 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // fallback. |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Whether users who cannot manage modules find Post by Email only on their Profile page, once settings move to My Jetpack. |
| 342 | * |
| 343 | * Uses the check that needs no admin menu, since this runs while the menu is built. |
| 344 | * |
| 345 | * @return bool |
| 346 | */ |
| 347 | private static function is_post_by_email_on_profile_only() { |
| 348 | return method_exists( My_Jetpack_Initializer::class, 'is_settings_consolidation_on_for_site' ) |
| 349 | && My_Jetpack_Initializer::is_settings_consolidation_on_for_site(); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Add action to render page specific HTML. |
| 354 | * |
| 355 | * @return void |
| 356 | */ |
| 357 | public function page_render() { |
| 358 | /** This action is already documented in class.jetpack-admin-page.php */ |
| 359 | do_action( 'jetpack_notices' ); |
| 360 | |
| 361 | if ( $this->should_render_wp_build() ) { |
| 362 | jetpack_plugin_jetpack_settings_dashboard_wp_admin_render_page(); // @phan-suppress-current-line PhanUndeclaredFunction -- should_render_wp_build() checks function_exists(); defined in the generated build/pages/, which Phan excludes. |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | echo '<p class="jp-settings-build-error">'; |
| 367 | esc_html_e( 'Jetpack Settings could not load. Try running: ', 'jetpack' ); |
| 368 | echo '<code>pnpm jetpack build plugins/jetpack</code>'; |
| 369 | echo '</p>'; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Load styles for static page. |
| 374 | */ |
| 375 | public function additional_styles() { |
| 376 | // The route bundle carries these styles. |
| 377 | if ( $this->should_render_wp_build() ) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | Jetpack_Admin_Page::load_wrapper_styles(); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Load admin page scripts. |
| 386 | */ |
| 387 | public function page_admin_scripts() { |
| 388 | // A fallback page, or one without the route (IDC-blocked or unbuilt), renders no app to feed. |
| 389 | if ( $this->is_redirecting || ! $this->should_render_wp_build() ) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | $status = new Status(); |
| 394 | $is_offline_mode = $status->is_offline_mode(); |
| 395 | $site_suffix = $status->get_site_suffix(); |
| 396 | |
| 397 | // wp-build enqueues the route bundle; this handle carries the inline state and classic dependencies. |
| 398 | wp_register_script( 'react-plugin', false, $this->get_wp_build_script_dependencies(), JETPACK__VERSION, true ); |
| 399 | wp_enqueue_script( 'react-plugin' ); |
| 400 | |
| 401 | $blog_id_prop = ''; |
| 402 | if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { |
| 403 | $blog_id = Connection_Manager::get_site_id( true ); |
| 404 | if ( $blog_id ) { |
| 405 | $blog_id_prop = ', currentBlogID: "' . (int) $blog_id . '"'; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if ( ! $is_offline_mode && Jetpack::is_connection_ready() ) { |
| 410 | // Required for Analytics. |
| 411 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
| 412 | } |
| 413 | |
| 414 | // Add objects to be passed to the initial state of the app. |
| 415 | // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280. |
| 416 | wp_add_inline_script( 'react-plugin', 'var Initial_State=' . wp_json_encode( Jetpack_Redux_State_Helper::get_initial_state(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); |
| 417 | |
| 418 | // This will set the default URL of the jp_redirects lib. |
| 419 | wp_add_inline_script( 'react-plugin', 'var jetpack_redirects = { currentSiteRawUrl: "' . $site_suffix . '"' . $blog_id_prop . ' };', 'before' ); |
| 420 | |
| 421 | // Adds Connection package initial state. |
| 422 | Connection_Initial_State::render_script( 'react-plugin' ); |
| 423 | } |
| 424 | } |