PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-explat/src/class-rest-controller.php +144 -0 16.2-beta → 16.3-a.7 View file →
@@ -1,0 +1,144 @@
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 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 + /**
57 + * Registers the REST routes.
58 + *
59 + * @access public
60 + * @static
61 + */
62 + public function register_rest_routes() {
63 + register_rest_route(
64 + static::$namespace,
65 + 'assignments',
66 + array(
67 + 'methods' => WP_REST_Server::READABLE,
68 + 'callback' => array( $this, 'get_assignments' ),
69 + 'permission_callback' => '__return_true',
70 + 'args' => array(
71 + 'experiment_name' => array(
72 + 'type' => 'string',
73 + ),
74 + 'anon_id' => array(
75 + 'type' => 'string',
76 + ),
77 + 'as_connected_user' => array(
78 + 'type' => 'boolean',
79 + ),
80 + 'platform' => array(
81 + 'type' => 'string',
82 + 'enum' => array( 'jetpack', 'calypso', 'wpcom' ),
83 + 'default' => 'jetpack',
84 + ),
85 + ),
86 + )
87 + );
88 + }
89 +
90 + /**
91 + * Get the assignments for a given experiment and anon_id
92 + *
93 + * @param WP_REST_Request $request The REST request object.
94 + * @return WP_REST_Response|WP_Error
95 + */
96 + public function get_assignments( $request ) {
97 + $response = null;
98 + $is_user_connected = ( new Jetpack_Connection() )->is_user_connected();
99 + $platform = $request->get_param( 'platform' );
100 + $request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/' . $platform;
101 + $args = array(
102 + 'experiment_names' => $request['experiment_name'],
103 + 'anon_id' => $request['anon_id'],
104 + );
105 +
106 + if ( $request['as_connected_user'] && $is_user_connected ) {
107 + $response = Client::wpcom_json_api_request_as_user(
108 + add_query_arg( $args, $request_path ),
109 + 'v2',
110 + array(
111 + 'headers' => array(
112 + 'User-Agent' => 'Jetpack MU WPCOM Plugin Experiment Assignment',
113 + ),
114 + )
115 + );
116 + } else {
117 + $response = wp_remote_get(
118 + add_query_arg( $args, self::WPCOM_API_BASE_URL . $request_path )
119 + );
120 + }
121 +
122 + if ( is_wp_error( $response ) ) {
123 + return new WP_Error(
124 + 'wp_error_fetching_assignment',
125 + $response->get_error_message(),
126 + array( 'status' => 500 )
127 + );
128 + }
129 +
130 + $response_code = wp_remote_retrieve_response_code( $response );
131 +
132 + if ( 200 !== $response_code ) {
133 + return new WP_Error(
134 + 'http_error_fetching_assignment',
135 + wp_remote_retrieve_response_message( $response ),
136 + array( 'status' => $response_code )
137 + );
138 + }
139 +
140 + return rest_ensure_response(
141 + json_decode( wp_remote_retrieve_body( $response ), true )
142 + );
143 + }
144 +}