Skip to content

Commit

Permalink
Upgrade references to the Requests library in the WP code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Aug 27, 2021
1 parent 9b1489c commit 6ad50c0
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 48 deletions.
40 changes: 19 additions & 21 deletions src/wp-includes/class-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
* @since 2.7.0
*/

if ( ! class_exists( 'Requests' ) ) {
require ABSPATH . WPINC . '/class-requests.php';

Requests::register_autoloader();
Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
if ( ! class_exists( 'Requests\Autoload' ) ) {
require ABSPATH . WPINC . '/Requests/Autoload.php';
Requests\Requests::set_certificate_path( ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
}

/**
Expand Down Expand Up @@ -274,14 +272,14 @@ public function request( $url, $args = array() ) {
if ( empty( $url ) || empty( $arrURL['scheme'] ) ) {
$response = new WP_Error( 'http_request_failed', __( 'A valid URL was not provided.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
do_action( 'http_api_debug', $response, 'response', 'Requests\Requests', $parsed_args, $url );
return $response;
}

if ( $this->block_request( $url ) ) {
$response = new WP_Error( 'http_request_not_executed', __( 'User has blocked requests through HTTP.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
do_action( 'http_api_debug', $response, 'response', 'Requests\Requests', $parsed_args, $url );
return $response;
}

Expand All @@ -298,7 +296,7 @@ public function request( $url, $args = array() ) {
if ( ! wp_is_writable( dirname( $parsed_args['filename'] ) ) ) {
$response = new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
/** This action is documented in wp-includes/class-http.php */
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
do_action( 'http_api_debug', $response, 'response', 'Requests\Requests', $parsed_args, $url );
return $response;
}
}
Expand Down Expand Up @@ -346,7 +344,7 @@ public function request( $url, $args = array() ) {
$options['max_bytes'] = $parsed_args['limit_response_size'];
}

