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. git-svn-id: https://develop.svn.wordpress.org/trunk@37989 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
3edf3be241
commit
194c8d0e33
@ -348,10 +348,14 @@ class WP_Http {
|
|||||||
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] );
|
$options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = Requests::request( $url, $headers, $data, $type, $options );
|
$requests_response = Requests::request( $url, $headers, $data, $type, $options );
|
||||||
|
|
||||||
// Convert the response into an array
|
// 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 ) {
|
catch ( Requests_Exception $e ) {
|
||||||
$response = new WP_Error( 'http_request_failed', $e->getMessage() );
|
$response = new WP_Error( 'http_request_failed', $e->getMessage() );
|
||||||
@ -382,6 +386,7 @@ class WP_Http {
|
|||||||
'message' => false,
|
'message' => false,
|
||||||
),
|
),
|
||||||
'cookies' => array(),
|
'cookies' => array(),
|
||||||
|
'http_response' => null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper object for a Requests_Response for compatibility.
|
* Wrapper object for a Requests_Response for standardisation.
|
||||||
*
|
*
|
||||||
* @package WordPress
|
* @package WordPress
|
||||||
* @subpackage HTTP
|
* @subpackage HTTP
|
||||||
* @since 4.6.0
|
* @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.
|
* 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.
|
* @return array WP_Http response array, per WP_Http::request().
|
||||||
*
|
|
||||||
* @param string|int $key Array offset.
|
|
||||||
* @return bool True if the offset exists, false otherwise.
|
|
||||||
*/
|
*/
|
||||||
public function offsetExists( $key ) {
|
public function to_array() {
|
||||||
$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':
|
|
||||||
return array(
|
return array(
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'body' => $this->get_data(),
|
||||||
|
'response' => array(
|
||||||
'code' => $this->get_status(),
|
'code' => $this->get_status(),
|
||||||
'message' => get_status_header_desc( $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 );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,15 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
|
$this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends test_head_request
|
||||||
|
*/
|
||||||
|
function test_returns_array() {
|
||||||
|
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
|
||||||
|
$response = wp_remote_head( $url );
|
||||||
|
$this->assertInternalType( 'array', $response );
|
||||||
|
}
|
||||||
|
|
||||||
function test_head_redirect() {
|
function test_head_redirect() {
|
||||||
// this url will 301 redirect
|
// this url will 301 redirect
|
||||||
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
|
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
|
||||||
|
Loading…
Reference in New Issue
Block a user