Completed
Push — add/bail-on-unsupported-wp ( c702b0...29ae98 )
by
unknown
14:04 queued 07:10
created

Jetpack_Plan::refresh_from_wpcom()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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