Skip to content

Commit

Permalink
[not verified] add trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lezama committed May 29, 2023
1 parent 74e8a03 commit 530e4b8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php
/**
* Send Email Preview endpoint.
* Handles the sending of email previews via the WordPress.com REST API.
*
* This file defines a class that is used to send email previews. It interacts with
* the WordPress.com REST API to send these previews.
*
* @package automattic/jetpack
*/

use Automattic\Jetpack\Connection\Client;
require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';
use Automattic\Jetpack\Connection\Manager;

/**
* Class WPCOM_REST_API_V2_Send_Email_Preview
* Class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview
*/
class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview extends WP_REST_Posts_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;

/**
* Constructor.
*/
Expand All @@ -37,7 +42,8 @@ public function register_routes() {
$options = array(
'show_in_index' => 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(
Expand All @@ -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,
Expand Down Expand Up @@ -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' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Trait WPCOM_REST_API_Proxy_Request_Trait
*
* Used to proxy requests to wpcom servers.
*
* @package automattic/jetpack
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Status\Visitor;

trait WPCOM_REST_API_Proxy_Request_Trait {

/**
* 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 );

$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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}

0 comments on commit 530e4b8

Please sign in to comment.