Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plans
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 1
 get_plans
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
30
 get_plan
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 get_plan_short_name
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * Plans Library
4 *
5 * Fetch plans data from WordPress.com.
6 *
7 * This file was copied and adapted from the Jetpack plugin on Mar 2022.
8 *
9 * @package automattic/jetpack-plans
10 */
11
12namespace Automattic\Jetpack;
13
14use Store_Product_List;
15
16/**
17 * Fetch data about available Plans from WordPress.com
18 */
19class Plans {
20    /**
21     * Get a list of all available plans from WordPress.com
22     *
23     * @since-jetpack 7.7.0
24     * @since 0.16.0 Accepts request arguments.
25     *
26     * @param array $args Request arguments, such as a `timeout` for a lookup in front of a page render.
27     * @return array The plans list
28     */
29    public static function get_plans( $args = array() ) {
30        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
31            if ( ! class_exists( 'Store_Product_List' ) ) {
32                require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
33            }
34
35            return Store_Product_List::api_only_get_active_plans_v1_4();
36        }
37
38        // We're on Jetpack, so it's safe to use this namespace.
39        $request = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
40            '/plans?_locale=' . get_user_locale(),
41            // We're using version 1.5 of the endpoint rather than the default version 2
42            // since the latter only returns Jetpack Plans, but we're also interested in
43            // WordPress.com plans, for consumers of this method that run on WP.com.
44            '1.5',
45            array_merge(
46                $args,
47                array(
48                    'method'  => 'GET',
49                    'headers' => array(
50                        'X-Forwarded-For' => ( new \Automattic\Jetpack\Status\Visitor() )->get_ip( true ),
51                    ),
52                )
53            ),
54            null,
55            'rest'
56        );
57
58        $body = wp_remote_retrieve_body( $request );
59        if ( 200 === wp_remote_retrieve_response_code( $request ) ) {
60            return json_decode( $body );
61        } else {
62            return $body;
63        }
64    }
65
66    /**
67     * Get plan information for a plan given its slug
68     *
69     * @since-jetpack 7.7.0
70     * @since 0.16.0 Accepts request arguments.
71     *
72     * @param string $plan_slug Plan slug.
73     * @param array  $args      Request arguments, as accepted by `get_plans()`.
74     *
75     * @return object The plan object
76     */
77    public static function get_plan( $plan_slug, $args = array() ) {
78        $plans = self::get_plans( $args );
79        if ( ! is_array( $plans ) ) {
80            return;
81        }
82
83        foreach ( $plans as $plan ) {
84            if ( $plan_slug === $plan->product_slug ) {
85                return $plan;
86            }
87        }
88    }
89
90    /**
91     * Efficiently get the short name of a plan from a slug.
92     *
93     * @param string $plan_slug Plan slug.
94     * @return string|null Short product name or null if not round.
95     */
96    public static function get_plan_short_name( $plan_slug ) {
97        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
98            if ( ! class_exists( 'Store_Product_List' ) ) {
99                require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
100            }
101
102            // Skip additional work like processing of coupons, since we only need the plan's short name.
103            $products = Store_Product_List::get();
104
105            foreach ( $products as $product ) {
106                if ( isset( $product['product_slug'] ) && $product['product_slug'] === $plan_slug ) {
107                    return $product['product_name_short'] ?? null;
108                }
109            }
110
111            return null;
112        }
113
114        // Fallback to less efficient method for Jetpack environments.
115        $plan = self::get_plan( $plan_slug );
116        return $plan->product_name_short ?? null;
117    }
118}