Completed
Push — remove/welcome-new-plan ( 1f5f3f )
by
unknown
81:32 queued 74:29
created

Jetpack_Plan   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 220
Duplicated Lines 6.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

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