ai-chat.php
98 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 | /** |
| 21 | * Registers our block for use in Gutenberg |
| 22 | * This is done via an action so that we can disable |
| 23 | * registration if we need to. |
| 24 | */ |
| 25 | function register_block() { |
| 26 | if ( |
| 27 | ( new Host() )->is_wpcom_simple() |
| 28 | || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) |
| 29 | ) { |
| 30 | Blocks::jetpack_register_block( |
| 31 | __DIR__, |
| 32 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 33 | ); |
| 34 | } |
| 35 | } |
| 36 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 37 | |
| 38 | /** |
| 39 | * Jetpack AI Paragraph block registration/dependency declaration. |
| 40 | * |
| 41 | * @param array $attr Array containing the Jetpack AI Chat block attributes. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | function load_assets( $attr ) { |
| 46 | /* |
| 47 | * Enqueue necessary scripts and styles. |
| 48 | */ |
| 49 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 50 | |
| 51 | $ask_button_label = isset( $attr['askButtonLabel'] ) ? $attr['askButtonLabel'] : __( 'Ask', 'jetpack' ); |
| 52 | $placeholder = isset( $attr['placeholder'] ) ? $attr['placeholder'] : __( 'Ask a question about this site.', 'jetpack' ); |
| 53 | |
| 54 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 55 | $blog_id = get_current_blog_id(); |
| 56 | $type = 'wpcom'; // WPCOM simple sites. |
| 57 | } else { |
| 58 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 59 | $type = 'jetpack'; // Self-hosted (includes Atomic) |
| 60 | } |
| 61 | |
| 62 | return sprintf( |
| 63 | '<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>', |
| 64 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 65 | esc_attr( $ask_button_label ), |
| 66 | esc_attr( $blog_id ), |
| 67 | esc_attr( $type ), |
| 68 | esc_attr( $placeholder ), |
| 69 | esc_attr( isset( $attr['showCopy'] ) ? ( $attr['showCopy'] ? 1 : 0 ) : 1 ), |
| 70 | esc_attr( isset( $attr['showFeedback'] ) ? ( $attr['showFeedback'] ? 1 : 0 ) : 1 ), |
| 71 | esc_attr( isset( $attr['showSources'] ) ? ( $attr['showSources'] ? 1 : 0 ) : 1 ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add the initial state for the AI Chat block. |
| 77 | */ |
| 78 | function add_ai_chat_block_data() { |
| 79 | // Only relevant to the editor right now. |
| 80 | if ( ! is_admin() ) { |
| 81 | return; |
| 82 | } |
| 83 | $search = new Search_Module_Control(); |
| 84 | $plan = new Search_Plan(); |
| 85 | $initial_state = array( |
| 86 | 'jetpackSettings' => array( |
| 87 | 'instant_search_enabled' => $search->is_instant_search_enabled(), |
| 88 | 'plan_supports_search' => $plan->supports_instant_search(), |
| 89 | ), |
| 90 | ); |
| 91 | wp_add_inline_script( |
| 92 | 'jetpack-blocks-editor', |
| 93 | 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', |
| 94 | 'before' |
| 95 | ); |
| 96 | } |
| 97 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data' ); |
| 98 |