Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
92.31% covered (success)
92.31%
12 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Site_Accelerator
88.00% covered (warning)
88.00%
22 / 25
92.31% covered (success)
92.31%
12 / 13
21.76
0.00% covered (danger)
0.00%
0 / 1
 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_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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_pricing_for_ui
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 is_active
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
 get_manage_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 activate_plugin
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 is_module_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 do_product_specific_activation
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 deactivate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Feature: Site Accelerator
4 *
5 * @package my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack\Products;
9
10use Automattic\Jetpack\My_Jetpack\Initializer;
11use Automattic\Jetpack\My_Jetpack\Module_Product;
12use Jetpack;
13use WP_Error;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Class responsible for handling the Site Accelerator module.
21 */
22class Site_Accelerator extends Module_Product {
23
24    /**
25     * The product slug
26     *
27     * @var string
28     */
29    public static $slug = 'site-accelerator';
30
31    /**
32     * The category of the product
33     *
34     * @var string
35     */
36    public static $category = 'performance';
37
38    /**
39     * The slug of the plugin associated with this product.
40     * Site Accelerator is a feature available as part of the Jetpack plugin.
41     *
42     * @var string
43     */
44    public static $plugin_slug = self::JETPACK_PLUGIN_SLUG;
45
46    /**
47     * The Plugin file associated with stats
48     *
49     * @var string|null
50     */
51    public static $plugin_filename = self::JETPACK_PLUGIN_FILENAME;
52
53    /**
54     * The Jetpack module name associated with this product: the Image CDN.
55     *
56     * @var string|null
57     */
58    public static $module_name = 'photon';
59
60    /**
61     * The Asset CDN module, which Site Accelerator switches together with the Image CDN.
62     *
63     * @var string
64     */
65    const ASSET_CDN_MODULE = 'photon-cdn';
66
67    /**
68     * Whether this module is a Jetpack feature
69     *
70     * @var boolean
71     */
72    public static $is_feature = true;
73
74    /**
75     * Whether this product requires a user connection
76     *
77     * @var boolean
78     */
79    public static $requires_user_connection = false;
80
81    /**
82     * Whether this product has a standalone plugin
83     *
84     * @var bool
85     */
86    public static $has_standalone_plugin = false;
87
88    /**
89     * Whether this product has a free offering
90     *
91     * @var bool
92     */
93    public static $has_free_offering = true;
94
95    /**
96     * Whether the product requires a plan to run
97     * The plan could be paid or free
98     *
99     * @var bool
100     */
101    public static $requires_plan = false;
102
103    /**
104     * Get the product name
105     *
106     * @return string
107     */
108    public static function get_name() {
109        return 'Site Accelerator';
110    }
111
112    /**
113     * Get the product title
114     *
115     * @return string
116     */
117    public static function get_title() {
118        return 'Site Accelerator';
119    }
120
121    /**
122     * Get the internationalized product description
123     *
124     * @return string
125     */
126    public static function get_description() {
127        return __( "Speed up your site with Jetpack's global CDN", 'jetpack-my-jetpack' );
128    }
129
130    /**
131     * Get the internationalized product long description
132     *
133     * @return string
134     */
135    public static function get_long_description() {
136        return __( "Speed up your site by serving its images and static files, like CSS and JavaScript, from Jetpack's global CDN", 'jetpack-my-jetpack' );
137    }
138
139    /**
140     * Get the internationalized feature list
141     *
142     * @return array Site Accelerattor features list
143     */
144    public static function get_features() {
145        return array();
146    }
147
148    /**
149     * Get the product princing details
150     *
151     * @return array Pricing details
152     */
153    public static function get_pricing_for_ui() {
154        return array(
155            'available' => true,
156            'is_free'   => true,
157        );
158    }
159
160    /**
161     * Checks whether the Product is active.
162     *
163     * @return boolean
164     */
165    public static function is_active() {
166        return static::is_jetpack_plugin_active();
167    }
168
169    /**
170     * Checks whether the plugin is installed
171     *
172     * @return boolean
173     */
174    public static function is_plugin_installed() {
175        return static::is_jetpack_plugin_installed();
176    }
177
178    /**
179     * Get the URL where the user manages the product
180     *
181     * @return ?string
182     */
183    public static function get_manage_url() {
184        if ( Initializer::is_settings_consolidation_on_for_site() ) {
185            return admin_url( 'admin.php?page=my-jetpack#/features?search=cdn' );
186        }
187
188        return admin_url( 'admin.php?page=jetpack#/settings?term=site%20accelerator' );
189    }
190
191    /**
192     * Activates the Jetpack plugin
193     *
194     * @return null|WP_Error Null on success, WP_Error on invalid file.
195     */
196    public static function activate_plugin(): ?WP_Error {
197        $plugin_filename = static::get_installed_plugin_filename( self::JETPACK_PLUGIN_SLUG );
198
199        if ( $plugin_filename ) {
200            return activate_plugin( $plugin_filename );
201        }
202    }
203
204    /**
205     * Checks whether either CDN module is on, as the Jetpack Settings toggle shows it.
206     *
207     * @since $$next-version$$
208     *
209     * @return bool
210     */
211    public static function is_module_active() {
212        return parent::is_module_active() || ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( self::ASSET_CDN_MODULE ) );
213    }
214
215    /**
216     * Turns on the Image CDN, then the Asset CDN on a best-effort basis, like the Jetpack Settings toggle.
217     *
218     * @since $$next-version$$
219     *
220     * @param bool|WP_Error $plugin_activation Is the result of the top level activation actions.
221     * @return bool|WP_Error
222     */
223    public static function do_product_specific_activation( $plugin_activation ) {
224        $result = parent::do_product_specific_activation( $plugin_activation );
225
226        if ( ! is_wp_error( $result ) ) {
227            Jetpack::activate_module( self::ASSET_CDN_MODULE, false, false );
228        }
229
230        return $result;
231    }
232
233    /**
234     * Turns off both CDN modules.
235     *
236     * @since $$next-version$$
237     *
238     * @return bool|WP_Error WP_Error when a host keeps a module on.
239     */
240    public static function deactivate() {
241        // Do not reorder: the parent checks is_module_active(), which still sees the Asset CDN if it is on.
242        $asset_cdn_result = class_exists( 'Jetpack' ) && Jetpack::deactivate_module( self::ASSET_CDN_MODULE );
243        $result           = parent::deactivate();
244
245        // Turning off an Image CDN that was already off returns false, so count the Asset CDN change.
246        return is_wp_error( $result ) ? $result : ( $result || $asset_cdn_result );
247    }
248}