HTTP: Add some new Cookie helper functions:

* `wp_remote_retrieve_cookies( $response )`
* `wp_remote_retrieve_cookie( $response, $name )`
* `wp_remote_retrieve_cookie_value( $response, $name )`

Adds unit tests.

Props johnbillion.
Fixes #33711.

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


git-svn-id: http://core.svn.wordpress.org/trunk@34333 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-09-22 03:14:25 +00:00
parent 8241813825
commit 62a91851de
2 changed files with 61 additions and 1 deletions

View File

@ -285,6 +285,66 @@ function wp_remote_retrieve_body( $response ) {
return $response['body']; return $response['body'];
} }
/**
* Retrieve only the body from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @return array An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error.
*/
function wp_remote_retrieve_cookies( $response ) {
if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
return array();
}
return $response['cookies'];
}
/**
* Retrieve a single cookie by name from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @param string $name The name of the cookie to retrieve.
* @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
*/
function wp_remote_retrieve_cookie( $response, $name ) {
$cookies = wp_remote_retrieve_cookies( $response );
if ( empty( $cookies ) ) {
return '';
}
foreach ( $cookies as $cookie ) {
if ( $cookie->name === $name ) {
return $cookie;
}
}
return '';
}
/**
* Retrieve a single cookie's value by name from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @param string $name The name of the cookie to retrieve.
* @return string The value of the cookie. Empty string if the cookie isn't present in the response.
*/
function wp_remote_retrieve_cookie_value( $response, $name ) {
$cookie = wp_remote_retrieve_cookie( $response, $name );
if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
return '';
}
return $cookie->value;
}
/** /**
* Determines if there is an HTTP Transport that can process this request. * Determines if there is an HTTP Transport that can process this request.
* *

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-34368'; $wp_version = '4.4-alpha-34369';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.