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

269 lines 6.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 from WordPress.com and caching the value 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 Jetpack
9 */
10
11 use Automattic\Jetpack\Connection\Client;
12
13 /**
14 * Provides methods methods for fetching the plan 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 const PLAN_OPTION = 'jetpack_active_plan';
25
26 const PLAN_DATA = array(
27 'free' => array(
28 'plans' => array(
29 'jetpack_free',
30 ),
31 'supports' => array(
32 'opentable',
33 'calendly',
34 'send-a-message',
35 ),
36 ),
37 'personal' => array(
38 'plans' => array(
39 'jetpack_personal',
40 'jetpack_personal_monthly',
41 'personal-bundle',
42 'personal-bundle-monthly',
43 'personal-bundle-2y',
44 ),
45 'supports' => array(
46 'akismet',
47 'donations',
48 'recurring-payments',
49 ),
50 ),
51 'premium' => array(
52 'plans' => array(
53 'jetpack_premium',
54 'jetpack_premium_monthly',
55 'value_bundle',
56 'value_bundle-monthly',
57 'value_bundle-2y',
58 ),
59 'supports' => array(
60 'simple-payments',
61 'vaultpress',
62 'videopress',
63 ),
64 ),
65 'business' => array(
66 'plans' => array(
67 'jetpack_business',
68 'jetpack_business_monthly',
69 'business-bundle',
70 'business-bundle-monthly',
71 'business-bundle-2y',
72 'ecommerce-bundle',
73 'ecommerce-bundle-monthly',
74 'ecommerce-bundle-2y',
75 'vip',
76 ),
77 'supports' => array(),
78 ),
79 );
80
81 /**
82 * Given a response to the `/sites/%d` endpoint, will parse the response and attempt to set the
83 * plan from the response.
84 *
85 * @param array $response The response from `/sites/%d`.
86 * @return bool Was the plan successfully updated?
87 */
88 public static function update_from_sites_response( $response ) {
89 // Bail if there was an error or malformed response.
90 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
91 return false;
92 }
93
94 $body = wp_remote_retrieve_body( $response );
95 if ( is_wp_error( $body ) ) {
96 return false;
97 }
98
99 // Decode the results.
100 $results = json_decode( $body, true );
101
102 // Bail if there were no results or plan details returned.
103 if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) {
104 return false;
105 }
106
107 // Store the new plan in an option and return true if updated.
108 $result = update_option( self::PLAN_OPTION, $results['plan'], true );
109 if ( ! $result ) {
110 // If we got to this point, then we know we need to update. So, assume there is an issue
111 // with caching. To fix that issue, we can delete the current option and then update.
112 delete_option( self::PLAN_OPTION );
113 $result = update_option( self::PLAN_OPTION, $results['plan'], true );
114 }
115
116 if ( $result ) {
117 // Reset the cache since we've just updated the plan.
118 self::$active_plan_cache = null;
119 }
120
121 return $result;
122 }
123
124 /**
125 * Make an API call to WordPress.com for plan status
126 *
127 * @uses Jetpack_Options::get_option()
128 * @uses Client::wpcom_json_api_request_as_blog()
129 * @uses update_option()
130 *
131 * @access public
132 * @static
133 *
134 * @return bool True if plan is updated, false if no update
135 */
136 public static function refresh_from_wpcom() {
137 // Make the API request.
138 $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) );
139 $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
140
141 return self::update_from_sites_response( $response );
142 }
143
144 /**
145 * Get the plan that this Jetpack site is currently using.
146 *
147 * @uses get_option()
148 *
149 * @access public
150 * @static
151 *
152 * @return array Active Jetpack plan details
153 */
154 public static function get() {
155 // this can be expensive to compute so we cache for the duration of a request.
156 if ( is_array( self::$active_plan_cache ) && ! empty( self::$active_plan_cache ) ) {
157 return self::$active_plan_cache;
158 }
159
160 $plan = get_option( self::PLAN_OPTION, array() );
161
162 // Set the default options.
163 $plan = wp_parse_args(
164 $plan,
165 array(
166 'product_slug' => 'jetpack_free',
167 'class' => 'free',
168 'features' => array(
169 'active' => array(),
170 ),
171 )
172 );
173
174 list( $plan['class'], $supports ) = self::get_class_and_features( $plan['product_slug'] );
175
176 // get available features.
177 foreach ( Jetpack::get_available_modules() as $module_slug ) {
178 $module = Jetpack::get_module( $module_slug );
179 if ( ! isset( $module ) || ! is_array( $module ) ) {
180 continue;
181 }
182 if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) {
183 $supports[] = $module_slug;
184 }
185 }
186
187 $plan['supports'] = $supports;
188
189 self::$active_plan_cache = $plan;
190
191 return $plan;
192 }
193
194 /**
195 * Get the class of plan and a list of features it supports
196 *
197 * @param string $plan_slug The plan that we're interested in.
198 * @return array Two item array, the plan class and the an array of features.
199 */
200 private static function get_class_and_features( $plan_slug ) {
201 $features = array();
202 foreach ( self::PLAN_DATA as $class => $details ) {
203 $features = array_merge( $features, $details['supports'] );
204 if ( in_array( $plan_slug, $details['plans'], true ) ) {
205 return array( $class, $features );
206 }
207 }
208 return array( 'free', self::PLAN_DATA['free']['supports'] );
209 }
210
211 /**
212 * Gets the minimum plan slug that supports the given feature
213 *
214 * @param string $feature The name of the feature.
215 * @return string|bool The slug for the minimum plan that supports.
216 * the feature or false if not found
217 */
218 public static function get_minimum_plan_for_feature( $feature ) {
219 foreach ( self::PLAN_DATA as $class => $details ) {
220 if ( in_array( $feature, $details['supports'], true ) ) {
221 return $details['plans'][0];
222 }
223 }
224 return false;
225 }
226
227 /**
228 * Determine whether the active plan supports a particular feature
229 *
230 * @uses Jetpack_Plan::get()
231 *
232 * @access public
233 * @static
234 *
235 * @param string $feature The module or feature to check.
236 *
237 * @return bool True if plan supports feature, false if not
238 */
239 public static function supports( $feature ) {
240 // Search product bypasses plan feature check.
241 if ( 'search' === $feature && (bool) get_option( 'has_jetpack_search_product' ) ) {
242 return true;
243 }
244
245 $plan = self::get();
246
247 // Manually mapping WordPress.com features to Jetpack module slugs.
248 foreach ( $plan['features']['active'] as $wpcom_feature ) {
249 switch ( $wpcom_feature ) {
250 case 'wordads-jetpack':
251 // WordAds are supported for this site.
252 if ( 'wordads' === $feature ) {
253 return true;
254 }
255 break;
256 }
257 }
258
259 if (
260 in_array( $feature, $plan['supports'], true )
261 || in_array( $feature, $plan['features']['active'], true )
262 ) {
263 return true;
264 }
265
266 return false;
267 }
268 }
269