Completed
Push — add/module-registration-refact... ( af2fe6...74b9d5 )
by
unknown
295:53 queued 288:51
created

Jetpack_Plan   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 221
Duplicated Lines 6.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 3
dl 14
loc 221
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C update_from_sites_response() 0 47 12
A refresh_from_wpcom() 0 7 1
C get() 14 88 11
B supports() 0 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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-2y',
134
		);
135
136
		if ( in_array( $plan['product_slug'], $personal_plans, true ) ) {
137
			// special support value, not a module but a separate plugin.
138
			$supports[]    = 'akismet';
139
			$plan['class'] = 'personal';
140
		}
141
142
		// Define what paid modules are supported by premium plans.
143
		$premium_plans = array(
144
			'jetpack_premium',
145
			'jetpack_premium_monthly',
146
			'value_bundle',
147
			'value_bundle-2y',
148
		);
149
150 View Code Duplication
		if ( in_array( $plan['product_slug'], $premium_plans, true ) ) {
151
			$supports[]    = 'akismet';
152
			$supports[]    = 'simple-payments';
153
			$supports[]    = 'vaultpress';
154
			$supports[]    = 'videopress';
155
			$plan['class'] = 'premium';
156
		}
157
158
		// Define what paid modules are supported by professional plans.
159
		$business_plans = array(
160
			'jetpack_business',
161
			'jetpack_business_monthly',
162
			'business-bundle',
163
			'business-bundle-2y',
164
			'ecommerce-bundle',
165
			'ecommerce-bundle-2y',
166
			'vip',
167
		);
168
169 View Code Duplication
		if ( in_array( $plan['product_slug'], $business_plans, true ) ) {
170
			$supports[]    = 'akismet';
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