These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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; |
||
0 ignored issues
–
show
|
|||
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 | $supports[] = 'recurring-payments'; |
||
134 | $plan['class'] = 'personal'; |
||
135 | } |
||
136 | |||
137 | // Define what paid modules are supported by premium plans. |
||
138 | $premium_plans = array( |
||
139 | 'jetpack_premium', |
||
140 | 'jetpack_premium_monthly', |
||
141 | 'value_bundle', |
||
142 | 'value_bundle-monthly', |
||
143 | 'value_bundle-2y', |
||
144 | ); |
||
145 | |||
146 | View Code Duplication | if ( in_array( $plan['product_slug'], $premium_plans, true ) ) { |
|
147 | $supports[] = 'akismet'; |
||
148 | $supports[] = 'recurring-payments'; |
||
149 | $supports[] = 'simple-payments'; |
||
150 | $supports[] = 'vaultpress'; |
||
151 | $supports[] = 'videopress'; |
||
152 | $plan['class'] = 'premium'; |
||
153 | } |
||
154 | |||
155 | // Define what paid modules are supported by professional plans. |
||
156 | $business_plans = array( |
||
157 | 'jetpack_business', |
||
158 | 'jetpack_business_monthly', |
||
159 | 'business-bundle', |
||
160 | 'business-bundle-monthly', |
||
161 | 'business-bundle-2y', |
||
162 | 'ecommerce-bundle', |
||
163 | 'ecommerce-bundle-monthly', |
||
164 | 'ecommerce-bundle-2y', |
||
165 | 'vip', |
||
166 | ); |
||
167 | |||
168 | View Code Duplication | if ( in_array( $plan['product_slug'], $business_plans, true ) ) { |
|
169 | $supports[] = 'akismet'; |
||
170 | $supports[] = 'recurring-payments'; |
||
171 | $supports[] = 'simple-payments'; |
||
172 | $supports[] = 'vaultpress'; |
||
173 | $supports[] = 'videopress'; |
||
174 | $plan['class'] = 'business'; |
||
175 | } |
||
176 | |||
177 | // get available features. |
||
178 | foreach ( Jetpack::get_available_modules() as $module_slug ) { |
||
179 | $module = Jetpack::get_module( $module_slug ); |
||
180 | if ( ! isset( $module ) || ! is_array( $module ) ) { |
||
181 | continue; |
||
182 | } |
||
183 | if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) { |
||
184 | $supports[] = $module_slug; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | $plan['supports'] = $supports; |
||
189 | |||
190 | self::$active_plan_cache = $plan; |
||
191 | |||
192 | return $plan; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Determine whether the active plan supports a particular feature |
||
197 | * |
||
198 | * @uses Jetpack_Plan::get() |
||
199 | * |
||
200 | * @access public |
||
201 | * @static |
||
202 | * |
||
203 | * @param string $feature The module or feature to check. |
||
204 | * |
||
205 | * @return bool True if plan supports feature, false if not |
||
206 | */ |
||
207 | public static function supports( $feature ) { |
||
208 | $plan = self::get(); |
||
209 | |||
210 | // Manually mapping WordPress.com features to Jetpack module slugs. |
||
211 | foreach ( $plan['features']['active'] as $wpcom_feature ) { |
||
212 | switch ( $wpcom_feature ) { |
||
213 | case 'wordads-jetpack': |
||
214 | // WordAds are supported for this site. |
||
215 | if ( 'wordads' === $feature ) { |
||
216 | return true; |
||
217 | } |
||
218 | break; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | if ( |
||
223 | in_array( $feature, $plan['supports'], true ) |
||
224 | || in_array( $feature, $plan['features']['active'], true ) |
||
225 | ) { |
||
226 | return true; |
||
227 | } |
||
228 | |||
229 | return false; |
||
230 | } |
||
231 | } |
||
232 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..