PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.4.1
Jetpack – WP Security, Backup, Speed, & Growth v11.4.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack-plan.php

class.jetpack-plan.php in Jetpack – WP Security, Backup, Speed, & Growth 11.4.1, at class.jetpack-plan.php

360 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Handles fetching of the site's plan and products from WordPress.com and caching values locally.
4 *
5 * Not to be confused with the `Jetpack_Plans` class (in `_inc/lib/plans.php`), which
6 * fetches general information about all available plans from WordPress.com, side-effect free.
7 *
8 * @package automattic/jetpack
9 */
10
11 use Automattic\Jetpack\Connection\Client;
12
13 /**
14 * Provides methods methods for fetching the site's plan and products from WordPress.com.
15 */
16 class Jetpack_Plan {
17 /**
18 * A cache variable to hold the active plan for the current request.
19 *
20 * @var array
21 */
22 private static $active_plan_cache;
23
24 /**
25 * The name of the option that will store the site's plan.
26 *
27 * @var string
28 */
29 const PLAN_OPTION = 'jetpack_active_plan';
30
31 /**
32 * The name of the option that will store the site's products.
33 *
34 * @var string
35 */
36 const SITE_PRODUCTS_OPTION = 'jetpack_site_products';
37
38 const PLAN_DATA = array(
39 'free' => array(
40 'plans' => array(
41 'jetpack_free',
42 ),
43 'supports' => array(
44 'opentable',
45 'calendly',
46 'send-a-message',
47 'whatsapp-button',
48 'social-previews',
49 'videopress',
50
51 'core/video',
52 'core/cover',
53 'core/audio',
54 ),
55 ),
56 'personal' => array(
57 'plans' => array(
58 'jetpack_personal',
59 'jetpack_personal_monthly',
60 'personal-bundle',
61 'personal-bundle-monthly',
62 'personal-bundle-2y',
63 'starter-plan',
64 ),
65 'supports' => array(
66 'akismet',
67 'payments',
68 'recurring-payments',
69 'premium-content/container',
70 'videopress',
71 ),
72 ),
73 'premium' => array(
74 'plans' => array(
75 'jetpack_premium',
76 'jetpack_premium_monthly',
77 'value_bundle',
78 'value_bundle-monthly',
79 'value_bundle-2y',
80 ),
81 'supports' => array(
82 'donations',
83 'simple-payments',
84 'vaultpress',
85 'videopress',
86 'republicize',
87 ),
88 ),
89 'security' => array(
90 'plans' => array(
91 'jetpack_security_daily',
92 'jetpack_security_daily_monthly',
93 'jetpack_security_realtime',
94 'jetpack_security_realtime_monthly',
95 'jetpack_security_t1_yearly',
96 'jetpack_security_t1_monthly',
97 'jetpack_security_t2_yearly',
98 'jetpack_security_t2_monthly',
99 ),
100 'supports' => array(),
101 ),
102 'business' => array(
103 'plans' => array(
104 'jetpack_business',
105 'jetpack_business_monthly',
106 'business-bundle',
107 'business-bundle-monthly',
108 'business-bundle-2y',
109 'ecommerce-bundle',
110 'ecommerce-bundle-monthly',
111 'ecommerce-bundle-2y',
112 'pro-plan',
113 ),
114 'supports' => array(),
115 ),
116
117 'complete' => array(
118 'plans' => array(
119 'jetpack_complete',
120 'jetpack_complete_monthly',
121 'vip',
122 ),
123 'supports' => array(),
124 ),
125 );
126
127 /**
128 * Given a response to the `/sites/%d` endpoint, will parse the response and attempt to set the
129 * site's plan and products from the response.
130 *
131 * @param array $response The response from `/sites/%d`.
132 * @return bool Was the plan successfully updated?
133 */
134 public static function update_from_sites_response( $response ) {
135 // Bail if there was an error or malformed response.
136 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
137 return false;
138 }
139
140 $body = wp_remote_retrieve_body( $response );
141 if ( is_wp_error( $body ) ) {
142 return false;
143 }
144
145 // Decode the results.
146 $results = json_decode( $body, true );
147
148 if ( ! is_array( $results ) ) {
149 return false;
150 }
151
152 if ( isset( $results['products'] ) ) {
153 // Store the site's products in an option and return true if updated.
154 self::store_data_in_option( self::SITE_PRODUCTS_OPTION, $results['products'] );
155 }
156
157 if ( ! isset( $results['plan'] ) ) {
158 return false;
159 }
160
161 $current_plan = get_option( self::PLAN_OPTION, array() );
162
163 if ( ! empty( $current_plan ) && $current_plan === $results['plan'] ) {
164 // Bail if the plans array hasn't changed.
165 return false;
166 }
167
168 // Store the new plan in an option and return true if updated.
169 $result = self::store_data_in_option( self::PLAN_OPTION, $results['plan'] );
170
171 if ( $result ) {
172 // Reset the cache since we've just updated the plan.
173 self::$active_plan_cache = null;
174 }
175
176 return $result;
177 }
178
179 /**
180 * Store data in an option.
181 *
182 * @param string $option The name of the option that will store the data.
183 * @param array $data Data to be store in an option.
184 * @return bool Were the subscriptions successfully updated?
185 */
186 private static function store_data_in_option( $option, $data ) {
187 $result = update_option( $option, $data, true );
188
189 // If something goes wrong with the update, so delete the current option and then update it.
190 if ( ! $result ) {
191 delete_option( $option );
192 $result = update_option( $option, $data, true );
193 }
194
195 return $result;
196 }
197
198 /**
199 * Make an API call to WordPress.com for plan status
200 *
201 * @uses Jetpack_Options::get_option()
202 * @uses Client::wpcom_json_api_request_as_blog()
203 * @uses update_option()
204 *
205 * @access public
206 * @static
207 *
208 * @return bool True if plan is updated, false if no update
209 */
210 public static function refresh_from_wpcom() {
211 // Make the API request.
212 $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) );
213 $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
214
215 return self::update_from_sites_response( $response );
216 }
217
218 /**
219 * Get the plan that this Jetpack site is currently using.
220 *
221 * @uses get_option()
222 *
223 * @access public
224 * @static
225 *
226 * @return array Active Jetpack plan details
227 */
228 public static function get() {
229 // this can be expensive to compute so we cache for the duration of a request.
230 if ( is_array( self::$active_plan_cache ) && ! empty( self::$active_plan_cache ) ) {
231 return self::$active_plan_cache;
232 }
233
234 $plan = get_option( self::PLAN_OPTION, array() );
235
236 // Set the default options.
237 $plan = wp_parse_args(
238 $plan,
239 array(
240 'product_slug' => 'jetpack_free',
241 'class' => 'free',
242 'features' => array(
243 'active' => array(),
244 ),
245 )
246 );
247
248 list( $plan['class'], $supports ) = self::get_class_and_features( $plan['product_slug'] );
249
250 // get available features.
251 foreach ( Jetpack::get_available_modules() as $module_slug ) {
252 $module = Jetpack::get_module( $module_slug );
253 if ( ! isset( $module ) || ! is_array( $module ) ) {
254 continue;
255 }
256 if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) {
257 $supports[] = $module_slug;
258 }
259 }
260
261 $plan['supports'] = $supports;
262
263 self::$active_plan_cache = $plan;
264
265 return $plan;
266 }
267
268 /**
269 * Get the site's products.
270 *
271 * @uses get_option()
272 *
273 * @access public
274 * @static
275 *
276 * @return array Active Jetpack products
277 */
278 public static function get_products() {
279 return get_option( self::SITE_PRODUCTS_OPTION, array() );
280 }
281
282 /**
283 * Get the class of plan and a list of features it supports
284 *
285 * @param string $plan_slug The plan that we're interested in.
286 * @return array Two item array, the plan class and the an array of features.
287 */
288 private static function get_class_and_features( $plan_slug ) {
289 $features = array();
290 foreach ( self::PLAN_DATA as $class => $details ) {
291 $features = array_merge( $features, $details['supports'] );
292 if ( in_array( $plan_slug, $details['plans'], true ) ) {
293 return array( $class, $features );
294 }
295 }
296 return array( 'free', self::PLAN_DATA['free']['supports'] );
297 }
298
299 /**
300 * Gets the minimum plan slug that supports the given feature
301 *
302 * @param string $feature The name of the feature.
303 * @return string|bool The slug for the minimum plan that supports.
304 * the feature or false if not found
305 */
306 public static function get_minimum_plan_for_feature( $feature ) {
307 foreach ( self::PLAN_DATA as $details ) {
308 if ( in_array( $feature, $details['supports'], true ) ) {
309 return $details['plans'][0];
310 }
311 }
312 return false;
313 }
314
315 /**
316 * Determine whether the active plan supports a particular feature
317 *
318 * @uses Jetpack_Plan::get()
319 *
320 * @access public
321 * @static
322 *
323 * @param string $feature The module or feature to check.
324 *
325 * @return bool True if plan supports feature, false if not
326 */
327 public static function supports( $feature ) {
328 // Hijack the feature eligibility check on WordPress.com sites since they are gated differently.
329 $should_wpcom_gate_feature = (
330 function_exists( 'wpcom_site_has_feature' ) &&
331 function_exists( 'wpcom_feature_exists' ) &&
332 wpcom_feature_exists( $feature )
333 );
334 if ( $should_wpcom_gate_feature ) {
335 return wpcom_site_has_feature( $feature );
336 }
337
338 // Search product bypasses plan feature check.
339 if ( 'search' === $feature && (bool) get_option( 'has_jetpack_search_product' ) ) {
340 return true;
341 }
342
343 // As of Q3 2021 - a videopress free tier is available to all plans.
344 if ( 'videopress' === $feature ) {
345 return true;
346 }
347
348 $plan = self::get();
349
350 if (
351 in_array( $feature, $plan['supports'], true )
352 || in_array( $feature, $plan['features']['active'], true )
353 ) {
354 return true;
355 }
356
357 return false;
358 }
359 }
360