-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ns/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters