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