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