HTTP API: Switch back to returning an array.

The array-compatibility object we started returning in r37428 unfortunately isn't enough like an array. In particular, `is_array()` checks fail, despite the object implementing ArrayAccess. Mea culpa.

This moves the WP_HTTP_Response object to a new http_response key in the array, and changes the value back to an actual array.

Fixes #37097.
See #33055.

Built from https://develop.svn.wordpress.org/trunk@37989


git-svn-id: http://core.svn.wordpress.org/trunk@37930 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2016-07-06 17:51:30 +00:00
parent b9d99d3288
commit ff6a4875a8
3 changed files with 23 additions and 86 deletions

View File

@ -348,10 +348,14 @@ class WP_Http {
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] );
try {
$response = Requests::request( $url, $headers, $data, $type, $options );
$requests_response = Requests::request( $url, $headers, $data, $type, $options );
// Convert the response into an array
$response = new WP_HTTP_Requests_Response( $response, $r['filename'] );
$http_response = new WP_HTTP_Requests_Response( $requests_response, $r['filename'] );
$response = $http_response->to_array();
// Add the original object to the array.
$response['http_response'] = $http_response;
}
catch ( Requests_Exception $e ) {
$response = new WP_Error( 'http_request_failed', $e->getMessage() );
@ -382,6 +386,7 @@ class WP_Http {
'message' => false,
),
'cookies' => array(),
'http_response' => null,
);
}

View File

@ -1,13 +1,13 @@
<?php
/**
* Wrapper object for a Requests_Response for compatibility.
* Wrapper object for a Requests_Response for standardisation.
*
* @package WordPress
* @subpackage HTTP
* @since 4.6.0
*/
class WP_HTTP_Requests_Response extends WP_HTTP_Response implements ArrayAccess {
class WP_HTTP_Requests_Response extends WP_HTTP_Response {
/**
* Requests Response object.
*
@ -142,88 +142,20 @@ class WP_HTTP_Requests_Response extends WP_HTTP_Response implements ArrayAccess
}
/**
* Check if an ArrayAccess offset exists.
* Convert the object to a WP_Http response array.
*
* This is for array access back-compat.
*
* @param string|int $key Array offset.
* @return bool True if the offset exists, false otherwise.
* @return array WP_Http response array, per WP_Http::request().
*/
public function offsetExists( $key ) {
$allowed = array( 'headers', 'body', 'response', 'cookies', 'filename' );
return in_array( $key, $allowed );
}
/**
* Get an ArrayAccess value.
*
* This is for array access back-compat.
*
* @param string|int $key Array offset to get.
* @return mixed Value if the key is a valid offset, null if invalid.
*/
public function offsetGet( $key ) {
switch ( $key ) {
case 'headers':
return $this->get_headers();
case 'body':
return $this->get_data();
case 'response':
public function to_array() {
return array(
'headers' => $this->get_headers(),
'body' => $this->get_data(),
'response' => array(
'code' => $this->get_status(),
'message' => get_status_header_desc( $this->get_status() ),
),
'cookies' => $this->get_cookies(),
'filename' => $this->filename,
);
case 'cookies':
return $this->get_cookies();
case 'filename':
return $this->filename;
}
return null;
}
/**
* Set an ArrayAccess value.
*
* This is for array access back-compat.
*
* @param string|int $key Array offset to set.
* @param mixed $value Value to set.
*/
public function offsetSet( $key, $value ) {
switch ( $key ) {
case 'headers':
$this->set_headers( $value );
break;
case 'body':
$this->set_data( $value );
break;
case 'response':
if ( isset( $value['code'] ) ) {
$this->set_status( $value['code'] );
}
break;
case 'filename':
$this->filename = $value;
break;
}
}
/**
* Unset an ArrayAccess value.
*
* This is for array access back-compat.
*
* @param string|int $key Array offset to remove.
*/
public function offsetUnset( $key ) {
$this->offsetSet( $key, null );
}
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-beta1-37988';
$wp_version = '4.6-beta1-37989';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.