headers = array(); } /** * Fetch a resource using the legacy flat-response shape. * * @param string $path Resource path. * @param array $params Query parameters. * @return array|WP_Error Flat response or explicit failure. */ public function rest_get( $path, $params ) { return $this->legacy_request( 'get', $path, $params ); } /** * Replace a resource using the legacy flat-response shape. * * @param string $path Resource path. * @param mixed $body Request body. * @return array|WP_Error Flat response or explicit failure. */ public function rest_put( $path, $body ) { return $this->legacy_request( 'put', $path, $body ); } /** * Create a resource using the legacy flat-response shape. * * @param string $path Resource path. * @param mixed $body Request body. * @return array|WP_Error Flat response or explicit failure. */ public function rest_post( $path, $body ) { return $this->legacy_request( 'post', $path, $body ); } /** * Delete a resource using the legacy flat-response shape. * * @param string $path Resource path. * @param mixed $body Request body. * @return array|WP_Error Flat response or explicit failure. */ public function rest_delete( $path, $body = array() ) { return $this->legacy_request( 'delete', $path, $body ); } /** * Execute the shared transport and adapt only its response shape. * * @param string $method Lowercase HTTP method. * @param string $path Resource path. * @param mixed $data Query fields or request body. * @return array|WP_Error Flat response or explicit failure. */ protected function legacy_request( $method, $path, $data ) { switch ( $method ) { case 'get': $response = parent::rest_get( $path, $data ); break; case 'put': $response = parent::rest_put( $path, $data ); break; case 'post': $response = parent::rest_post( $path, $data ); break; case 'delete': $response = parent::rest_delete( $path, $data ); break; default: return new WP_Error( 'adzworld_rest_method', __( 'The requested REST method is not supported.', 'adz-world' ) ); } if ( is_wp_error( $response ) ) { return $response; } $headers = wp_remote_retrieve_headers( $response ); return array( 'code' => wp_remote_retrieve_response_code( $response ), 'headers' => $headers instanceof Traversable ? iterator_to_array( $headers ) : $headers, 'body' => wp_remote_retrieve_body( $response ), ); } }