Completed
Push — update/plan-refactor-logic-to-... ( 6c1b0a...6fa81c )
by
unknown
113:54 queued 106:58
created

Jetpack_Plan::update_from_sites_response()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 12
nop 1
dl 0
loc 47
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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