// If we've got cookies, use and convert them to Requests_Cookie.
// If we've got cookies, use and convert them to Requests\Cookie.
if ( ! empty( $parsed_args['cookies'] ) ) {
$options['cookies'] = WP_Http::normalize_cookies( $parsed_args['cookies'] );
}
Expand Down Expand Up @@ -378,7 +376,7 @@ public function request( $url, $args = array() ) {
// Check for proxies.
$proxy = new WP_HTTP_Proxy();
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' . $proxy->port() );
$options['proxy'] = new Requests\Proxy\HTTP( $proxy->host() . ':' . $proxy->port() );

if ( $proxy->use_authentication() ) {
$options['proxy']->use_authentication = true;
Expand All @@ -391,15 +389,15 @@ public function request( $url, $args = array() ) {
mbstring_binary_safe_encoding();

try {
$requests_response = Requests::request( $url, $headers, $data, $type, $options );
$requests_response = Requests\Requests::request( $url, $headers, $data, $type, $options );

// Convert the response into an array.
$http_response = new WP_HTTP_Requests_Response( $requests_response, $parsed_args['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() );
}

Expand All @@ -416,7 +414,7 @@ public function request( $url, $args = array() ) {
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/
do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url );
do_action( 'http_api_debug', $response, 'response', 'Requests\Requests', $parsed_args, $url );
if ( is_wp_error( $response ) ) {
return $response;
}
Expand Down Expand Up @@ -452,16 +450,16 @@ public function request( $url, $args = array() ) {
* @since 4.6.0
*
* @param array $cookies Array of cookies to send with the request.
* @return Requests_Cookie_Jar Cookie holder object.
* @return Requests\Cookie\Jar Cookie holder object.
*/
public static function normalize_cookies( $cookies ) {
$cookie_jar = new Requests_Cookie_Jar();
$cookie_jar = new Requests\Cookie\Jar();

foreach ( $cookies as $name => $value ) {
if ( $value instanceof WP_Http_Cookie ) {
$cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) );
$cookie_jar[ $value->name ] = new Requests\Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) );
} elseif ( is_scalar( $value ) ) {
$cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
$cookie_jar[ $name ] = new Requests\Cookie( $name, $value );
}
}

Expand All @@ -481,12 +479,12 @@ public static function normalize_cookies( $cookies ) {
* @param array $headers Headers for the redirect.
* @param string|array $data Body to send with the request.
* @param array $options Redirect request options.
* @param Requests_Response $original Response object.
* @param Requests\Response $original Response object.
*/
public static function browser_redirect_compatibility( $location, $headers, $data, &$options, $original ) {
// Browser compatibility.
if ( 302 === $original->status_code ) {
$options['type'] = Requests::GET;
$options['type'] = Requests\Requests::GET;
}
}

Expand All @@ -495,12 +493,12 @@ public static function browser_redirect_compatibility( $location, $headers, $dat
*
* @since 4.7.5
*
* @throws Requests_Exception On unsuccessful URL validation.
* @throws Requests\Exception On unsuccessful URL validation.
* @param string $location URL to redirect to.
*/
public static function validate_redirects( $location ) {
if ( ! wp_http_validate_url( $location ) ) {
throw new Requests_Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
throw new Requests\Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/class-wp-http-requests-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*
* @since 4.7.0
*
* @see Requests_Hooks
* @see Requests\Hooks
*/
class WP_HTTP_Requests_Hooks extends Requests_Hooks {
class WP_HTTP_Requests_Hooks extends Requests\Hooks {
/**
* Requested URL.
*
Expand Down
16 changes: 8 additions & 8 deletions src/wp-includes/class-wp-http-requests-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/**
* Core wrapper object for a Requests_Response for standardisation.
* Core wrapper object for a Requests\Response for standardisation.
*
* @since 4.6.0
*
Expand All @@ -19,7 +19,7 @@ class WP_HTTP_Requests_Response extends WP_HTTP_Response {
* Requests Response object.
*
* @since 4.6.0
* @var Requests_Response
* @var Requests\Response
*/
protected $response;

Expand All @@ -36,10 +36,10 @@ class WP_HTTP_Requests_Response extends WP_HTTP_Response {
*
* @since 4.6.0
*
* @param Requests_Response $response HTTP response.
* @param Requests\Response $response HTTP response.
* @param string $filename Optional. File name. Default empty.
*/
public function __construct( Requests_Response $response, $filename = '' ) {
public function __construct( Requests\Response $response, $filename = '' ) {
$this->response = $response;
$this->filename = $filename;
}
Expand All @@ -49,7 +49,7 @@ public function __construct( Requests_Response $response, $filename = '' ) {
*
* @since 4.6.0
*
* @return Requests_Response HTTP response.
* @return Requests\Response HTTP response.
*/
public function get_response_object() {
return $this->response;
Expand All @@ -60,11 +60,11 @@ public function get_response_object() {
*
* @since 4.6.0
*
* @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
* @return \Requests\Utility\CaseInsensitiveDictionary Map of header name to header value.
*/
public function get_headers() {
// Ensure headers remain case-insensitive.
$converted = new Requests_Utility_CaseInsensitiveDictionary();
$converted = new Requests\Utility\CaseInsensitiveDictionary();

foreach ( $this->response->headers->getAll() as $key => $value ) {
if ( count( $value ) === 1 ) {
Expand All @@ -85,7 +85,7 @@ public function get_headers() {
* @param array $headers Map of header name to header value.
*/
public function set_headers( $headers ) {
$this->response->headers = new Requests_Response_Headers( $headers );
$this->response->headers = new Requests\Response\Headers( $headers );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/wp-includes/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ function wp_remote_head( $url, $args = array() ) {
* Retrieve only the headers from the raw response.
*
* @since 2.7.0
* @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
* @since 4.6.0 Return value changed from an array to an Requests\Utility\CaseInsensitiveDictionary instance.
*
* @see \Requests_Utility_CaseInsensitiveDictionary
* @see \Requests\Utility\CaseInsensitiveDictionary
*
* @param array|WP_Error $response HTTP response.
* @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
* @return array|\Requests\Utility\CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
*/
function wp_remote_retrieve_headers( $response ) {
if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ function rest_parse_request_arg( $value, $request, $param ) {
function rest_is_ip_address( $ip ) {
$ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';

if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) {
if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests\IPv6::check_ipv6( $ip ) ) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/feed/wpSimplePieFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function mocked_response_single_header_values() {
);

return array(
'headers' => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
'headers' => new Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
'body' => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
'response' => array(
'code' => 200,
Expand Down Expand Up @@ -114,7 +114,7 @@ public function mocked_response_multiple_header_values() {
),
);

$response['headers'] = new Requests_Utility_CaseInsensitiveDictionary( $multiple_value_headers );
$response['headers'] = new Requests\Utility\CaseInsensitiveDictionary( $multiple_value_headers );

return $response;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ function test_is_serialized( $value, $expected ) {
*/
function test_deserialize_request_utility_filtered_iterator_objects( $value ) {
$serialized = maybe_serialize( $value );
if ( get_class( $value ) === 'Requests_Utility_FilteredIterator' ) {
if ( get_class( $value ) === 'Requests\Utility\FilteredIterator' ) {
$new_value = unserialize( $serialized );
$property = ( new ReflectionClass( 'Requests_Utility_FilteredIterator' ) )->getProperty( 'callback' );
$property = ( new ReflectionClass( 'Requests\Utility\FilteredIterator' ) )->getProperty( 'callback' );
$property->setAccessible( true );
$callback_value = $property->getValue( $new_value );
$this->assertSame( null, $callback_value );
Expand All @@ -377,8 +377,8 @@ function test_deserialize_request_utility_filtered_iterator_objects( $value ) {

function data_serialize_deserialize_objects() {
return array(
array( new Requests_Utility_FilteredIterator( array( 1 ), 'md5' ) ),
array( new Requests_Utility_FilteredIterator( array( 1, 2 ), 'sha1' ) ),
array( new Requests\Utility\FilteredIterator( array( 1 ), 'md5' ) ),
array( new Requests\Utility\FilteredIterator( array( 1, 2 ), 'sha1' ) ),
array( new ArrayIterator( array( 1, 2, 3 ) ) ),
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/http/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ function test_get_response_cookies_with_name_value_array() {
*/
function test_get_cookie_host_only() {
// Emulate WP_Http::request() internals.
$requests_response = new Requests_Response();
$requests_response = new Requests\Response();

$requests_response->cookies['test'] = Requests_Cookie::parse( 'test=foo; domain=.wordpress.org' );
$requests_response->cookies['test'] = Requests\Cookie::parse( 'test=foo; domain=.wordpress.org' );

$requests_response->cookies['test']->flags['host-only'] = false; // https://github.com/WordPress/Requests/issues/306

Expand All @@ -231,7 +231,7 @@ function test_get_cookie_host_only() {
$this->assertSame( $cookie->domain, 'wordpress.org' );
$this->assertFalse( $cookie->host_only, 'host-only flag not set' );

// Regurgitate (Requests_Cookie -> WP_Http_Cookie -> Requests_Cookie).
// Regurgitate (Requests\Cookie -> WP_Http_Cookie -> Requests\Cookie).
$cookies = WP_Http::normalize_cookies( wp_remote_retrieve_cookies( $response ) );
$this->assertFalse( $cookies['test']->flags['host-only'], 'host-only flag data lost' );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/http/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ public function test_normalize_cookies_scalar_values() {
)
);

$this->assertInstanceOf( 'Requests_Cookie_Jar', $cookie_jar );
$this->assertInstanceOf( 'Requests\Cookie\Jar', $cookie_jar );

foreach ( array_keys( $cookies ) as $cookie ) {
if ( 'foo' === $cookie ) {
$this->assertArrayNotHasKey( $cookie, $cookie_jar );
} else {
$this->assertInstanceOf( 'Requests_Cookie', $cookie_jar[ $cookie ] );
$this->assertInstanceOf( 'Requests\Cookie', $cookie_jar[ $cookie ] );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function mocked_rss_response() {
);

return array(
'headers' => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
'headers' => new Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
'body' => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
'response' => array(
'code' => 200,
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/widgets/wpWidgetRss.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function mocked_rss_response() {
);

return array(
'headers' => new Requests_Utility_CaseInsensitiveDictionary( $single_value_headers ),
'headers' => new Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
'body' => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
'response' => array(
'code' => 200,
Expand Down

0 comments on commit 6ad50c0

Please sign in to comment.