Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.28% covered (warning)
89.28%
308 / 345
71.43% covered (warning)
71.43%
45 / 63
CRAP
0.00% covered (danger)
0.00%
0 / 1
Product
89.28% covered (warning)
89.28%
308 / 345
71.43% covered (warning)
71.43%
45 / 63
230.14
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
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_endpoints
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_ai_assistant_feature
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_wpcom_free_product_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_installed_plugin_filename
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
 get_info
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
2
 get_related_plan_slugs
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 get_wpcom_info
96.55% covered (success)
96.55%
28 / 29
0.00% covered (danger)
0.00%
0 / 1
4
 get_site_features_from_wpcom
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
7
 reset_site_features_cache
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 does_site_have_feature
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 get_name
n/a
0 / 0
n/a
0 / 0
0
 get_title
n/a
0 / 0
n/a
0 / 0
0
 get_description
n/a
0 / 0
n/a
0 / 0
0
 get_long_description
n/a
0 / 0
n/a
0 / 0
0
 get_tiers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_features
n/a
0 / 0
n/a
0 / 0
0
 get_features_by_tier
n/a
0 / 0
n/a
0 / 0
1
 get_pricing_for_ui
n/a
0 / 0
n/a
0 / 0
0
 get_bundle_pricing
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
13
 get_purchase_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_host_purchase_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_manage_url
n/a
0 / 0
n/a
0 / 0
0
 get_manage_urls_by_feature
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
 get_post_checkout_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_post_checkout_urls_by_feature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_wpcom_product_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_disclaimers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_standalone_info
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 has_paid_plan_for_product
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
9
 has_free_plan_for_product
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has_any_plan_for_product
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_paid_plan_product_slugs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_paid_bundles_that_include_product
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
5.01
 get_paid_plan_purchase_for_product
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
 get_paid_plan_expiration_date
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 get_paid_plan_expiration_status
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 is_paid_plan_expired
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 is_paid_plan_expiring
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_manage_paid_plan_purchase_url
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 get_renew_paid_plan_purchase_url
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 has_trial_support
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_upgradable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_bundle_product
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_upgradable_by_bundle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_supported_products
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_owned
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 get_status
81.58% covered (warning)
81.58%
31 / 38
0.00% covered (danger)
0.00%
0 / 1
47.03
 is_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
