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

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