Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.58% covered (warning)
81.58%
31 / 38
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%
6 / 6
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/**
18 * Register the AI assistant plugin.
19 * The feature is only available on sites
20 * with a working connection to WordPress.com.
21 *
22 * @return void
23 */
24function register_plugin() {
25    // Check Jetpack AI feature availability.
26    if (
27        (
28            new Host() )->is_wpcom_simple()
29            || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode()
30        )
31        && apply_filters( 'jetpack_ai_enabled', true )
32    ) {
33        // Register AI assistant plugin.
34        \Jetpack_Gutenberg::set_extension_available( FEATURE_NAME );
35    }
36}
37add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' );
38
39// Initialize the AI sidebar (Agents Manager CDN loader + provider registration).
40require_once __DIR__ . '/ai-sidebar/class-jetpack-ai-sidebar.php';
41Jetpack_AI_Sidebar::init();
42
43// Initialize Reader Chat. Must run in both admin and frontend contexts
44// (admin: register_setting exposes the toggle via Search settings;
45// frontend: wp_enqueue_scripts mounts the widget on reader pages).
46// Loading here ensures it runs whenever ai-assistant-plugin does, which
47// is both on block-editor requests and regular admin pages â€” a strict
48// superset of the Blocks-module-gated path that modules/blocks.php
49// previously used.
50require_once __DIR__ . '/reader-chat/class-jetpack-reader-chat.php';
51Jetpack_Reader_Chat::init();
52
53/**
54 * Register the `jetpack_ai_agents_enabled` site option.
55 *
56 * Backs the AI Agent Access toggle in the Jetpack Search dashboard, which
57 * lets site owners opt in to AI assistants answering reader questions using
58 * their blog's content.
59 *
60 * @since 15.9
61 *
62 * @return void
63 */
64function register_ai_agents_setting() {
65    $show_in_rest = ! ( new Host() )->is_wpcom_simple();
66
67    register_setting(
68        'general',
69        'jetpack_ai_agents_enabled',
70        array(
71            'type'              => 'boolean',
72            'description'       => __( 'Whether AI Agent Access is enabled on this site.', 'jetpack' ),
73            'sanitize_callback' => 'rest_sanitize_boolean',
74            'show_in_rest'      => $show_in_rest,
75            'default'           => false,
76        )
77    );
78}
79add_action( 'init', __NAMESPACE__ . '\register_ai_agents_setting' );
80
81/**
82 * Add the AI Agent Access setting to Jetpack Sync's option whitelist.
83 *
84 * Atomic and self-hosted Jetpack sites write `jetpack_ai_agents_enabled`
85 * locally via /wp/v2/settings. Syncing the option keeps connected sites and
86 * the WP.com-hosted ability permission gate aligned.
87 *
88 * @since 15.9
89 *
90 * @param array $options Option names allowed to sync.
91 * @return array Updated option names.
92 */
93function add_ai_agents_sync_options_whitelist( array $options ): array {
94    $options[] = 'jetpack_ai_agents_enabled';
95    return array_values( array_unique( $options ) );
96}
97add_filter( 'jetpack_sync_options_whitelist', __NAMESPACE__ . '\add_ai_agents_sync_options_whitelist' );
98
99// Populate the available extensions with ai-assistant-plugin.
100add_filter(
101    'jetpack_set_available_extensions',
102    function ( $extensions ) {
103        return array_merge(
104            (array) $extensions,
105            array(
106                FEATURE_NAME,
107            )
108        );
109    }
110);