PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
jetpack / jetpack_vendor / automattic / jetpack-podcast / src / endpoints / trait-relay-response.php

trait-relay-response.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3, at jetpack_vendor/automattic/jetpack-podcast/src/endpoints/trait-relay-response.php

47 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared helper for the podcast REST proxy endpoints: relays a
4 * `Connection\Client` response back to the local REST client.
5 *
6 * @package automattic/jetpack-podcast
7 */
8
9 namespace Automattic\Jetpack\Podcast;
10
11 use WP_Error;
12 use WP_REST_Response;
13
14 /**
15 * Used by the wpcom/v2 proxy controllers (Podcast_Stats_Endpoint,
16 * Podcast_Distribution_Endpoint, Posts_To_Podcast_Endpoint) to round-trip the
17 * upstream wpcom response: preserves status code and decodes the JSON body so
18 * `apiFetch` on the client surfaces 4xx/5xx errors with their `{code, message}`
19 * payloads intact.
20 */
21 trait Relay_Response {
22
23 /**
24 * Relay an upstream Connection\Client response back to the local REST client.
25 * Preserves the upstream HTTP status code so 4xx/5xx mappings flow through.
26 *
27 * @param array|WP_Error $response The raw response from Connection\Client.
28 *
29 * @return WP_REST_Response|WP_Error
30 */
31 private function relay_response( $response ) {
32 if ( is_wp_error( $response ) ) {
33 return $response;
34 }
35
36 $code = (int) wp_remote_retrieve_response_code( $response );
37 $body = wp_remote_retrieve_body( $response );
38 $decoded = json_decode( $body, true );
39
40 $rest_response = rest_ensure_response( null === $decoded ? $body : $decoded );
41 if ( $code >= 100 && $code < 600 ) {
42 $rest_response->set_status( $code );
43 }
44 return $rest_response;
45 }
46 }
47