Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.55% |
65 / 71 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_Proxy | |
94.20% |
65 / 69 |
|
60.00% |
3 / 5 |
15.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| register_route | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
2.01 | |||
| forward | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| forward_user_path | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
7 | |||
| with_declared_args_only | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Local REST routes that forward the dashboard's WordPress.com API calls as the current user. |
| 4 | * |
| 5 | * @package automattic/jetpack-monetize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Monetize; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request; |
| 13 | use WP_Error; |
| 14 | use WP_REST_Request; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Registers `jetpack/v4/monetize/<api>/<path>` routes that each forward to the same `<api>/sites/<id>/<path>` on WordPress.com. |
| 22 | * |
| 23 | * The local path mirrors the upstream one, so the page's client only swaps the prefix: Simple sites call WordPress.com directly. |
| 24 | * A `me/` path is the current user's, so it forwards to `<api>/me/…` with no site in it. |
| 25 | * |
| 26 | * @since 0.2.0 |
| 27 | */ |
| 28 | class WPCOM_Proxy { |
| 29 | use WPCOM_REST_API_Proxy_Request; |
| 30 | |
| 31 | const REST_NAMESPACE = 'jetpack/v4'; |
| 32 | |
| 33 | const REST_BASE = 'monetize'; |
| 34 | |
| 35 | /** |
| 36 | * The WordPress.com APIs a route can forward to, with each one's base path and version. |
| 37 | * |
| 38 | * @var array<string, string[]> |
| 39 | */ |
| 40 | const APIS = array( |
| 41 | 'wpcom/v2' => array( 'wpcom', 'v2' ), |
| 42 | 'rest/v1.1' => array( 'rest', '1.1' ), |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * Set up a proxy for one upstream path. |
| 47 | * |
| 48 | * @param string $api A key of APIS. |
| 49 | * @param string $upstream_path Path under `/sites/<id>/`. |
| 50 | */ |
| 51 | private function __construct( $api, $upstream_path ) { |
| 52 | list( $this->base_api_path, $this->version ) = self::APIS[ $api ]; |
| 53 | $this->rest_base = $upstream_path; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Register a route that forwards to the same path on WordPress.com. |
| 58 | * |
| 59 | * @param string $api A key of APIS. |
| 60 | * @param string $path Route pattern under `/sites/<id>/`, e.g. `memberships/product/(?P<id>\d+)`, or a `me/` path. |
| 61 | * @param string|array $methods HTTP methods the route accepts. |
| 62 | * @param array $args Route args: only these and `_locale` are forwarded, once validated and sanitized. |
| 63 | * @return bool Whether the route was registered. |
| 64 | */ |
| 65 | public static function register_route( $api, $path, $methods, array $args = array() ) { |
| 66 | if ( ! isset( self::APIS[ $api ] ) ) { |
| 67 | /* translators: %s is a WordPress.com API, such as wpcom/v2. */ |
| 68 | $message = sprintf( __( 'Unknown WordPress.com API "%s".', 'jetpack-monetize' ), $api ); |
| 69 | _doing_it_wrong( __METHOD__, esc_html( $message ), '0.2.0' ); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return register_rest_route( |
| 74 | self::REST_NAMESPACE, |
| 75 | '/' . self::REST_BASE . '/' . $api . '/' . $path, |
| 76 | array( |
| 77 | 'methods' => $methods, |
| 78 | 'callback' => static function ( WP_REST_Request $request ) use ( $api ) { |
| 79 | return self::forward( $api, $request ); |
| 80 | }, |
| 81 | 'permission_callback' => array( Dashboard::class, 'can_manage' ), |
| 82 | // apiFetch adds `_locale=user` to every request, so WordPress.com answers in the user's language. |
| 83 | 'args' => $args + array( |
| 84 | '_locale' => array( |
| 85 | 'type' => 'string', |
| 86 | 'enum' => array( 'user' ), |
| 87 | ), |
| 88 | ), |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Forward a request to its upstream path as the current user. |
| 95 | * |
| 96 | * @param string $api A key of APIS. |
| 97 | * @param WP_REST_Request $request The request to one of this class's routes. |
| 98 | * @return \WP_REST_Response|\WP_Error |
| 99 | */ |
| 100 | public static function forward( $api, WP_REST_Request $request ) { |
| 101 | $prefix = '/' . self::REST_NAMESPACE . '/' . self::REST_BASE . '/' . $api . '/'; |
| 102 | $upstream = substr( $request->get_route(), strlen( $prefix ) ); |
| 103 | |
| 104 | if ( str_starts_with( $upstream, 'me/' ) ) { |
| 105 | return rest_ensure_response( self::forward_user_path( $api, $upstream, self::with_declared_args_only( $request ) ) ); |
| 106 | } |
| 107 | |
| 108 | return rest_ensure_response( ( new self( $api, $upstream ) )->proxy_request_to_wpcom_as_user( self::with_declared_args_only( $request ) ) ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Forward a request for one of the current user's own paths, which the connection trait would put under the site. |
| 113 | * |
| 114 | * @param string $api A key of APIS. |
| 115 | * @param string $upstream The `me/` path. |
| 116 | * @param WP_REST_Request $forwarded The request, with its declared args only. |
| 117 | * @return mixed|WP_Error The response body, or an error with WordPress.com's status. |
| 118 | */ |
| 119 | private static function forward_user_path( $api, $upstream, WP_REST_Request $forwarded ) { |
| 120 | if ( ! ( new Connection_Manager() )->is_user_connected() ) { |
| 121 | return new WP_Error( |
| 122 | 'rest_unauthorized', |
| 123 | __( 'Please connect your user account to WordPress.com', 'jetpack-monetize' ), |
| 124 | array( 'status' => rest_authorization_required_code() ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | list( $base_api_path, $version ) = self::APIS[ $api ]; |
| 129 | $query = $forwarded->get_query_params(); |
| 130 | $response = Client::wpcom_json_api_request_as_user( |
| 131 | '/' . $upstream . ( $query ? '?' . http_build_query( $query, '', '&' ) : '' ), |
| 132 | $version, |
| 133 | array( |
| 134 | 'method' => $forwarded->get_method(), |
| 135 | 'headers' => array( 'Content-Type' => 'application/json' ), |
| 136 | ), |
| 137 | $forwarded->get_body() ? $forwarded->get_body() : null, |
| 138 | $base_api_path |
| 139 | ); |
| 140 | |
| 141 | if ( is_wp_error( $response ) ) { |
| 142 | return $response; |
| 143 | } |
| 144 | |
| 145 | $status = (int) wp_remote_retrieve_response_code( $response ); |
| 146 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 147 | |
| 148 | if ( $status >= 400 ) { |
| 149 | $error = is_array( $body ) ? $body : array(); |
| 150 | return new WP_Error( |
| 151 | $error['code'] ?? 'unknown_error', |
| 152 | $error['message'] ?? __( 'An unknown error occurred.', 'jetpack-monetize' ), |
| 153 | array( 'status' => $status ) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | return $body; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * A copy of the request that keeps only its route's declared args, with the values WordPress sanitized. |
| 162 | * |
| 163 | * The body goes out as JSON whatever its incoming encoding, since that is the only body the proxy sends. |
| 164 | * |
| 165 | * @param WP_REST_Request $request The request to one of this class's routes. |
| 166 | * @return WP_REST_Request |
| 167 | */ |
| 168 | private static function with_declared_args_only( WP_REST_Request $request ) { |
| 169 | $declared = $request->get_attributes()['args'] ?? array(); |
| 170 | $forwarded = new WP_REST_Request( $request->get_method(), $request->get_route() ); |
| 171 | $forwarded->set_query_params( array_intersect_key( $request->get_query_params(), $declared ) ); |
| 172 | |
| 173 | $body = array_intersect_key( array_merge( $request->get_body_params(), (array) $request->get_json_params() ), $declared ); |
| 174 | |
| 175 | // WordPress.com ignores a DELETE body when it checks the signature, so its body hash would fail the user token. |
| 176 | if ( 'DELETE' === $request->get_method() ) { |
| 177 | $forwarded->set_query_params( $forwarded->get_query_params() + $body ); |
| 178 | $body = array(); |
| 179 | } |
| 180 | |
| 181 | if ( $body ) { |
| 182 | $forwarded->set_header( 'Content-Type', 'application/json' ); |
| 183 | $forwarded->set_body( (string) wp_json_encode( $body, JSON_UNESCAPED_SLASHES ) ); |
| 184 | } |
| 185 | |
| 186 | return $forwarded; |
| 187 | } |
| 188 | } |