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 +54 -25 12.6.4 → 16.3-beta View file →
@@ -11,10 +11,21 @@
11 11
12 12 use Automattic\Jetpack\Blocks;
13 13 use Automattic\Jetpack\Search\Module_Control as Search_Module_Control;
14 14 use Automattic\Jetpack\Search\Plan as Search_Plan;
15 +use Automattic\Jetpack\Search\Search_Blocks;
15 16 use Jetpack_Gutenberg;
16 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 +
17 28 /**
18 29 * Registers our block for use in Gutenberg
19 30 * This is done via an action so that we can disable
20 31 * registration if we need to.
@@ -19,34 +30,34 @@
19 30 * This is done via an action so that we can disable
20 31 * registration if we need to.
21 32 */
22 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.
23 41 Blocks::jetpack_register_block(
24 42 __DIR__,
25 43 array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
26 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 + }
27 56 }
28 57 add_action( 'init', __NAMESPACE__ . '\register_block' );
29 58
30 59 /**
31 - * Register the setting for the AI prompt override.
32 - */
33 -function register_settings() {
34 - register_setting(
35 - 'general',
36 - 'jetpack_search_ai_prompt_override',
37 - array(
38 - 'type' => 'string',
39 - 'show_in_rest' => true,
40 - 'description' => __( 'Override for the Jetpack AI prompt in the Jetpack AI Search feature.', 'jetpack' ),
41 - 'defualt' => '',
42 - )
43 - );
44 -}
45 -
46 -add_action( 'rest_api_init', __NAMESPACE__ . '\register_settings' );
47 -
48 -/**
49 60 * Jetpack AI Paragraph block registration/dependency declaration.
50 61 *
51 62 * @param array $attr Array containing the Jetpack AI Chat block attributes.
52 63 *
@@ -52,15 +63,21 @@
52 63 *
53 64 * @return string
54 65 */
55 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 +
56 73 /*
57 74 * Enqueue necessary scripts and styles.
58 75 */
59 76 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
60 77
61 - $ask_button_label = isset( $attr['askButtonLabel'] ) ? $attr['askButtonLabel'] : __( 'Ask', 'jetpack' );
62 - $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' );
63 80
64 81 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
65 82 $blog_id = get_current_blog_id();
66 83 $type = 'wpcom'; // WPCOM simple sites.
@@ -69,14 +86,17 @@
69 86 $type = 'jetpack'; // Self-hosted (includes Atomic)
70 87 }
71 88
72 89 return sprintf(
73 - '<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"></div>',
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>',
74 91 esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
75 92 esc_attr( $ask_button_label ),
76 93 esc_attr( $blog_id ),
77 94 esc_attr( $type ),
78 - esc_attr( $placeholder )
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 )
79 99 );
80 100 }
81 101
82 102 /**
@@ -82,20 +102,29 @@
82 102 /**
83 103 * Add the initial state for the AI Chat block.
84 104 */
85 105 function add_ai_chat_block_data() {
106 + // Only relevant to the editor right now.
107 + if ( ! is_admin() ) {
108 + return;
109 + }
86 110 $search = new Search_Module_Control();
87 111 $plan = new Search_Plan();
88 112 $initial_state = array(
89 113 'jetpackSettings' => array(
90 - 'search_module_active' => $search->is_active(),
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(),
91 119 'instant_search_enabled' => $search->is_instant_search_enabled(),
92 120 'plan_supports_search' => $plan->supports_instant_search(),
121 + 'supports_paid_search' => Search_Blocks::supports_paid_search(),
93 122 ),
94 123 );
95 124 wp_add_inline_script(
96 125 'jetpack-blocks-editor',
97 - '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 ) . ';',
98 127 'before'
99 128 );
100 129 }
101 -add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data' );
130 +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 );