From 4d7188202f7a607e1784b046c353e5bdd8549216 Mon Sep 17 00:00:00 2001 From: Nick Croft Date: Mon, 18 Nov 2024 09:09:19 -0500 Subject: [PATCH] Fix invalid offset error in API response It is possible for the API request to generate an invalid offset error if the array keys for status or body do not exist. This fixes it by checking to ensure the keys exist before attempting to use them. It will generate a WP_Error if one or both of the keys are missing in the response. --- includes/class-api.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/includes/class-api.php b/includes/class-api.php index c0dded4..b3b6e9e 100644 --- a/includes/class-api.php +++ b/includes/class-api.php @@ -480,6 +480,14 @@ protected function send_request( $method, $path, $query = [], $args = [], $cache $body = json_decode( wp_remote_retrieve_body( $response ), true ); + if ( ! isset( $body['status'] ) || ! isset( $body['data'] ) ) { + return new WP_Error( + 'wp101-api', + __( 'The WP101 API request response was invalid.', 'wp101' ), + $body['data'] + ); + } + if ( 'fail' === $body['status'] ) { return new WP_Error( 'wp101-api',