← All changes
|
extensions/plugins/ai-assistant-plugin/ai-assistant-plugin.php
+98
-9
12.5.2
→
16.3-a.1
View file →
| @@ -6,27 +6,116 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Extensions\AiAssistantPlugin; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 11 | +use Automattic\Jetpack\Status; | |
| 12 | +use Automattic\Jetpack\Status\Host; | |
| 13 | + | |
| 10 | 14 | // Feature name. |
| 11 | 15 | const FEATURE_NAME = 'ai-assistant-plugin'; |
| 12 | 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 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 | +} | |
| 48 | +add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' ); | |
| 49 | + | |
| 50 | +// Initialize the AI sidebar (Agents Manager CDN loader + provider registration). | |
| 51 | +require_once __DIR__ . '/ai-sidebar/class-jetpack-ai-sidebar.php'; | |
| 52 | +Jetpack_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. | |
| 61 | +require_once __DIR__ . '/reader-chat/class-jetpack-reader-chat.php'; | |
| 62 | +Jetpack_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 | + */ | |
| 75 | +function 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 | +} | |
| 90 | +add_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 | + */ | |
| 104 | +function add_ai_agents_sync_options_whitelist( array $options ): array { | |
| 105 | + $options[] = 'jetpack_ai_agents_enabled'; | |
| 106 | + return array_values( array_unique( $options ) ); | |
| 107 | +} | |
| 108 | +add_filter( 'jetpack_sync_options_whitelist', __NAMESPACE__ . '\add_ai_agents_sync_options_whitelist' ); | |
| 109 | + | |
| 13 | 110 | // Populate the available extensions with ai-assistant-plugin. |
| 14 | 111 | add_filter( |
| 15 | 112 | 'jetpack_set_available_extensions', |
| 16 | 113 | function ( $extensions ) { |
| 17 | 114 | return array_merge( |
| 18 | - $extensions, | |
| 115 | + (array) $extensions, | |
| 19 | 116 | array( |
| 20 | 117 | FEATURE_NAME, |
| 21 | 118 | ) |
| 22 | 119 | ); |
| 23 | - } | |
| 24 | -); | |
| 25 | - | |
| 26 | -// Set the ai-assistant-plugin availability, depending on the site plan. | |
| 27 | -add_action( | |
| 28 | - 'jetpack_register_gutenberg_extensions', | |
| 29 | - function () { | |
| 30 | - \Jetpack_Gutenberg::set_extension_available( FEATURE_NAME ); | |
| 31 | 120 | } |
| 32 | 121 | ); |