ai-chat.php
102 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\Search\Module_Control as Search_Module_Control; |
| 14 | use Automattic\Jetpack\Search\Plan as Search_Plan; |
| 15 | use Jetpack_Gutenberg; |
| 16 | |
| 17 | /** |
| 18 | * Registers our block for use in Gutenberg |
| 19 | * This is done via an action so that we can disable |
| 20 | * registration if we need to. |
| 21 | */ |
| 22 | function register_block() { |
| 23 | Blocks::jetpack_register_block( |
| 24 | __DIR__, |
| 25 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 26 | ); |
| 27 | } |
| 28 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 29 | |
| 30 | /** |
| 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 | * Jetpack AI Paragraph block registration/dependency declaration. |
| 50 | * |
| 51 | * @param array $attr Array containing the Jetpack AI Chat block attributes. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | function load_assets( $attr ) { |
| 56 | /* |
| 57 | * Enqueue necessary scripts and styles. |
| 58 | */ |
| 59 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 60 | |
| 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' ); |
| 63 | |
| 64 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 65 | $blog_id = get_current_blog_id(); |
| 66 | $type = 'wpcom'; // WPCOM simple sites. |
| 67 | } else { |
| 68 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 69 | $type = 'jetpack'; // Self-hosted (includes Atomic) |
| 70 | } |
| 71 | |
| 72 | 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>', |
| 74 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 75 | esc_attr( $ask_button_label ), |
| 76 | esc_attr( $blog_id ), |
| 77 | esc_attr( $type ), |
| 78 | esc_attr( $placeholder ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Add the initial state for the AI Chat block. |
| 84 | */ |
| 85 | function add_ai_chat_block_data() { |
| 86 | $search = new Search_Module_Control(); |
| 87 | $plan = new Search_Plan(); |
| 88 | $initial_state = array( |
| 89 | 'jetpackSettings' => array( |
| 90 | 'search_module_active' => $search->is_active(), |
| 91 | 'instant_search_enabled' => $search->is_instant_search_enabled(), |
| 92 | 'plan_supports_search' => $plan->supports_instant_search(), |
| 93 | ), |
| 94 | ); |
| 95 | wp_add_inline_script( |
| 96 | 'jetpack-blocks-editor', |
| 97 | 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', |
| 98 | 'before' |
| 99 | ); |
| 100 | } |
| 101 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data' ); |
| 102 |