Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
15 / 21 |
|
100.00% |
1 / 1 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\AiAssistantPlugin\register_plugin | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
5 | |||
| 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 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 | // Populate the available extensions with ai-assistant-plugin. |
| 65 | add_filter( |
| 66 | 'jetpack_set_available_extensions', |
| 67 | function ( $extensions ) { |
| 68 | return array_merge( |
| 69 | (array) $extensions, |
| 70 | array( |
| 71 | FEATURE_NAME, |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | ); |