Completed
Push — try/e2e-plan-data-stability ( f6df0d...aa1591 )
by Yaroslav
07:02
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
 * Not to be confused with the `Jetpack_Plans` class (in `_inc/lib/plans.php`), which
6
 * fetches general information about all available plans from WordPress.com, side-effect free.
7
 *
8
 * @package Jetpack
9
 */
10
11
use Automattic\Jetpack\Connection\Client;
12
13
/**
14
 * Provides methods methods for fetching the plan from WordPress.com.
15
 */
16
class Jetpack_Plan {
17
	/**
18
	 * A cache variable to hold the active plan for the current request.
19
	 *
20
	 * @var array
21
	 */
22
	private static $active_plan_cache;
23
24
	const PLAN_OPTION = 'jetpack_active_plan';
25
26
	/**
27
	 * Given a response to the `/sites/%d` endpoint, will parse the response and attempt to set the
28
	 * plan from the response.
29
	 *
30
	 * @param array $response The response from `/sites/%d`.
31
	 * @return bool Was the plan successfully updated?
32
	 */
33
	public static function update_from_sites_response( $response ) {
34
		// Bail if there was an error or malformed response.
35
		if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
36
			return false;
37
		}
38
39
		$body = wp_remote_retrieve_body( $response );
40
		if ( is_wp_error( $body ) ) {
41
			return false;
42
		}
43
44
		// Decode the results.
45
		$results = json_decode( $body, true );
46
47
		// Bail if there were no results or plan details returned.
48
		if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) {
49
			return false;
50
		}
51
52
		// Store the new plan in an option and return true if updated.
53
		$result = update_option( self::PLAN_OPTION, $results['plan'], true );
54
		if ( ! $result ) {
55
			// If we got to this point, then we know we need to update. So, assume there is an issue
56
			// with caching. To fix that issue, we can delete the current option and then update.
57
			delete_option( self::PLAN_OPTION );
58
			$result = update_option( self::PLAN_OPTION, $results['plan'], true );
59
		}
60
61
		if ( $result ) {
62
			// Reset the cache since we've just updated the plan.
63
			self::$active_plan_cache = null;
64
		}
65
66
		return $result;
67
	}
68
69
	/**
70
	 * Make an API call to WordPress.com for plan status
71
	 *
72
	 * @uses Jetpack_Options::get_option()
73
	 * @uses Client::wpcom_json_api_request_as_blog()
74
	 * @uses update_option()
75
	 *
76
	 * @access public
77
	 * @static
78
	 *
79
	 * @return bool True if plan is updated, false if no update
80
	 */
81
	public static function refresh_from_wpcom() {
82
		// Make the API request.
83
		$request  = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) );
84
		$response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
85
86
		return self::update_from_sites_response( $response );
87
	}
88
89
	/**
90
	 * Get the plan that this Jetpack site is currently using.
91
	 *
92
	 * @uses get_option()
93
	 *
94
	 * @access public
95
	 * @static
96
	 *
97
	 * @return array Active Jetpack plan details
98
	 */
