Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| REST_Controller | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| register_rest_routes | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
2 | |||
| get_assignments | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The ExPlat Rest Controller class. |
| 4 | * Registers the REST routes for ExPlat backend |
| 5 | * |
| 6 | * @package automattic/jetpack-explat |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\ExPlat; |
| 10 | |
| 11 | use Automattic\Jetpack\Connection\Client; |
| 12 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
| 13 | use WP_Error; |
| 14 | use WP_REST_Request; |
| 15 | use WP_REST_Response; |
| 16 | use WP_REST_Server; |
| 17 | |
| 18 | /** |
| 19 | * Registers general REST routes for ExPlat. |
| 20 | */ |
| 21 | class REST_Controller { |
| 22 | /** |
| 23 | * Namespace for the REST API. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public static $namespace = 'jetpack/v4/explat'; |
| 28 | |
| 29 | /** |
| 30 | * Current version of the ExPlat API and components |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | const EXPLAT_API_VERSION = '0.1.0'; |
| 35 | |
| 36 | /** |
| 37 | * Base API URI for WordPress.com |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | const WPCOM_API_BASE_URL = 'https://public-api.wordpress.com/wpcom/v2'; |
| 42 | |
| 43 | /** |
| 44 | * Registers the REST routes. |
| 45 | * |
| 46 | * @access public |
| 47 | * @static |
| 48 | */ |
| 49 | public function register_rest_routes() { |
| 50 | register_rest_route( |
| 51 | static::$namespace, |
| 52 | 'assignments', |
| 53 | array( |
| 54 | 'methods' => WP_REST_Server::READABLE, |
| 55 | 'callback' => array( $this, 'get_assignments' ), |
| 56 | 'permission_callback' => '__return_true', |
| 57 | 'args' => array( |
| 58 | 'experiment_name' => array( |
| 59 | 'type' => 'string', |
| 60 | ), |
| 61 | 'anon_id' => array( |
| 62 | 'type' => 'string', |
| 63 | ), |
| 64 | 'as_connected_user' => array( |
| 65 | 'type' => 'boolean', |
| 66 | ), |
| 67 | 'platform' => array( |
| 68 | 'type' => 'string', |
| 69 | 'enum' => array( 'jetpack', 'calypso', 'wpcom' ), |
| 70 | 'default' => 'jetpack', |
| 71 | ), |
| 72 | ), |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the assignments for a given experiment and anon_id |
| 79 | * |
| 80 | * @param WP_REST_Request $request The REST request object. |
| 81 | * @return WP_REST_Response|WP_Error |
| 82 | */ |
| 83 | public function get_assignments( $request ) { |
| 84 | $response = null; |
| 85 | $is_user_connected = ( new Jetpack_Connection() )->is_user_connected(); |
| 86 | $platform = $request->get_param( 'platform' ); |
| 87 | $request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/' . $platform; |
| 88 | $args = array( |
| 89 | 'experiment_name' => $request['experiment_name'], |
| 90 | 'anon_id' => $request['anon_id'], |
| 91 | ); |
| 92 | |
| 93 | if ( $request['as_connected_user'] && $is_user_connected ) { |
| 94 | $response = Client::wpcom_json_api_request_as_user( |
| 95 | add_query_arg( $args, $request_path ), |
| 96 | 'v2' |
| 97 | ); |
| 98 | } else { |
| 99 | $response = wp_remote_get( |
| 100 | add_query_arg( $args, self::WPCOM_API_BASE_URL . $request_path ) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | if ( is_wp_error( $response ) ) { |
| 105 | return new WP_Error( |
| 106 | 'wp_error_fetching_assignment', |
| 107 | $response->get_error_message(), |
| 108 | array( 'status' => 500 ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 113 | |
| 114 | if ( 200 !== $response_code ) { |
| 115 | return new WP_Error( |
| 116 | 'http_error_fetching_assignment', |
| 117 | wp_remote_retrieve_response_message( $response ), |
| 118 | array( 'status' => $response_code ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | return rest_ensure_response( |
| 123 | json_decode( wp_remote_retrieve_body( $response ), true ) |
| 124 | ); |
| 125 | } |
| 126 | } |