Skip to content

Commit

Permalink
Move WPCOM_REST_API_Proxy_Request trait to the connection package
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Jan 14, 2025
1 parent e9d9bc9 commit 93ae9f7
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Moved WPCOM_REST_API_Proxy_Request trait to the connection package
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<?php
/**
* Trait WPCOM_REST_API_Proxy_Request_Trait
* Trait WPCOM_REST_API_Proxy_Request
*
* Used to proxy requests to wpcom servers.
*
* @package automattic/jetpack
* @package automattic/jetpack-connection
*/

namespace Automattic\Jetpack\Connection\Traits;

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

trait WPCOM_REST_API_Proxy_Request_Trait {
trait WPCOM_REST_API_Proxy_Request {

/**
* Proxy request to wpcom servers on behalf of a user or using the Site-level Connection (blog token).
*
* @param WP_Rest_Request $request Request to proxy.
* @param WP_REST_Request $request Request to proxy.
* @param string $path Path to append to the rest base.
* @param string $context Whether the request should be proxied on behalf of the current user or using the Site-level Connection, aka 'blog' token. Can be Either 'user' or 'blog'. Defaults to 'user'.
* @param bool $allow_fallback_to_blog If the $context is 'user', whether we should fallback to using the Site-level Connection in case the current user is not connected.
* @param array $request_options Request options to pass to wp_remote_request.
*
* @return mixed|WP_Error Response from wpcom servers or an error.
*/
public function proxy_request_to_wpcom( $request, $path = '', $context = 'user', $allow_fallback_to_blog = false ) {
public function proxy_request_to_wpcom( $request, $path = '', $context = 'user', $allow_fallback_to_blog = false, $request_options = array() ) {
$blog_id = \Jetpack_Options::get_option( 'id' );
$path = '/sites/' . rawurldecode( $blog_id ) . '/' . rawurldecode( ltrim( $this->rest_base, '/' ) ) . ( $path ? '/' . rawurldecode( ltrim( $path, '/' ) ) : '' );

Check failure on line 33 in projects/packages/connection/src/traits/trait-wpcom-rest-api-proxy-request.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredProperty Reference to undeclared property \Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request->rest_base
$query_params = $request->get_query_params();
Expand All @@ -40,20 +45,23 @@ public function proxy_request_to_wpcom( $request, $path = '', $context = 'user',
}
$api_url = add_query_arg( $query_params, $path );

$request_options = array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
$request_options = array_replace_recursive(
array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
),
'method' => $request->get_method(),
),
'method' => $request->get_method(),
$request_options
);

// If no body is present, passing it as $request->get_body() will cause an error.
$body = $request->get_body() ? $request->get_body() : null;

$response = new WP_Error(
'rest_unauthorized',
__( 'Please connect your user account to WordPress.com', 'jetpack' ),
__( 'Please connect your user account to WordPress.com', 'jetpack-connection' ),
array( 'status' => rest_authorization_required_code() )
);

Expand Down Expand Up @@ -86,7 +94,7 @@ public function proxy_request_to_wpcom( $request, $path = '', $context = 'user',

if ( $response_status >= 400 ) {
$code = isset( $response_body['code'] ) ? $response_body['code'] : 'unknown_error';

Check failure on line 96 in projects/packages/connection/src/traits/trait-wpcom-rest-api-proxy-request.php

View workflow job for this annotation

GitHub Actions / Static analysis

Plugin PhanPluginDuplicateConditionalNullCoalescing "isset(X) ? X : Y" can usually be simplified to "X ?? Y" in PHP 7. The duplicated expression X was $response_body['code']
$message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack' );
$message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack-connection' );

Check failure on line 97 in projects/packages/connection/src/traits/trait-wpcom-rest-api-proxy-request.php

View workflow job for this annotation

GitHub Actions / Static analysis

Plugin PhanPluginDuplicateConditionalNullCoalescing "isset(X) ? X : Y" can usually be simplified to "X ?? Y" in PHP 7. The duplicated expression X was $response_body['message']

return new WP_Error( $code, $message, array( 'status' => $response_status ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';

/**
Expand All @@ -19,7 +19,7 @@
*/
class WPCOM_REST_API_V2_Endpoint_Email_Preview extends WP_REST_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* @since 12.6
*/

use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Following
*/
class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_List extends WP_REST_Controller {
use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* @since 12.6
*/

use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_Subscriptions_Count
*/
class WPCOM_REST_API_V2_Endpoint_Newsletter_Categories_Subscriptions_Count extends WP_REST_Controller {
use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
*/

use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';

/**
* Class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview
* Handles the sending of email previews via the WordPress.com REST API
*/
class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview extends WP_REST_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* @package automattic/jetpack
*/

// Ensure WPCOM_REST_API_Proxy_Request_Trait is present.
require_once __DIR__ . '/trait-wpcom-rest-api-proxy-request-trait.php';
use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;

/**
* REST API endpoint wpcom/v3/sites/%s/blogging-prompts.
*/
class WPCOM_REST_API_V3_Endpoint_Blogging_Prompts extends WP_REST_Posts_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

const TEMPLATE_BLOG_ID = 205876834;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* @since 7.3.0
*/

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

/**
* Class WPCOM_REST_API_V2_Endpoint_Memberships
* This introduces V2 endpoints.
*/
class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller {

use WPCOM_REST_API_Proxy_Request_Trait;
use WPCOM_REST_API_Proxy_Request;

/**
* WPCOM_REST_API_V2_Endpoint_Memberships constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Moved WPCOM_REST_API_Proxy_Request trait to the connection package

0 comments on commit 93ae9f7

Please sign in to comment.