← All changes
|
jetpack_vendor/automattic/jetpack-explat/src/class-rest-controller.php
+31
-5
13.7.2
→
16.3-beta
View file →
| @@ -10,8 +10,10 @@ | ||
| 10 | 10 | |
| 11 | 11 | use Automattic\Jetpack\Connection\Client; |
| 12 | 12 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
| 13 | 13 | use WP_Error; |
| 14 | +use WP_REST_Request; | |
| 15 | +use WP_REST_Response; | |
| 14 | 16 | use WP_REST_Server; |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | 19 | * Registers general REST routes for ExPlat. |
| @@ -38,8 +40,21 @@ | ||
| 38 | 40 | */ |
| 39 | 41 | const WPCOM_API_BASE_URL = 'https://public-api.wordpress.com/wpcom/v2'; |
| 40 | 42 | |
| 41 | 43 | /** |
| 44 | + * Registers the REST routes on the `rest_api_init` hook. | |
| 45 | + * | |
| 46 | + * Instantiated here, rather than eagerly, so the controller class only loads | |
| 47 | + * on requests that reach `rest_api_init`. Static so the callback can be | |
| 48 | + * unregistered. | |
| 49 | + * | |
| 50 | + * @access public | |
| 51 | + */ | |
| 52 | + public static function register() { | |
| 53 | + ( new self() )->register_rest_routes(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 42 | 57 | * Registers the REST routes. |
| 43 | 58 | * |
| 44 | 59 | * @access public |
| 45 | 60 | * @static |
| @@ -61,8 +76,13 @@ | ||
| 61 | 76 | ), |
| 62 | 77 | 'as_connected_user' => array( |
| 63 | 78 | 'type' => 'boolean', |
| 64 | 79 | ), |
| 80 | + 'platform' => array( | |
| 81 | + 'type' => 'string', | |
| 82 | + 'enum' => array( 'jetpack', 'calypso', 'wpcom' ), | |
| 83 | + 'default' => 'jetpack', | |
| 84 | + ), | |
| 65 | 85 | ), |
| 66 | 86 | ) |
| 67 | 87 | ); |
| 68 | 88 | } |
| @@ -70,23 +90,29 @@ | ||
| 70 | 90 | /** |
| 71 | 91 | * Get the assignments for a given experiment and anon_id |
| 72 | 92 | * |
| 73 | 93 | * @param WP_REST_Request $request The REST request object. |
| 74 | - * @return WP_REST_Response | |
| 94 | + * @return WP_REST_Response|WP_Error | |
| 75 | 95 | */ |
| 76 | 96 | public function get_assignments( $request ) { |
| 77 | 97 | $response = null; |
| 78 | 98 | $is_user_connected = ( new Jetpack_Connection() )->is_user_connected(); |
| 79 | - $request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/jetpack'; | |
| 99 | + $platform = $request->get_param( 'platform' ); | |
| 100 | + $request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/' . $platform; | |
| 80 | 101 | $args = array( |
| 81 | - 'experiment_name' => $request['experiment_name'], | |
| 82 | - 'anon_id' => $request['anon_id'], | |
| 102 | + 'experiment_names' => $request['experiment_name'], | |
| 103 | + 'anon_id' => $request['anon_id'], | |
| 83 | 104 | ); |
| 84 | 105 | |
| 85 | 106 | if ( $request['as_connected_user'] && $is_user_connected ) { |
| 86 | 107 | $response = Client::wpcom_json_api_request_as_user( |
| 87 | 108 | add_query_arg( $args, $request_path ), |
| 88 | - 'v2' | |
| 109 | + 'v2', | |
| 110 | + array( | |
| 111 | + 'headers' => array( | |
| 112 | + 'User-Agent' => 'Jetpack MU WPCOM Plugin Experiment Assignment', | |
| 113 | + ), | |
| 114 | + ) | |
| 89 | 115 | ); |
| 90 | 116 | } else { |
| 91 | 117 | $response = wp_remote_get( |
| 92 | 118 | add_query_arg( $args, self::WPCOM_API_BASE_URL . $request_path ) |