Completed
Push — update/plan-refactor-logic-to-... ( b9d862 )
by
unknown
102:47 queued 83:44
created

Jetpack_Plan::refresh_from_wpcom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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