PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.8.2
Jetpack – WP Security, Backup, Speed, & Growth v10.8.2
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 10.8.2, at class.jetpack-plan.php

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