99
	public static function get() {
100
		$plan      = get_option( self::PLAN_OPTION, array() );
101
		$backtrace = wp_debug_backtrace_summary();
0 ignored issues
show
$backtrace is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
103
		if ( ! empty( $plan ) ) {
104
			error_log( print_r( 'GET PLAN DATA', 1 ) );
105
			error_log( print_r( $plan['product_slug'], 1 ) );
106
		}
107
108
		// this can be expensive to compute so we cache for the duration of a request.
109
		if ( is_array( self::$active_plan_cache ) && ! empty( self::$active_plan_cache ) ) {
110
			return self::$active_plan_cache;
111
		}
112
113
		$plan = get_option( self::PLAN_OPTION, array() );
114
115
		// Set the default options.
116
		$plan = wp_parse_args(
117
			$plan,
118
			array(
119
				'product_slug' => 'jetpack_free',
120
				'class'        => 'free',
121
				'features'     => array(
122
					'active' => array(),
123
				),
124
			)
125
		);
126
127
		$supports = array();
128
129
		// Define what paid modules are supported by personal plans.
130
		$personal_plans = array(
131
			'jetpack_personal',
132
			'jetpack_personal_monthly',
133
			'personal-bundle',
134
			'personal-bundle-monthly',
135
			'personal-bundle-2y',
136
		);
137
138
		if ( in_array( $plan['product_slug'], $personal_plans, true ) ) {
139
			// special support value, not a module but a separate plugin.
140
			$supports[]    = 'akismet';
141
			$supports[]    = 'recurring-payments';
142
			$plan['class'] = 'personal';
143
		}
144
145
		// Define what paid modules are supported by premium plans.
146
		$premium_plans = array(
147
			'jetpack_premium',
148
			'jetpack_premium_monthly',
149
			'value_bundle',
150
			'value_bundle-monthly',
151
			'value_bundle-2y',
152
		);
153
154
		if ( in_array( $plan['product_slug'], $premium_plans, true ) ) {
155
			$supports[]    = 'akismet';
156
			$supports[]    = 'recurring-payments';
157
			$supports[]    = 'simple-payments';
158
			$supports[]    = 'vaultpress';
159
			$supports[]    = 'videopress';
160
			$plan['class'] = 'premium';
161
		}
162
163
		// Define what paid modules are supported by professional plans.
164
		$business_plans = array(
165
			'jetpack_business',
166
			'jetpack_business_monthly',
167
			'business-bundle',
168
			'business-bundle-monthly',
169
			'business-bundle-2y',
170
			'ecommerce-bundle',
171
			'ecommerce-bundle-monthly',
172
			'ecommerce-bundle-2y',
173
			'vip',
174
		);
175
176
		if ( in_array( $plan['product_slug'], $business_plans, true ) ) {
177
			$supports[]    = 'akismet';
178
			$supports[]    = 'recurring-payments';
179
			$supports[]    = 'simple-payments';
180
			$supports[]    = 'vaultpress';
181
			$supports[]    = 'videopress';
182
			$plan['class'] = 'business';
183
		}
184
185
		// get available features.
186
		foreach ( Jetpack::get_available_modules() as $module_slug ) {
187
			$module = Jetpack::get_module( $module_slug );
188
			if ( ! isset( $module ) || ! is_array( $module ) ) {
189
				continue;
190
			}
191
			if ( in_array( 'free', $module['plan_classes'], true ) || in_array( $plan['class'], $module['plan_classes'], true ) ) {
192
				$supports[] = $module_slug;
193
			}
194
		}
195
196
		$plan['supports'] = $supports;
197
198
		self::$active_plan_cache = $plan;
199
200
		return $plan;
201
	}
202
203
	/**
204
	 * Determine whether the active plan supports a particular feature
205
	 *
206
	 * @uses Jetpack_Plan::get()
207
	 *
208
	 * @access public
209
	 * @static
210
	 *
211
	 * @param string $feature The module or feature to check.
212
	 *
213
	 * @return bool True if plan supports feature, false if not
214
	 */
215
	public static function supports( $feature ) {
216
		$plan = self::get();
217
218
		// Manually mapping WordPress.com features to Jetpack module slugs.
219
		foreach ( $plan['features']['active'] as $wpcom_feature ) {
220
			switch ( $wpcom_feature ) {
221
				case 'wordads-jetpack':
222
					// WordAds are supported for this site.
223
					if ( 'wordads' === $feature ) {
224
						return true;
225
					}
226
					break;
227
			}
228
		}
229
230
		if (
231
			in_array( $feature, $plan['supports'], true )
232
			|| in_array( $feature, $plan['features']['active'], true )
233
		) {
234
			return true;
235
		}
236
237
		return false;
238
	}
239
}
240