Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.35% covered (success)
95.35%
41 / 43
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AI_Answers
95.35% covered (success)
95.35%
41 / 43
50.00% covered (danger)
50.00%
2 / 4
8
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_behavior_meta
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 get_behavior_instructions
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 is_enabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * AI Answers feature — behavior meta and enabled flag.
4 *
5 * @package automattic/jetpack-search
6 */
7
8namespace 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 */
14class 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}