Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.27% covered (danger)
47.27%
26 / 55
46.67% covered (danger)
46.67%
7 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Security
47.17% covered (danger)
47.17%
25 / 53
46.67% covered (danger)
46.67%
7 / 15
134.49
0.00% covered (danger)
0.00%
0 / 1
 get_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_long_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_features
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_pricing_for_ui
100.00% covered (success)
100.00%
8 / 8
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
 is_module_active
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 do_product_specific_activation
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 is_active
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 get_paid_plan_product_slugs
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 has_required_plan
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 is_bundle_product
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_supported_products
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_manage_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Security product
4 *
5 * @package my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack\Products;
9
10use Automattic\Jetpack\My_Jetpack\Module_Product;
11use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
12use WP_Error;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * Class responsible for handling the Security product
20 */
21class Security extends Module_Product {
22
23    /**
24     * The product slug
25     *
26     * @var string
27     */
28    public static $slug = 'security';
29
30    /**
31     * The Jetpack module name
32     *
33     * @var string
34     */
35    public static $module_name = 'security';
36
37    /**
38     * Get the product name
39     *
40     * @return string
41     */
42    public static function get_name() {
43        return 'Security Bundle';
44    }
45
46    /**
47     * Get the product title
48     *
49     * @return string
50     */
51    public static function get_title() {
52        return 'Jetpack Security';
53    }
54
55    /**
56     * Get the internationalized product description
57     *
58     * @return string
59     */
60    public static function get_description() {
61        return __( 'Comprehensive site security, including VaultPress Backup, Scan, and Akismet Anti-spam.', 'jetpack-my-jetpack' );
62    }
63
64    /**
65     * Get the internationalized product long description
66     *
67     * @return string
68     */
69    public static function get_long_description() {
70        return __( 'Comprehensive site security, including VaultPress Backup, Scan, and Akismet Anti-spam.', 'jetpack-my-jetpack' );
71    }
72
73    /**
74     * Get the internationalized features list
75     *
76     * @return array Security features list
77     */
78    public static function get_features() {
79        return array(
80            _x( 'Real-time cloud backups with 10GB storage', 'Security Product Feature', 'jetpack-my-jetpack' ),
81            _x( 'Automated real-time malware scan', 'Security Product Feature', 'jetpack-my-jetpack' ),
82            _x( 'One-click fixes for most threats', 'Security Product Feature', 'jetpack-my-jetpack' ),
83            _x( 'Comment & form spam protection', 'Security Product Feature', 'jetpack-my-jetpack' ),
84        );
85    }
86
87    /**
88     * Get the product pricing details
89     *
90     * @return array Pricing details
91     */
92    public static function get_pricing_for_ui() {
93        $product_slug = static::get_wpcom_product_slug();
94        return array_merge(
95            array(
96                'available'          => true,
97                'wpcom_product_slug' => $product_slug,
98            ),
99            Wpcom_Products::get_product_pricing( $product_slug )
100        );
101    }
102
103    /**
104     * Get the WPCOM product slug used to make the purchase
105     *
106     * @return ?string
107     */
108    public static function get_wpcom_product_slug() {
109        return 'jetpack_security_t1_yearly';
110    }
111
112    /**
113     * Checks whether the Jetpack module is active
114     *
115     * This is a bundle and not a product. We should not use this information for anything
116     *
117     * @return bool
118     */
119    public static function is_module_active() {
120        return false;
121    }
122
123    /**
124     * Activates the product by installing and activating its plugin
125     *
126     * @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.
127     * @return boolean|\WP_Error
128     */
129    public static function do_product_specific_activation( $current_result ) {
130
131        $product_activation = parent::do_product_specific_activation( $current_result );
132
133        if ( is_wp_error( $product_activation ) && 'module_activation_failed' === $product_activation->get_error_code() ) {
134            // A bundle is not a module. There's nothing in the plugin to be activated, so it's ok to fail to activate the module.
135            $product_activation = true;
136        }
137
138        // At this point, Jetpack plugin is installed. Let's activate each individual product.
139        $activation = Anti_Spam::activate();
140        if ( is_wp_error( $activation ) ) {
141            return $activation;
142        }
143
144        $activation = Backup::activate();
145        if ( is_wp_error( $activation ) ) {
146            return $activation;
147        }
148
149        $activation = Scan::activate();
150        if ( is_wp_error( $activation ) ) {
151            return $activation;
152        }
153
154        return $activation;
155    }
156
157    /**
158     * Checks whether the Product is active
159     *
160     * Security is a bundle and not a module. Activation takes place on WPCOM. So lets consider it active if jetpack is active and has the plan.
161     *
162     * @return boolean
163     */
164    public static function is_active() {
165        return static::is_jetpack_plugin_active() && static::has_required_plan();
166    }
167
168    /**
169     * Get the product-slugs of the paid plans for this product.
170     * (Do not include bundle plans, unless it's a bundle plan itself).
171     *
172     * @return array
173     */
174    public static function get_paid_plan_product_slugs() {
175        return array(
176            'jetpack_security_t1_yearly',
177            'jetpack_security_t1_monthly',
178            'jetpack_security_t1_bi_yearly',
179            'jetpack_security_t2_yearly',
180            'jetpack_security_t2_monthly',
181        );
182    }
183
184    /**
185     * Checks whether the current plan (or purchases) of the site already supports the product
186     *
187     * @return boolean
188     */
189    public static function has_required_plan() {
190        $purchases_data = Wpcom_Products::get_site_current_purchases();
191        if ( is_wp_error( $purchases_data ) ) {
192            return false;
193        }
194        if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) {
195            foreach ( $purchases_data as $purchase ) {
196                if (
197                    str_starts_with( $purchase->product_slug, 'jetpack_security' ) ||
198                    str_starts_with( $purchase->product_slug, 'jetpack_complete' )
199                ) {
200                    return true;
201                }
202            }
203        }
204        return false;
205    }
206
207    /**
208     * Checks whether product is a bundle.
209     *
210     * @return boolean True
211     */
212    public static function is_bundle_product() {
213        return true;
214    }
215
216    /**
217     * Return all the products it contains.
218     *
219     * @return array Product slugs
220     */
221    public static function get_supported_products() {
222        return array( 'backup', 'scan', 'anti-spam' );
223    }
224
225    /**
226     * Get the URL where the user manages the product
227     *
228     * @return ?string
229     */
230    public static function get_manage_url() {
231        return '';
232    }
233}