PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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-ai-page.php +186 -46 16.2-beta → 16.3-a.7 View file →
@@ -12,8 +12,9 @@
12 12 use Automattic\Jetpack\Agents_Manager\Agents_Manager;
13 13 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
14 14 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
15 15 use Automattic\Jetpack\Feature_Flags\Feature_Flags;
16 +use Automattic\Jetpack\Modules;
16 17 use Automattic\Jetpack\Redirect;
17 18 use Automattic\Jetpack\Status;
18 19 use Automattic\Jetpack\Status\Host;
19 20 use Automattic\Jetpack\Terms_Of_Service;
@@ -23,8 +24,10 @@
23 24 exit( 0 );
24 25 }
25 26
26 27 require_once dirname( __DIR__ ) . '/class-jetpack-ai-feature-flags.php';
28 +require_once dirname( __DIR__ ) . '/class-jetpack-ai-settings.php';
29 +require_once __DIR__ . '/class-jetpack-wp-build-page.php';
27 30
28 31 /**
29 32 * Builds the Jetpack AI admin page and its sidebar menu entry.
30 33 */
@@ -30,8 +33,58 @@
30 33 */
31 34 class Jetpack_AI_Page {
32 35
33 36 /**
37 + * The wp-build route's page id, which must not be the `jetpack-ai` menu slug.
38 + *
39 + * @var string
40 + */
41 + const WP_BUILD_PAGE_ID = 'jetpack-ai-hub';
42 +
43 + /**
44 + * Whether this request renders through wp-build.
45 + *
46 + * Checks the render function too: if the build output is missing, the request would
47 + * otherwise get no bundle at all now that the legacy entry is gone.
48 + *
49 + * @since 16.3
50 + *
51 + * @return bool
52 + */
53 + public static function should_render_wp_build() {
54 + return function_exists( 'jetpack_plugin_jetpack_ai_hub_wp_admin_render_page' );
55 + }
56 +
57 + /**
58 + * Whether the current request targets the AI Hub admin page.
59 + *
60 + * @since 16.3
61 + *
62 + * @return bool
63 + */
64 + private static function is_ai_admin_request() {
65 + if ( ! is_admin() ) {
66 + return false;
67 + }
68 +
69 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the page slug only.
70 + return isset( $_GET['page'] ) && 'jetpack-ai' === sanitize_text_field( wp_unslash( $_GET['page'] ) );
71 + }
72 +
73 + /**
74 + * Load wp-build for the AI Hub page.
75 + *
76 + * @since 16.3
77 + *
78 + * @return void
79 + */
80 + public static function maybe_load_wp_build() {
81 + if ( self::is_ai_admin_request() ) {
82 + Jetpack_WP_Build_Page::load( self::WP_BUILD_PAGE_ID );
83 + }
84 + }
85 +
86 + /**
34 87 * Register the page and its page-specific hooks.
35 88 *
36 89 * The AI Hub owns its full React layout, so it does not need the legacy
37 90 * Jetpack_Admin_Page lifecycle. Keeping this controller independent also
@@ -77,9 +130,13 @@
77 130 'Jetpack AI',
78 131 'manage_options',
79 132 'jetpack-ai',
80 133 array( $this, 'render' ),
81 - 4
134 + null,
135 + array(
136 + 'product' => 'jetpack-ai',
137 + 'key' => 'jetpack-ai',
138 + )
82 139 );
83 140 }
84 141
85 142 /**
@@ -196,18 +253,12 @@
196 253 /**
197 254 * Enqueue scripts and styles for the AI admin page.
198 255 */
199 256 public function page_admin_scripts() {
200 - $script_path = JETPACK__PLUGIN_DIR . '_inc/build/jetpack-ai-admin.asset.php';
201 - $script_deps = array( 'wp-element', 'wp-components', 'wp-i18n', 'wp-polyfill' );
257 + // wp-build owns the route bundle and its dependencies; this handle only carries the
258 + // inline settings below, so the plugin version is version enough to bust its cache.
202 259 $script_version = JETPACK__VERSION;
203 260
204 - if ( file_exists( $script_path ) ) {
205 - $asset_manifest = include $script_path;
206 - $script_deps = $asset_manifest['dependencies'];
207 - $script_version = $asset_manifest['version'];
208 - }
209 -
210 261 $blog_id = Connection_Manager::get_site_id( true );
211 262 $status = new Status();
212 263 $site_suffix = $status->get_site_suffix();
213 264 // Use the plain hostname for the Atomic activity log URL — get_site_suffix() can
@@ -214,14 +265,23 @@
214 265 // include '::' for subdirectory installs, which would break the URL. This matches
215 266 // the approach used by jetpack-mu-wpcom for the sidebar Activity Log link.
216 267 $site_host = wp_parse_url( home_url(), PHP_URL_HOST );
217 268 $activity_log_site = ( is_string( $site_host ) && '' !== $site_host ) ? $site_host : $site_suffix;
218 - // On Atomic link to WPCOM activity log; on self-hosted link to the local wp-admin page.
219 - $activity_log_url = ( new Host() )->is_woa_site()
220 - ? 'https://wordpress.com/activity-log/' . $activity_log_site
221 - : admin_url( 'admin.php?page=jetpack-activity-log' );
222 269
223 270 /*
271 + * On Atomic link to WPCOM activity log; on self-hosted link to the local
272 + * wp-admin page, which only exists while the `activity-log` module is on.
273 + * An empty URL hides the row rather than linking to an unregistered page.
274 + */
275 + if ( ( new Host() )->is_woa_site() ) {
276 + $activity_log_url = 'https://wordpress.com/activity-log/' . $activity_log_site;
277 + } elseif ( ( new Modules() )->is_active( 'activity-log' ) ) {
278 + $activity_log_url = admin_url( 'admin.php?page=jetpack-activity-log' );
279 + } else {
280 + $activity_log_url = '';
281 + }
282 +
283 + /*
224 284 * Link SEO settings to the dedicated Jetpack SEO page where it exists,
225 285 * falling back to the Traffic settings card. Checking the `rsm_jetpack_seo`
226 286 * filter is required in addition to the cohort check: is_seo_surface_visible()
227 287 * alone returns true on all of wpcom-platform even while the flag is off —
@@ -227,9 +287,9 @@
227 287 * alone returns true on all of wpcom-platform even while the flag is off —
228 288 * it answers only the cohort half, and page registration requires both
229 289 * (see packages/seo Initializer::init()).
230 290 */
231 - $seo_settings_url = admin_url( 'admin.php?page=jetpack#/traffic' );
291 + $seo_settings_url = admin_url( 'admin.php?page=jetpack-settings#/traffic' );
232 292 $is_internal_test = jetpack_is_internal_testing_environment();
233 293 $show_scheduled_tasks_view = self::is_scheduled_tasks_enabled();
234 294 if (
235 295 // The exact-symbol guard matters: the autoloader can select an older
@@ -241,18 +301,13 @@
241 301 ) {
242 302 $seo_settings_url = admin_url( 'admin.php?page=jetpack-seo' );
243 303 }
244 304
245 - wp_enqueue_script(
246 - 'jetpack-ai-admin',
247 - plugins_url( '_inc/build/jetpack-ai-admin.js', JETPACK__PLUGIN_FILE ),
248 - $script_deps,
249 - $script_version,
250 - true
251 - );
305 + // The route bundle is registered by wp-build; this handle exists only to carry the
306 + // inline settings below, which the app reads from `window.jetpackAiSettings`.
307 + wp_register_script( 'jetpack-ai-admin', false, array(), $script_version, true );
308 + wp_enqueue_script( 'jetpack-ai-admin' );
252 309
253 - wp_set_script_translations( 'jetpack-ai-admin', 'jetpack' );
254 -
255 310 // The Tracks sender (w.js); without it, queued events never leave the
256 311 // browser. Consent-gated like the other surfaces that load it.
257 312 $can_send_tracks = ( new Tracking( 'jetpack', new Connection_Manager() ) )->should_enable_tracking( new Terms_Of_Service(), $status );
258 313 if ( $can_send_tracks ) {
@@ -258,13 +313,15 @@
258 313 if ( $can_send_tracks ) {
259 314 Tracking::register_tracks_functions_scripts( true );
260 315 }
261 316
262 - if ( $show_scheduled_tasks_view ) {
263 - Connection_Initial_State::render_script( 'jetpack-ai-admin' );
264 - }
317 + // Unconditional, as on the other Jetpack admin pages: the connection store
318 + // reads it, and only Scheduled tasks used to need it here.
319 + Connection_Initial_State::render_script( 'jetpack-ai-admin' );
265 320
266 - $host = new Host();
321 + $host = new Host();
322 + $has_my_jetpack = self::has_my_jetpack();
323 + $is_offline_mode = $status->is_offline_mode();
267 324
268 325 /**
269 326 * Filters the host-specific AI Hub configuration.
270 327 *
@@ -279,13 +336,27 @@
279 336 // Keep this filterable so hosts can close them independently.
280 337 'showGatedViews' => ! $host->is_vip_site()
281 338 && ( ! $host->is_wpcom_platform() || ( $host->is_woa_site() && $is_internal_test ) ),
282 339 'showA12sBadge' => $host->is_woa_site() && $is_internal_test,
283 - 'isUserConnected' => ( new Connection_Manager() )->is_user_connected(),
284 - // My Jetpack is removed on VIP, so that route dead-ends there.
285 - 'userConnectionUrl' => $host->is_vip_site()
286 - ? 'admin.php?page=jetpack#/connect-user'
287 - : 'admin.php?page=my-jetpack#/connection',
340 + // The same verdicts the feature-settings endpoint reports. That call
341 + // exists for the AI Features toggles; the notice must not wait on it.
342 + 'isUserConnected' => Jetpack_AI_Settings::user_is_connected(),
343 + 'isConnected' => Jetpack_AI_Settings::site_is_connected(),
344 + 'hostAllowsAi' => Jetpack_AI_Settings::host_allows_ai(),
345 + 'masterEnabled' => Jetpack_AI_Settings::is_master_enabled(),
346 + // The route, not a flag: each one documents a different hook.
347 + 'masterForcedOff' => Jetpack_AI_Settings::get_master_forced_off_route(),
348 + 'isOfflineMode' => $is_offline_mode,
349 + 'canConnectSite' => current_user_can( 'jetpack_connect' ),
350 + // These three answer one question; a filter changing one alone leaves
351 + // a label pointing at a page that is not there.
352 + 'hasMyJetpack' => $has_my_jetpack,
353 + 'userConnectionUrl' => $has_my_jetpack
354 + ? 'admin.php?page=my-jetpack#/connection'
355 + : 'admin.php?page=jetpack-settings#/connect-user',
356 + 'manageUrl' => $has_my_jetpack
357 + ? 'admin.php?page=my-jetpack#/products'
358 + : 'admin.php?page=jetpack_modules',
288 359 'mcpSettingsApi' => array(
289 360 'path' => '/wpcom/v2/jetpack-ai/mcp-settings',
290 361 'format' => 'jetpack',
291 362 ),
@@ -299,13 +370,32 @@
299 370 $settings = array(
300 371 'blogId' => $blog_id ? (int) $blog_id : 0,
301 372 'activityLogUrl' => $activity_log_url,
302 373 'seoSettingsUrl' => $seo_settings_url,
374 + 'searchSettingsUrl' => self::get_search_settings_url(),
303 375 'siteAdminUrl' => admin_url(),
304 - 'userConnectionUrl' => esc_url_raw( $config['userConnectionUrl'] ),
376 + 'userConnectionUrl' => esc_url_raw( $config['userConnectionUrl'] ?? '' ),
377 + 'manageUrl' => esc_url_raw( $config['manageUrl'] ?? '' ),
378 + 'hasMyJetpack' => ! empty( $config['hasMyJetpack'] ),
379 + 'isConnected' => ! empty( $config['isConnected'] ),
380 + 'hostAllowsAi' => ! empty( $config['hostAllowsAi'] ),
381 + 'masterEnabled' => ! empty( $config['masterEnabled'] ),
382 + 'masterForcedOff' => in_array(
383 + $config['masterForcedOff'] ?? '',
384 + array(
385 + Jetpack_AI_Settings::FORCED_OFF_ROUTE_FILTER,
386 + Jetpack_AI_Settings::FORCED_OFF_ROUTE_FILTER_VIP,
387 + Jetpack_AI_Settings::FORCED_OFF_ROUTE_MODULES,
388 + ),
389 + true
390 + ) ? $config['masterForcedOff'] : '',
391 + 'isOfflineMode' => ! empty( $config['isOfflineMode'] ),
392 + 'canConnectSite' => ! empty( $config['canConnectSite'] ),
305 393 'apiRoot' => esc_url_raw( rest_url() ),
306 394 'apiNonce' => wp_create_nonce( 'wp_rest' ),
307 395 'pluginUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
396 + // Images ship from the plugin directory, so the plugin version is what busts their cache.
397 + 'assetsVersion' => JETPACK__VERSION,
308 398 // The redirect entry bakes in the jetpack_ai_yearly product and
309 399 // a post-checkout return to this page, so both can be
310 400 // retargeted without shipping a code change.
311 401 'upgradeUrl' => Redirect::get_url( 'jetpack-ai-hub-upgrade' ),
@@ -329,9 +419,9 @@
329 419 'isTest' => $is_internal_test,
330 420 // Identity for Tracks; the lookup can call WordPress.com on a
331 421 // cache miss, so it shares the sender's guard.
332 422 'tracksUserData' => $can_send_tracks ? self::get_tracks_user_data() : null,
333 - 'mcpSettingsApi' => $config['mcpSettingsApi'],
423 + 'mcpSettingsApi' => $config['mcpSettingsApi'] ?? array(),
334 424 );
335 425
336 426 wp_add_inline_script(
337 427 'jetpack-ai-admin',
@@ -356,15 +446,8 @@
356 446 absint( $blog_id )
357 447 ),
358 448 'before'
359 449 );
360 -
361 - wp_enqueue_style(
362 - 'jetpack-ai-admin',
363 - plugins_url( '_inc/build/jetpack-ai-admin.css', JETPACK__PLUGIN_FILE ),
364 - array( 'wp-components' ),
365 - $script_version
366 - );
367 450 }
368 451
369 452 /**
370 453 * Connected-user identity for Tracks; null when no WordPress.com account is
@@ -411,8 +494,50 @@
411 494 return '' !== $email && '@automattic.com' === substr( $email, -15 );
412 495 }
413 496
414 497 /**
498 + * The Search dashboard page the AI Answers row links to, or '' once a host
499 + * has removed it. Removal is remove_submenu_page() on `admin_menu`, which only
500 + * unsets the registry entry — menu_page_url() still answers for the page and
501 + * wp-admin then denies it — so the registry is read after that hook has run.
502 + *
503 + * @return string
504 + */
505 + private static function get_search_settings_url() {
506 + global $submenu;
507 +
508 + // Search registers under the Jetpack menu, or menu-less (parent '') when
509 + // `jetpack_search_should_add_search_submenu` says no; both are reachable.
510 + foreach ( array( 'jetpack', '' ) as $parent ) {
511 + foreach ( (array) ( $submenu[ $parent ] ?? array() ) as $item ) {
512 + if ( isset( $item[2] ) && 'jetpack-search' === $item[2] ) {
513 + return admin_url( 'admin.php?page=jetpack-search#/ai-answers' );
514 + }
515 + }
516 + }
517 +
518 + return '';
519 + }
520 +
521 + /**
522 + * Whether My Jetpack is loaded on this host.
523 + *
524 + * Hosts drop it with the `jetpack_my_jetpack_should_initialize` filter, and
525 + * VIP removes it from outside this codebase, where that filter cannot answer.
526 + *
527 + * @return bool
528 + */
529 + private static function has_my_jetpack() {
530 + if ( ( new Host() )->is_vip_site() ) {
531 + return false;
532 + }
533 +
534 + return class_exists( 'Automattic\\Jetpack\\My_Jetpack\\Initializer' )
535 + && method_exists( 'Automattic\\Jetpack\\My_Jetpack\\Initializer', 'should_initialize' )
536 + && \Automattic\Jetpack\My_Jetpack\Initializer::should_initialize();
537 + }
538 +
539 + /**
415 540 * Name of the purchase granting this site AI ("Jetpack Complete"), from
416 541 * My Jetpack's purchase data — its Plans section's source.
417 542 *
418 543 * @return array{name: string} An empty string when nothing paid grants AI
@@ -502,14 +627,29 @@
502 627 $this->page_render();
503 628 }
504 629
505 630 /**
506 - * Render the page container. The React app mounts into this div.
631 + * Render the page, or say why it could not be rendered.
507 632 *
508 - * AdminPage from @automattic/jetpack-components handles the full-page layout.
633 + * The generated wp-build page owns the markup the app mounts into.
509 634 */
510 635 public function page_render() {
511 - ?>
512 - <div id="jetpack-ai-root"></div>
513 - <?php
636 + if ( self::should_render_wp_build() ) {
637 + jetpack_plugin_jetpack_ai_hub_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.
638 + return;
639 + }
640 +
641 + // The build output is missing; say so rather than leaving a silent blank page.
642 + printf(
643 + '<div class="wrap"><h1>%s</h1><div class="notice notice-error"><p>%s</p></div></div>',
644 + esc_html__( 'Jetpack AI', 'jetpack' ),
645 + esc_html__( 'Jetpack AI could not be loaded because its assets are missing. Reinstalling or updating the plugin usually fixes this. If it keeps happening, contact your site administrator or host.', 'jetpack' )
646 + );
514 647 }
515 648 }
649 +
650 +/*
651 + * wp-build must load before add_actions() runs on any host, so hook it here: Jetpack_Admin and
652 + * mu-wpcom's WordPress.com Simple integration both require this file before `admin_menu`, and
653 + * add_action() dedupes.
654 + */
655 +add_action( 'admin_menu', array( 'Jetpack_AI_Page', 'maybe_load_wp_build' ), 1 );