class-plans.php
81 lines
| 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 | /** |
| 15 | * Fetch data about available Plans from WordPress.com |
| 16 | */ |
| 17 | class Plans { |
| 18 | /** |
| 19 | * Get a list of all available plans from WordPress.com |
| 20 | * |
| 21 | * @since-jetpack 7.7.0 |
| 22 | * |
| 23 | * @return array The plans list |
| 24 | */ |
| 25 | public static function get_plans() { |
| 26 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 27 | if ( ! class_exists( 'Store_Product_List' ) ) { |
| 28 | require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php'; |
| 29 | } |
| 30 | |
| 31 | return Store_Product_List::api_only_get_active_plans_v1_4(); |
| 32 | } |
| 33 | |
| 34 | // We're on Jetpack, so it's safe to use this namespace. |
| 35 | $request = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( |
| 36 | '/plans?_locale=' . get_user_locale(), |
| 37 | // We're using version 1.5 of the endpoint rather than the default version 2 |
| 38 | // since the latter only returns Jetpack Plans, but we're also interested in |
| 39 | // WordPress.com plans, for consumers of this method that run on WP.com. |
| 40 | '1.5', |
| 41 | array( |
| 42 | 'method' => 'GET', |
| 43 | 'headers' => array( |
| 44 | 'X-Forwarded-For' => ( new Automattic\Jetpack\Status\Visitor() )->get_ip( true ), |
| 45 | ), |
| 46 | ), |
| 47 | null, |
| 48 | 'rest' |
| 49 | ); |
| 50 | |
| 51 | $body = wp_remote_retrieve_body( $request ); |
| 52 | if ( 200 === wp_remote_retrieve_response_code( $request ) ) { |
| 53 | return json_decode( $body ); |
| 54 | } else { |
| 55 | return $body; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get plan information for a plan given its slug |
| 61 | * |
| 62 | * @since-jetpack 7.7.0 |
| 63 | * |
| 64 | * @param string $plan_slug Plan slug. |
| 65 | * |
| 66 | * @return object The plan object |
| 67 | */ |
| 68 | public static function get_plan( $plan_slug ) { |
| 69 | $plans = self::get_plans(); |
| 70 | if ( ! is_array( $plans ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | foreach ( $plans as $plan ) { |
| 75 | if ( $plan_slug === $plan->product_slug ) { |
| 76 | return $plan; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |