PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | _inc/lib/admin-pages/class.jetpack-react-page.php +212 -169 12.0.3 → 16.3-a.5 View file →
@@ -1,18 +1,18 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 -use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
3 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
2 +
3 +use Automattic\Jetpack\Assets\Logo;
4 +use Automattic\Jetpack\Redirect;
4 5 use Automattic\Jetpack\Status;
5 6
6 7 require_once __DIR__ . '/class.jetpack-admin-page.php';
7 -require_once __DIR__ . '/class-jetpack-redux-state-helper.php';
8 8
9 9 /**
10 - * Builds the landing page and its menu.
10 + * Registers the Jetpack menu parent, whose page only redirects.
11 11 */
12 12 class Jetpack_React_Page extends Jetpack_Admin_Page {
13 13 /**
14 - * Show the landing page only when Jetpack is connected.
14 + * Register the menu parent before the site connects too.
15 15 *
16 16 * @var bool
17 17 */
18 18 protected $dont_show_if_not_active = false;
@@ -17,21 +17,60 @@
17 17 */
18 18 protected $dont_show_if_not_active = false;
19 19
20 20 /**
21 - * Used for fallback when REST API is disabled.
21 + * Legacy hashes the Settings page renders; they forward there with their hash.
22 22 *
23 - * @var bool
23 + * Mirrors `settingsRoutes` in `_inc/client/main.jsx`, plus the connection screens.
24 + *
25 + * @since 16.3
26 + * @var string[]
24 27 */
25 - protected $is_redirecting = false;
28 + const SETTINGS_ROUTES = array(
29 + '/settings',
30 + '/security',
31 + '/performance',
32 + '/writing',
33 + '/sharing',
34 + '/discussion',
35 + '/earn',
36 + '/reader',
37 + '/traffic',
38 + '/privacy',
39 + '/setup',
40 + '/connect-user',
41 + '/connect-user-setup',
42 + );
26 43
27 44 /**
45 + * Forwards Settings hashes with their hash, maps known routes, and falls back for the rest.
46 + *
47 + * @var string
48 + */
49 + const LEGACY_ROUTE_REDIRECT_SCRIPT = <<<'JS'
50 +function ( settings, forward, routes, fallback ) {
51 + var hash = window.location.hash;
52 + var path = hash.replace( /^#\/?/, '/' ).split( '?' )[ 0 ] || '/';
53 + var target = fallback;
54 + if ( forward.indexOf( path ) !== -1 ) {
55 + target = settings + hash;
56 + } else if ( Object.prototype.hasOwnProperty.call( routes, path ) ) {
57 + target = routes[ path ];
58 + }
59 + window.location.replace( target );
60 +}
61 +JS;
62 +
63 + /**
28 64 * Add the main admin Jetpack menu.
29 65 *
30 66 * @return string|false Return value from WordPress's `add_menu_page()`.
31 67 */
32 68 public function get_page_hook() {
33 - return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div', 3 );
69 + $logo = new Logo();
70 + // Keep this fallback in sync with Jetpack_Network::add_network_admin_menu().
71 + $icon = method_exists( $logo, 'get_base64_admin_menu_logo' ) ? $logo->get_base64_admin_menu_logo() : $logo->get_base64_logo();
72 + return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), $icon, 3 );
34 73 }
35 74
36 75 /**
37 76 * Add page action.
@@ -42,21 +81,24 @@
42 81 public function add_page_actions( $hook ) {
43 82 /** This action is documented in class.jetpack-admin.php */
44 83 do_action( 'jetpack_admin_menu', $hook );
45 84
46 - if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is view logic.
85 + if ( ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
86 + return;
87 + }
88 + $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
89 + if ( 'jetpack' !== $page ) {
90 + if ( strpos( $page, 'jetpack/' ) === 0 ) {
91 + $section = substr( $page, 8 );
92 + wp_safe_redirect( admin_url( 'admin.php?page=jetpack#/' . $section ) );
93 + exit( 0 );
94 + }
47 95 return; // No need to handle the fallback redirection if we are not on the Jetpack page.
48 96 }
49 97
50 - // Adding a redirect meta tag if the REST API is disabled.
51 - if ( ! $this->is_rest_api_enabled() ) {
52 - $this->is_redirecting = true;
53 - add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) );
54 - }
98 + // After the action handlers and the connection controller, which exit when they act.
99 + add_action( "load-$hook", array( $this, 'render_redirect_document' ), PHP_INT_MAX );
55 100
56 - // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled.
57 - add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) );
58 -
59 101 // If this is the first time the user is viewing the admin, don't show JITMs.
60 102 // This filter is added just in time because this function is called on admin_menu
61 103 // and JITMs are initialized on admin_init.
62 104 if ( Jetpack::is_connection_ready() && ! Jetpack_Options::get_option( 'first_admin_view', false ) ) {
@@ -65,156 +107,205 @@
65 107 }
66 108 }
67 109
68 110 /**
69 - * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
111 + * Remove the main Jetpack submenu if a site is in offline mode or connected
112 + * or if My Jetpack is available.
113 + * At that point, admins can access the Jetpack Dashboard instead.
70 114 *
71 - * Works in Dev Mode or when user is connected.
72 - *
73 - * @since 4.3.0
115 + * @since 13.8
74 116 */
75 - public function jetpack_add_dashboard_sub_nav_item() {
76 - if ( ( new Status() )->is_offline_mode() || Jetpack::is_connection_ready() ) {
77 - add_submenu_page( 'jetpack', __( 'Dashboard', 'jetpack' ), __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/dashboard', '__return_null' );
117 + public function remove_jetpack_menu() {
118 + $is_offline_mode = ( new Status() )->is_offline_mode();
119 + $has_my_jetpack = (
120 + class_exists( 'Automattic\Jetpack\My_Jetpack\Initializer' ) &&
121 + method_exists( 'Automattic\Jetpack\My_Jetpack\Initializer', 'should_initialize' ) &&
122 + \Automattic\Jetpack\My_Jetpack\Initializer::should_initialize()
123 + );
124 +
125 + if ( $is_offline_mode || $has_my_jetpack || Jetpack::is_connection_ready() ) {
78 126 remove_submenu_page( 'jetpack', 'jetpack' );
79 127 }
80 128 }
81 129
82 130 /**
83 - * Determine whether a user can access the Jetpack Settings page.
131 + * Where links into page=jetpack land.
84 132 *
85 - * Rules are:
86 - * - user is allowed to see the Jetpack Admin
87 - * - site is connected or in offline mode
88 - * - non-admins only need access to the settings when there are modules they can manage.
133 + * Settings hashes keep their hash on the Settings page. Admins go to My Jetpack
134 + * wherever it runs; everyone else, and a request with a pending error, lands on
135 + * Settings. While the partner coupon screen applies, every route goes there.
89 136 *
90 - * @return bool $can_access_settings Can the user access settings.
137 + * @return array{settings: string, forward: string[], routes: array<string, string>, fallback: string}
91 138 */
92 - private function can_access_settings() {
93 - $connection = new Connection_Manager( 'jetpack' );
94 - $status = new Status();
139 + public static function get_legacy_route_redirects() {
140 + $settings_url = admin_url( 'admin.php?page=jetpack-settings' );
141 + $coupon_screen = self::get_partner_coupon_redirect();
142 + if ( $coupon_screen ) {
143 + return array(
144 + 'settings' => $settings_url,
145 + 'forward' => array(),
146 + 'routes' => array(),
147 + 'fallback' => $coupon_screen,
148 + );
149 + }
95 150
96 - // User must have the necessary permissions to see the Jetpack settings pages.
97 - if ( ! current_user_can( 'edit_posts' ) ) {
98 - return false;
151 + $table = array(
152 + 'settings' => $settings_url,
153 + 'forward' => self::SETTINGS_ROUTES,
154 + 'routes' => array(),
155 + 'fallback' => $settings_url,
156 + );
157 +
158 + if ( ! self::should_redirect_legacy_routes() ) {
159 + return $table;
99 160 }
100 161
101 - // In offline mode, allow access to admins.
102 - if ( $status->is_offline_mode() && current_user_can( 'manage_options' ) ) {
103 - return true;
104 - }
162 + $pricing_url = Redirect::get_url( 'jetpack-plans' );
163 + $table['routes'] = array(
164 + '/plans' => $pricing_url,
165 + '/plans-prompt' => $pricing_url,
166 + '/newsletter' => admin_url( 'admin.php?page=jetpack-newsletter' ),
167 + );
105 168
106 - // If not in offline mode but site is not connected, bail.
107 - if ( ! Jetpack::is_connection_ready() ) {
108 - return false;
109 - }
169 + if ( self::can_use_my_jetpack() ) {
170 + $my_jetpack = admin_url( 'admin.php?page=my-jetpack' );
110 171
111 - /*
112 - * Additional checks for non-admins.
113 - */
114 - if ( ! current_user_can( 'manage_options' ) ) {
115 - // If the site isn't connected at all, bail.
116 - if ( ! $connection->has_connected_owner() ) {
117 - return false;
172 + foreach ( array( 'akismet', 'backup', 'scan', 'search', 'security', 'videopress' ) as $product ) {
173 + $table['routes'][ '/product/' . $product ] = $my_jetpack . '#/add-' . $product;
118 174 }
119 175
120 - /*
121 - * If they haven't connected their own account yet,
122 - * they have no use for the settings page.
123 - * They will not be able to manage any settings.
124 - */
125 - if ( ! $connection->is_user_connected() ) {
126 - return false;
176 + $table['routes']['/license/activation'] = $my_jetpack . '#/add-license';
177 +
178 + foreach ( array( '/reconnect', '/disconnect', '/woo-setup' ) as $route ) {
179 + $table['routes'][ $route ] = $my_jetpack . '#/connection';
127 180 }
128 181
129 - /*
130 - * Non-admins only have access to settings
131 - * for the following modules:
132 - * - Publicize
133 - * - Post By Email
134 - * If those modules are not available, bail.
135 - */
136 - if (
137 - ! Jetpack::is_module_active( 'post-by-email' )
138 - && ! Jetpack::is_module_active( 'publicize' )
139 - ) {
140 - return false;
141 - }
182 + $table['fallback'] = $my_jetpack;
142 183 }
143 184
144 - // fallback.
145 - return true;
185 + return $table;
146 186 }
147 187
148 188 /**
149 - * Jetpack Settings sub-link.
189 + * Whether this request may leave Settings.
150 190 *
151 - * @since 4.3.0
152 - * @since 9.7.0 If Connection does not have an owner, restrict it to admins
191 + * @return bool
153 192 */
154 - public function jetpack_add_settings_sub_nav_item() {
155 - if ( $this->can_access_settings() ) {
156 - add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/settings', '__return_null' );
193 + public static function should_redirect_legacy_routes() {
194 + // A pending error only renders via the Settings app's state notices.
195 + return ! Jetpack::state( 'error' );
196 + }
197 +
198 + /**
199 + * Print the legacy route redirect; it runs in the browser because the server never sees the hash.
200 + */
201 + public function print_legacy_route_redirect() {
202 + $table = self::get_legacy_route_redirects();
203 + $flags = JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP;
204 +
205 + wp_print_inline_script_tag(
206 + sprintf(
207 + '( %s )( %s, %s, %s, %s );',
208 + self::LEGACY_ROUTE_REDIRECT_SCRIPT,
209 + wp_json_encode( $table['settings'], $flags ),
210 + wp_json_encode( $table['forward'], $flags ),
211 + wp_json_encode( (object) $table['routes'], $flags ),
212 + wp_json_encode( $table['fallback'], $flags )
213 + )
214 + );
215 + }
216 +
217 + /**
218 + * Replace page=jetpack with the redirect document; nothing else renders here.
219 + *
220 + * @since 16.3
221 + *
222 + * @return never
223 + */
224 + public function render_redirect_document() {
225 + $table = self::get_legacy_route_redirects();
226 + if ( $table['settings'] === $table['fallback'] ) {
227 + // Settings renders this request's notices, so its state must survive the hop.
228 + Jetpack::restate();
157 229 }
230 +
231 + $this->print_redirect_document();
232 + exit( 0 );
158 233 }
159 234
160 235 /**
161 - * Fallback redirect meta tag if the REST API is disabled.
236 + * Print a bare document that only redirects.
162 237 *
163 - * @return void
238 + * @since 16.3
164 239 */
165 - public function add_fallback_head_meta() {
166 - echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">';
240 + public function print_redirect_document() {
241 + ?>
242 +<!DOCTYPE html>
243 +<html <?php language_attributes(); ?>>
244 +<head>
245 +<meta charset="<?php echo esc_attr( get_bloginfo( 'charset' ) ); ?>">
246 +<title>Jetpack</title><?php // "Jetpack" is a product name, do not translate. ?>
247 + <?php
248 + if ( $this->is_rest_api_enabled() ) {
249 + $this->print_legacy_route_redirect();
250 + $this->add_noscript_head_meta();
251 + } else {
252 + $this->add_fallback_head_meta();
253 + }
254 + ?>
255 +</head>
256 +<body></body>
257 +</html>
258 + <?php
167 259 }
168 260
169 261 /**
170 - * Fallback meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled.
262 + * Whether My Jetpack can take over for the current user.
171 263 *
172 - * @return void
264 + * @return bool
173 265 */
174 - public function add_noscript_head_meta() {
175 - echo '<noscript>';
176 - $this->add_fallback_head_meta();
177 - echo '</noscript>';
266 + private static function can_use_my_jetpack() {
267 + return current_user_can( 'manage_options' )
268 + && class_exists( 'Automattic\Jetpack\My_Jetpack\Initializer' )
269 + && method_exists( 'Automattic\Jetpack\My_Jetpack\Initializer', 'should_initialize' )
270 + && \Automattic\Jetpack\My_Jetpack\Initializer::should_initialize();
178 271 }
179 272
180 273 /**
181 - * Custom menu order.
274 + * The My Jetpack coupon screen, while it should replace this page.
182 275 *
183 - * @deprecated since 9.2.0
184 - * @param array $menu_order Menu order.
185 - * @return array
276 + * An older My Jetpack bounces showCouponRedemption back here, so only forward to one that renders it.
277 + *
278 + * @return string|null
186 279 */
187 - public function jetpack_menu_order( $menu_order ) {
188 - _deprecated_function( __METHOD__, 'jetpack-9.2' );
280 + private static function get_partner_coupon_redirect() {
281 + if (
282 + ! class_exists( 'Automattic\Jetpack\My_Jetpack\Initializer' )
283 + || ! method_exists( 'Automattic\Jetpack\My_Jetpack\Initializer', 'get_partner_coupon_screen' )
284 + || null === \Automattic\Jetpack\My_Jetpack\Initializer::get_partner_coupon_screen()
285 + ) {
286 + return null;
287 + }
189 288
190 - return $menu_order;
289 + return admin_url( 'admin.php?page=my-jetpack&showCouponRedemption=1' );
191 290 }
192 291
193 292 /**
194 - * Add action to render page specific HTML.
293 + * Formerly added the Settings sub-link.
195 294 *
295 + * @since 4.3.0
296 + * @deprecated 16.3 Jetpack_Settings_React_Page registers the Settings page.
297 + */
298 + public function jetpack_add_settings_sub_nav_item() {
299 + _deprecated_function( __METHOD__, 'jetpack-16.3' );
300 + }
301 +
302 + /**
303 + * Nothing renders here: render_redirect_document() exits on load.
304 + *
196 305 * @return void
197 306 */
198 - public function page_render() {
199 - /** This action is already documented in class.jetpack-admin-page.php */
200 - do_action( 'jetpack_notices' );
201 -
202 - // Fetch static.html.
203 - $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, Not fetching a remote file.
204 -
205 - if ( false === $static_html ) {
206 -
207 - // If we still have nothing, display an error.
208 - echo '<p>';
209 - esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' );
210 - echo '<code>pnpm run distclean && pnpm jetpack build plugins/jetpack</code>';
211 - echo '</p>';
212 - } else {
213 - // We got the static.html so let's display it.
214 - echo $static_html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
215 - }
216 - }
307 + public function page_render() {}
217 308 /**
218 309 * Allow robust deep links to React.
219 310 *
220 311 * The Jetpack dashboard requires fragments/hash values to make
@@ -231,9 +322,9 @@
231 322 return;
232 323 }
233 324
234 325 $allowed_paths = array(
235 - 'product-purchased' => admin_url( '/admin.php?page=jetpack#/recommendations/product-purchased' ),
326 + 'product-purchased' => admin_url( 'admin.php?page=jetpack' ),
236 327 );
237 328
238 329 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
239 330 $target = sanitize_text_field( wp_unslash( $_GET['jp-react-redirect'] ) );
@@ -238,61 +329,13 @@
238 329 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
239 330 $target = sanitize_text_field( wp_unslash( $_GET['jp-react-redirect'] ) );
240 331 if ( isset( $allowed_paths[ $target ] ) ) {
241 332 wp_safe_redirect( $allowed_paths[ $target ] );
242 - exit;
333 + exit( 0 );
243 334 }
244 335 }
245 336
246 337 /**
247 - * Load styles for static page.
338 + * Nothing loads here: render_redirect_document() exits on load.
248 339 */
249 - public function additional_styles() {
250 - Jetpack_Admin_Page::load_wrapper_styles();
251 - }
252 -
253 - /**
254 - * Load admin page scripts.
255 - */
256 - public function page_admin_scripts() {
257 - if ( $this->is_redirecting ) {
258 - return; // No need for scripts on a fallback page.
259 - }
260 -
261 - $status = new Status();
262 - $is_offline_mode = $status->is_offline_mode();
263 - $site_suffix = $status->get_site_suffix();
264 - $script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/admin.asset.php';
265 - $script_dependencies = array( 'jquery', 'wp-polyfill' );
266 - $version = JETPACK__VERSION;
267 - if ( file_exists( $script_deps_path ) ) {
268 - $asset_manifest = include $script_deps_path;
269 - $script_dependencies = $asset_manifest['dependencies'];
270 - $version = $asset_manifest['version'];
271 - }
272 -
273 - wp_enqueue_script(
274 - 'react-plugin',
275 - plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ),
276 - $script_dependencies,
277 - $version,
278 - true
279 - );
280 -
281 - if ( ! $is_offline_mode && Jetpack::is_connection_ready() ) {
282 - // Required for Analytics.
283 - wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
284 - }
285 -
286 - wp_set_script_translations( 'react-plugin', 'jetpack' );
287 -
288 - // Add objects to be passed to the initial state of the app.
289 - // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
290 - wp_add_inline_script( 'react-plugin', 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Redux_State_Helper::get_initial_state() ) ) . '"));', 'before' );
291 -
292 - // This will set the default URL of the jp_redirects lib.
293 - wp_add_inline_script( 'react-plugin', 'var jetpack_redirects = { currentSiteRawUrl: "' . $site_suffix . '" };', 'before' );
294 -
295 - // Adds Connection package initial state.
296 - wp_add_inline_script( 'react-plugin', Connection_Initial_State::render(), 'before' );
297 - }
340 + public function page_admin_scripts() {}
298 341 }