| 1 |
<?php |
| 2 |
/** |
| 3 |
* @since 1.4 |
| 4 |
*/ |
| 5 |
defined( 'WPINC' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* http_build_url() compatibility |
| 9 |
*/ |
| 10 |
if ( ! function_exists( 'http_build_url' ) ) { |
| 11 |
if ( ! defined( 'HTTP_URL_REPLACE' ) ) { |
| 12 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 13 |
define( 'HTTP_URL_REPLACE', 1 ); // Replace every part of the first URL when there's one of the second URL |
| 14 |
} |
| 15 |
if ( ! defined( 'HTTP_URL_JOIN_PATH' ) ) { |
| 16 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 17 |
define( 'HTTP_URL_JOIN_PATH', 2 ); // Join relative paths |
| 18 |
} |
| 19 |
if ( ! defined( 'HTTP_URL_JOIN_QUERY' ) ) { |
| 20 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 21 |
define( 'HTTP_URL_JOIN_QUERY', 4 ); // Join query strings |
| 22 |
} |
| 23 |
if ( ! defined( 'HTTP_URL_STRIP_USER' ) ) { |
| 24 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 25 |
define( 'HTTP_URL_STRIP_USER', 8 ); // Strip any user authentication information |
| 26 |
} |
| 27 |
if ( ! defined( 'HTTP_URL_STRIP_PASS' ) ) { |
| 28 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 29 |
define( 'HTTP_URL_STRIP_PASS', 16 ); // Strip any password authentication information |
| 30 |
} |
| 31 |
if ( ! defined( 'HTTP_URL_STRIP_AUTH' ) ) { |
| 32 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 33 |
define( 'HTTP_URL_STRIP_AUTH', 32 ); // Strip any authentication information |
| 34 |
} |
| 35 |
if ( ! defined( 'HTTP_URL_STRIP_PORT' ) ) { |
| 36 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 37 |
define( 'HTTP_URL_STRIP_PORT', 64 ); // Strip explicit port numbers |
| 38 |
} |
| 39 |
if ( ! defined( 'HTTP_URL_STRIP_PATH' ) ) { |
| 40 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 41 |
define( 'HTTP_URL_STRIP_PATH', 128 ); // Strip complete path |
| 42 |
} |
| 43 |
if ( ! defined( 'HTTP_URL_STRIP_QUERY' ) ) { |
| 44 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 45 |
define( 'HTTP_URL_STRIP_QUERY', 256 ); // Strip query string |
| 46 |
} |
| 47 |
if ( ! defined( 'HTTP_URL_STRIP_FRAGMENT' ) ) { |
| 48 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 49 |
define( 'HTTP_URL_STRIP_FRAGMENT', 512 ); // Strip any fragments (#identifier) |
| 50 |
} |
| 51 |
if ( ! defined( 'HTTP_URL_STRIP_ALL' ) ) { |
| 52 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- polyfill constant must match the upstream PECL name. |
| 53 |
define( 'HTTP_URL_STRIP_ALL', 1024 ); // Strip anything but scheme and host |
| 54 |
} |
| 55 |
|
| 56 |
// Build an URL |
| 57 |
// The parts of the second URL will be merged into the first according to the flags argument. |
| 58 |
// |
| 59 |
// @param mixed (Part(s) of) an URL in form of a string or associative array like parse_url() returns |
| 60 |
// @param mixed Same as the first argument |
| 61 |
// @param int A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default |
| 62 |
// @param array If set, it will be filled with the parts of the composed url like parse_url() would return |
| 63 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- polyfill must match the PHP/PECL function name. |
| 64 |
function http_build_url( $url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false ) { |
| 65 |
$keys = array( 'user', 'pass', 'port', 'path', 'query', 'fragment' ); |
| 66 |
|
| 67 |
// HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs |
| 68 |
if ( $flags & HTTP_URL_STRIP_ALL ) { |
| 69 |
$flags |= HTTP_URL_STRIP_USER; |
| 70 |
$flags |= HTTP_URL_STRIP_PASS; |
| 71 |
$flags |= HTTP_URL_STRIP_PORT; |
| 72 |
$flags |= HTTP_URL_STRIP_PATH; |
| 73 |
$flags |= HTTP_URL_STRIP_QUERY; |
| 74 |
$flags |= HTTP_URL_STRIP_FRAGMENT; |
| 75 |
} |
| 76 |
// HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS |
| 77 |
elseif ( $flags & HTTP_URL_STRIP_AUTH ) { |
| 78 |
$flags |= HTTP_URL_STRIP_USER; |
| 79 |
$flags |= HTTP_URL_STRIP_PASS; |
| 80 |
} |
| 81 |
|
| 82 |
// Parse the original URL |
| 83 |
// - Suggestion by Sayed Ahad Abbas |
| 84 |
// In case you send a parse_url array as input |
| 85 |
$parse_url = ! is_array( $url ) ? wp_parse_url( $url ) : $url; |
| 86 |
|
| 87 |
// Scheme and Host are always replaced |
| 88 |
if ( isset( $parts['scheme'] ) ) { |
| 89 |
$parse_url['scheme'] = $parts['scheme']; |
| 90 |
} |
| 91 |
if ( isset( $parts['host'] ) ) { |
| 92 |
$parse_url['host'] = $parts['host']; |
| 93 |
} |
| 94 |
|
| 95 |
// (If applicable) Replace the original URL with it's new parts |
| 96 |
if ( $flags & HTTP_URL_REPLACE ) { |
| 97 |
foreach ( $keys as $key ) { |
| 98 |
if ( isset( $parts[ $key ] ) ) { |
| 99 |
$parse_url[ $key ] = $parts[ $key ]; |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
// Join the original URL path with the new path |
| 104 |
if ( isset( $parts['path'] ) && ( $flags & HTTP_URL_JOIN_PATH ) ) { |
| 105 |
if ( isset( $parse_url['path'] ) ) { |
| 106 |
$parse_url['path'] = rtrim( str_replace( basename( $parse_url['path'] ), '', $parse_url['path'] ), '/' ) . '/' . ltrim( $parts['path'], '/' ); |
| 107 |
} else { |
| 108 |
$parse_url['path'] = $parts['path']; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Join the original query string with the new query string |
| 113 |
if ( isset( $parts['query'] ) && ( $flags & HTTP_URL_JOIN_QUERY ) ) { |
| 114 |
if ( isset( $parse_url['query'] ) ) { |
| 115 |
$parse_url['query'] .= '&' . $parts['query']; |
| 116 |
} else { |
| 117 |
$parse_url['query'] = $parts['query']; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Strips all the applicable sections of the URL |
| 123 |
// Note: Scheme and Host are never stripped |
| 124 |
foreach ( $keys as $key ) { |
| 125 |
if ( $flags & (int) constant( 'HTTP_URL_STRIP_' . strtoupper( $key ) ) ) { |
| 126 |
unset( $parse_url[ $key ] ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$new_url = $parse_url; |
| 131 |
|
| 132 |
return ( isset( $parse_url['scheme'] ) ? $parse_url['scheme'] . '://' : '' ) |
| 133 |
. ( isset( $parse_url['user'] ) ? $parse_url['user'] . ( isset( $parse_url['pass'] ) ? ':' . $parse_url['pass'] : '' ) . '@' : '' ) |
| 134 |
. ( isset( $parse_url['host'] ) ? $parse_url['host'] : '' ) |
| 135 |
. ( isset( $parse_url['port'] ) ? ':' . $parse_url['port'] : '' ) |
| 136 |
. ( isset( $parse_url['path'] ) ? $parse_url['path'] : '' ) |
| 137 |
. ( isset( $parse_url['query'] ) ? '?' . $parse_url['query'] : '' ) |
| 138 |
. ( isset( $parse_url['fragment'] ) ? '#' . $parse_url['fragment'] : '' ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if ( ! function_exists( 'array_key_first' ) ) { |
| 144 |
function array_key_first( array $arr ) { |
| 145 |
foreach ( $arr as $k => $unused ) { |
| 146 |
return $k; |
| 147 |
} |
| 148 |
return null; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! function_exists( 'array_column' ) ) { |
| 153 |
function array_column( $array, $column_name ) { |
| 154 |
return array_map( |
| 155 |
function ( $element ) use ( $column_name ) { |
| 156 |
return $element[ $column_name ]; |
| 157 |
}, |
| 158 |
$array |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
|