| @@ -9,15 +9,23 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\AIChat; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | -use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 14 | 13 | use Automattic\Jetpack\Search\Module_Control as Search_Module_Control; |
| 15 | 14 | use Automattic\Jetpack\Search\Plan as Search_Plan; |
| 16 | -use Automattic\Jetpack\Status; | |
| 17 | -use Automattic\Jetpack\Status\Host; | |
| 15 | +use Automattic\Jetpack\Search\Search_Blocks; | |
| 18 | 16 | use Jetpack_Gutenberg; |
| 19 | 17 | |
| 18 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | + exit( 0 ); | |
| 20 | +} | |
| 21 | + | |
| 22 | +// Required directly rather than relying on the plugin bootstrap: on | |
| 23 | +// WordPress.com Simple the extension files load through wpcom's own loader | |
| 24 | +// and load-jetpack.php never runs. Without it the is_ai_enabled() master-gate | |
| 25 | +// check below would fatal there. | |
| 26 | +require_once __DIR__ . '/../../../_inc/lib/class-jetpack-ai-settings.php'; | |
| 27 | + | |
| 20 | 28 | /** |
| 21 | 29 | * Registers our block for use in Gutenberg |
| 22 | 30 | * This is done via an action so that we can disable |
| 23 | 31 | * registration if we need to. |
| @@ -22,15 +30,28 @@ | ||
| 22 | 30 | * This is done via an action so that we can disable |
| 23 | 31 | * registration if we need to. |
| 24 | 32 | */ |
| 25 | 33 | function register_block() { |
| 26 | - if ( | |
| 27 | - ( new Host() )->is_wpcom_simple() | |
| 28 | - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) | |
| 29 | - ) { | |
| 30 | - Blocks::jetpack_register_block( | |
| 31 | - __DIR__, | |
| 32 | - array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) | |
| 34 | + if ( ! \Jetpack_AI_Settings::site_is_connected() ) { | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + | |
| 38 | + // The block stays registered while Jetpack AI is off so the render callback | |
| 39 | + // runs and can output nothing. Otherwise WordPress echoes the saved markup, | |
| 40 | + // an empty div, wherever the block sits. | |
| 41 | + Blocks::jetpack_register_block( | |
| 42 | + __DIR__, | |
| 43 | + array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) | |
| 44 | + ); | |
| 45 | + | |
| 46 | + // Registration queues its own "available" mark on this action, so run after it. | |
| 47 | + if ( ! \Jetpack_AI_Settings::is_ai_enabled() ) { | |
| 48 | + add_action( | |
| 49 | + 'jetpack_register_gutenberg_extensions', | |
| 50 | + static function () { | |
| 51 | + Jetpack_Gutenberg::set_extension_unavailable( 'ai-chat', 'ai_disabled' ); | |
| 52 | + }, | |
| 53 | + 20 | |
| 33 | 54 | ); |
| 34 | 55 | } |
| 35 | 56 | } |
| 36 | 57 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| @@ -42,15 +63,21 @@ | ||
| 42 | 63 | * |
| 43 | 64 | * @return string |
| 44 | 65 | */ |
| 45 | 66 | function load_assets( $attr ) { |
| 67 | + // With Jetpack AI off there is nothing to ask, so render nothing rather | |
| 68 | + // than an empty container. | |
| 69 | + if ( ! \Jetpack_AI_Settings::is_ai_enabled() ) { | |
| 70 | + return ''; | |
| 71 | + } | |
| 72 | + | |
| 46 | 73 | /* |
| 47 | 74 | * Enqueue necessary scripts and styles. |
| 48 | 75 | */ |
| 49 | 76 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 50 | 77 | |
| 51 | - $ask_button_label = isset( $attr['askButtonLabel'] ) ? $attr['askButtonLabel'] : __( 'Ask', 'jetpack' ); | |
| 52 | - $placeholder = isset( $attr['placeholder'] ) ? $attr['placeholder'] : __( 'Ask a question about this site.', 'jetpack' ); | |
| 78 | + $ask_button_label = $attr['askButtonLabel'] ?? __( 'Ask', 'jetpack' ); | |
| 79 | + $placeholder = $attr['placeholder'] ?? __( 'Ask a question about this site.', 'jetpack' ); | |
| 53 | 80 | |
| 54 | 81 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 55 | 82 | $blog_id = get_current_blog_id(); |
| 56 | 83 | $type = 'wpcom'; // WPCOM simple sites. |
| @@ -83,15 +110,21 @@ | ||
| 83 | 110 | $search = new Search_Module_Control(); |
| 84 | 111 | $plan = new Search_Plan(); |
| 85 | 112 | $initial_state = array( |
| 86 | 113 | 'jetpackSettings' => array( |
| 114 | + // `module_active` reflects whether the Jetpack Search module is on, which is | |
| 115 | + // what controls site indexing — independent of the chosen front-end experience | |
| 116 | + // (Overlay / Theme / Inline / Embedded). The AI Chat block only needs the | |
| 117 | + // index, so it gates on this rather than on `instant_search_enabled`. | |
| 118 | + 'module_active' => $search->is_active(), | |
| 87 | 119 | 'instant_search_enabled' => $search->is_instant_search_enabled(), |
| 88 | 120 | 'plan_supports_search' => $plan->supports_instant_search(), |
| 121 | + 'supports_paid_search' => Search_Blocks::supports_paid_search(), | |
| 89 | 122 | ), |
| 90 | 123 | ); |
| 91 | 124 | wp_add_inline_script( |
| 92 | 125 | 'jetpack-blocks-editor', |
| 93 | - 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', | |
| 126 | + 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', | |
| 94 | 127 | 'before' |
| 95 | 128 | ); |
| 96 | 129 | } |
| 97 | -add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data' ); | |
| 130 | +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 ); | |