PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | extensions/blocks/ai-chat/ai-chat.php +88 -6 12.2.3 → 16.3-beta View file →
@@ -9,13 +9,23 @@
9 9
10 10 namespace Automattic\Jetpack\Extensions\AIChat;
11 11
12 12 use Automattic\Jetpack\Blocks;
13 +use Automattic\Jetpack\Search\Module_Control as Search_Module_Control;
14 +use Automattic\Jetpack\Search\Plan as Search_Plan;
15 +use Automattic\Jetpack\Search\Search_Blocks;
13 16 use Jetpack_Gutenberg;
14 17
15 -const FEATURE_NAME = 'ai-chat';
16 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
18 +if ( ! defined( 'ABSPATH' ) ) {
19 + exit( 0 );
20 +}
17 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 +
18 28 /**
19 29 * Registers our block for use in Gutenberg
20 30 * This is done via an action so that we can disable
21 31 * registration if we need to.
@@ -20,12 +30,30 @@
20 30 * This is done via an action so that we can disable
21 31 * registration if we need to.
22 32 */
23 33 function register_block() {
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.
24 41 Blocks::jetpack_register_block(
25 - BLOCK_NAME,
42 + __DIR__,
26 43 array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
27 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
54 + );
55 + }
28 56 }
29 57 add_action( 'init', __NAMESPACE__ . '\register_block' );
30 58
31 59 /**
@@ -35,14 +63,68 @@
35 63 *
36 64 * @return string
37 65 */
38 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 +
39 73 /*
40 74 * Enqueue necessary scripts and styles.
41 75 */
42 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
76 + Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
43 77
78 + $ask_button_label = $attr['askButtonLabel'] ?? __( 'Ask', 'jetpack' );
79 + $placeholder = $attr['placeholder'] ?? __( 'Ask a question about this site.', 'jetpack' );
80 +
81 + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
82 + $blog_id = get_current_blog_id();
83 + $type = 'wpcom'; // WPCOM simple sites.
84 + } else {
85 + $blog_id = \Jetpack_Options::get_option( 'id' );
86 + $type = 'jetpack'; // Self-hosted (includes Atomic)
87 + }
88 +
44 89 return sprintf(
45 - '<div class="%1$s" id="jetpack-ai-chat"></div>',
46 - esc_attr( Blocks::classes( FEATURE_NAME, $attr ) )
90 + '<div class="%1$s" data-ask-button-label="%2$s" id="jetpack-ai-chat" data-blog-id="%3$d" data-blog-type="%4$s" data-placeholder="%5$s" data-show-copy="%6$d" data-show-feedback="%7$d" data-show-sources="%8$d"></div>',
91 + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
92 + esc_attr( $ask_button_label ),
93 + esc_attr( $blog_id ),
94 + esc_attr( $type ),
95 + esc_attr( $placeholder ),
96 + esc_attr( isset( $attr['showCopy'] ) ? ( $attr['showCopy'] ? 1 : 0 ) : 1 ),
97 + esc_attr( isset( $attr['showFeedback'] ) ? ( $attr['showFeedback'] ? 1 : 0 ) : 1 ),
98 + esc_attr( isset( $attr['showSources'] ) ? ( $attr['showSources'] ? 1 : 0 ) : 1 )
47 99 );
48 100 }
101 +
102 +/**
103 + * Add the initial state for the AI Chat block.
104 + */
105 +function add_ai_chat_block_data() {
106 + // Only relevant to the editor right now.
107 + if ( ! is_admin() ) {
108 + return;
109 + }
110 + $search = new Search_Module_Control();
111 + $plan = new Search_Plan();
112 + $initial_state = array(
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(),
119 + 'instant_search_enabled' => $search->is_instant_search_enabled(),
120 + 'plan_supports_search' => $plan->supports_instant_search(),
121 + 'supports_paid_search' => Search_Blocks::supports_paid_search(),
122 + ),
123 + );
124 + wp_add_inline_script(
125 + 'jetpack-blocks-editor',
126 + 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
127 + 'before'
128 + );
129 +}
130 +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 );