$value ) { $request->set_param( $key, $value ); } $response = rest_do_request( $request ); if ( is_wp_error( $response ) ) { // Only truly fundamental dispatch failures (e.g. no matching route) // come back this way โ€” application-level errors returned by a route // callback are converted by WP_REST_Server into an error-shaped // WP_REST_Response instead (handled below). return $response; } if ( ! $response instanceof WP_REST_Response ) { return new WP_Error( 'invalid_rest_response', __( 'Unexpected internal REST response.', 'templately' ) ); } $data = $response->get_data(); if ( $response->get_status() >= 400 ) { return new WP_Error( $data['code'] ?? 'internal_rest_error', $data['message'] ?? __( 'An internal REST request failed.', 'templately' ), $data['data'] ?? [] ); } // A failed route can also come back with a status BELOW 400: a // transport-level failure (cloud unreachable) carries `data.status = 0`, // and WP_REST_Server derives the HTTP status from that field โ€” so the // check above never fires and callers would iterate an error body as if // it were their payload. Detect the flattened WP_Error shape directly // (same signature RestEnvelope::is_wp_error_shape() keys on). if ( is_array( $data ) && isset( $data['code'], $data['message'] ) && is_string( $data['code'] ) && ! array_key_exists( 'success', $data ) && isset( $data['data'] ) && is_array( $data['data'] ) && array_key_exists( 'status', $data['data'] ) ) { return new WP_Error( $data['code'], $data['message'], $data['data'] ); } return $response; } }