diff --git a/src/wp-includes/http-functions.php b/src/wp-includes/http-functions.php index 748be1c855..e4e8507cc3 100644 --- a/src/wp-includes/http-functions.php +++ b/src/wp-includes/http-functions.php @@ -285,6 +285,66 @@ function wp_remote_retrieve_body( $response ) { 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. * diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index ce2631495f..355682bf12 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -75,4 +75,32 @@ class Tests_HTTP_Functions extends WP_UnitTestCase { $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); $this->assertWPError( $response ); } + + /** + * @ticket 33711 + */ + function test_get_response_cookies() { + $url = 'https://wordpress.org/wp-login.php'; + + $response = wp_remote_head( $url ); + $cookies = wp_remote_retrieve_cookies( $response ); + + $this->assertInternalType( 'array', $cookies ); + $this->assertNotEmpty( $cookies ); + + $cookie = wp_remote_retrieve_cookie( $response, 'wordpress_test_cookie' ); + $this->assertInstanceOf( 'WP_Http_Cookie', $cookie ); + $this->assertSame( 'wordpress_test_cookie', $cookie->name ); + $this->assertSame( 'WP Cookie check', $cookie->value ); + + $value = wp_remote_retrieve_cookie_value( $response, 'wordpress_test_cookie' ); + $this->assertSame( 'WP Cookie check', $value ); + + $no_value = wp_remote_retrieve_cookie_value( $response, 'not_a_cookie' ); + $this->assertSame( '', $no_value ); + + $no_cookie = wp_remote_retrieve_cookie( $response, 'not_a_cookie' ); + $this->assertSame( '', $no_cookie ); + } + }