diff --git a/src/Blocks/CheckoutMapBlock.php b/src/Blocks/CheckoutMapBlock.php index fe9e37a..693f7e9 100644 --- a/src/Blocks/CheckoutMapBlock.php +++ b/src/Blocks/CheckoutMapBlock.php @@ -65,7 +65,7 @@ public static function extend_cart_data(): array 'city' => $city, 'points' => $city !== -1 ? $api->getOffices([ 'city_code' => $city, - ])['body'] : '[]', + ])->body() : '[]', ]; } diff --git a/src/CdekApi.php b/src/CdekApi.php index 601a1a9..a75bc83 100644 --- a/src/CdekApi.php +++ b/src/CdekApi.php @@ -310,8 +310,8 @@ public function getOffices($filter) 'GET', $this->tokenStorage->getToken(), $filter, - )->json(); - if (!$result) { + ); + if (!$result->body()) { return [ 'success' => false, 'message' => esc_html__( diff --git a/src/Controllers/LocationController.php b/src/Controllers/LocationController.php index e94456d..bac3a21 100644 --- a/src/Controllers/LocationController.php +++ b/src/Controllers/LocationController.php @@ -20,10 +20,10 @@ public static function getPoints(WP_REST_Request $data): WP_REST_Response { $data = (new CdekApi)->getOffices($data->get_params()); - return new WP_REST_Response($data instanceof WP_Error ? $data : $data['body'], 200, [ - 'x-current-page' => $data['headers']['x-current-page'] ?? null, - 'x-total-pages' => $data['headers']['x-total-pages'] ?? null, - 'x-total-elements' => $data['headers']['x-total-elements'] ?? null, + return new WP_REST_Response($data->body(), 200, [ + 'x-current-page' => $data->getHeaders()['x-current-page'] ?? null, + 'x-total-pages' => $data->getHeaders()['x-total-pages'] ?? null, + 'x-total-elements' => $data->getHeaders()['x-total-elements'] ?? null, ]); } diff --git a/src/Transport/HttpResponse.php b/src/Transport/HttpResponse.php index 7f8e2b4..c743752 100644 --- a/src/Transport/HttpResponse.php +++ b/src/Transport/HttpResponse.php @@ -51,7 +51,7 @@ public function getStatusCode(): int */ public function data(): array { - if (!isset($this->headers['content-type']) || $this->headers['content-type'] !== 'application/json') { + if (!isset($this->headers['content-type']) || strpos($this->headers['content-type'], 'application/json') === false) { throw new RuntimeException('Response is not JSON'); } @@ -67,7 +67,7 @@ public function data(): array */ public function error(): array { - if (!isset($this->headers['content-type']) || $this->headers['content-type'] !== 'application/json') { + if (!isset($this->headers['content-type']) || strpos($this->headers['content-type'], 'application/json') === false) { throw new RuntimeException('Response is not JSON'); } @@ -83,7 +83,7 @@ public function error(): array */ public function json(): array { - if (!isset($this->headers['content-type']) || $this->headers['content-type'] !== 'application/json') { + if (!isset($this->headers['content-type']) || strpos($this->headers['content-type'], 'application/json') === false) { throw new RuntimeException('Response is not JSON'); } diff --git a/src/UI/AdminShippingFields.php b/src/UI/AdminShippingFields.php index ac69419..4a9ffe7 100644 --- a/src/UI/AdminShippingFields.php +++ b/src/UI/AdminShippingFields.php @@ -48,8 +48,7 @@ public function __invoke($item_id, $item): void break; case MetaKeys::OFFICE_CODE: try { - $officeAddress = json_decode((new CdekApi)->getOffices(['code' => $meta['value']])['body'], - true, 512, JSON_THROW_ON_ERROR); + $officeAddress = (new CdekApi)->getOffices(['code' => $meta['value']])->json(); if (empty($officeAddress[0]['location']['address'])) { self::renderOffice(esc_html__('Not available for order', 'cdekdelivery')); diff --git a/src/UI/CheckoutMap.php b/src/UI/CheckoutMap.php index 54fdd82..3493c21 100644 --- a/src/UI/CheckoutMap.php +++ b/src/UI/CheckoutMap.php @@ -37,7 +37,7 @@ public function __invoke($shippingMethodCurrent): void $points = $city !== -1 ? $api->getOffices([ 'city_code' => $city, - ])['body'] : '[]'; + ])->body() : '[]'; $mapAutoClose = CheckoutHelper::getMapAutoClose();