wp/wp-includes/http.php
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 18:28:13 +0200
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 13 d255fe9cd479
permissions -rw-r--r--
upgrade wordpress to 5.2.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     3
 * Core HTTP Request API
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     5
 * Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     6
 * decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * Returns the initialized WP_Http Object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    18
 * @staticvar WP_Http $http
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    19
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @return WP_Http HTTP Transport object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
function _wp_http_get_object() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    23
	static $http = null;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    25
	if ( is_null( $http ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		$http = new WP_Http();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    27
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	return $http;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 * Retrieve the raw response from a safe HTTP request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * This function is ideal when the HTTP request is being made to an arbitrary
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 * URL. The URL is validated to avoid redirection and request forgery attacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
function wp_safe_remote_request( $url, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	$args['reject_unsafe_urls'] = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    48
	$http                       = _wp_http_get_object();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	return $http->request( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
 * Retrieve the raw response from a safe HTTP request using the GET method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
 * This function is ideal when the HTTP request is being made to an arbitrary
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
 * URL. The URL is validated to avoid redirection and request forgery attacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
function wp_safe_remote_get( $url, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	$args['reject_unsafe_urls'] = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    69
	$http                       = _wp_http_get_object();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	return $http->get( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
 * Retrieve the raw response from a safe HTTP request using the POST method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
 * This function is ideal when the HTTP request is being made to an arbitrary
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
 * URL. The URL is validated to avoid redirection and request forgery attacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
function wp_safe_remote_post( $url, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	$args['reject_unsafe_urls'] = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    90
	$http                       = _wp_http_get_object();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	return $http->post( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
 * Retrieve the raw response from a safe HTTP request using the HEAD method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 * This function is ideal when the HTTP request is being made to an arbitrary
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 * URL. The URL is validated to avoid redirection and request forgery attacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
 * @param string $url Site URL to retrieve.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
 * @param array $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
function wp_safe_remote_head( $url, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	$args['reject_unsafe_urls'] = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   111
	$http                       = _wp_http_get_object();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	return $http->head( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 * Retrieve the raw response from the HTTP request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
 * The array structure is a little complex:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
 *     $res = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
 *         'headers'  => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
 *         'response' => array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
 *             'code'    => int,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
 *             'message' => string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
 *         )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
 *     );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * All of the headers in $res['headers'] are with the name as the key and the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * value as the value. So to get the User-Agent, you would do the following.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
 *     $user_agent = $res['headers']['user-agent'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 * The body is the raw response content and can be retrieved from $res['body'].
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * This function is called first to make the request and there are other API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 * functions to abstract out the above convoluted setup.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
 * Request method defaults for helper functions:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
 *  - Default 'GET'  for wp_remote_get()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
 *  - Default 'POST' for wp_remote_post()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
 *  - Default 'HEAD' for wp_remote_head()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
 * @see WP_Http::request() For additional information on default arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   151
function wp_remote_request( $url, $args = array() ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   152
	$http = _wp_http_get_object();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   153
	return $http->request( $url, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
 * Retrieve the raw response from the HTTP request using the GET method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   168
function wp_remote_get( $url, $args = array() ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   169
	$http = _wp_http_get_object();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   170
	return $http->get( $url, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
 * Retrieve the raw response from the HTTP request using the POST method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   185
function wp_remote_post( $url, $args = array() ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   186
	$http = _wp_http_get_object();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   187
	return $http->post( $url, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 * Retrieve the raw response from the HTTP request using the HEAD method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
 * @see wp_remote_request() For more information on the response array format.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
 * @see WP_Http::request() For default arguments information.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
 * @param string $url  Site URL to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
 * @param array  $args Optional. Request arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
 * @return WP_Error|array The response or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   202
function wp_remote_head( $url, $args = array() ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   203
	$http = _wp_http_get_object();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   204
	return $http->head( $url, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 * Retrieve only the headers from the raw response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 * @since 2.7.0
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   211
 * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   212
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   213
 * @see \Requests_Utility_CaseInsensitiveDictionary
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * @param array $response HTTP response.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   216
 * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
function wp_remote_retrieve_headers( $response ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   219
	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
		return array();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   221
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
	return $response['headers'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
 * Retrieve a single header by name from the raw response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   231
 * @param array  $response
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
 * @param string $header Header name to retrieve value from.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
function wp_remote_retrieve_header( $response, $header ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   236
	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
		return '';
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   238
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   240
	if ( isset( $response['headers'][ $header ] ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   241
		return $response['headers'][ $header ];
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   242
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
	return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
 * Retrieve only the response code from the raw response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 * Will return an empty array if incorrect parameter value is given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
 * @param array $response HTTP response.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
 * @return int|string The response code as an integer. Empty string on incorrect parameter given.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
function wp_remote_retrieve_response_code( $response ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   258
	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		return '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   260
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	return $response['response']['code'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
 * Retrieve only the response message from the raw response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 * Will return an empty array if incorrect parameter value is given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 * @param array $response HTTP response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
 * @return string The response message. Empty string on incorrect parameter given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
function wp_remote_retrieve_response_message( $response ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   276
	if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
		return '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   278
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	return $response['response']['message'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 * Retrieve only the body from the raw response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
 * @param array $response HTTP response.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
 * @return string The body of the response. Empty string if no body or incorrect parameter given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
function wp_remote_retrieve_body( $response ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   292
	if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		return '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   294
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	return $response['body'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   300
 * Retrieve only the cookies from the raw response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   301
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   302
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   303
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   304
 * @param array $response HTTP response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   305
 * @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.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   306
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   307
function wp_remote_retrieve_cookies( $response ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   308
	if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   309
		return array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   310
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   311
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   312
	return $response['cookies'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   313
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   314
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   315
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   316
 * Retrieve a single cookie by name from the raw response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   317
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   318
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   319
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   320
 * @param array  $response HTTP response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   321
 * @param string $name     The name of the cookie to retrieve.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   322
 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   323
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   324
function wp_remote_retrieve_cookie( $response, $name ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   325
	$cookies = wp_remote_retrieve_cookies( $response );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   326
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   327
	if ( empty( $cookies ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   328
		return '';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   329
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   330
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   331
	foreach ( $cookies as $cookie ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   332
		if ( $cookie->name === $name ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   333
			return $cookie;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   334
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   335
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   336
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   337
	return '';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   338
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   339
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   340
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   341
 * Retrieve a single cookie's value by name from the raw response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   342
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   343
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   344
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   345
 * @param array  $response HTTP response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   346
 * @param string $name     The name of the cookie to retrieve.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   347
 * @return string The value of the cookie. Empty string if the cookie isn't present in the response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   348
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   349
function wp_remote_retrieve_cookie_value( $response, $name ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   350
	$cookie = wp_remote_retrieve_cookie( $response, $name );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   351
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   352
	if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   353
		return '';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   354
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   355
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   356
	return $cookie->value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   357
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   358
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   359
/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
 * Determines if there is an HTTP Transport that can process this request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
 * @param array  $capabilities Array of capabilities to test or a wp_remote_request() $args array.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   365
 * @param string $url          Optional. If given, will check if the URL requires SSL and adds
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   366
 *                             that requirement to the capabilities array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
function wp_http_supports( $capabilities = array(), $url = null ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   371
	$http = _wp_http_get_object();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	$capabilities = wp_parse_args( $capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	$count = count( $capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
	if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
		$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   382
	if ( $url && ! isset( $capabilities['ssl'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		$scheme = parse_url( $url, PHP_URL_SCHEME );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
		if ( 'https' == $scheme || 'ssl' == $scheme ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
			$capabilities['ssl'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   389
	return (bool) $http->_get_first_available_transport( $capabilities );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
 * Get the HTTP Origin of the current request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
 * @return string URL of the origin. Empty string if no origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
function get_http_origin() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	$origin = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   401
	if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   402
		$origin = $_SERVER['HTTP_ORIGIN'];
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   403
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
	 * Change the origin of an HTTP request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
	 * @param string $origin The original origin for the request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	return apply_filters( 'http_origin', $origin );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 * Retrieve list of allowed HTTP origins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 * @return array Array of origin URLs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
function get_allowed_http_origins() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	$admin_origin = parse_url( admin_url() );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   424
	$home_origin  = parse_url( home_url() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
	// @todo preserve port?
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   427
	$allowed_origins = array_unique(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   428
		array(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   429
			'http://' . $admin_origin['host'],
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   430
			'https://' . $admin_origin['host'],
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   431
			'http://' . $home_origin['host'],
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   432
			'https://' . $home_origin['host'],
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   433
		)
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   434
	);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
	 * Change the origin types allowed for HTTP requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	 * @param array $allowed_origins {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	 *     Default allowed HTTP origins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	 *     @type string Non-secure URL for admin origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	 *     @type string Secure URL for admin origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	 *     @type string Non-secure URL for home origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
	 *     @type string Secure URL for home origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	 * }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   449
	return apply_filters( 'allowed_http_origins', $allowed_origins );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
 * Determines if the HTTP origin is an authorized one.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
 * @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   458
 * @return string Origin URL if allowed, empty string if not.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
function is_allowed_http_origin( $origin = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	$origin_arg = $origin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   463
	if ( null === $origin ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		$origin = get_http_origin();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   465
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   467
	if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		$origin = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   469
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	 * Change the allowed HTTP origin result.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   476
	 * @param string $origin     Origin URL if allowed, empty string if not.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   477
	 * @param string $origin_arg Original origin string passed into is_allowed_http_origin function.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
 * Send Access-Control-Allow-Origin and related headers if the current request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
 * is from an allowed origin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
 * If the request is an OPTIONS request, the script exits with either access
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
 * control headers sent, or a 403 response if the origin is not allowed. For
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
 * other request methods, you will receive a return value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   492
 * @return string|false Returns the origin URL if headers are sent. Returns false
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   493
 *                      if headers are not sent.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
function send_origin_headers() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	$origin = get_http_origin();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	if ( is_allowed_http_origin( $origin ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   499
		@header( 'Access-Control-Allow-Origin: ' . $origin );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		@header( 'Access-Control-Allow-Credentials: true' );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   501
		if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
			exit;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   503
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
		return $origin;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
		status_header( 403 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
		exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
 * Validate a URL for safe use in the HTTP API.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
 * @since 3.5.2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   520
 * @param string $url
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   521
 * @return false|string URL or false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
function wp_http_validate_url( $url ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   524
	$original_url = $url;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   525
	$url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   526
	if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
		return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   528
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
	$parsed_url = @parse_url( $url );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   531
	if ( ! $parsed_url || empty( $parsed_url['host'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
		return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   533
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   535
	if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
		return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   537
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   539
	if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   541
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
	$parsed_home = @parse_url( get_option( 'home' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   545
	if ( isset( $parsed_home['host'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   546
		$same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   547
	} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   548
		$same_host = false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   549
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
	if ( ! $same_host ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		$host = trim( $parsed_url['host'], '.' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   553
		if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
			$ip = $host;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
			$ip = gethostbyname( $host );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   557
			if ( $ip === $host ) { // Error condition for gethostbyname()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
				$ip = false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   559
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		if ( $ip ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
			$parts = array_map( 'intval', explode( '.', $ip ) );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   563
			if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0]
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
				|| ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
				|| ( 192 === $parts[0] && 168 === $parts[1] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
			) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
				// If host appears local, reject unless specifically allowed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
				/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
				 * Check if HTTP request is external or not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
				 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
				 * Allows to change and allow external requests for the HTTP request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
				 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
				 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
				 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   575
				 * @param bool   false Whether HTTP request is external or not.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
				 * @param string $host IP of the requested host.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   577
				 * @param string $url  URL of the requested host.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
				 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   579
				if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
					return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   581
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   586
	if ( empty( $parsed_url['port'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
		return $url;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   588
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	$port = $parsed_url['port'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   591
	if ( 80 === $port || 443 === $port || 8080 === $port ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
		return $url;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   593
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   595
	if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
		return $url;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   597
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
 * Whitelists allowed redirect hosts for safe HTTP requests as well.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   605
 * Attached to the {@see 'http_request_host_is_external'} filter.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   609
 * @param bool   $is_external
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
 * @param string $host
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
function allowed_http_request_hosts( $is_external, $host ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   614
	if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
		$is_external = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   616
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
	return $is_external;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
 * Whitelists any domain in a multisite installation for safe HTTP requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   623
 * Attached to the {@see 'http_request_host_is_external'} filter.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   627
 * @global wpdb $wpdb WordPress database abstraction object.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   628
 * @staticvar array $queried
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   629
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   630
 * @param bool   $is_external
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
 * @param string $host
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
function ms_allowed_http_request_hosts( $is_external, $host ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
	static $queried = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   637
	if ( $is_external ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
		return $is_external;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   639
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   640
	if ( $host === get_network()->domain ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
		return true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   642
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   643
	if ( isset( $queried[ $host ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
		return $queried[ $host ];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   645
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
	$queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
	return $queried[ $host ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   649
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   650
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   651
 * A wrapper for PHP's parse_url() function that handles consistency in the return
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   652
 * values across PHP versions.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   653
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   654
 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   655
 * schemeless and relative url's with :// in the path. This function works around
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   656
 * those limitations providing a standard output on PHP 5.2~5.4+.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   657
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   658
 * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   659
 * in the query are being handled inconsistently. This function works around those
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   660
 * differences as well.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   661
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   662
 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   663
 * when URL parsing failed.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   664
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   665
 * @since 4.4.0
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   666
 * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   667
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   668
 * @link https://secure.php.net/manual/en/function.parse-url.php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   669
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   670
 * @param string $url       The URL to parse.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   671
 * @param int    $component The specific component to retrieve. Use one of the PHP
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   672
 *                          predefined constants to specify which one.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   673
 *                          Defaults to -1 (= return all parts as an array).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   674
 * @return mixed False on parse failure; Array of URL components on success;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   675
 *               When a specific component has been requested: null if the component
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   676
 *               doesn't exist in the given URL; a string or - in the case of
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   677
 *               PHP_URL_PORT - integer when it does. See parse_url()'s return values.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   678
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   679
function wp_parse_url( $url, $component = -1 ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   680
	$to_unset = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   681
	$url      = strval( $url );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   682
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   683
	if ( '//' === substr( $url, 0, 2 ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   684
		$to_unset[] = 'scheme';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   685
		$url        = 'placeholder:' . $url;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   686
	} elseif ( '/' === substr( $url, 0, 1 ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   687
		$to_unset[] = 'scheme';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   688
		$to_unset[] = 'host';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   689
		$url        = 'placeholder://placeholder' . $url;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   690
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   691
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   692
	$parts = @parse_url( $url );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   693
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   694
	if ( false === $parts ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   695
		// Parsing failure.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   696
		return $parts;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   697
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   698
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   699
	// Remove the placeholder values.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   700
	foreach ( $to_unset as $key ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   701
		unset( $parts[ $key ] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   702
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   703
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   704
	return _get_component_from_parsed_url_array( $parts, $component );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   705
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   706
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   707
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   708
 * Retrieve a specific component from a parsed URL array.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   709
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   710
 * @internal
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   711
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   712
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   713
 * @access private
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   714
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   715
 * @link https://secure.php.net/manual/en/function.parse-url.php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   716
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   717
 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   718
 * @param int    $component The specific component to retrieve. Use one of the PHP
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   719
 *                          predefined constants to specify which one.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   720
 *                          Defaults to -1 (= return all parts as an array).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   721
 * @return mixed False on parse failure; Array of URL components on success;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   722
 *               When a specific component has been requested: null if the component
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   723
 *               doesn't exist in the given URL; a string or - in the case of
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   724
 *               PHP_URL_PORT - integer when it does. See parse_url()'s return values.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   725
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   726
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   727
	if ( -1 === $component ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   728
		return $url_parts;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   729
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   730
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   731
	$key = _wp_translate_php_url_constant_to_key( $component );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   732
	if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   733
		return $url_parts[ $key ];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   734
	} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   735
		return null;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   736
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   737
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   738
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   739
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   740
 * Translate a PHP_URL_* constant to the named array keys PHP uses.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   741
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   742
 * @internal
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   743
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   744
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   745
 * @access private
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   746
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   747
 * @link https://secure.php.net/manual/en/url.constants.php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   748
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   749
 * @param int $constant PHP_URL_* constant.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   750
 * @return string|bool The named key or false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   751
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   752
function _wp_translate_php_url_constant_to_key( $constant ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   753
	$translation = array(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   754
		PHP_URL_SCHEME   => 'scheme',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   755
		PHP_URL_HOST     => 'host',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   756
		PHP_URL_PORT     => 'port',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   757
		PHP_URL_USER     => 'user',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   758
		PHP_URL_PASS     => 'pass',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   759
		PHP_URL_PATH     => 'path',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   760
		PHP_URL_QUERY    => 'query',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   761
		PHP_URL_FRAGMENT => 'fragment',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   762
	);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   763
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   764
	if ( isset( $translation[ $constant ] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   765
		return $translation[ $constant ];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   766
	} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   767
		return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   768
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   769
}