| @@ -9,13 +9,11 @@ | ||
| 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 | |
| 20 | 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 21 | 19 | exit( 0 ); |
| @@ -32,18 +30,28 @@ | ||
| 32 | 30 | * This is done via an action so that we can disable |
| 33 | 31 | * registration if we need to. |
| 34 | 32 | */ |
| 35 | 33 | function register_block() { |
| 36 | - if ( | |
| 37 | - ( | |
| 38 | - ( new Host() )->is_wpcom_simple() | |
| 39 | - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) | |
| 40 | - ) | |
| 41 | - && \Jetpack_AI_Settings::is_ai_enabled() | |
| 42 | - ) { | |
| 43 | - Blocks::jetpack_register_block( | |
| 44 | - __DIR__, | |
| 45 | - 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 | |
| 46 | 54 | ); |
| 47 | 55 | } |
| 48 | 56 | } |
| 49 | 57 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| @@ -55,8 +63,14 @@ | ||
| 55 | 63 | * |
| 56 | 64 | * @return string |
| 57 | 65 | */ |
| 58 | 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 | + | |
| 59 | 73 | /* |
| 60 | 74 | * Enqueue necessary scripts and styles. |
| 61 | 75 | */ |
| 62 | 76 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| @@ -103,8 +117,9 @@ | ||
| 103 | 117 | // index, so it gates on this rather than on `instant_search_enabled`. |
| 104 | 118 | 'module_active' => $search->is_active(), |
| 105 | 119 | 'instant_search_enabled' => $search->is_instant_search_enabled(), |
| 106 | 120 | 'plan_supports_search' => $plan->supports_instant_search(), |
| 121 | + 'supports_paid_search' => Search_Blocks::supports_paid_search(), | |
| 107 | 122 | ), |
| 108 | 123 | ); |
| 109 | 124 | wp_add_inline_script( |
| 110 | 125 | 'jetpack-blocks-editor', |