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