Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.03% |
9 / 29 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| REST_Purchases | |
31.03% |
9 / 29 |
|
33.33% |
1 / 3 |
28.99 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| permissions_callback | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| get_site_current_purchases | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Sets up the Purchases REST API endpoints. |
| 4 | * |
| 5 | * @package automattic/my-jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\My_Jetpack; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use WP_Error; |
| 13 | |
| 14 | /** |
| 15 | * Registers the REST routes for Purchases. |
| 16 | * |
| 17 | * @phan-constructor-used-for-side-effects |
| 18 | */ |
| 19 | class REST_Purchases { |
| 20 | /** |
| 21 | * Constructor. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | register_rest_route( |
| 25 | 'my-jetpack/v1', |
| 26 | '/site/purchases', |
| 27 | array( |
| 28 | 'methods' => \WP_REST_Server::READABLE, |
| 29 | 'callback' => __CLASS__ . '::get_site_current_purchases', |
| 30 | 'permission_callback' => __CLASS__ . '::permissions_callback', |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check user capability to access the endpoint. |
| 37 | * |
| 38 | * @access public |
| 39 | * @static |
| 40 | * |
| 41 | * @return true|WP_Error |
| 42 | */ |
| 43 | public static function permissions_callback() { |
| 44 | $connection = new Connection_Manager(); |
| 45 | $is_site_connected = $connection->is_connected(); |
| 46 | |
| 47 | if ( ! $is_site_connected ) { |
| 48 | return new WP_Error( |
| 49 | 'not_connected', |
| 50 | __( 'Your site is not connected to Jetpack.', 'jetpack-my-jetpack' ), |
| 51 | array( |
| 52 | 'status' => 400, |
| 53 | ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return current_user_can( 'edit_posts' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Site purchases endpoint. |
| 62 | * |
| 63 | * @return array|WP_Error of site purchases. |
| 64 | */ |
| 65 | public static function get_site_current_purchases() { |
| 66 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 67 | $wpcom_endpoint = sprintf( '/upgrades?site=%1$d&locale=%2$s', $site_id, get_user_locale() ); |
| 68 | $wpcom_api_version = '1.2'; |
| 69 | $response = Client::wpcom_json_api_request_as_blog( $wpcom_endpoint, $wpcom_api_version ); |
| 70 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 71 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 72 | |
| 73 | if ( is_wp_error( $response ) || empty( $response['body'] ) || 200 !== $response_code ) { |
| 74 | return new WP_Error( 'site_data_fetch_failed', 'Site data fetch failed', array( 'status' => $response_code ? $response_code : 400 ) ); |
| 75 | } |
| 76 | |
| 77 | return rest_ensure_response( $body ); |
| 78 | } |
| 79 | } |