Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\AIChat\register_block | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| Automattic\Jetpack\Extensions\AIChat\load_assets | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
90 | |||
| Automattic\Jetpack\Extensions\AIChat\add_ai_chat_block_data | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | /** |
| 25 | * Registers our block for use in Gutenberg |
| 26 | * This is done via an action so that we can disable |
| 27 | * registration if we need to. |
| 28 | */ |
| 29 | function register_block() { |
| 30 | if ( |
| 31 | ( new Host() )->is_wpcom_simple() |
| 32 | || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) |
| 33 | ) { |
| 34 | Blocks::jetpack_register_block( |
| 35 | __DIR__, |
| 36 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 37 | ); |
| 38 | } |
| 39 | } |
| 40 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 41 | |
| 42 | /** |
| 43 | * Jetpack AI Paragraph block registration/dependency declaration. |
| 44 | * |
| 45 | * @param array $attr Array containing the Jetpack AI Chat block attributes. |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | function load_assets( $attr ) { |
| 50 | /* |
| 51 | * Enqueue necessary scripts and styles. |
| 52 | */ |
| 53 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 54 | |
| 55 | $ask_button_label = $attr['askButtonLabel'] ?? __( 'Ask', 'jetpack' ); |
| 56 | $placeholder = $attr['placeholder'] ?? __( 'Ask a question about this site.', 'jetpack' ); |
| 57 | |
| 58 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 59 | $blog_id = get_current_blog_id(); |
| 60 | $type = 'wpcom'; // WPCOM simple sites. |
| 61 | } else { |
| 62 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 63 | $type = 'jetpack'; // Self-hosted (includes Atomic) |
| 64 | } |
| 65 | |
| 66 | return sprintf( |
| 67 | '<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>', |
| 68 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 69 | esc_attr( $ask_button_label ), |
| 70 | esc_attr( $blog_id ), |
| 71 | esc_attr( $type ), |
| 72 | esc_attr( $placeholder ), |
| 73 | esc_attr( isset( $attr['showCopy'] ) ? ( $attr['showCopy'] ? 1 : 0 ) : 1 ), |
| 74 | esc_attr( isset( $attr['showFeedback'] ) ? ( $attr['showFeedback'] ? 1 : 0 ) : 1 ), |
| 75 | esc_attr( isset( $attr['showSources'] ) ? ( $attr['showSources'] ? 1 : 0 ) : 1 ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add the initial state for the AI Chat block. |
| 81 | */ |
| 82 | function add_ai_chat_block_data() { |
| 83 | // Only relevant to the editor right now. |
| 84 | if ( ! is_admin() ) { |
| 85 | return; |
| 86 | } |
| 87 | $search = new Search_Module_Control(); |
| 88 | $plan = new Search_Plan(); |
| 89 | $initial_state = array( |
| 90 | 'jetpackSettings' => array( |
| 91 | // `module_active` reflects whether the Jetpack Search module is on, which is |
| 92 | // what controls site indexing — independent of the chosen front-end experience |
| 93 | // (Overlay / Theme / Inline / Embedded). The AI Chat block only needs the |
| 94 | // index, so it gates on this rather than on `instant_search_enabled`. |
| 95 | 'module_active' => $search->is_active(), |
| 96 | 'instant_search_enabled' => $search->is_instant_search_enabled(), |
| 97 | 'plan_supports_search' => $plan->supports_instant_search(), |
| 98 | ), |
| 99 | ); |
| 100 | wp_add_inline_script( |
| 101 | 'jetpack-blocks-editor', |
| 102 | 'var Jetpack_AIChatBlock = ' . wp_json_encode( $initial_state, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', |
| 103 | 'before' |
| 104 | ); |
| 105 | } |
| 106 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_ai_chat_block_data', 11 ); |