Skip to content

Commit

Permalink
don't extend WP_REST_Posts_Controller and make sure we request as the…
Browse files Browse the repository at this point in the history
… user
  • Loading branch information
lezama committed Jun 1, 2023
1 parent 9d5e75e commit 4cb108b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function __construct() {
*/
public function register_routes() {
$options = array(
'show_in_index' => true,
'methods' => 'POST',
'show_in_index' => true,
'methods' => 'POST',
// if this is not a wpcom site, we need to proxy the request to wpcom
'callback' => ( ( new Host() )->is_wpcom_simple() ) ? array( $this, 'send_email_preview' ) : array( $this, 'proxy_request_to_wpcom_as_user' ),
'permission_callback' => array( $this, 'permissions_check' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*/

use Automattic\Jetpack\Connection\Client;
<<<<<<< HEAD
=======
use Automattic\Jetpack\Connection\Manager;
>>>>>>> 6634c6eabe ([not verified] Add Send Email Preview endpoint)
use Automattic\Jetpack\Status\Visitor;

trait WPCOM_REST_API_Proxy_Request_Trait {
Expand All @@ -24,11 +20,7 @@ trait WPCOM_REST_API_Proxy_Request_Trait {
*
* @return mixed|WP_Error Response from wpcom servers or an error.
*/
<<<<<<< HEAD
public function proxy_request_to_wpcom_as_user( $request, $path = '' ) {
=======
public function proxy_request_to_wpcom( $request, $path = '' ) {
>>>>>>> 6634c6eabe ([not verified] Add Send Email Preview endpoint)
$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 );
Expand All @@ -41,14 +33,7 @@ public function proxy_request_to_wpcom( $request, $path = '' ) {
'method' => $request->get_method(),
);

<<<<<<< HEAD
$response = Client::wpcom_json_api_request_as_user( $api_url, $this->version, $request_options, $request->get_body(), $this->base_api_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( $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 );
>>>>>>> 6634c6eabe ([not verified] Add Send Email Preview endpoint)

if ( is_wp_error( $response ) ) {
return $response;
Expand Down

0 comments on commit 4cb108b

Please sign in to comment.