Нет описания

plans.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 automattic/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. return Store_Product_List::api_only_get_active_plans_v1_4();
  26. }
  27. // We're on Jetpack, so it's safe to use this namespace.
  28. $request = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
  29. '/plans?_locale=' . get_user_locale(),
  30. // We're using version 1.5 of the endpoint rather than the default version 2
  31. // since the latter only returns Jetpack Plans, but we're also interested in
  32. // WordPress.com plans, for consumers of this method that run on WP.com.
  33. '1.5',
  34. array(
  35. 'method' => 'GET',
  36. 'headers' => array(
  37. 'X-Forwarded-For' => Jetpack::current_user_ip( true ),
  38. ),
  39. ),
  40. null,
  41. 'rest'
  42. );
  43. $body = wp_remote_retrieve_body( $request );
  44. if ( 200 === wp_remote_retrieve_response_code( $request ) ) {
  45. return json_decode( $body );
  46. } else {
  47. return $body;
  48. }
  49. }
  50. /**
  51. * Get plan information for a plan given its slug
  52. *
  53. * @since 7.7.0
  54. *
  55. * @param string $plan_slug Plan slug.
  56. *
  57. * @return object The plan object
  58. */
  59. public static function get_plan( $plan_slug ) {
  60. $plans = self::get_plans();
  61. if ( ! is_array( $plans ) ) {
  62. return;
  63. }
  64. foreach ( $plans as $plan ) {
  65. if ( $plan_slug === $plan->product_slug ) {
  66. return $plan;
  67. }
  68. }
  69. }
  70. }