Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.74% covered (danger)
40.74%
110 / 270
57.14% covered (warning)
57.14%
20 / 35
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Ai
40.67% covered (danger)
40.67%
109 / 268
57.14% covered (warning)
57.14%
20 / 35
2231.25
0.00% covered (danger)
0.00%
0 / 1
 get_plugin_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_filename
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_tiers
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
3.19
 get_features_by_tier
3.39% covered (danger)
3.39%
2 / 59
0.00% covered (danger)
0.00%
0 / 1
18.43
 get_current_usage_tier
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
 get_next_usage_tier
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
14.11
 get_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_long_description_by_usage_tier
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
 get_long_description
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_features_by_usage_tier
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
6
 get_features
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_pricing_for_ui_by_usage_tier
15.38% covered (danger)
15.38%
4 / 26
0.00% covered (danger)
0.00%
0 / 1
58.07
 get_pricing_for_ui
22.22% covered (danger)
22.22%
8 / 36
0.00% covered (danger)
0.00%
0 / 1
11.53
 get_wpcom_product_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_wpcom_monthly_product_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_wpcom_bi_yearly_product_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_paid_plan_product_slugs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 is_upgradable
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
12.03
 get_post_checkout_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_post_activation_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_feature_ui_enabled
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
10
 get_manage_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 is_plugin_installed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_plugin_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_active
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 is_activated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_module_active
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_ai_assistant_feature
33.33% covered (danger)
33.33%
4 / 12
0.00% covered (danger)
0.00%
0 / 1
16.67
 are_tier_plans_enabled
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
 is_site_connected
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 extend_plugin_action_links
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 admin_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 add_ai_block
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * AI product
4 *
5 * @package my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack\Products;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\My_Jetpack\Initializer;
12use Automattic\Jetpack\My_Jetpack\Module_Product;
13use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
14use Automattic\Jetpack\Status\Host;
15use WP_Post;
16
17if ( ! defined( 'ABSPATH' ) ) {
18    exit( 0 );
19}
20
21/**
22 * Class responsible for handling the Jetpack AI product
23 */
24class Jetpack_Ai extends Module_Product {
25
26    const CURRENT_TIER_SLUG  = 'free';
27    const UPGRADED_TIER_SLUG = 'upgraded';
28
29    /**
30     * The product slug
31     *
32     * @var string
33     */
34    public static $slug = 'jetpack-ai';
35
36    /**
37     * The Jetpack module name associated with this product
38     *
39     * @var string
40     */
41    public static $module_name = 'ai';
42
43    /**
44     * The category of the product
45     *
46     * @var string
47     */
48    public static $category = 'create';
49
50    /**
51     * Whether this product has a free offering
52     *
53     * @var bool
54     */
55    public static $has_free_offering = true;
56
57    /**
58     * The feature slug that identifies the paid plan
59     *
60     * @var string
61     */
62    public static $feature_identifying_paid_plan = 'ai-assistant';
63
64    /**
65     * Get the plugin slug - ovewrite it and return Jetpack's
66     *
67     * @return ?string
68     */
69    public static function get_plugin_slug() {
70        return self::JETPACK_PLUGIN_SLUG;
71    }
72
73    /**
74     * Get the plugin filename - ovewrite it and return Jetpack's
75     *
76     * @return ?string
77     */
78    public static function get_plugin_filename() {
79        return self::JETPACK_PLUGIN_FILENAME;
80    }
81
82    /**
83     * Get the product name
84     *
85     * @return string
86     */
87    public static function get_name() {
88        return 'AI';
89    }
90
91    /**
92     * Get the product title
93     *
94     * @return string
95     */
96    public static function get_title() {
97        return 'Jetpack AI';
98    }
99
100    /**
101     * Get the product's available tiers
102     *
103     * @return string[] Slugs of the available tiers
104     */
105    public static function get_tiers() {
106        if ( ! self::are_tier_plans_enabled() ) {
107            return parent::get_tiers();
108        }
109
110        return array(
111            self::UPGRADED_TIER_SLUG,
112            self::CURRENT_TIER_SLUG,
113        );
114    }
115
116    /**
117     * Get the internationalized comparison of free vs upgraded features
118     *
119     * @return array[] Protect features comparison
120     */
121    public static function get_features_by_tier() {
122        if ( ! self::are_tier_plans_enabled() ) {
123            return parent::get_features_by_tier();
124        }
125
126        $current_tier        = self::get_current_usage_tier();
127        $current_description = 0 === $current_tier
128            ? __( 'Up to 20 requests', 'jetpack-my-jetpack' )
129            /* translators: number of requests */
130            : sprintf( __( 'Up to %d requests per month', 'jetpack-my-jetpack' ), $current_tier );
131        $next_tier        = self::get_next_usage_tier();
132        $next_description = $next_tier === null
133            ? __( 'Let\'s get in touch', 'jetpack-my-jetpack' )
134            /* translators: number of requests */
135            : sprintf( __( 'Up to %d requests per month', 'jetpack-my-jetpack' ), $next_tier );
136
137        return array(
138            array(
139                'name'  => __( 'Number of requests', 'jetpack-my-jetpack' ),
140                'info'  => array(
141                    'title'   => __( 'Requests', 'jetpack-my-jetpack' ),
142                    'content' => __( 'Increase your monthly request limit. Upgrade now and have the option to further increase your requests with additional upgrades.', 'jetpack-my-jetpack' ),
143                ),
144                'tiers' => array(
145                    self::CURRENT_TIER_SLUG  => array(
146                        'included'    => true,
147                        'description' => $current_description,
148                    ),
149                    self::UPGRADED_TIER_SLUG => array(
150                        'included'    => true,
151                        'description' => $next_description,
152                    ),
153                ),
154            ),
155            array(
156                'name'  => __( 'Generate and edit content', 'jetpack-my-jetpack' ),
157                'tiers' => array(
158                    self::CURRENT_TIER_SLUG  => array( 'included' => true ),
159                    self::UPGRADED_TIER_SLUG => array( 'included' => true ),
160                ),
161            ),
162            array(
163                'name'  => __( 'Build forms from prompts', 'jetpack-my-jetpack' ),
164                'tiers' => array(
165                    self::CURRENT_TIER_SLUG  => array( 'included' => true ),
166                    self::UPGRADED_TIER_SLUG => array( 'included' => true ),
167                ),
168            ),
169            array(
170                'name'  => __( 'Get feedback on posts', 'jetpack-my-jetpack' ),
171                'tiers' => array(
172                    self::CURRENT_TIER_SLUG  => array( 'included' => true ),
173                    self::UPGRADED_TIER_SLUG => array( 'included' => true ),
174                ),
175            ),
176            array(
177                'name'  => __( 'Generate featured images', 'jetpack-my-jetpack' ),
178                'tiers' => array(
179                    self::CURRENT_TIER_SLUG  => array( 'included' => true ),
180                    self::UPGRADED_TIER_SLUG => array( 'included' => true ),
181                ),
182            ),
183        );
184    }
185
186    /**
187     * Get the current usage tier
188     *
189     * @return int
190     */
191    public static function get_current_usage_tier() {
192        if ( ! self::is_site_connected() ) {
193            return 0;
194        }
195
196        $info = self::get_ai_assistant_feature();
197
198        // Bail early if it's not possible to fetch the feature data.
199        if ( is_wp_error( $info ) ) {
200            return 0;
201        }
202
203        $current_tier = $info['current-tier']['value'] ?? null;
204
205        return $current_tier;
206    }
207
208    /**
209     * Get the next usage tier
210     *
211     * @return int
212     */
213    public static function get_next_usage_tier() {
214        if ( ! self::is_site_connected() || ! self::has_paid_plan_for_product() ) {
215            // without site connection we can't know if tiers are enabled or not,
216            // hence we can't know if the next tier is 100 or 1 (unlimited).
217            return 100;
218        }
219
220        $info = self::get_ai_assistant_feature();
221
222        // Bail early if it's not possible to fetch the feature data or if it's included in a plan.
223        if ( is_wp_error( $info ) || empty( $info ) ) {
224            return null;
225        }
226
227        // Trust the next tier provided by the feature data.
228        $next_tier = $info['next-tier']['value'] ?? null;
229
230        return $next_tier;
231    }
232
233    /**
234     * Get the internationalized product description
235     *
236     * @return string
237     */
238    public static function get_description() {
239        return __( 'Turn your ideas into ready‑to‑publish content at lightspeed.', 'jetpack-my-jetpack' );
240    }
241
242    /**
243     * Get the internationalized usage tier long description by tier
244     *
245     * @param int|null $tier The usage tier.
246     * @return string
247     */
248    public static function get_long_description_by_usage_tier( $tier ) {
249        switch ( (int) $tier ) {
250            case 1:
251                return __( 'Jetpack AI Assistant brings the power of AI right into your WordPress editor, letting your content creation soar to new heights.', 'jetpack-my-jetpack' );
252            case 100:
253                return __( 'The most advanced AI technology Jetpack has to offer.', 'jetpack-my-jetpack' );
254            default:
255                return __( 'Upgrade and increase the amount of your available monthly requests to continue using the most advanced AI technology Jetpack has to offer.', 'jetpack-my-jetpack' );
256        }
257    }
258
259    /**
260     * Get the internationalized product long description
261     *
262     * @return string
263     */
264    public static function get_long_description() {
265        $next_tier = self::get_next_usage_tier();
266
267        return self::get_long_description_by_usage_tier( $next_tier );
268    }
269
270    /**
271     * Get the internationalized usage tier features by tier
272     *
273     * @param int $tier The usage tier.
274     * @return string
275     */
276    public static function get_features_by_usage_tier( $tier ) {
277        $is_tier_plan = $tier && intval( $tier ) > 1;
278
279        if ( $tier === 100 && ( ! self::is_site_connected() || ! self::has_paid_plan_for_product() ) ) {
280            // in these cases, get_next_usage_tier() will return 100
281            // 100 is fine as default when tiered plans are enabled, but not otherwise
282            $is_tier_plan = false;
283        }
284
285        $features = array(
286            __( 'High request capacity *', 'jetpack-my-jetpack' ),
287            __( 'Generate text, tables, lists, and forms', 'jetpack-my-jetpack' ),
288            __( 'Easily refine content to your liking', 'jetpack-my-jetpack' ),
289            __( 'Make your content easier to read', 'jetpack-my-jetpack' ),
290            __( 'Generate images with one-click', 'jetpack-my-jetpack' ),
291            __( 'Optimize your titles for better performance', 'jetpack-my-jetpack' ),
292            __( 'Priority support', 'jetpack-my-jetpack' ),
293        );
294
295        $tiered_features = array(
296            __( 'Prompt based content generation', 'jetpack-my-jetpack' ),
297            __( 'Generate text, tables, and lists', 'jetpack-my-jetpack' ),
298            __( 'Adaptive tone adjustment', 'jetpack-my-jetpack' ),
299            __( 'Superior spelling and grammar correction', 'jetpack-my-jetpack' ),
300            __( 'Title & summary generation', 'jetpack-my-jetpack' ),
301            __( 'Priority support', 'jetpack-my-jetpack' ),
302            /* translators: %d is the number of requests. */
303            sprintf( __( 'Up to %d requests per month', 'jetpack-my-jetpack' ), $tier ),
304        );
305
306        return $is_tier_plan ? $tiered_features : $features;
307    }
308
309    /**
310     * Get the internationalized features list
311     *
312     * @return array Jetpack AI features list
313     */
314    public static function get_features() {
315        $next_tier = self::get_next_usage_tier();
316
317        return self::get_features_by_usage_tier( $next_tier );
318    }
319
320    /**
321     * Get the product pricing details by tier
322     *
323     * @param int|null $tier The usage tier.
324     * @return array Pricing details
325     */
326    public static function get_pricing_for_ui_by_usage_tier( $tier ) {
327        if ( $tier === null ) {
328            return array();
329        }
330
331        $product = Wpcom_Products::get_product( static::get_wpcom_product_slug() );
332
333        if ( empty( $product ) ) {
334            return array();
335        }
336
337        $tier_plans_enabled = self::are_tier_plans_enabled();
338
339        /*
340         * when tiers are enabled and the price tier list is empty,
341         * we may need to renew the cache for the product data so
342         * we get the new price tier list.
343         *
344         * if the list is still empty after the fresh data, we will
345         * default to empty pricing (by returning an empty array).
346         */
347        if ( empty( $product->price_tier_list ) && $tier_plans_enabled ) {
348            $product = Wpcom_Products::get_product( static::get_wpcom_product_slug(), true );
349        }
350
351        // get the base pricing for the unlimited plan, for compatibility
352        $base_pricing = Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() );
353
354        $price_tier_list = $product->price_tier_list;
355        $yearly_prices   = array();
356
357        foreach ( $price_tier_list as $price_tier ) {
358            if ( isset( $price_tier->maximum_units ) && isset( $price_tier->maximum_price ) ) {
359                    // The prices are in cents
360                    $yearly_prices[ $price_tier->maximum_units ] = $price_tier->maximum_price / 100;
361            }
362        }
363
364        // add the base pricing to the list
365        $prices = array( 1 => $base_pricing );
366
367        foreach ( $yearly_prices as $units => $price ) {
368            $prices[ $units ] = array_merge(
369                $base_pricing,
370                array(
371                    'full_price'            => $price,
372                    'discount_price'        => $price,
373                    'is_introductory_offer' => false,
374                    'introductory_offer'    => null,
375                )
376            );
377        }
378
379        return $prices[ $tier ] ?? array();
380    }
381
382    /**
383     * Get the product pricing details
384     *
385     * @return array Pricing details
386     */
387    public static function get_pricing_for_ui() {
388        // no tiers
389        if ( ! self::are_tier_plans_enabled() ) {
390            return array_merge(
391                array(
392                    'available'          => true,
393                    'wpcom_product_slug' => static::get_wpcom_product_slug(),
394                ),
395                // hardcoding 1 as next tier if tiers are not enabled
396                self::get_pricing_for_ui_by_usage_tier( 1 )
397            );
398        }
399
400        $next_tier              = self::get_next_usage_tier();
401        $current_tier           = self::get_current_usage_tier();
402        $current_call_to_action = $current_tier === 0
403            ? __( 'Continue for free', 'jetpack-my-jetpack' )
404            : __( 'I\'m fine with my plan, thanks', 'jetpack-my-jetpack' );
405        $next_call_to_action    = $next_tier === null
406            ? __( 'Contact Us', 'jetpack-my-jetpack' )
407            : __( 'Upgrade', 'jetpack-my-jetpack' );
408
409        return array(
410            'tiers' => array(
411                self::CURRENT_TIER_SLUG  => array_merge(
412                    self::get_pricing_for_ui_by_usage_tier( $current_tier ),
413                    array(
414                        'available'      => true,
415                        'is_free'        => true,
416                        'call_to_action' => $current_call_to_action,
417                    )
418                ),
419                self::UPGRADED_TIER_SLUG => array_merge(
420                    self::get_pricing_for_ui_by_usage_tier( $next_tier ),
421                    array(
422                        'wpcom_product_slug' => static::get_wpcom_product_slug(),
423                        'quantity'           => $next_tier,
424                        'call_to_action'     => $next_call_to_action,
425                    )
426                ),
427            ),
428        );
429    }
430
431    /**
432     * Get the WPCOM product slug used to make the purchase
433     *
434     * @return string
435     */
436    public static function get_wpcom_product_slug() {
437        return 'jetpack_ai_yearly';
438    }
439
440    /**
441     * Get the WPCOM monthly product slug used to make the purchase
442     *
443     * @return string
444     */
445    public static function get_wpcom_monthly_product_slug() {
446        return 'jetpack_ai_monthly';
447    }
448
449    /**
450     * Get the WPCOM bi-yearly product slug used to make the purchase
451     *
452     * @return string
453     */
454    public static function get_wpcom_bi_yearly_product_slug() {
455        return 'jetpack_ai_bi_yearly';
456    }
457
458    /**
459     * Get the product-slugs of the paid plans for this product.
460     * (Do not include bundle plans, unless it's a bundle plan itself).
461     *
462     * @return array
463     */
464    public static function get_paid_plan_product_slugs() {
465        return array(
466            'jetpack_ai_yearly',
467            'jetpack_ai_monthly',
468            'jetpack_ai_bi_yearly',
469        );
470    }
471
472    /**
473     * Checks whether the product can be upgraded to a different product.
474     *
475     * @return boolean
476     */
477    public static function is_upgradable() {
478        $has_ai_feature     = static::does_site_have_feature( 'ai-assistant' );
479        $tier_plans_enabled = self::are_tier_plans_enabled();
480        $current_tier       = self::get_current_usage_tier();
481
482        if ( $has_ai_feature && ! $tier_plans_enabled && $current_tier >= 1 ) {
483            return false;
484        }
485
486        $next_tier = self::get_next_usage_tier();
487
488        // The check below is debatable, not having the feature should not flag as not upgradable.
489        // If user is free (tier = 0), not unlimited (tier = 1) and has a next tier, then it's upgradable.
490        if ( $current_tier !== null && $current_tier !== 1 && $next_tier ) {
491            return true;
492        }
493
494        // Mark as not upgradable if user is on unlimited tier or does not have any plan.
495        if ( ! $has_ai_feature || null === $current_tier || 1 === $current_tier ) {
496            return false;
497        }
498
499        return true;
500    }
501
502    /**
503     * Get the URL the user is taken after purchasing the product through the checkout
504     *
505     * @return ?string
506     */
507    public static function get_post_checkout_url() {
508        return self::get_manage_url();
509    }
510
511    /**
512     * Get the URL the user is taken after activating the product through the checkout
513     *
514     * @return ?string
515     */
516    public static function get_post_activation_url() {
517        return self::get_manage_url();
518    }
519
520    /**
521     * Whether My Jetpack should surface the AI feature controls.
522     *
523     * @return bool
524     */
525    public static function is_feature_ui_enabled() {
526        if (
527            ! self::is_plugin_active()
528            || ! method_exists( '\\Jetpack', 'is_module' )
529            || ! \Jetpack::is_module( 'ai' )
530            // @phan-suppress-next-line PhanUndeclaredClassReference -- Supplied by the optional Jetpack plugin.
531            || ! method_exists( '\\Jetpack_AI_Settings', 'is_feature_enabled' )
532        ) {
533            return false;
534        }
535
536        $connection = new Connection_Manager();
537        if ( ! $connection->is_connected() || ! $connection->has_connected_owner() ) {
538            return false;
539        }
540
541        $host = new Host();
542        if ( ! $host->is_wpcom_platform() ) {
543            return true;
544        }
545
546        return $host->is_woa_site()
547            && function_exists( 'jetpack_is_internal_testing_environment' )
548            && jetpack_is_internal_testing_environment();
549    }
550
551    /**
552     * Get the URL where the user manages the product.
553     *
554     * @return ?string
555     */
556    public static function get_manage_url() {
557        if ( self::is_feature_ui_enabled() ) {
558            return admin_url( 'admin.php?page=jetpack-ai' );
559        }
560
561        return admin_url( 'admin.php?page=my-jetpack#/jetpack-ai' );
562    }
563
564    /**
565     * Checks whether the plugin is installed
566     *
567     * @return boolean
568     */
569    public static function is_plugin_installed() {
570        return self::is_jetpack_plugin_installed();
571    }
572
573    /**
574     * Checks whether the plugin is active
575     *
576     * @return boolean
577     */
578    public static function is_plugin_active() {
579        return (bool) static::is_jetpack_plugin_active();
580    }
581
582    /**
583     * Checks whether the Product is active
584     *
585     * Overrides Module_Product::is_active() to also respect the jetpack_ai_enabled
586     * filter. The parent already checks that the Jetpack plugin and the 'ai' module
587     * are active; this override layers the host/master-off signal on top so the
588     * product card reflects it.
589     *
590     * @return boolean
591     */
592    public static function is_active() {
593        /**
594         * Filter to enable/disable Jetpack AI.
595         *
596         * @since 5.28.3
597         *
598         * @param boolean $enabled True if Jetpack AI should be enabled, false otherwise. Default true.
599         */
600        $is_enabled = apply_filters( 'jetpack_ai_enabled', true );
601
602        return $is_enabled && parent::is_active();
603    }
604
605    /**
606     * Checks whether the site has switched the product on, respecting the jetpack_ai_enabled filter.
607     *
608     * @return boolean
609     */
610    public static function is_activated() {
611        /** This filter is documented in projects/packages/my-jetpack/src/products/class-jetpack-ai.php */
612        return apply_filters( 'jetpack_ai_enabled', true ) && parent::is_activated();
613    }
614
615    /**
616     * Whether the 'ai' module backs this product's UI state.
617     *
618     * @return bool
619     */
620    public static function is_module_active() {
621        if ( ! self::is_feature_ui_enabled() ) {
622            return true;
623        }
624
625        return parent::is_module_active();
626    }
627
628    /**
629     * Get data about the AI Assistant feature
630     *
631     * @return array
632     */
633    public static function get_ai_assistant_feature() {
634        // Bail early if the plugin is not active.
635        if ( ! self::is_jetpack_plugin_installed() ) {
636            return array();
637        }
638
639        // Check if the global constant is defined.
640        if ( ! defined( 'JETPACK__PLUGIN_DIR' ) ) {
641            return array();
642        }
643
644        // Bail early if the site is not connected.
645        if ( ! self::is_site_connected() ) {
646            return array();
647        }
648
649        // Check if class exists. If not, try to require it once.
650        if ( ! class_exists( 'Jetpack_AI_Helper' ) ) {
651            $class_file_path = JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php';
652
653            // Check whether the file exists
654            if ( ! file_exists( $class_file_path ) ) {
655                return array();
656            }
657
658            require_once $class_file_path;
659        }
660
661        return \Jetpack_AI_Helper::get_ai_assistance_feature();
662    }
663
664    /**
665     * Get the AI Assistant tiered plans status
666     *
667     * @return boolean
668     */
669    public static function are_tier_plans_enabled() {
670        $info = self::get_ai_assistant_feature();
671        if ( is_wp_error( $info ) ) {
672            // this is another faulty default value, we'll assume disabled while
673            // production is enabled
674            return false;
675        }
676
677        if ( ! empty( $info ) && isset( $info['tier-plans-enabled'] ) ) {
678            return boolval( $info['tier-plans-enabled'] );
679        }
680        return false;
681    }
682
683    /**
684     * Checks whether the site is connected to WordPress.com.
685     *
686     * @return boolean
687     */
688    private static function is_site_connected() {
689        return ( new Connection_Manager() )->is_connected();
690    }
691
692    /**
693     * Get the URL where the user manages the product
694     *
695     * NOTE: this method is the only thing that resembles an initialization for the product.
696     *
697     * @return void
698     */
699    public static function extend_plugin_action_links() {
700        add_action( 'myjetpack_enqueue_scripts', array( static::class, 'admin_enqueue_scripts' ) );
701        add_filter( 'default_content', array( static::class, 'add_ai_block' ), 10, 2 );
702    }
703
704    /**
705     * Enqueue the AI Assistant script
706     *
707     * The script is just a global variable used for the nonce, needed for the create post link.
708     *
709     * @return void
710     */
711    public static function admin_enqueue_scripts() {
712        wp_register_script(
713            'my_jetpack_ai_app',
714            false,
715            array(),
716            Initializer::PACKAGE_VERSION,
717            array( 'in_footer' => true )
718        );
719        wp_localize_script(
720            'my_jetpack_ai_app',
721            'jetpackAi',
722            array(
723                'nonce' => wp_create_nonce( 'ai-assistant-content-nonce' ),
724            )
725        );
726        wp_enqueue_script( 'my_jetpack_ai_app' );
727    }
728
729    /**
730     * Add AI block to the post content
731     *
732     * Used only from the link on the product page, the filter will insert an AI Assistant block in the post content.
733     *
734     * @param string  $content The post content.
735     * @param WP_Post $post The post object.
736     * @return string
737     */
738    public static function add_ai_block( $content, $post ) {
739        if ( isset( $_GET['use_ai_block'] ) && isset( $_GET['_wpnonce'] )
740            && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'ai-assistant-content-nonce' )
741            && ! empty( $post )
742            && ! is_wp_error( $post )
743            && current_user_can( 'edit_post', $post->ID )
744            && '' === $content
745        ) {
746            return '<!-- wp:jetpack/ai-assistant /-->';
747        }
748        return $content;
749    }
750}