Completed
Push — renovate/jest-monorepo ( bd2eaf...d289c3 )
by
unknown
44:07 queued 37:28
created

Jetpack_Plans   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_plans() 0 33 5
A get_plan() 0 12 4
1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Plans Library
4
 *
5
 * Fetch plans data from WordPress.com.
6
 *
7
 * Not to be confused with the `Jetpack_Plan` (singular)
8
 * class, which stores and syncs data about the site's _current_ plan.
9
 *
10
 * @package Jetpack
11
 */
12
class Jetpack_Plans {
13
	/**
14
	 * Get a list of all available plans from WordPress.com
15
	 *
16
	 * @since 7.7.0
17
	 *
18
	 * @return array The plans list
19
	 */
20
	public static function get_plans() {
21
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
22
			if ( ! class_exists( 'Store_Product_List' ) ) {
23
				require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
24
			}
25
26
			return Store_Product_List::get_active_plans_v1_5();
27
		}
28
29
		// We're on Jetpack, so it's safe to use this namespace.
30
		$request = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
31
			'/plans?_locale=' . get_user_locale(),
32
			// We're using version 1.5 of the endpoint rather than the default version 2
33
			// since the latter only returns Jetpack Plans, but we're also interested in
34
			// WordPress.com plans, for consumers of this method that run on WP.com.
35
			'1.5',
36
			array(
37
				'method'  => 'GET',
38
				'headers' => array(
39
					'X-Forwarded-For' => Jetpack::current_user_ip( true ),
40
				),
41
			),
42
			null,
43
			'rest'
44
		);
45
46
		$body = wp_remote_retrieve_body( $request );
47
		if ( 200 === wp_remote_retrieve_response_code( $request ) ) {
48
			return json_decode( $body );
49
		} else {
50
			return $body;
51
		}
52
	}
53
54
	/**
55
	 * Get plan information for a plan given its slug
56
	 *
57
	 * @since 7.7.0
58
	 *
59
	 * @param string $plan_slug Plan slug.
60
	 *
61
	 * @return object The plan object
62
	 */
63
	public static function get_plan( $plan_slug ) {
64
		$plans = self::get_plans();
65
		if ( ! is_array( $plans ) ) {
66
			return;
67
		}
68
69
		foreach ( $plans as $plan ) {
70
			if ( $plan_slug === $plan->product_slug ) {
71
				return $plan;
72
			}
73
		}
74
	}
75
}
76