4
 is_held_off
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_activated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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_jetpack_plugin_installed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_jetpack_plugin_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_module_active
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 get_module_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 activate_plugin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 do_activation
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
7.03
 should_use_standalone_plugin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 activate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 do_product_specific_activation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deactivate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_actions_links
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 filter_action_links
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 extend_plugin_action_links
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 extend_core_plugin_action_links
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 install_and_activate_standalone
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
10.40
 does_module_need_attention
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Base product
4 *
5 * @package my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack;
9
10use Automattic\Jetpack\Connection\Client;
11use Automattic\Jetpack\Connection\Manager as Connection_Manager;
12use Automattic\Jetpack\Modules;
13use Automattic\Jetpack\Plugins_Installer;
14use Automattic\Jetpack\Status;
15use Automattic\Jetpack\Status\Host;
16use Jetpack_Options;
17use WP_Error;
18
19/**
20 * Class responsible for handling the products
21 */
22abstract class Product {
23
24    /**
25     * The product slug
26     *
27     * @var string
28     */
29    public static $slug = null;
30
31    /**
32     * The Jetpack module name, if any.
33     *
34     * @var ?string
35     */
36    public static $module_name = null;
37
38    /**
39     * The filename (id) of the plugin associated with this product. Can be a string with a single value or a list of possible values
40     *
41     * @var string|string[]
42     */
43    public static $plugin_filename = null;
44
45    /**
46     * The slug of the plugin associated with this product. If not defined, it will default to the Jetpack plugin
47     *
48     * @var string
49     */
50    public static $plugin_slug = null;
51
52    /**
53     * The category of the product in the Jetpack ecosystem. The options are performance, growth, security, management, and create
54     *
55     * @var string
56     */
57    public static $category = null;
58
59    /**
60     * The Jetpack plugin slug
61     *
62     * @var string
63     */
64    const JETPACK_PLUGIN_SLUG = 'jetpack';
65
66    /**
67     * The Jetpack plugin filename
68     *
69     * @var array
70     */
71    const JETPACK_PLUGIN_FILENAME = array(
72        'jetpack/jetpack.php',
73        'jetpack-dev/jetpack.php',
74    );
75
76    /**
77     * The duration of time after the plan expiration date that we stop showing the plan status as "expired".
78     *
79     * @var string
80     */
81    const EXPIRATION_CUTOFF_TIME = '+2 months';
82
83    /**
84     * Transient key for storing site features
85     *
86     * @var string;
87     */
88    const MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY = 'my-jetpack-site-features';
89
90    /**
91     * How long, in seconds, a site features lookup is reused before WPCOM is asked again
92     *
93     * @var int
94     */
95    const MY_JETPACK_SITE_FEATURES_CACHE_DURATION = 15;
96
97    /**
98     * A failed site features lookup, kept so one outage does not fire a request per product
99     *
100     * @var WP_Error|null
101     */
102    private static $site_features_failure = null;
103
104    /**
105     * Unix time at which the memoized failure above stops being used
106     *
107     * @var int
108     */
109    private static $site_features_failure_expires = 0;
110
111    /**
112     * The host's answer for each product get_wpcom_info() is describing, wrapped so a null answer counts.
113     *
114     * @var array<string, array{0: ?string}>
115     */
116    private static $host_purchase_urls = array();
117
118    /**
119     * A successful site features lookup, kept so one WPCOM request serves a whole render
120     * even when the transient write does not retain (e.g. an object cache evicting under load)
121     *
122     * @var array|null
123     */
124    private static $site_features_success = null;
125
126    /**
127     * Unix time at which the memoized success above stops being used
128     *
129     * @var int
130     */
131    private static $site_features_success_expires = 0;
132
133    /**
134     * Whether this module is a Jetpack feature
135     *
136     * @var boolean
137     */
138    public static $is_feature = false;
139
140    /**
141     * Whether this product requires a site connection
142     *
143     * @var string
144     */
145    public static $requires_site_connection = true;
146
147    /**
148     * Whether this product requires a user connection
149     *
150     * @var string
151     */
152    public static $requires_user_connection = true;
153
154    /**
155     * Whether this product has a standalone plugin
156     *
157     * @var bool
158     */
159    public static $has_standalone_plugin = false;
160
161    /**
162     * Whether this product has a free offering
163     *
164     * @var bool
165     */
166    public static $has_free_offering = false;
167
168    /**
169     * Whether the product requires a plan to run
170     * The plan could be paid or free
171     *
172     * @var bool
173     */
174    public static $requires_plan = false;
175
176    /**
177     * The feature slug that identifies the paid plan
178     *
179     * @var string
180     */
181    public static $feature_identifying_paid_plan = '';
182
183    /**
184     * Get the plugin slug
185     *
186     * @return ?string
187     */
188    public static function get_plugin_slug() {
189        return static::$plugin_slug;
190    }
191
192    /**
193     * Get the plugin filename
194     *
195     * @return ?string
196     */
197    public static function get_plugin_filename() {
198        return static::$plugin_filename;
199    }
200
201    /**
202     * This method will be called in the class initializer to register the product's endpoints
203     *
204     * @return void
205     */
206    public static function register_endpoints(): void {
207        // This method should be implemented in the child class.
208    }
209    /**
210     * Get data about the AI Assistant feature
211     *
212     * @return array
213     */
214    public static function get_ai_assistant_feature() {
215        // This method should be optionally set in the child class.
216        return array();
217    }
218
219    /**
220     * Get the WPCOM free product slug
221     *
222     * @return ?string
223     */
224    public static function get_wpcom_free_product_slug() {
225        return null;
226    }
227
228    /**
229     * Get the active installed copy, falling back to the first installed filename.
230     *
231     * @param string $plugin Which plugin to check. jetpack for the jetpack plugin or product for the product specific plugin.
232     *
233     * @return ?string
234     */
235    public static function get_installed_plugin_filename( $plugin = 'product' ) {
236        $all_plugins = Plugins_Installer::get_plugins();
237        $filename    = self::JETPACK_PLUGIN_SLUG === $plugin ? self::JETPACK_PLUGIN_FILENAME : static::get_plugin_filename();
238        if ( ! is_array( $filename ) ) {
239            $filename = array( $filename );
240        }
241        $first_installed = null;
242        // Products without a standalone plugin don't declare a filename, so drop the empty values.
243        foreach ( array_filter( $filename ) as $name ) {
244            if ( ! isset( $all_plugins[ $name ] ) ) {
245                continue;
246            }
247            if ( $all_plugins[ $name ]['active'] ) {
248                return $name;
249            }
250            if ( null === $first_installed ) {
251                $first_installed = $name;
252            }
253        }
254        return $first_installed;
255    }
256
257    /**
258     * Get the Static Product Info
259     *
260     * @throws \Exception If required attribute is not declared in the child class.
261     * @return array
262     */
263    public static function get_info() {
264        if ( static::$slug === null ) {
265            throw new \Exception( 'Product classes must declare the $slug attribute.' );
266        }
267        return array(
268            'slug'                            => static::$slug,
269            'plugin_slug'                     => static::get_plugin_slug(),
270            'name'                            => static::get_name(),
271            'title'                           => static::get_title(),
272            'category'                        => static::$category,
273            /* Maintain legacy compatibility with the old product info structure. See: #42271 */
274            'description'                     => static::get_description(),
275            'long_description'                => static::get_long_description(),
276            'tiers'                           => static::get_tiers(),
277            'features'                        => static::get_features(),
278            /* End of legacy compatibility fields. */
279            'disclaimers'                     => static::get_disclaimers(),
280            'is_bundle'                       => static::is_bundle_product(),
281            'is_plugin_active'                => static::is_plugin_active(),
282            'is_upgradable_by_bundle'         => static::is_upgradable_by_bundle(),
283            'is_feature'                      => static::$is_feature,
284            'supported_products'              => static::get_supported_products(),
285            'wpcom_product_slug'              => static::get_wpcom_product_slug(),
286            'requires_user_connection'        => static::$requires_user_connection,
287            'feature_identifying_paid_plan'   => static::$feature_identifying_paid_plan,
288            'has_free_offering'               => static::$has_free_offering,
289            'manage_url'                      => static::get_manage_url(),
290            // Whether to wait for `host_purchase_url`, which costs lookups and so comes with get_wpcom_info().
291            'host_may_sell'                   => Main_Features::may_host_sell_product( static::$slug ),
292            'post_activation_url'             => static::get_post_activation_url(),
293            'post_activation_urls_by_feature' => static::get_manage_urls_by_feature(),
294            'standalone_plugin_info'          => static::get_standalone_info(),
295            'class'                           => static::class,
296            'post_checkout_url'               => static::get_post_checkout_url(),
297            'post_checkout_urls_by_feature'   => static::get_post_checkout_urls_by_feature(),
298            'related_plan_slugs'              => static::get_related_plan_slugs(),
299        );
300    }
301
302    /**
303     * Get the related plan slugs including Free and Paid ones.
304     *
305     * @return array
306     */
307    public static function get_related_plan_slugs() {
308        $slugs = array_merge(
309            static::get_paid_bundles_that_include_product(),
310            static::get_paid_plan_product_slugs()
311        );
312
313        $free_product_slug = static::get_wpcom_free_product_slug();
314
315        if ( $free_product_slug ) {
316            $slugs[] = $free_product_slug;
317        }
318
319        return $slugs;
320    }
321
322    /**
323     * Get the Product Info that requires http requests to get
324     *
325     * @throws \Exception If required attribute is not declared in the child class.
326     * @return array
327     */
328    public static function get_wpcom_info() {
329        if ( static::$slug === null ) {
330            throw new \Exception( 'Product classes must declare the $slug attribute.' );
331        }
332
333        // Held for this call only: get_purchase_url() wants the same answer, and each ask costs ownership lookups.
334        $host_purchase_url                         = static::get_host_purchase_url();
335        $purchase_url                              = null;
336        self::$host_purchase_urls[ static::$slug ] = array( $host_purchase_url );
337        try {
338            $purchase_url = static::get_purchase_url();
339        } finally {
340            unset( self::$host_purchase_urls[ static::$slug ] );
341        }
342
343        $product_data = array(
344            'status'                        => static::get_status(),
345            'pricing_for_ui'                => static::get_pricing_for_ui(),
346            'is_upgradable'                 => static::is_upgradable(),
347            'description'                   => static::get_description(),
348            'tiers'                         => static::get_tiers(),
349            'features'                      => static::get_features(),
350            'long_description'              => static::get_long_description(),
351            'has_any_plan_for_product'      => static::has_any_plan_for_product(),
352            'has_free_plan_for_product'     => static::has_free_plan_for_product(),
353            'has_paid_plan_for_product'     => static::has_paid_plan_for_product(),
354            'purchase_url'                  => $purchase_url,
355            'host_purchase_url'             => $host_purchase_url,
356            'manage_paid_plan_purchase_url' => static::get_manage_paid_plan_purchase_url(),
357            'renew_paid_plan_purchase_url'  => static::get_renew_paid_plan_purchase_url(),
358            'does_module_need_attention'    => static::does_module_need_attention(),
359        );
360
361        if ( static::$slug === 'jetpack-ai' ) {
362            $product_data['ai-assistant-feature'] = static::get_ai_assistant_feature();
363        }
364        if ( in_array( static::$slug, array( 'security', 'growth' ), true ) ) {
365            $product_data['purchase_replacement'] = Products::get_bundle_offer( static::$slug );
366        }
367
368        return $product_data;
369    }
370
371    /**
372     * Collect the site's active features
373     *
374     * @return WP_Error|array
375     */
376    public static function get_site_features_from_wpcom() {
377        // Read the cache first, so a memoized failure can never outrank a warm lookup.
378        $stored_features = get_transient( self::MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY );
379        if ( $stored_features !== false ) {
380            return $stored_features;
381        }
382
383        /*
384         * Checked after the transient so it can never outrank a warm cache, but kept so a
385         * dashboard render still makes a single WPCOM request when the transient write is dropped.
386         */
387        if ( self::$site_features_success !== null && time() < self::$site_features_success_expires ) {
388            return self::$site_features_success;
389        }
390
391        if ( self::$site_features_failure !== null && time() < self::$site_features_failure_expires ) {
392            return self::$site_features_failure;
393        }
394
395        $site_id  = Jetpack_Options::get_option( 'id' );
396        $response = Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/features', $site_id ), '1.1' );
397
398        if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
399            self::$site_features_failure         = new WP_Error( 'site_features_fetch_failed' );
400            self::$site_features_failure_expires = time() + self::MY_JETPACK_SITE_FEATURES_CACHE_DURATION;
401            return self::$site_features_failure;
402        }
403
404        $body           = wp_remote_retrieve_body( $response );
405        $feature_return = json_decode( $body );
406
407        $features = array(
408            'active'    => $feature_return->active,
409            'available' => $feature_return->available,
410        );
411
412        // set a short transient to help with multiple lookups on the same page load.
413        set_transient( self::MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY, $features, self::MY_JETPACK_SITE_FEATURES_CACHE_DURATION );
414
415        self::$site_features_success         = $features;
416        self::$site_features_success_expires = time() + self::MY_JETPACK_SITE_FEATURES_CACHE_DURATION;
417
418        return $features;
419    }
420
421    /**
422     * Forget the cached site features â€” both in-process memos and the transient â€” so the next
423     * call asks WPCOM again.
424     *
425     * @return void
426     */
427    public static function reset_site_features_cache() {
428        self::$site_features_failure         = null;
429        self::$site_features_failure_expires = 0;
430        self::$site_features_success         = null;
431        self::$site_features_success_expires = 0;
432        delete_transient( self::MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY );
433    }
434
435    /**
436     * Check to see if the site has a feature
437     * This will check the features provided by the site plans and products (including free ones)
438     *
439     * @param string $feature - the feature to check for.
440     * @return bool
441     */
442    public static function does_site_have_feature( $feature ) {
443        if ( ! $feature ) {
444            return false;
445        }
446
447        // Simple runs the API call in-process, which 404s for the v1.1 features route, so ask WordPress.com's own check.
448        if ( ( new Host() )->is_wpcom_simple() && function_exists( 'wpcom_site_has_feature' ) ) {
449            return \wpcom_site_has_feature( $feature );
450        }
451
452        $features = self::get_site_features_from_wpcom();
453        if ( is_wp_error( $features ) ) {
454            return false;
455        }
456
457        return in_array( $feature, $features['active'], true );
458    }
459
460    /**
461     * Get the product name
462     *
463     * @return string
464     */
465    abstract public static function get_name();
466
467    /**
468     * Get the product title
469     *
470     * @return string
471     */
472    abstract public static function get_title();
473
474    /**
475     * Get the internationalized product description
476     *
477     * @return string
478     */
479    abstract public static function get_description();
480
481    /**
482     * Get the internationalized product long description
483     *
484     * @return string
485     */
486    abstract public static function get_long_description();
487
488    /**
489     * Get the tiers for the product
490     *
491     * @return boolean|string[] The slugs of the tiers (i.e. [ "free", "basic", "advanced" ]), or False if the product has no tiers.
492     */
493    public static function get_tiers() {
494        return array();
495    }
496
497    /**
498     * Get the internationalized features list
499     *
500     * @return array
501     */
502    abstract public static function get_features();
503
504    /**
505     * Get the internationalized comparison of features grouped by each tier
506     *
507     * @deprecated 6.10.0
508     *
509     * @return array
510     */
511    public static function get_features_by_tier() {
512        _deprecated_function( __METHOD__, 'jetpack-my-jetpack-6.10.0' );
513
514        return array();
515    }
516
517    /**
518     * Get the product pricing
519     *
520     * @return array
521     */
522    abstract public static function get_pricing_for_ui();
523
524    /**
525     * Prepare supported billing terms, requiring confirmed availability for Pro.
526     *
527     * @since 6.12.0
528     * @param string[] $product_slugs Store product slugs, preferred term first.
529     * @return array Pricing details and term choices.
530     */
531    protected static function get_bundle_pricing( $product_slugs ) {
532        $terms = array();
533        foreach ( $product_slugs as $slug ) {
534            $product           = Wpcom_Products::get_product( $slug );
535            $valid_product     = is_object( $product ) && isset( $product->cost ) && isset( $product->currency_code ) && isset( $product->product_term ) && is_numeric( $product->cost ) && $product->cost >= 0;
536            $term              = array_merge(
537                array(
538                    'available'          => false,
539                    'wpcom_product_slug' => $slug,
540                ),
541                $valid_product ? Wpcom_Products::get_product_pricing( $slug ) : array()
542            );
543            $expected_term     = str_ends_with( $slug, '_monthly' ) ? 'month' : ( str_ends_with( $slug, '_bi_yearly' ) ? 'two years' : 'year' );
544            $on_sale           = str_starts_with( $slug, 'jetpack_pro_' ) ? true === ( $product->available ?? false ) : false !== ( $product->available ?? null );
545            $term['available'] = $valid_product && $on_sale && ( $term['product_term'] ?? '' ) === $expected_term;
546            $terms[]           = $term;
547        }
548        $available = array_values( array_filter( $terms, fn( $term ) => true === $term['available'] ) );
549        return array_merge( $available[0] ?? $terms[0], array( 'terms' => $terms ) );
550    }
551
552    /**
553     * Get the URL where the user can purchase the product iff it doesn't have an interstitial page in My Jetpack.
554     *
555     * @since $$next-version$$ Returns the host's own address when it sells the product's feature.
556     *
557     * @return ?string
558     */
559    public static function get_purchase_url() {
560        // Most Jetpack products sell through an interstitial page within My Jetpack, so only a host names an address.
561        return static::get_host_purchase_url();
562    }
563
564    /**
565     * Get the URL where the site's host sells the product, in place of My Jetpack's interstitial page.
566     *
567     * @since $$next-version$$
568     *
569     * @return ?string Null when no host sells it or the site already has it.
570     */
571    public static function get_host_purchase_url() {
572        if ( isset( self::$host_purchase_urls[ static::$slug ] ) ) {
573            return self::$host_purchase_urls[ static::$slug ][0];
574        }
575
576        return Main_Features::get_host_purchase_url( static::$slug );
577    }
578
579    /**
580     * Get the URL where the user manages the product
581     *
582     * @return ?string
583     */
584    abstract public static function get_manage_url();
585
586    /**
587     * Get the URL where the user manages the product for each product feature
588     *
589     * @return ?array
590     */
591    public static function get_manage_urls_by_feature() {
592        return null;
593    }
594
595    /**
596     * Get the URL the user is taken after activating the product
597     *
598     * @return ?string
599     */
600    public static function get_post_activation_url() {
601        return static::get_manage_url();
602    }
603
604    /**
605     * Get the URL the user is taken after purchasing the product through the checkout
606     *
607     * @return ?string
608     */
609    public static function get_post_checkout_url() {
610        return null;
611    }
612
613    /**
614     * Get the URL the user is taken after purchasing the product through the checkout for each product feature
615     *
616     * @return ?array
617     */
618    public static function get_post_checkout_urls_by_feature() {
619        return null;
620    }
621
622    /**
623     * Get the WPCOM product slug used to make the purchase
624     *
625     * @return ?string
626     */
627    public static function get_wpcom_product_slug() {
628        return null;
629    }
630
631    /**
632     * Get the disclaimers corresponding to a feature
633     *
634     * @return ?array
635     */
636    public static function get_disclaimers() {
637        return array();
638    }
639
640    /**
641     * Get the standalone plugin related info
642     *
643     * @return array
644     */
645    public static function get_standalone_info() {
646        $is_standalone_installed = static::$has_standalone_plugin && self::is_plugin_installed();
647        $is_standalone_active    = static::$has_standalone_plugin && self::is_plugin_active();
648
649        return array(
650            'has_standalone_plugin'   => static::$has_standalone_plugin,
651            'is_standalone_installed' => $is_standalone_installed,
652            'is_standalone_active'    => $is_standalone_active,
653        );
654    }
655
656    /**
657     * Checks whether the site has a paid plan for the product.
658     *
659     * This function relies on the product's `$feature_identifying_paid_plan` and `get_paid_plan_product_slugs()` function.
660     * If the product does not define a `$feature_identifying_paid_plan`, be sure the product includes functions for both
661     * `get_paid_plan_product_slugs()` and `get_paid_bundles_that_include_product()` which return all the product slugs and
662     * bundle slugs that include the product, respectively.
663     *
664     * @return boolean
665     */
666    public static function has_paid_plan_for_product() {
667        // First check site features (if there's a feature that identifies the paid plan)
668        if ( static::$feature_identifying_paid_plan ) {
669            if ( static::does_site_have_feature( static::$feature_identifying_paid_plan ) ) {
670                return true;
671            }
672        }
673        // Otherwise check site purchases
674        $plans_with_product = array_merge(
675            static::get_paid_bundles_that_include_product(),
676            static::get_paid_plan_product_slugs()
677        );
678
679        $purchases_data = Wpcom_Products::get_site_current_purchases();
680        if ( is_wp_error( $purchases_data ) ) {
681            return false;
682        }
683        if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) {
684            foreach ( $purchases_data as $purchase ) {
685                foreach ( $plans_with_product as $plan ) {
686                    if ( strpos( $purchase->product_slug, $plan ) !== false ) {
687                        return true;
688                    }
689                }
690            }
691        }
692
693        return false;
694    }
695
696    /**
697     * Checks whether the site has a free plan for the product
698     * Note, this should not return true if a product does not have a WPCOM plan (ex: search free, Akismet Free, stats free)
699     *
700     * @return false
701     */
702    public static function has_free_plan_for_product() {
703        return false;
704    }
705
706    /**
707     * Checks whether the site has any WPCOM plan for a product (paid or free)
708     *
709     * @return bool
710     */
711    public static function has_any_plan_for_product() {
712        return static::has_paid_plan_for_product() || static::has_free_plan_for_product();
713    }
714
715    /**
716     * Get the product-slugs of the paid plans for this product.
717     * (Do not include bundle plans, unless it's a bundle plan itself).
718     *
719     * @return array
720     */
721    public static function get_paid_plan_product_slugs() {
722        return array();
723    }
724
725    /**
726     * Get the product-slugs of the paid bundles/plans that this product/module is included in.
727     *
728     * This function relies on the product's `$feature_identifying_paid_plan`
729     * If the product does not define a `$feature_identifying_paid_plan`, be sure to include this
730     * function in the product's class and have it return all the paid bundle slugs that include
731     * the product.
732     *
733     * @return array
734     */
735    public static function get_paid_bundles_that_include_product() {
736        if ( static::is_bundle_product() ) {
737            return array();
738        }
739        $features = static::get_site_features_from_wpcom();
740        if ( is_wp_error( $features ) ) {
741            return array();
742        }
743        $idendifying_feature = static::$feature_identifying_paid_plan;
744        if ( empty( $features['available'] ) ) {
745            return array();
746        }
747        $paid_bundles   = $features['available']->$idendifying_feature ?? array();
748        $current_bundle = Wpcom_Products::get_site_current_plan( true );
749
750        if ( in_array( static::$feature_identifying_paid_plan, $current_bundle['features']['active'], true ) ) {
751            $paid_bundles[] = $current_bundle['product_slug'];
752        }
753
754        return $paid_bundles;
755    }
756
757    /**
758     * Gets the paid plan's purchase/subsciption info, or null if no paid plan purchases.
759     *
760     * @return object|null
761     */
762    public static function get_paid_plan_purchase_for_product() {
763        $paid_plans = array_merge(
764            static::get_paid_plan_product_slugs(),
765            static::get_paid_bundles_that_include_product()
766        );
767
768        $purchases_data = Wpcom_Products::get_site_current_purchases();
769        if ( is_wp_error( $purchases_data ) ) {
770            return null;
771        }
772
773        if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) {
774            foreach ( $purchases_data as $purchase ) {
775                foreach ( $paid_plans as $plan ) {
776                    if ( strpos( $purchase->product_slug, $plan ) !== false ) {
777                        return $purchase;
778                    }
779                }
780            }
781        }
782
783        return null;
784    }
785
786    /**
787     * Gets the paid plan's expiry date.
788     *
789     * @return string
790     */
791    public static function get_paid_plan_expiration_date() {
792        $purchase = static::get_paid_plan_purchase_for_product();
793        if ( ! $purchase ) {
794            return 'paid-plan-does-not-exist';
795        }
796
797        return $purchase->expiry_date;
798    }
799
800    /**
801     * Gets the paid plan's expiry status.
802     *
803     * @return string
804     */
805    public static function get_paid_plan_expiration_status() {
806        $purchase = static::get_paid_plan_purchase_for_product();
807        if ( ! $purchase ) {
808            return 'paid-plan-does-not-exist';
809        }
810
811        return $purchase->expiry_status;
812    }
813
814    /**
815     * Checks if the paid plan is expired or not.
816     *
817     * @param bool $not_expired_after_cutoff - whether to not return the plan as expired if the plan has been expired for some duration of time.
818     * @return bool
819     */
820    public static function is_paid_plan_expired( $not_expired_after_cutoff = false ) {
821        $expiry_status = static::get_paid_plan_expiration_status();
822        $expiry_date   = static::get_paid_plan_expiration_date();
823        $expiry_cutoff = strtotime( $expiry_date . ' ' . self::EXPIRATION_CUTOFF_TIME );
824
825        return $not_expired_after_cutoff
826            ? $expiry_status === Products::STATUS_EXPIRED && strtotime( 'now' ) < $expiry_cutoff
827            : $expiry_status === Products::STATUS_EXPIRED;
828    }
829
830    /**
831     * Checks if the paid plan is expiring soon or not.
832     *
833     * @return bool
834     */
835    public static function is_paid_plan_expiring() {
836        $expiry_status = static::get_paid_plan_expiration_status();
837
838        return $expiry_status === Products::STATUS_EXPIRING_SOON;
839    }
840
841    /**
842     * Gets the url to manage the paid plan's purchased subscription (for plan renewal, canceling, removal, etc).
843     *
844     * @return string|null The url to the purchase management page.
845     */
846    public static function get_manage_paid_plan_purchase_url() {
847        $purchase    = static::get_paid_plan_purchase_for_product();
848        $site_suffix = ( new Status() )->get_site_suffix();
849
850        if ( $purchase && $site_suffix ) {
851            return 'https://wordpress.com/me/purchases/' . $site_suffix . '/' . $purchase->ID;
852        }
853
854        return null;
855    }
856
857    /**
858     * Gets the url to renew the paid plan's purchased subscription.
859     *
860     * @return string|null The url to the checkout renewal page.
861     */
862    public static function get_renew_paid_plan_purchase_url() {
863        $purchase    = static::get_paid_plan_purchase_for_product();
864        $site_suffix = ( new Status() )->get_site_suffix();
865
866        if ( $purchase && $site_suffix ) {
867            return 'https://wordpress.com/checkout/' . $purchase->product_slug . '/renew/' . $purchase->ID . '/' . $site_suffix;
868        }
869
870        return null;
871    }
872
873    /**
874     * Checks whether the product supports trial or not
875     *
876     * Returns true if it supports. Return false otherwise.
877     *
878     * Free products will always return false.
879     *
880     * @return boolean
881     */
882    public static function has_trial_support() {
883        return false;
884    }
885
886    /**
887     * Checks whether the product can be upgraded to a different product.
888     *
889     * @return boolean
890     */
891    public static function is_upgradable() {
892        return ! static::has_paid_plan_for_product() && ! static::is_bundle_product();
893    }
894
895    /**
896     * Checks whether product is a bundle.
897     *
898     * @return boolean True if product is a bundle. Otherwise, False.
899     */
900    public static function is_bundle_product() {
901        return false;
902    }
903
904    /**
905     * Check whether the product is upgradable
906     * by a product bundle.
907     *
908     * @return boolean|array Bundles list or False if not upgradable by a bundle.
909     */
910    public static function is_upgradable_by_bundle() {
911        return false;
912    }
913
914    /**
915     * In case it's a bundle product,
916     * return all the products it contains.
917     * Empty array by default.
918     *
919     * @return array Product slugs
920     */
921    public static function get_supported_products() {
922        return array();
923    }
924
925    /**
926     * Determine if the product is owned or not
927     * An owned product is defined as a product that is any of the following
928     * - Active
929     * - Has historically been active
930     * - The user has a plan that includes the product
931     * - The user has the standalone plugin for the product installed
932     *
933     * @return boolean
934     */
935    public static function is_owned() {
936        $historically_active_modules = Jetpack_Options::get_option( 'historically_active_modules', array() );
937        $standalone_info             = static::get_standalone_info();
938        if ( ( static::is_active() && Jetpack_Options::get_option( 'id' ) ) ||
939            $standalone_info['is_standalone_installed'] ||
940            in_array( static::$slug, $historically_active_modules, true ) ||
941            static::has_any_plan_for_product()
942        ) {
943            return true;
944        }
945
946        return false;
947    }
948
949    /**
950     * Undocumented function
951     *
952     * @return string
953     */
954    public static function get_status() {
955        if ( ! static::is_plugin_installed() ) {
956            $status = Products::STATUS_PLUGIN_ABSENT;
957            if ( static::has_paid_plan_for_product() ) {
958                $status = Products::STATUS_PLUGIN_ABSENT_WITH_PLAN;
959            }
960        } elseif ( static::is_active() ) {
961            $status = Products::STATUS_ACTIVE;
962            // We only consider missing site & user connection an error when the Product is active.
963            if ( static::$requires_site_connection && ! ( new Connection_Manager() )->is_connected() ) {
964                // Site has never been connected before
965                if ( ! Jetpack_Options::get_option( 'id' ) && ! static::is_owned() ) {
966                    $status = Products::STATUS_NEEDS_FIRST_SITE_CONNECTION;
967                } else {
968                    $status = Products::STATUS_SITE_CONNECTION_ERROR;
969                }
970            } elseif ( static::$requires_user_connection && ! ( new Connection_Manager() )->has_connected_owner() ) {
971                $status = Products::STATUS_USER_CONNECTION_ERROR;
972            } elseif ( static::has_paid_plan_for_product() ) {
973                $needs_attention = static::does_module_need_attention();
974                if ( ! empty( $needs_attention ) && is_array( $needs_attention ) ) {
975                    $status = Products::STATUS_NEEDS_ATTENTION__WARNING;
976                    if ( isset( $needs_attention['type'] ) && 'error' === $needs_attention['type'] ) {
977                        $status = Products::STATUS_NEEDS_ATTENTION__ERROR;
978                    }
979                }
980                if ( static::is_paid_plan_expired() ) {
981                    $status = Products::STATUS_EXPIRED;
982                } elseif ( static::is_paid_plan_expiring() ) {
983                    $status = Products::STATUS_EXPIRING_SOON;
984                }
985            } elseif ( static::is_upgradable() ) {
986                $status = Products::STATUS_CAN_UPGRADE;
987            }
988            // Check specifically for inactive modules, which will prevent a product from being active
989            // Bundles have no module of their own, so they fall through to the plan checks below.
990        } elseif ( static::get_module_name() && ! static::is_bundle_product() && ! static::is_module_active() ) {
991            $status = Products::STATUS_MODULE_DISABLED;
992            // If there is not a plan associated with the disabled module, encourage a plan first
993            // Getting a plan set up should help resolve any connection issues
994            // However if the standalone plugin for this product is active, then we will defer to showing errors that prevent the module from being active
995            // This is because if a standalone plugin is installed, we expect the product to not show as "inactive" on My Jetpack
996            if ( ! static::has_any_plan_for_product() && ( static::$requires_plan || ( static::$has_standalone_plugin && ! self::is_plugin_active() ) ) ) {
997                $status = static::is_owned() && static::$has_free_offering && ! static::$requires_plan ? Products::STATUS_NEEDS_ACTIVATION : Products::STATUS_NEEDS_PLAN;
998            } elseif ( static::$requires_site_connection && ! ( new Connection_Manager() )->is_connected() ) {
999                // Site has never been connected before and product is not owned
1000                if ( ! Jetpack_Options::get_option( 'id' ) && ! static::is_owned() ) {
1001                    $status = Products::STATUS_NEEDS_FIRST_SITE_CONNECTION;
1002                } else {
1003                    $status = Products::STATUS_SITE_CONNECTION_ERROR;
1004                }
1005            } elseif ( static::$requires_user_connection && ! ( new Connection_Manager() )->has_connected_owner() ) {
1006                $status = Products::STATUS_USER_CONNECTION_ERROR;
1007            }
1008        } elseif ( ! static::has_any_plan_for_product() ) {
1009            $status = static::is_owned() && static::$has_free_offering && ! static::$requires_plan ? Products::STATUS_NEEDS_ACTIVATION : Products::STATUS_NEEDS_PLAN;
1010        } else {
1011            $status = Products::STATUS_INACTIVE;
1012        }
1013        return $status;
1014    }
1015
1016    /**
1017     * Checks whether the Product is active
1018     *
1019     * @return boolean
1020     */
1021    public static function is_active() {
1022        return static::is_plugin_active() && ( static::has_any_plan_for_product() || ( ! static::$requires_plan && static::$has_free_offering ) );
1023    }
1024
1025    /**
1026     * Whether something other than its switch keeps the product off, such as a host or a filter.
1027     *
1028     * Turning its module or plugin on can't bring it back, so nothing should offer to.
1029     *
1030     * @since $$next-version$$
1031     *
1032     * @return bool
1033     */
1034    public static function is_held_off() {
1035        return false;
1036    }
1037
1038    /**
1039     * Checks whether the site has switched the product on, whether or not it has a plan for it.
1040     *
1041     * Unlike is_active(), this never asks WordPress.com, so it is safe on every admin page load.
1042     * An override must keep it that way.
1043     *
1044     * @return boolean
1045     */
1046    public static function is_activated() {
1047        return static::is_plugin_active();
1048    }
1049
1050    /**
1051     * Checks whether the plugin is installed
1052     *
1053     * @return boolean
1054     */
1055    public static function is_plugin_installed() {
1056        return (bool) static::get_installed_plugin_filename();
1057    }
1058
1059    /**
1060     * Checks whether the plugin is active
1061     *
1062     * @return boolean
1063     */
1064    public static function is_plugin_active() {
1065        return Plugins_Installer::is_plugin_active( static::get_installed_plugin_filename() );
1066    }
1067
1068    /**
1069     * Checks whether the Jetpack plugin is installed
1070     *
1071     * @return boolean
1072     */
1073    public static function is_jetpack_plugin_installed() {
1074        return (bool) static::get_installed_plugin_filename( 'jetpack' );
1075    }
1076
1077    /**
1078     * Checks whether the Jetpack plugin is active
1079     *
1080     * @return boolean
1081     */
1082    public static function is_jetpack_plugin_active() {
1083        return Plugins_Installer::is_plugin_active( static::get_installed_plugin_filename( 'jetpack' ) );
1084    }
1085
1086    /**
1087     * Checks whether the Jetpack module is active only if a module_name is defined
1088     *
1089     * @return bool
1090     */
1091    public static function is_module_active() {
1092        $module_name = static::get_module_name();
1093        if ( $module_name ) {
1094            return ( new Modules() )->is_active( $module_name );
1095        }
1096        return true;
1097    }
1098
1099    /**
1100     * Get the Jetpack module that switches the product on, if any.
1101     *
1102     * @since 6.12.0
1103     *
1104     * @return ?string
1105     */
1106    public static function get_module_name() {
1107        return static::$module_name;
1108    }
1109
1110    /**
1111     * Activates the plugin
1112     *
1113     * @return null|WP_Error Null on success, WP_Error on invalid file.
1114     */
1115    public static function activate_plugin() {
1116        return activate_plugin( static::get_installed_plugin_filename() );
1117    }
1118
1119    /**
1120     * Perform the top level activation routines, which is installing and activating the required plugin
1121     *
1122     * @return bool|WP_Error
1123     */
1124    private static function do_activation() {
1125        if ( static::is_active() ) {
1126            return true;
1127        }
1128
1129        // Default to installing the standalone plugin for the product
1130        if ( ! self::is_plugin_installed() && static::should_use_standalone_plugin() ) {
1131            $installed = Plugins_Installer::install_plugin( static::get_plugin_slug() );
1132            if ( is_wp_error( $installed ) ) {
1133                return $installed;
1134            }
1135        }
1136
1137        if ( ! current_user_can( 'activate_plugins' ) ) {
1138            return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack-my-jetpack' ) );
1139        }
1140
1141        $result = static::activate_plugin();
1142        if ( is_wp_error( $result ) ) {
1143            return $result;
1144        }
1145
1146        return true;
1147    }
1148
1149    /**
1150     * Whether activating the product installs and activates its standalone plugin, rather than relying on the Jetpack plugin.
1151     *
1152     * @since 6.12.0
1153     *
1154     * @return bool
1155     */
1156    public static function should_use_standalone_plugin() {
1157        return true;
1158    }
1159
1160    /**
1161     * Activates the product by installing and activating its plugin
1162     *
1163     * @return boolean|WP_Error
1164     */
1165    final public static function activate() {
1166
1167        $result = self::do_activation();
1168
1169        $result = static::do_product_specific_activation( $result );
1170
1171        $product_slug = static::$slug;
1172
1173        /**
1174         * Fires after My Jetpack activates a product and filters the result
1175         * Use this filter to run additional routines for a product activation on stand-alone plugins
1176         *
1177         * @param bool|WP_Error $result The result of the previous steps of activation.
1178         */
1179        $result = apply_filters( "my_jetpack_{$product_slug}_activation", $result );
1180
1181        return $result;
1182    }
1183
1184    /**
1185     * Override this method to perform product specific activation routines.
1186     *
1187     * @param bool|WP_Error $current_result Is the result of the top level activation actions. You probably won't do anything if it is an WP_Error.
1188     * @return bool|WP_Error
1189     */
1190    public static function do_product_specific_activation( $current_result ) {
1191        return $current_result;
1192    }
1193
1194    /**
1195     * Deactivate the product
1196     *
1197     * @return boolean
1198     */
1199    public static function deactivate() {
1200        deactivate_plugins( static::get_installed_plugin_filename() );
1201        return true;
1202    }
1203
1204    /**
1205     * Returns filtered Jetpack plugin actions links.
1206     *
1207     * @param array $actions - Jetpack plugin action links.
1208     * @return array           Filtered Jetpack plugin actions links.
1209     */
1210    public static function get_plugin_actions_links( $actions ) {
1211        // My Jetpack action link.
1212        $my_jetpack_home_link = array(
1213            'jetpack-home' => sprintf(
1214                '<a href="%1$s" title="%3$s">%2$s</a>',
1215                admin_url( 'admin.php?page=my-jetpack' ),
1216                __( 'My Jetpack', 'jetpack-my-jetpack' ),
1217                __( 'My Jetpack dashboard', 'jetpack-my-jetpack' )
1218            ),
1219        );
1220
1221        // Otherwise, add it to the beginning of the array.
1222        return array_merge( $my_jetpack_home_link, $actions );
1223    }
1224
1225    /**
1226     * Filter the action links for the plugins specified.
1227     *
1228     * @param string|string[] $filenames The plugin filename(s) to filter the action links for.
1229     */
1230    private static function filter_action_links( $filenames ) {
1231        foreach ( $filenames as $filename ) {
1232            $hook     = 'plugin_action_links_' . $filename;
1233            $callback = array( static::class, 'get_plugin_actions_links' );
1234            if ( ! has_filter( $hook, $callback ) ) {
1235                add_filter( $hook, $callback, 20, 2 );
1236            }
1237        }
1238    }
1239
1240    /**
1241     * Extend the plugin action links.
1242     */
1243    public static function extend_plugin_action_links() {
1244        $filenames = static::get_plugin_filename();
1245        if ( ! is_array( $filenames ) ) {
1246            $filenames = array( $filenames );
1247        }
1248
1249        self::filter_action_links( $filenames );
1250    }
1251
1252    /**
1253     * Extend the Jetpack plugin action links.
1254     */
1255    public static function extend_core_plugin_action_links() {
1256        $filenames = self::JETPACK_PLUGIN_FILENAME;
1257
1258        self::filter_action_links( $filenames );
1259    }
1260
1261    /**
1262     * Install and activate the standalone plugin in the case it's missing.
1263     *
1264     * @return boolean|WP_Error
1265     */
1266    public static function install_and_activate_standalone() {
1267        /**
1268         * Check for the presence of the standalone plugin, ignoring Jetpack presence.
1269         *
1270         * If the standalone plugin is not installed and the user can install plugins, proceed with the installation.
1271         */
1272        if ( ! static::is_plugin_installed() ) {
1273            /**
1274             * Check for permissions
1275             */
1276            if ( ! current_user_can( 'install_plugins' ) ) {
1277                return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack-my-jetpack' ) );
1278            }
1279
1280            /**
1281             * Install the plugin
1282             */
1283            $installed = Plugins_Installer::install_plugin( static::get_plugin_slug() );
1284            if ( is_wp_error( $installed ) ) {
1285                return $installed;
1286            }
1287        }
1288
1289        /**
1290         * Activate the installed plugin
1291         */
1292        $result = static::activate_plugin();
1293
1294        if ( is_wp_error( $result ) ) {
1295            return $result;
1296        }
1297
1298        return true;
1299    }
1300
1301    /**
1302     * Determines whether the module/plugin/product needs the users attention.
1303     * Typically due to some sort of error where user troubleshooting is needed.
1304     *
1305     * @return boolean
1306     */
1307    public static function does_module_need_attention() {
1308        return false;
1309    }
1310}