classic-search
8 months ago
customberg
8 months ago
customizer
3 months ago
dashboard
2 months ago
initializers
2 months ago
inline-search
3 weeks ago
instant-search
2 months ago
search-blocks
3 weeks ago
widgets
2 months ago
wpes
9 months ago
class-ai-answers.php
3 months ago
class-cli.php
3 months ago
class-helper.php
1 month ago
class-module-control.php
2 months ago
class-options.php
3 months ago
class-package.php
3 weeks ago
class-plan.php
9 months ago
class-rest-controller.php
2 months ago
class-settings.php
3 months ago
class-stats.php
9 months ago
class-template-tags.php
9 months ago
class-ai-answers.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AI Answers feature — behavior meta and enabled flag. |
| 4 | * |
| 5 | * @package automattic/jetpack-search |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Search; |
| 9 | |
| 10 | /** |
| 11 | * Registers behavior meta on the Gutenberg Guidelines CPT and exposes the |
| 12 | * jetpack_search_ai_answers_enabled option. |
| 13 | */ |
| 14 | class AI_Answers { |
| 15 | const BEHAVIOR_META_KEY = '_guideline_block_jetpack_search-ai-summary'; |
| 16 | const BEHAVIOR_OPTION_KEY = 'jetpack_search_ai_behavior_instructions'; |
| 17 | |
| 18 | /** |
| 19 | * Hook up meta/setting registration. |
| 20 | */ |
| 21 | public function init() { |
| 22 | add_action( 'rest_api_init', array( $this, 'register_behavior_meta' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register the behavior instructions storage for the REST API. |
| 27 | * |
| 28 | * When the Gutenberg Guidelines CPT is present, registers the block-specific |
| 29 | * meta key on it. Otherwise registers a site option exposed via /wp/v2/settings. |
| 30 | */ |
| 31 | public function register_behavior_meta() { |
| 32 | if ( post_type_exists( 'wp_guideline' ) ) { |
| 33 | register_post_meta( |
| 34 | 'wp_guideline', |
| 35 | self::BEHAVIOR_META_KEY, |
| 36 | array( |
| 37 | 'single' => true, |
| 38 | 'type' => 'string', |
| 39 | 'show_in_rest' => true, |
| 40 | 'default' => '', |
| 41 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 42 | 'auth_callback' => function () { |
| 43 | return current_user_can( 'manage_options' ); |
| 44 | }, |
| 45 | ) |
| 46 | ); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | register_setting( |
| 51 | 'options', |
| 52 | self::BEHAVIOR_OPTION_KEY, |
| 53 | array( |
| 54 | 'type' => 'string', |
| 55 | 'default' => '', |
| 56 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 57 | 'show_in_rest' => true, |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Retrieve the behavior instructions. |
| 64 | * |
| 65 | * Reads from the Gutenberg Guidelines CPT when available, otherwise falls |
| 66 | * back to the site option. |
| 67 | * |
| 68 | * @return string Behavior instructions, or empty string if none saved. |
| 69 | */ |
| 70 | public static function get_behavior_instructions() { |
| 71 | if ( post_type_exists( 'wp_guideline' ) ) { |
| 72 | $posts = get_posts( |
| 73 | array( |
| 74 | 'post_type' => 'wp_guideline', |
| 75 | 'posts_per_page' => 1, |
| 76 | 'post_status' => 'publish', |
| 77 | ) |
| 78 | ); |
| 79 | if ( ! empty( $posts ) ) { |
| 80 | $guidelines = get_post_meta( $posts[0]->ID, self::BEHAVIOR_META_KEY, true ); |
| 81 | return is_string( $guidelines ) ? $guidelines : ''; |
| 82 | } |
| 83 | } |
| 84 | return (string) get_option( self::BEHAVIOR_OPTION_KEY, '' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Whether AI Answers is enabled for the current site. |
| 89 | */ |
| 90 | public static function is_enabled() { |
| 91 | return (bool) apply_filters( |
| 92 | 'jetpack_search_ai_answers_enabled', |
| 93 | (bool) get_option( 'jetpack_search_ai_answers_enabled', false ) |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 |