Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.38% covered (warning)
78.38%
29 / 37
100.00% covered (success)
100.00%
3 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\AiAssistantPlugin\register_plugin
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
Automattic\Jetpack\Extensions\AiAssistantPlugin\register_ai_agents_setting
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\Extensions\AiAssistantPlugin\add_ai_agents_sync_options_whitelist
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Block Editor - AI Assistant plugin feature.
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Extensions\AiAssistantPlugin;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\Status;
12use Automattic\Jetpack\Status\Host;
13
14// Feature name.
15const FEATURE_NAME = 'ai-assistant-plugin';
16
17// Required directly rather than relying on the plugin bootstrap: on
18// WordPress.com Simple the extension files load through wpcom's own loader
19// and load-jetpack.php never runs.
20require_once __DIR__ . '/../../../_inc/lib/class-jetpack-ai-settings.php';
21
22/**
23 * Register the AI assistant plugin.
24 * The feature is only available on sites
25 * with a working connection to WordPress.com.
26 *
27 * @return void
28 */
29function register_plugin() {
30    // Connection check and the AI master switch. The per-feature switches on
31    // the AI settings page hide sections inside the "Improve with AI" panel
32    // (see extensions/blocks/ai-assistant/ai-assistant.php), not the panel:
33    // it also carries the usage meter and the upgrade prompt. Grouping the
34    // connection term explicitly makes the master gate (host + master via
35    // is_ai_enabled()) effective on WordPress.com Simple — the previous
36    // precedence short-circuited it there.
37    if (
38        (
39            ( new Host() )->is_wpcom_simple()
40            || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() )
41        )
42        && \Jetpack_AI_Settings::is_ai_enabled()
43    ) {
44        // Register AI assistant plugin.
45        \Jetpack_Gutenberg::set_extension_available( FEATURE_NAME );
46    }
47}
48add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' );
49
50// Initialize the AI sidebar (Agents Manager CDN loader + provider registration).
51require_once __DIR__ . '/ai-sidebar/class-jetpack-ai-sidebar.php';
52Jetpack_AI_Sidebar::init();
53
54// Initialize Reader Chat. Must run in both admin and frontend contexts
55// (admin: register_setting exposes the toggle via Search settings;
56// frontend: wp_enqueue_scripts mounts the widget on reader pages).
57// Loading here ensures it runs whenever ai-assistant-plugin does, which
58// is both on block-editor requests and regular admin pages — a strict
59// superset of the Blocks-module-gated path that modules/blocks.php
60// previously used.
61require_once __DIR__ . '/reader-chat/class-jetpack-reader-chat.php';
62Jetpack_Reader_Chat::init();
63
64/**
65 * Register the `jetpack_ai_agents_enabled` site option.
66 *
67 * Backs the AI Agent Access toggle in the Jetpack Search dashboard, which
68 * lets site owners opt in to AI assistants answering reader questions using
69 * their blog's content.
70 *
71 * @since 15.9
72 *
73 * @return void
74 */
75function register_ai_agents_setting() {
76    $show_in_rest = ! ( new Host() )->is_wpcom_simple();
77
78    register_setting(
79        'jetpack_search',
80        'jetpack_ai_agents_enabled',
81        array(
82            'type'              => 'boolean',
83            'description'       => __( 'Whether AI Agent Access is enabled on this site.', 'jetpack' ),
84            'sanitize_callback' => 'rest_sanitize_boolean',
85            'show_in_rest'      => $show_in_rest,
86            'default'           => false,
87        )
88    );
89}
90add_action( 'init', __NAMESPACE__ . '\register_ai_agents_setting' );
91
92/**
93 * Add the AI Agent Access setting to Jetpack Sync's option whitelist.
94 *
95 * Atomic and self-hosted Jetpack sites write `jetpack_ai_agents_enabled`
96 * locally via /wp/v2/settings. Syncing the option keeps connected sites and
97 * the WP.com-hosted ability permission gate aligned.
98 *
99 * @since 15.9
100 *
101 * @param array $options Option names allowed to sync.
102 * @return array Updated option names.
103 */
104function add_ai_agents_sync_options_whitelist( array $options ): array {
105    $options[] = 'jetpack_ai_agents_enabled';
106    return array_values( array_unique( $options ) );
107}
108add_filter( 'jetpack_sync_options_whitelist', __NAMESPACE__ . '\add_ai_agents_sync_options_whitelist' );
109
110// Populate the available extensions with ai-assistant-plugin.
111add_filter(
112    'jetpack_set_available_extensions',
113    function ( $extensions ) {
114        return array_merge(
115            (array) $extensions,
116            array(
117                FEATURE_NAME,
118            )
119        );
120    }
121);