PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.8.3
Jetpack – WP Security, Backup, Speed, & Growth v7.8.3
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
229 lines 6.0 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 /**
27 * Given a response to the `/sites/%d` endpoint, will parse the response and attempt to set the
28 * plan from the response.
29 *
30 * @param array $response The response from `/sites/%d`.
31 * @return bool Was the plan successfully updated?
32 */
33 public static function update_from_sites_response( $response ) {
34 // Bail if there was an error or malformed response.
35 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
36 return false;
37 }
38
39 $body = wp_remote_retrieve_body( $response );
40 if ( is_wp_error( $body ) ) {
41 return false;
42 }
43
44 // Decode the results.
45 $results = json_decode( $body, true );
46
47 // Bail if there were no results or plan details returned.
48 if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) {
49 return false;
50 }
51
52 // Store the new plan in an option and return true if updated.
53 $result = update_option( self::PLAN_OPTION, $results['plan'], true );
54 if ( ! $result ) {
55 // If we got to this point, then we know we need to update. So, assume there is an issue
56 // with caching. To fix that issue, we can delete the current option and then update.
57 delete_option( self::PLAN_OPTION );
58 $result = update_option( self::PLAN_OPTION, $results['plan'], true );
59 }
60
61 if ( $result ) {
62 // Reset the cache since we've just updated the plan.
63 self::$active_plan_cache = null;
64 }
65
66 return $result;
67 }
68
69 /**
70 * Make an API call to WordPress.com for plan status
71 *
72 * @uses Jetpack_Options::get_option()
73 * @uses Client::wpcom_json_api_request_as_blog()
74 * @uses update_option()
75 *
76 * @access public
77 * @static
78 *
79 * @return bool True if plan is updated, false if no update
80 */
81 public static function refresh_from_wpcom() {
82 // Make the API request.
83 $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) );
84 $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
85
86 return self::update_from_sites_response( $response );
87 }
88
89 /**
90 * Get the plan that this Jetpack site is currently using.
91 *
92 * @uses get_option()
93 *
94 * @access public
95 * @static
96 *
97 * @return array Active Jetpack plan details
98 */
99 public static function get() {
100 // this can be expensive to compute so we cache for the duration of a request.
101 if ( is_array( self::$active_plan_cache ) && ! empty( self::$active_plan_cache ) ) {
102 return self::$active_plan_cache;
103 }
104
105 $plan = get_option( self::PLAN_OPTION, array() );
106
107 // Set the default options.
108 $plan = wp_parse_args(
109 $plan,
110 array(
111 'product_slug' => 'jetpack_free',
112 'class' => 'free',
113 'features' => array(
114 'active' => array(),
115 ),
116 )
117 );
118
119 $supports = array();
120
121 // Define what paid modules are supported by personal plans.
122 $personal_plans = array(
123 'jetpack_personal',
124 'jetpack_personal_monthly',
125 'personal-bundle',
126 'personal-bundle-monthly',
127 'personal-bundle-2y',
128 );
129
130 if ( in_array( $plan['product_slug'], $personal_plans, true ) ) {
131 // special support value, not a module but a separate plugin.
132 $supports[] = 'akismet';
133 $plan['class'] = 'personal';
134 }
135
136 // Define what paid modules are supported by premium plans.
137 $premium_plans = array(
138 'jetpack_premium',
139 'jetpack_premium_monthly',
140 'value_bundle',
141 'value_bundle-monthly',
142 'value_bundle-2y',
143 );
144
145 if ( in_array( $plan['product_slug'], $premium_plans, true ) ) {
146 $supports[] = 'akismet';
147 $supports[] = 'simple-payments';
148 $supports[] = 'vaultpress';
149 $supports[] = 'videopress';
150 $plan['class'] = 'premium';
151 }
152
153 // Define what paid modules are supported by professional plans.
154 $business_plans = array(
155 'jetpack_business',
156 'jetpack_business_monthly',
157 'business-bundle',
158 'business-bundle-monthly',
159 'business-bundle-2y',
160 'ecommerce-bundle',
161 'ecommerce-bundle-monthly',
162 'ecommerce-bundle-2y',
163 'vip',
164 );
165
166 if ( in_array( $plan['product_slug'], $business_plans, true ) ) {
167 $supports[] = 'akismet';
168 $supports[] = 'simple-payments';
169 $supports[] = 'vaultpress';
170 $supports[] = 'videopress';
171 $plan['class'] = 'business';
172 }
173
174 // get available features.
175 foreach ( Jetpack::get_available_modules() as $module_slug ) {
176 $module = Jetpack::get_module( $module_slug );
177 if ( ! isset( $module ) || ! is_array( $module ) ) {
178 continue;
179 }
180 if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) {
181 $supports[] = $module_slug;
182 }
183 }
184
185 $plan['supports'] = $supports;
186
187 self::$active_plan_cache = $plan;
188
189 return $plan;
190 }
191
192 /**
193 * Determine whether the active plan supports a particular feature
194 *
195 * @uses Jetpack_Plan::get()
196 *
197 * @access public
198 * @static
199 *
200 * @param string $feature The module or feature to check.
201 *
202 * @return bool True if plan supports feature, false if not
203 */
204 public static function supports( $feature ) {
205 $plan = self::get();
206
207 // Manually mapping WordPress.com features to Jetpack module slugs.
208 foreach ( $plan['features']['active'] as $wpcom_feature ) {
209 switch ( $wpcom_feature ) {
210 case 'wordads-jetpack':
211 // WordAds are supported for this site.
212 if ( 'wordads' === $feature ) {
213 return true;
214 }
215 break;
216 }
217 }
218
219 if (
220 in_array( $feature, $plan['supports'], true )
221 || in_array( $feature, $plan['features']['active'], true )
222 ) {
223 return true;
224 }
225
226 return false;
227 }
228 }
229