← All changes
|
vendor/automattic/jetpack-plans/src/class-plans.php
+33
-2
13.0.2
→
16.3-beta
View file →
| @@ -10,8 +10,10 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace Automattic\Jetpack; |
| 13 | 13 | |
| 14 | +use Store_Product_List; | |
| 15 | + | |
| 14 | 16 | /** |
| 15 | 17 | * Fetch data about available Plans from WordPress.com |
| 16 | 18 | */ |
| 17 | 19 | class Plans { |
| @@ -31,9 +33,9 @@ | ||
| 31 | 33 | return Store_Product_List::api_only_get_active_plans_v1_4(); |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | // We're on Jetpack, so it's safe to use this namespace. |
| 35 | - $request = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( | |
| 37 | + $request = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( | |
| 36 | 38 | '/plans?_locale=' . get_user_locale(), |
| 37 | 39 | // We're using version 1.5 of the endpoint rather than the default version 2 |
| 38 | 40 | // since the latter only returns Jetpack Plans, but we're also interested in |
| 39 | 41 | // WordPress.com plans, for consumers of this method that run on WP.com. |
| @@ -40,9 +42,9 @@ | ||
| 40 | 42 | '1.5', |
| 41 | 43 | array( |
| 42 | 44 | 'method' => 'GET', |
| 43 | 45 | 'headers' => array( |
| 44 | - 'X-Forwarded-For' => ( new Automattic\Jetpack\Status\Visitor() )->get_ip( true ), | |
| 46 | + 'X-Forwarded-For' => ( new \Automattic\Jetpack\Status\Visitor() )->get_ip( true ), | |
| 45 | 47 | ), |
| 46 | 48 | ), |
| 47 | 49 | null, |
| 48 | 50 | 'rest' |
| @@ -75,6 +77,35 @@ | ||
| 75 | 77 | if ( $plan_slug === $plan->product_slug ) { |
| 76 | 78 | return $plan; |
| 77 | 79 | } |
| 78 | 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; | |
| 79 | 110 | } |
| 80 | 111 | } |