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