diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-send-email-preview.php b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-send-email-preview.php index 009f8271caef2..d2df71aaf000f 100644 --- a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-send-email-preview.php +++ b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-send-email-preview.php @@ -1,18 +1,23 @@ true, 'methods' => 'POST', - 'callback' => array( $this, 'send_email_preview' ), + // if this is not a wpcom site, we need to proxy the request to wpcom + 'callback' => ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? array( $this, 'send_email_preview' ) : array( $this, 'proxy_request_to_wpcom' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array( 'id' => array( @@ -47,11 +53,6 @@ public function register_routes() { ), ); - // if this is not a wpcom site, we need to proxy the request to wpcom - if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { - $options['callback'] = array( $this, 'proxy_request_to_wpcom' ); - } - register_rest_route( $this->namespace, $this->rest_base, @@ -118,40 +119,6 @@ public function send_email_preview( $request ) { return new WP_REST_Response( 'Email preview sent successfully.', 200 ); } - /** - * Proxy request to wpcom servers for the site and user. - * - * @param WP_Rest_Request $request Request to proxy. - * @param string $path Path to append to the rest base. - * - * @return mixed|WP_Error Response from wpcom servers or an error. - */ - public function proxy_request_to_wpcom( $request, $path = '' ) { - $blog_id = \Jetpack_Options::get_option( 'id' ); - $path = '/sites/' . rawurldecode( $blog_id ) . rawurldecode( $this->rest_base ) . ( $path ? '/' . rawurldecode( $path ) : '' ); - $api_url = add_query_arg( $request->get_query_params(), $path ); - - // Prefer request as user, if possible. Fall back to blog request to show prompt data for unconnected users. - $response = ( ( new Manager() )->is_user_connected() ) - ? Client::wpcom_json_api_request_as_user( $api_url, $this->version, array( 'method' => $request->get_method() ), $request->get_body(), $this->base_api_path ) - : Client::wpcom_json_api_request_as_blog( $api_url, $this->version, array( 'method' => $request->get_method() ), $request->get_body(), $this->base_api_path ); - - if ( is_wp_error( $response ) ) { - return $response; - } - - $response_status = wp_remote_retrieve_response_code( $response ); - $response_body = json_decode( wp_remote_retrieve_body( $response ) ); - - if ( $response_status >= 400 ) { - $code = isset( $response_body->code ) ? $response_body->code : 'unknown_error'; - $message = isset( $response_body->message ) ? $response_body->message : __( 'An unknown error occurred.', 'jetpack' ); - - return new WP_Error( $code, $message, array( 'status' => $response_status ) ); - } - - return $response_body; - } } wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Send_Email_Preview' ); diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php new file mode 100644 index 0000000000000..1a54c848d0d56 --- /dev/null +++ b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php @@ -0,0 +1,58 @@ +rest_base ) . ( $path ? '/' . rawurldecode( $path ) : '' ); + $api_url = add_query_arg( $request->get_query_params(), $path ); + + $request_options = array( + 'headers' => array( + 'Content-Type' => 'application/json', + 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), + ), + 'method' => $request->get_method(), + ); + + // Prefer request as user, if possible. Fall back to blog request to show prompt data for unconnected users. + $response = ( ( new Manager() )->is_user_connected() ) + ? Client::wpcom_json_api_request_as_user( $api_url, $this->version, array( $request_options ), $request->get_body(), $this->base_api_path ) + : Client::wpcom_json_api_request_as_blog( $api_url, $this->version, array( $request_options ), $request->get_body(), $this->base_api_path ); + + if ( is_wp_error( $response ) ) { + return $response; + } + + $response_status = wp_remote_retrieve_response_code( $response ); + $response_body = json_decode( wp_remote_retrieve_body( $response ) ); + + if ( $response_status >= 400 ) { + $code = isset( $response_body->code ) ? $response_body->code : 'unknown_error'; + $message = isset( $response_body->message ) ? $response_body->message : __( 'An unknown error occurred.', 'jetpack' ); + + return new WP_Error( $code, $message, array( 'status' => $response_status ) ); + } + + return $response_body; + } +} diff --git a/projects/plugins/jetpack/tests/php/core-api/wpcom-endpoints/test-class-wpcom-rest-api-v2-endpoint-send-email-preview.php b/projects/plugins/jetpack/tests/php/core-api/wpcom-endpoints/test-class-wpcom-rest-api-v2-endpoint-send-email-preview.php index 1006490ecee40..d62f751853d1d 100644 --- a/projects/plugins/jetpack/tests/php/core-api/wpcom-endpoints/test-class-wpcom-rest-api-v2-endpoint-send-email-preview.php +++ b/projects/plugins/jetpack/tests/php/core-api/wpcom-endpoints/test-class-wpcom-rest-api-v2-endpoint-send-email-preview.php @@ -126,23 +126,4 @@ public function test_email_preview_permissions_check_wrong_role() { $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); } - /** - * Test the valid case of the endpoint. - * - * @covers ::send_email_preview - */ - public function test_handle_request() { - wp_set_current_user( static::$user_id_editor ); - - $request = new WP_REST_Request( Requests::POST, static::$path ); - $request->set_body_params( - array( - 'id' => static::$post_id, - ) - ); - $response = $this->server->dispatch( $request ); - - $this->assertNotInstanceOf( 'WP_Error', $response ); - $this->assertEquals( 200, $response->get_status() ); - } }