business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-color.php
2 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
1 year ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-app-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
2 years ago
class-wpcom-rest-api-v2-endpoint-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-goodreads.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-profile.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
1 year ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
2 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
trait-wpcom-rest-api-proxy-request-trait.php
69 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Trait WPCOM_REST_API_Proxy_Request_Trait |
| 4 | * |
| 5 | * Used to proxy requests to wpcom servers. |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Status\Visitor; |
| 12 | |
| 13 | trait WPCOM_REST_API_Proxy_Request_Trait { |
| 14 | |
| 15 | /** |
| 16 | * Proxy request to wpcom servers for the site and user. |
| 17 | * |
| 18 | * @param WP_Rest_Request $request Request to proxy. |
| 19 | * @param string $path Path to append to the rest base. |
| 20 | * |
| 21 | * @return mixed|WP_Error Response from wpcom servers or an error. |
| 22 | */ |
| 23 | public function proxy_request_to_wpcom_as_user( $request, $path = '' ) { |
| 24 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 25 | $path = '/sites/' . rawurldecode( $blog_id ) . rawurldecode( $this->rest_base ) . ( $path ? '/' . rawurldecode( $path ) : '' ); |
| 26 | $query_params = $request->get_query_params(); |
| 27 | |
| 28 | /* |
| 29 | * A rest_route parameter can be added when using plain permalinks. |
| 30 | * It is not necessary to pass them to WordPress.com, |
| 31 | * and may even cause issues with some endpoints. |
| 32 | * Let's remove it. |
| 33 | */ |
| 34 | if ( isset( $query_params['rest_route'] ) ) { |
| 35 | unset( $query_params['rest_route'] ); |
| 36 | } |
| 37 | $api_url = add_query_arg( $query_params, $path ); |
| 38 | |
| 39 | $request_options = array( |
| 40 | 'headers' => array( |
| 41 | 'Content-Type' => 'application/json', |
| 42 | 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), |
| 43 | ), |
| 44 | 'method' => $request->get_method(), |
| 45 | ); |
| 46 | |
| 47 | // If no body is present, passing it as $request->get_body() will cause an error. |
| 48 | $body = $request->get_body() ? $request->get_body() : null; |
| 49 | |
| 50 | $response = Client::wpcom_json_api_request_as_user( $api_url, $this->version, $request_options, $body, $this->base_api_path ); |
| 51 | |
| 52 | if ( is_wp_error( $response ) ) { |
| 53 | return $response; |
| 54 | } |
| 55 | |
| 56 | $response_status = wp_remote_retrieve_response_code( $response ); |
| 57 | $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 58 | |
| 59 | if ( $response_status >= 400 ) { |
| 60 | $code = isset( $response_body['code'] ) ? $response_body['code'] : 'unknown_error'; |
| 61 | $message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack' ); |
| 62 | |
| 63 | return new WP_Error( $code, $message, array( 'status' => $response_status ) ); |
| 64 | } |
| 65 | |
| 66 | return $response_body; |
| 67 | } |
| 68 | } |
| 69 |