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