diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index cb6833772a765..b97dfd014be41 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -3428,7 +3428,8 @@ function rest_convert_error_to_response( $error ) { $status = array_reduce( $error->get_all_error_data(), static function ( $status, $error_data ) { - return $error_data['status'] ?? $status; + // Note: $error_data may not be an array (e.g. stdClass), so is_array() check is intentional. + return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; }, 500 ); diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 692c363ac7595..bd3a9346f70b2 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -592,6 +592,19 @@ public function test_error_to_response_with_additional_data() { $this->assertSame( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] ); } + /** + * @ticket 64901 + */ + public function test_error_to_response_with_stdclass_data() { + $error = new WP_Error( 'test', 'test', (object) array( 'status' => 400 ) ); + + $response = rest_convert_error_to_response( $error ); + $this->assertInstanceOf( 'WP_REST_Response', $response ); + + // stdClass data should not cause a fatal, status should default to 500. + $this->assertSame( 500, $response->get_status() ); + } + public function test_rest_error() { $data = array( 'code' => 'wp-api-test-error',