PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
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 +45 -29 12.6.4 → 16.3-a.5 View file →
@@ -9,12 +9,26 @@
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;
13 14 use Automattic\Jetpack\Search\Module_Control as Search_Module_Control;
14 15 use Automattic\Jetpack\Search\Plan as Search_Plan;
16 +use Automattic\Jetpack\Search\Search_Blocks;
17 +use Automattic\Jetpack\Status;
18 +use Automattic\Jetpack\Status\Host;
15 19 use Jetpack_Gutenberg;
16 20
21 +if ( ! defined( 'ABSPATH' ) ) {
22 + exit( 0 );
23 +}
24 +
25 +// Required directly rather than relying on the plugin bootstrap: on
26 +// WordPress.com Simple the extension files load through wpcom's own loader
27 +// and load-jetpack.php never runs. Without it the is_ai_enabled() master-gate
28 +// check below would fatal there.
29 +require_once __DIR__ . '/../../../_inc/lib/class-jetpack-ai-settings.php';
30 +
17 31 /**
18 32 * Registers our block for use in Gutenberg
19 33 * This is done via an action so that we can disable
20 34 * registration if we need to.
@@ -19,34 +33,24 @@
19 33 * This is done via an action so that we can disable
20 34 * registration if we need to.
21 35 */
22 36 function register_block() {
23 - Blocks::jetpack_register_block(
24 - __DIR__,
25 - array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
26 - );
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 + Blocks::jetpack_register_block(
45 + __DIR__,
46 + array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
47 + );
48 + }
27 49 }
28 50 add_action( 'init', __NAMESPACE__ . '\register_block' );
29 51
30 52 /**
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 53 * Jetpack AI Paragraph block registration/dependency declaration.
50 54 *
51 55 * @param array $attr Array containing the Jetpack AI Chat block attributes.
52 56 *
@@ -57,10 +61,10 @@
57 61 * Enqueue necessary scripts and styles.
58 62 */
59 63 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
60 64
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' );
65 + $ask_button_label = $attr['askButtonLabel'] ?? __( 'Ask', 'jetpack' );
66 + $placeholder = $attr['placeholder'] ?? __( 'Ask a question about this site.', 'jetpack' );
63 67
64 68 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
65 69 $blog_id = get_current_blog_id();
66 70 $type = 'wpcom'; // WPCOM simple sites.
@@ -69,14 +73,17 @@
69 73 $type = 'jetpack'; // Self-hosted (includes Atomic)
70 74 }
71 75
72 76 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>',
77 + '<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 78 esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
75 79 esc_attr( $ask_button_label ),
76 80 esc_attr( $blog_id ),
77 81 esc_attr( $type ),
78 - esc_attr( $placeholder )
82 + esc_attr( $placeholder ),
83 + esc_attr( isset( $attr['showCopy'] ) ? ( $attr['showCopy'] ? 1 : 0 ) : 1 ),
84 + esc_attr( isset( $attr['showFeedback'] ) ? ( $attr['showFeedback'] ? 1 : 0 ) : 1 ),
85 + esc_attr( isset( $attr['showSources'] ) ? ( $attr['showSources'] ? 1 : 0 ) : 1 )
79 86 );
80 87 }
81 88
82 89 /**
@@ -82,20 +89,29 @@
82 89 /**
83 90 * Add the initial state for the AI Chat block.
84 91 */
85 92 function add_ai_chat_block_data() {
93 + // Only relevant to the editor right now.
94 + if ( ! is_admin() ) {
95 + return;
96 + }
86 97 $search = new Search_Module_Control();
87 98 $plan = new Search_Plan();
88 99 $initial_state = array(
89 100 'jetpackSettings' => array(
90 - 'search_module_active' => $search->is_active(),
101 + // `module_active` reflects whether the Jetpack Search module is on, which is
102 + // what controls site indexing — independent of the chosen front-end experience
103 + // (Overlay / Theme / Inline / Embedded). The AI Chat block only needs the
104 + // index, so it gates on this rather than on `instant_search_enabled`.
105 + 'module_active' => $search->is_active(),
91 106 'instant_search_enabled' => $search->is_instant_search_enabled(),
92 107 'plan_supports_search' => $plan->supports_instant_search(),
108 + 'supports_paid_search' => Search_Blocks::supports_paid_search(),
93 109 ),
94 110 );
95 111 wp_add_inline_script(
96 112 'jetpack-blocks-editor',
97 - 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
113 + 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
98 114 'before'
99 115 );
100 116 }
101 -add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data' );
117 +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 );