PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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 13.8.3 All 506 releases
← All changes | extensions/blocks/ai-chat/ai-chat.php +78 -9 12.1.3 → 16.3-a.7 View file →
@@ -9,13 +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;
14 +use Automattic\Jetpack\Search\Module_Control as Search_Module_Control;
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;
13 19 use Jetpack_Gutenberg;
14 20
15 -const FEATURE_NAME = 'ai-chat';
16 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
21 +if ( ! defined( 'ABSPATH' ) ) {
22 + exit( 0 );
23 +}
17 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 +
18 31 /**
19 32 * Registers our block for use in Gutenberg
20 33 * This is done via an action so that we can disable
21 34 * registration if we need to.
@@ -20,12 +33,20 @@
20 33 * This is done via an action so that we can disable
21 34 * registration if we need to.
22 35 */
23 36 function register_block() {
24 - Blocks::jetpack_register_block(
25 - BLOCK_NAME,
26 - array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
27 - );
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 + }
28 49 }
29 50 add_action( 'init', __NAMESPACE__ . '\register_block' );
30 51
31 52 /**
@@ -38,11 +59,59 @@
38 59 function load_assets( $attr ) {
39 60 /*
40 61 * Enqueue necessary scripts and styles.
41 62 */
42 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
63 + Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
43 64
65 + $ask_button_label = $attr['askButtonLabel'] ?? __( 'Ask', 'jetpack' );
66 + $placeholder = $attr['placeholder'] ?? __( 'Ask a question about this site.', 'jetpack' );
67 +
68 + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
69 + $blog_id = get_current_blog_id();
70 + $type = 'wpcom'; // WPCOM simple sites.
71 + } else {
72 + $blog_id = \Jetpack_Options::get_option( 'id' );
73 + $type = 'jetpack'; // Self-hosted (includes Atomic)
74 + }
75 +
44 76 return sprintf(
45 - '<div class="%1$s" id="jetpack-ai-chat"></div>',
46 - esc_attr( Blocks::classes( FEATURE_NAME, $attr ) )
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>',
78 + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
79 + esc_attr( $ask_button_label ),
80 + esc_attr( $blog_id ),
81 + esc_attr( $type ),
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 )
47 86 );
48 87 }
88 +
89 +/**
90 + * Add the initial state for the AI Chat block.
91 + */
92 +function add_ai_chat_block_data() {
93 + // Only relevant to the editor right now.
94 + if ( ! is_admin() ) {
95 + return;
96 + }
97 + $search = new Search_Module_Control();
98 + $plan = new Search_Plan();
99 + $initial_state = array(
100 + 'jetpackSettings' => array(
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(),
106 + 'instant_search_enabled' => $search->is_instant_search_enabled(),
107 + 'plan_supports_search' => $plan->supports_instant_search(),
108 + 'supports_paid_search' => Search_Blocks::supports_paid_search(),
109 + ),
110 + );
111 + wp_add_inline_script(
112 + 'jetpack-blocks-editor',
113 + 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
114 + 'before'
115 + );
116 +}
117 +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 );