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
1 year 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-publicize-share-status.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
1 year ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
1 year 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
1 year ago
trait-wpcom-rest-api-proxy-request-trait.php
121 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\Connection\Manager; |
| 12 | use Automattic\Jetpack\Status\Visitor; |
| 13 | |
| 14 | trait WPCOM_REST_API_Proxy_Request_Trait { |
| 15 | |
| 16 | /** |
| 17 | * Proxy request to wpcom servers on behalf of a user or using the Site-level Connection (blog token). |
| 18 | * |
| 19 | * @param WP_Rest_Request $request Request to proxy. |
| 20 | * @param string $path Path to append to the rest base. |
| 21 | * @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'. |
| 22 | * @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. |
| 23 | * |
| 24 | * @return mixed|WP_Error Response from wpcom servers or an error. |
| 25 | */ |
| 26 | public function proxy_request_to_wpcom( $request, $path = '', $context = 'user', $allow_fallback_to_blog = false ) { |
| 27 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 28 | $path = '/sites/' . rawurldecode( $blog_id ) . '/' . rawurldecode( ltrim( $this->rest_base, '/' ) ) . ( $path ? '/' . rawurldecode( ltrim( $path, '/' ) ) : '' ); |
| 29 | $query_params = $request->get_query_params(); |
| 30 | $manager = new Manager(); |
| 31 | |
| 32 | /* |
| 33 | * A rest_route parameter can be added when using plain permalinks. |
| 34 | * It is not necessary to pass them to WordPress.com, |
| 35 | * and may even cause issues with some endpoints. |
| 36 | * Let's remove it. |
| 37 | */ |
| 38 | if ( isset( $query_params['rest_route'] ) ) { |
| 39 | unset( $query_params['rest_route'] ); |
| 40 | } |
| 41 | $api_url = add_query_arg( $query_params, $path ); |
| 42 | |
| 43 | $request_options = array( |
| 44 | 'headers' => array( |
| 45 | 'Content-Type' => 'application/json', |
| 46 | 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), |
| 47 | ), |
| 48 | 'method' => $request->get_method(), |
| 49 | ); |
| 50 | |
| 51 | // If no body is present, passing it as $request->get_body() will cause an error. |
| 52 | $body = $request->get_body() ? $request->get_body() : null; |
| 53 | |
| 54 | $response = new WP_Error( |
| 55 | 'rest_unauthorized', |
| 56 | __( 'Please connect your user account to WordPress.com', 'jetpack' ), |
| 57 | array( 'status' => rest_authorization_required_code() ) |
| 58 | ); |
| 59 | |
| 60 | if ( 'user' === $context ) { |
| 61 | if ( ! $manager->is_user_connected() ) { |
| 62 | if ( false === $allow_fallback_to_blog ) { |
| 63 | return $response; |
| 64 | } |
| 65 | |
| 66 | $context = 'blog'; |
| 67 | } else { |
| 68 | $response = Client::wpcom_json_api_request_as_user( $api_url, $this->version, $request_options, $body, $this->base_api_path ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ( 'blog' === $context ) { |
| 73 | if ( ! $manager->is_connected() ) { |
| 74 | return $response; |
| 75 | } |
| 76 | |
| 77 | $response = Client::wpcom_json_api_request_as_blog( $api_url, $this->version, $request_options, $body, $this->base_api_path ); |
| 78 | } |
| 79 | |
| 80 | if ( is_wp_error( $response ) ) { |
| 81 | return $response; |
| 82 | } |
| 83 | |
| 84 | $response_status = wp_remote_retrieve_response_code( $response ); |
| 85 | $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 86 | |
| 87 | if ( $response_status >= 400 ) { |
| 88 | $code = isset( $response_body['code'] ) ? $response_body['code'] : 'unknown_error'; |
| 89 | $message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack' ); |
| 90 | |
| 91 | return new WP_Error( $code, $message, array( 'status' => $response_status ) ); |
| 92 | } |
| 93 | |
| 94 | return $response_body; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Proxy request to wpcom servers on behalf of a user. |
| 99 | * |
| 100 | * @param WP_Rest_Request $request Request to proxy. |
| 101 | * @param string $path Path to append to the rest base. |
| 102 | * |
| 103 | * @return mixed|WP_Error Response from wpcom servers or an error. |
| 104 | */ |
| 105 | public function proxy_request_to_wpcom_as_user( $request, $path = '' ) { |
| 106 | return $this->proxy_request_to_wpcom( $request, $path, 'user' ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Proxy request to wpcom servers using the Site-level Connection (blog token). |
| 111 | * |
| 112 | * @param WP_Rest_Request $request Request to proxy. |
| 113 | * @param string $path Path to append to the rest base. |
| 114 | * |
| 115 | * @return mixed|WP_Error Response from wpcom servers or an error. |
| 116 | */ |
| 117 | public function proxy_request_to_wpcom_as_blog( $request, $path = '' ) { |
| 118 | return $this->proxy_request_to_wpcom( $request, $path, 'blog' ); |
| 119 | } |
| 120 | } |
| 121 |