From 7965d155db74e412e122a67df88433b30e545543 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 15 Jan 2025 03:40:52 -0800 Subject: [PATCH] Temporarily restore WPCOM_REST_API_Proxy_Request_Trait for deployments (#41083) * Temporarily restore WPCOM_REST_API_Proxy_Request_Trait for deployments * Update baseline.php --- projects/plugins/jetpack/.phan/baseline.php | 1 + ...ait-wpcom-rest-api-proxy-request-trait.php | 120 ++++++++++++++++++ ...add-back-the-moved-file-to-fix-deployments | 5 + 3 files changed, 126 insertions(+) create mode 100644 projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php create mode 100644 projects/plugins/jetpack/changelog/update-add-back-the-moved-file-to-fix-deployments diff --git a/projects/plugins/jetpack/.phan/baseline.php b/projects/plugins/jetpack/.phan/baseline.php index 60c2077e00526..4deb8b388d2d3 100644 --- a/projects/plugins/jetpack/.phan/baseline.php +++ b/projects/plugins/jetpack/.phan/baseline.php @@ -141,6 +141,7 @@ '_inc/lib/core-api/wpcom-endpoints/publicize-connections.php' => ['PhanParamSignatureMismatch', 'PhanTypeMismatchArgument'], '_inc/lib/core-api/wpcom-endpoints/publicize-services.php' => ['PhanParamSignatureMismatch', 'PhanPluginMixedKeyNoKey', 'PhanTypeMismatchArgument'], '_inc/lib/core-api/wpcom-endpoints/service-api-keys.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeArraySuspicious', 'PhanTypeMismatchReturnProbablyReal'], + '_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanUndeclaredProperty'], '_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMissingReturn'], '_inc/lib/debugger/class-jetpack-cxn-test-base.php' => ['PhanDeprecatedFunctionInternal', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchReturn'], '_inc/lib/debugger/class-jetpack-cxn-tests.php' => ['PhanPluginSimplifyExpressionBool'], diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php new file mode 100644 index 0000000000000..57015e3f344b8 --- /dev/null +++ b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/trait-wpcom-rest-api-proxy-request-trait.php @@ -0,0 +1,120 @@ +rest_base, '/' ) ) . ( $path ? '/' . rawurldecode( ltrim( $path, '/' ) ) : '' ); + $query_params = $request->get_query_params(); + $manager = new Manager(); + + /* + * A rest_route parameter can be added when using plain permalinks. + * It is not necessary to pass them to WordPress.com, + * and may even cause issues with some endpoints. + * Let's remove it. + */ + if ( isset( $query_params['rest_route'] ) ) { + unset( $query_params['rest_route'] ); + } + $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 ), + ), + 'method' => $request->get_method(), + ); + + // 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' ), + array( 'status' => rest_authorization_required_code() ) + ); + + if ( 'user' === $context ) { + if ( ! $manager->is_user_connected() ) { + if ( false === $allow_fallback_to_blog ) { + return $response; + } + + $context = 'blog'; + } else { + $response = Client::wpcom_json_api_request_as_user( $api_url, $this->version, $request_options, $body, $this->base_api_path ); + } + } + + if ( 'blog' === $context ) { + if ( ! $manager->is_connected() ) { + return $response; + } + + $response = Client::wpcom_json_api_request_as_blog( $api_url, $this->version, $request_options, $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 ), true ); + + 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; + } + + /** + * Proxy request to wpcom servers on behalf of a 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_as_user( $request, $path = '' ) { + return $this->proxy_request_to_wpcom( $request, $path, 'user' ); + } + + /** + * Proxy request to wpcom servers using the Site-level Connection (blog token). + * + * @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_as_blog( $request, $path = '' ) { + return $this->proxy_request_to_wpcom( $request, $path, 'blog' ); + } +} diff --git a/projects/plugins/jetpack/changelog/update-add-back-the-moved-file-to-fix-deployments b/projects/plugins/jetpack/changelog/update-add-back-the-moved-file-to-fix-deployments new file mode 100644 index 0000000000000..cc61dda7a0827 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-add-back-the-moved-file-to-fix-deployments @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Retain the file for deployments + +