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