: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Validate redirected URLs.
* @throws WpOrg\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 WpOrg\Requests\Exception( __( 'A valid URL was not provided.' ), 'wp_http.redirect_failed_validation' );
* Tests which transports are capable of supporting the request.
* @deprecated 6.4.0 Use WpOrg\Requests\Requests::get_transport_class()
* @see WpOrg\Requests\Requests::get_transport_class()
* @param array $args Request arguments.
* @param string $url URL to request.
* @return string|false Class name for the first transport that claims to support the request.
* False if no transport claims to support the request.
public function _get_first_available_transport( $args, $url = null ) {
$transports = array( 'curl', 'streams' );
* Filters which HTTP transports are available and in what order.
* @deprecated 6.4.0 Use WpOrg\Requests\Requests::get_transport_class()
* @param string[] $transports Array of HTTP transports to check. Default array contains
* 'curl' and 'streams', in that order.
* @param array $args HTTP request arguments.
* @param string $url The URL to request.
$request_order = apply_filters_deprecated( 'http_api_transports', array( $transports, $args, $url ), '6.4.0' );
// Loop over each transport on each HTTP request looking for one which will serve this request's needs.
foreach ( $request_order as $transport ) {
if ( in_array( $transport, $transports, true ) ) {
$transport = ucfirst( $transport );
$class = 'WP_Http_' . $transport;
// Check to see if this transport is a possibility, calls the transport statically.
if ( ! call_user_func( array( $class, 'test' ), $args, $url ) ) {
* Dispatches a HTTP request to a supporting transport.
* Tests each transport in order to find a transport which matches the request arguments.
* Also caches the transport instance to be used later.
* The order for requests is cURL, and then PHP Streams.
* @deprecated 5.1.0 Use WP_Http::request()
* @see WP_Http::request()
* @param string $url URL to request.
* @param array $args Request arguments.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error.
private function _dispatch_request( $url, $args ) {
static $transports = array();
$class = $this->_get_first_available_transport( $args, $url );
return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
// Transport claims to support request, instantiate it and give it a whirl.
if ( empty( $transports[ $class ] ) ) {
$transports[ $class ] = new $class();
$response = $transports[ $class ]->request( $url, $args );
/** This action is documented in wp-includes/class-wp-http.php */
do_action( 'http_api_debug', $response, 'response', $class, $args, $url );
if ( is_wp_error( $response ) ) {
/** This filter is documented in wp-includes/class-wp-http.php */
return apply_filters( 'http_response', $response, $args, $url );
* Uses the POST HTTP method.
* Used for sending data that is expected to be in the body.
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error. See WP_Http::response() for details.
public function post( $url, $args = array() ) {
$defaults = array( 'method' => 'POST' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
* Uses the GET HTTP method.
* Used for sending data that is expected to be in the body.
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error. See WP_Http::response() for details.
public function get( $url, $args = array() ) {
$defaults = array( 'method' => 'GET' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
* Uses the HEAD HTTP method.
* Used for sending data that is expected to be in the body.
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
* A WP_Error instance upon error. See WP_Http::response() for details.
public function head( $url, $args = array() ) {
$defaults = array( 'method' => 'HEAD' );
$parsed_args = wp_parse_args( $args, $defaults );
return $this->request( $url, $parsed_args );
* Parses the responses and splits the parts into headers and body.
* @param string $response The full response string.
* Array with response headers and body.
* @type string $headers HTTP response headers.
* @type string $body HTTP response body.
public static function processResponse( $response ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$response = explode( "\r\n\r\n", $response, 2 );
'headers' => $response[0],
'body' => isset( $response[1] ) ? $response[1] : '',
* Transforms header string into an array.
* @param string|array $headers The original headers. If a string is passed, it will be converted
* to an array. If an array is passed, then it is assumed to be
* raw header data with numeric keys with the headers as the values.
* No headers must be passed that were already processed.
* @param string $url Optional. The URL that was requested. Default empty.
* Processed string headers. If duplicate headers are encountered,
* then a numbered array is returned as the value of that header-key.
* @type array $response {
* @type int $code The response status code. Default 0.
* @type string $message The response message. Default empty.
* @type array $newheaders The processed header data as a multidimensional array.
* @type WP_Http_Cookie[] $cookies If the original headers contain the 'Set-Cookie' key,
* an array containing `WP_Http_Cookie` objects is returned.
public static function processHeaders( $headers, $url = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
// Split headers, one per array element.
if ( is_string( $headers ) ) {
// Tolerate line terminator: CRLF = LF (RFC 2616 19.3).
$headers = str_replace( "\r\n", "\n", $headers );
* Unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>,
* <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2).
$headers = preg_replace( '/\n[ \t]/', ' ', $headers );
// Create the headers array.
$headers = explode( "\n", $headers );
* If a redirection has taken place, The headers for each page request may have been passed.
* In this case, determine the final HTTP header and parse from there.
for ( $i = count( $headers ) - 1; $i >= 0; $i-- ) {
if ( ! empty( $headers[ $i ] ) && ! str_contains( $headers[ $i ], ':' ) ) {
$headers = array_splice( $headers, $i );
foreach ( (array) $headers as $tempheader ) {
if ( empty( $tempheader ) ) {
if ( ! str_contains( $tempheader, ':' ) ) {
$stack = explode( ' ', $tempheader, 3 );
list( , $response['code'], $response['message']) = $stack;
list($key, $value) = explode( ':', $tempheader, 2 );
$key = strtolower( $key );
if ( isset( $newheaders[ $key ] ) ) {
if ( ! is_array( $newheaders[ $key ] ) ) {
$newheaders[ $key ] = array( $newheaders[ $key ] );
$newheaders[ $key ][] = $value;
$newheaders[ $key ] = $value;
if ( 'set-cookie' === $key ) {
$cookies[] = new WP_Http_Cookie( $value, $url );
// Cast the Response Code to an int.
$response['code'] = (int) $response['code'];
'headers' => $newheaders,
* Takes the arguments for a ::request() and checks for the cookie array.
* If it's found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances,
* which are each parsed into strings and added to the Cookie: header (within the arguments array).
* Edits the array by reference.
* @param array $r Full array of args passed into ::request()
public static function buildCookieHeader( &$r ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
if ( ! empty( $r['cookies'] ) ) {
// Upgrade any name => value cookie pairs to WP_HTTP_Cookie instances.
foreach ( $r['cookies'] as $name => $value ) {
if ( ! is_object( $value ) ) {
$r['cookies'][ $name ] = new WP_Http_Cookie(
foreach ( (array) $r['cookies'] as $cookie ) {
$cookies_header .= $cookie->getHeaderValue() . '; ';
$cookies_header = substr( $cookies_header, 0, -2 );
$r['headers']['cookie'] = $cookies_header;
* Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
* Based off the HTTP http_encoding_dechunk function.
* @link https://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding.
* @param string $body Body content.
* @return string Chunked decoded body on success or raw body on failure.
public static function chunkTransferDecode( $body ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
// The body is not chunked encoded or is malformed.
if ( ! preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', trim( $body ) ) ) {
// We'll be altering $body, so need a backup in case of error.
$has_chunk = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $body, $match );
if ( ! $has_chunk || empty( $match[1] ) ) {
$length = hexdec( $match[1] );
$chunk_length = strlen( $match[0] );
// Parse out the chunk of data.
$parsed_body .= substr( $body, $chunk_length, $length );
// Remove the chunk from the raw data.
$body = substr( $body, $length + $chunk_length );
if ( '0' === trim( $body ) ) {
* Determines whether an HTTP API request to the given URL should be blocked.
* Those who are behind a proxy and want to prevent access to certain hosts may do so. This will
* prevent plugins from working and core functionality, if you don't include `api.wordpress.org`.
* You block external URL requests by defining `WP_HTTP_BLOCK_EXTERNAL` as true in your `wp-config.php`
* file and this will only allow localhost and your site to make requests. The constant
* `WP_ACCESSIBLE_HOSTS` will allow additional hosts to go through for requests. The format of the
* `WP_ACCESSIBLE_HOSTS` constant is a comma separated list of hostnames to allow, wildcard domains
* are supported, eg `*.wordpress.org` will allow for all subdomains of `wordpress.org` to be contacted.
* @link https://core.trac.wordpress.org/ticket/8927 Allow preventing external requests.
* @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS
* @param string $uri URI of url.
* @return bool True to block, false to allow.
public function block_request( $uri ) {
// We don't need to block requests, because nothing is blocked.
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) {
$check = parse_url( $uri );
$home = parse_url( get_option( 'siteurl' ) );
// Don't block requests back to ourselves by default.
if ( 'localhost' === $check['host'] || ( isset( $home['host'] ) && $home['host'] === $check['host'] ) ) {
* Filters whether to block local HTTP API requests.
* A local request is one to `localhost` or to the same host as the site itself.
* @param bool $block Whether to block local requests. Default false.
return apply_filters( 'block_local_requests', false );
if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
static $accessible_hosts = null;
static $wildcard_regex = array();
if ( null === $accessible_hosts ) {
$accessible_hosts = preg_split( '|,\s*|', WP_ACCESSIBLE_HOSTS );
if ( str_contains( WP_ACCESSIBLE_HOSTS, '*' ) ) {
$wildcard_regex = array();
foreach ( $accessible_hosts as $host ) {
$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
if ( ! empty( $wildcard_regex ) ) {
return ! preg_match( $wildcard_regex, $check['host'] );
return ! in_array( $check['host'], $accessible_hosts, true ); // Inverse logic, if it's in the array, then don't block it.
* Used as a wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7.
* @deprecated 4.4.0 Use wp_parse_url()
* @param string $url The URL to parse.
* @return bool|array False on failure; Array of URL components on success;
* See parse_url()'s return values.
protected static function parse_url( $url ) {
_deprecated_function( __METHOD__, '4.4.0', 'wp_parse_url()' );
return wp_parse_url( $url );
* Converts a relative URL to an absolute URL relative to a given URL.
* If an Absolute URL is provided, no processing of that URL is done.
* @param string $maybe_relative_path The URL which might be relative.
* @param string $url The URL which $maybe_relative_path is relative to.
* @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned.
public static function make_absolute_url( $maybe_relative_path, $url ) {
return $maybe_relative_path;
$url_parts = wp_parse_url( $url );
return $maybe_relative_path;
$relative_url_parts = wp_parse_url( $maybe_relative_path );
if ( ! $relative_url_parts ) {
return $maybe_relative_path;
// Check for a scheme on the 'relative' URL.
if ( ! empty( $relative_url_parts['scheme'] ) ) {
return $maybe_relative_path;
$absolute_path = $url_parts['scheme'] . '://';
// Schemeless URLs will make it this far, so we check for a host in the relative URL
// and convert it to a protocol-URL.
if ( isset( $relative_url_parts['host'] ) ) {
$absolute_path .= $relative_url_parts['host'];
if ( isset( $relative_url_parts['port'] ) ) {
$absolute_path .= ':' . $relative_url_parts['port'];
$absolute_path .= $url_parts['host'];
if ( isset( $url_parts['port'] ) ) {
$absolute_path .= ':' . $url_parts['port'];
// Start off with the absolute URL path.
$path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/';
// If it's a root-relative path, then great.
if ( ! empty( $relative_url_parts['path'] ) && '/' === $relative_url_parts['path'][0] ) {
$path = $relative_url_parts['path'];
// Else it's a relative path.
} elseif ( ! empty( $relative_url_parts['path'] ) ) {
// Strip off any file components from the absolute path.
$path = substr( $path, 0, strrpos( $path, '/' ) + 1 );