Completed
Push — try/e2e-plan-data-stability ( 2d3ec1...84547c )
by Yaroslav
13:05 queued 05:55
created

class.jetpack-plan.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $active_plan_cache.

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..

Loading history...
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
		$plan      = get_option( self::PLAN_OPTION, array() );
101
		$backtrace = wp_debug_backtrace_summary();
102
103
		if ( ! empty( $plan ) ) {
104
			error_log( print_r( 'GET PLAN DATA', 1 ) );
105
			error_log( print_r( $plan['product_slug'], 1 ) );
106
			error_log( print_r( $backtrace, 1 ) );
107
		}
108
109
		// this can be expensive to compute so we cache for the duration of a request.
110
		if ( is_array( self::$active_plan_cache ) && ! empty( self::$active_plan_cache ) ) {
111
			return self::$active_plan_cache;
112
		}
113
114
		$plan = get_option( self::PLAN_OPTION, array() );
115
116
		// Set the default options.
117
		$plan = wp_parse_args(
118
			$plan,
119
			array(
120
				'product_slug' => 'jetpack_free',
121
				'class'        => 'free',
122
				'features'     => array(
123
					'active' => array(),
124
				),
125
			)
126
		);
127
128
		$supports = array();
129
130
		// Define what paid modules are supported by personal plans.
131
		$personal_plans = array(
132
			'jetpack_personal',
133
			'jetpack_personal_monthly',
134
			'personal-bundle',
135
			'personal-bundle-monthly',
136
			'personal-bundle-2y',
137
		);
138
139
		if ( in_array( $plan['product_slug'], $personal_plans, true ) ) {
140
			// special support value, not a module but a separate plugin.
141
			$supports[]    = 'akismet';
142
			$supports[]    = 'recurring-payments';
143
			$plan['class'] = 'personal';
144
		}
145
146
		// Define what paid modules are supported by premium plans.
147
		$premium_plans = array(
148
			'jetpack_premium',
149
			'jetpack_premium_monthly',
150
			'value_bundle',
151
			'value_bundle-monthly',
152
			'value_bundle-2y',
153
		);
154
155
		if ( in_array( $plan['product_slug'], $premium_plans, true ) ) {
156
			$supports[]    = 'akismet';
157
			$supports[]    = 'recurring-payments';
158
			$supports[]    = 'simple-payments';
159
			$supports[]    = 'vaultpress';
160
			$supports[]    = 'videopress';
161
			$plan['class'] = 'premium';
162
		}
163
164
		// Define what paid modules are supported by professional plans.
165
		$business_plans = array(
166
			'jetpack_business',
167
			'jetpack_business_monthly',
168
			'business-bundle',
169
			'business-bundle-monthly',
170
			'business-bundle-2y',
171
			'ecommerce-bundle',
172
			'ecommerce-bundle-monthly',
173
			'ecommerce-bundle-2y',
174
			'vip',
175
		);
176
177
		if ( in_array( $plan['product_slug'], $business_plans, true ) ) {
178
			$supports[]    = 'akismet';
179
			$supports[]    = 'recurring-payments';
180
			$supports[]    = 'simple-payments';
181
			$supports[]    = 'vaultpress';
182
			$supports[]    = 'videopress';
183
			$plan['class'] = 'business';
184
		}
185
186
		// get available features.
187
		foreach ( Jetpack::get_available_modules() as $module_slug ) {
188
			$module = Jetpack::get_module( $module_slug );
189
			if ( ! isset( $module ) || ! is_array( $module ) ) {
190
				continue;
191
			}
192
			if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) {
193
				$supports[] = $module_slug;
194
			}
195
		}
196
197
		$plan['supports'] = $supports;
198
199
		self::$active_plan_cache = $plan;
200
201
		return $plan;
202
	}
203
204
	/**
205
	 * Determine whether the active plan supports a particular feature
206
	 *
207
	 * @uses Jetpack_Plan::get()
208
	 *
209
	 * @access public
210
	 * @static
211
	 *
212
	 * @param string $feature The module or feature to check.
213
	 *
214
	 * @return bool True if plan supports feature, false if not
215
	 */
216
	public static function supports( $feature ) {
217
		$plan = self::get();
218
219
		// Manually mapping WordPress.com features to Jetpack module slugs.
220
		foreach ( $plan['features']['active'] as $wpcom_feature ) {
221
			switch ( $wpcom_feature ) {
222
				case 'wordads-jetpack':
223
					// WordAds are supported for this site.
224
					if ( 'wordads' === $feature ) {
225
						return true;
226
					}
227
					break;
228
			}
229
		}
230
231
		if (
232
			in_array( $feature, $plan['supports'], true )
233
			|| in_array( $feature, $plan['features']['active'], true )
234
		) {
235
			return true;
236
		}
237
238
		return false;
239
	}
240
}
241