template-tags
6 days ago
multibyte.php
6 days ago
query.php
6 days ago
url.php
6 days ago
utils.php
6 days ago
url.php
183 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Functions, or polyfills, related to URL manipulation |
| 4 | * |
| 5 | * @since 4.9.11 |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * The code for the `tribe_build_url` function is from https://github.com/jakeasmith/http_build_url |
| 10 | * |
| 11 | * @see https://github.com/jakeasmith/http_build_url |
| 12 | * |
| 13 | * URL constants as defined in the PHP Manual under "Constants usable with |
| 14 | * http_build_url()". |
| 15 | * |
| 16 | * @see http://us2.php.net/manual/en/http.constants.php#http.constants.url |
| 17 | */ |
| 18 | if ( ! defined( 'HTTP_URL_REPLACE' ) ) { |
| 19 | define( 'HTTP_URL_REPLACE', 1 ); |
| 20 | } |
| 21 | if ( ! defined( 'HTTP_URL_JOIN_PATH' ) ) { |
| 22 | define( 'HTTP_URL_JOIN_PATH', 2 ); |
| 23 | } |
| 24 | if ( ! defined( 'HTTP_URL_JOIN_QUERY' ) ) { |
| 25 | define( 'HTTP_URL_JOIN_QUERY', 4 ); |
| 26 | } |
| 27 | if ( ! defined( 'HTTP_URL_STRIP_USER' ) ) { |
| 28 | define( 'HTTP_URL_STRIP_USER', 8 ); |
| 29 | } |
| 30 | if ( ! defined( 'HTTP_URL_STRIP_PASS' ) ) { |
| 31 | define( 'HTTP_URL_STRIP_PASS', 16 ); |
| 32 | } |
| 33 | if ( ! defined( 'HTTP_URL_STRIP_AUTH' ) ) { |
| 34 | define( 'HTTP_URL_STRIP_AUTH', 32 ); |
| 35 | } |
| 36 | if ( ! defined( 'HTTP_URL_STRIP_PORT' ) ) { |
| 37 | define( 'HTTP_URL_STRIP_PORT', 64 ); |
| 38 | } |
| 39 | if ( ! defined( 'HTTP_URL_STRIP_PATH' ) ) { |
| 40 | define( 'HTTP_URL_STRIP_PATH', 128 ); |
| 41 | } |
| 42 | if ( ! defined( 'HTTP_URL_STRIP_QUERY' ) ) { |
| 43 | define( 'HTTP_URL_STRIP_QUERY', 256 ); |
| 44 | } |
| 45 | if ( ! defined( 'HTTP_URL_STRIP_FRAGMENT' ) ) { |
| 46 | define( 'HTTP_URL_STRIP_FRAGMENT', 512 ); |
| 47 | } |
| 48 | if ( ! defined( 'HTTP_URL_STRIP_ALL' ) ) { |
| 49 | define( 'HTTP_URL_STRIP_ALL', 1024 ); |
| 50 | } |
| 51 | |
| 52 | if ( ! function_exists( 'tribe_build_url' ) ) { |
| 53 | /** |
| 54 | * Build a URL. |
| 55 | * |
| 56 | * The parts of the second URL will be merged into the first according to |
| 57 | * the flags argument. |
| 58 | * |
| 59 | * @since 4.9.11 |
| 60 | * |
| 61 | * @param mixed $url (part(s) of) an URL in form of a string or associative array like parse_url() returns. |
| 62 | * @param mixed $parts Same as the first argument. |
| 63 | * @param int $flags A bitmask of binary ordered HTTP_URL constants; HTTP_URL_REPLACE is the default. |
| 64 | * @param array $new_url if set, it will be filled with the parts of the composed url like `parse_url()` would |
| 65 | * return. |
| 66 | * |
| 67 | * @return string The URL built from its parts. |
| 68 | */ |
| 69 | function tribe_build_url( $url, $parts = [], $flags = HTTP_URL_REPLACE, &$new_url = [] ) { |
| 70 | is_array( $url ) || $url = parse_url( $url ); |
| 71 | is_array( $parts ) || $parts = parse_url( $parts ); |
| 72 | |
| 73 | ( isset( $url['query'] ) && is_string( $url['query'] ) ) || $url['query'] = null; |
| 74 | ( isset( $parts['query'] ) && is_string( $parts['query'] ) ) || $parts['query'] = null; |
| 75 | |
| 76 | $keys = [ 'user', 'pass', 'port', 'path', 'query', 'fragment' ]; |
| 77 | |
| 78 | // HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags. |
| 79 | if ( $flags & HTTP_URL_STRIP_ALL ) { |
| 80 | $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS |
| 81 | | HTTP_URL_STRIP_PORT | HTTP_URL_STRIP_PATH |
| 82 | | HTTP_URL_STRIP_QUERY | HTTP_URL_STRIP_FRAGMENT; |
| 83 | } elseif ( $flags & HTTP_URL_STRIP_AUTH ) { |
| 84 | $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS; |
| 85 | } |
| 86 | |
| 87 | // Schema and host are alwasy replaced |
| 88 | foreach ( [ 'scheme', 'host' ] as $part ) { |
| 89 | if ( isset( $parts[ $part ] ) ) { |
| 90 | $url[ $part ] = $parts[ $part ]; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if ( $flags & HTTP_URL_REPLACE ) { |
| 95 | foreach ( $keys as $key ) { |
| 96 | if ( isset( $parts[ $key ] ) ) { |
| 97 | $url[ $key ] = $parts[ $key ]; |
| 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | if ( isset( $parts['path'] ) && ( $flags & HTTP_URL_JOIN_PATH ) ) { |
| 102 | if ( isset( $url['path'] ) && substr( $parts['path'], 0, 1 ) !== '/' ) { |
| 103 | // Workaround for trailing slashes |
| 104 | $url['path'] .= 'a'; |
| 105 | $url['path'] = rtrim( |
| 106 | str_replace( basename( $url['path'] ), '', $url['path'] ), |
| 107 | '/' |
| 108 | ) . '/' . ltrim( $parts['path'], '/' ); |
| 109 | } else { |
| 110 | $url['path'] = $parts['path']; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if ( isset( $parts['query'] ) && ( $flags & HTTP_URL_JOIN_QUERY ) ) { |
| 115 | if ( isset( $url['query'] ) ) { |
| 116 | parse_str( $url['query'], $url_query ); |
| 117 | parse_str( $parts['query'], $parts_query ); |
| 118 | |
| 119 | $url['query'] = http_build_query( |
| 120 | array_replace_recursive( |
| 121 | $url_query, |
| 122 | $parts_query |
| 123 | ) |
| 124 | ); |
| 125 | } else { |
| 126 | $url['query'] = $parts['query']; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if ( isset( $url['path'] ) && $url['path'] !== '' && substr( $url['path'], 0, 1 ) !== '/' ) { |
| 132 | $url['path'] = '/' . $url['path']; |
| 133 | } |
| 134 | |
| 135 | foreach ( $keys as $key ) { |
| 136 | $strip = 'HTTP_URL_STRIP_' . strtoupper( $key ); |
| 137 | if ( $flags & constant( $strip ) ) { |
| 138 | unset( $url[ $key ] ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $parsed_string = ''; |
| 143 | |
| 144 | if ( ! empty( $url['scheme'] ) ) { |
| 145 | $parsed_string .= $url['scheme'] . '://'; |
| 146 | } |
| 147 | |
| 148 | if ( ! empty( $url['user'] ) ) { |
| 149 | $parsed_string .= $url['user']; |
| 150 | |
| 151 | if ( isset( $url['pass'] ) ) { |
| 152 | $parsed_string .= ':' . $url['pass']; |
| 153 | } |
| 154 | |
| 155 | $parsed_string .= '@'; |
| 156 | } |
| 157 | |
| 158 | if ( ! empty( $url['host'] ) ) { |
| 159 | $parsed_string .= $url['host']; |
| 160 | } |
| 161 | |
| 162 | if ( ! empty( $url['port'] ) ) { |
| 163 | $parsed_string .= ':' . $url['port']; |
| 164 | } |
| 165 | |
| 166 | if ( ! empty( $url['path'] ) ) { |
| 167 | $parsed_string .= $url['path']; |
| 168 | } |
| 169 | |
| 170 | if ( ! empty( $url['query'] ) ) { |
| 171 | $parsed_string .= '?' . $url['query']; |
| 172 | } |
| 173 | |
| 174 | if ( ! empty( $url['fragment'] ) ) { |
| 175 | $parsed_string .= '#' . $url['fragment']; |
| 176 | } |
| 177 | |
| 178 | $new_url = $url; |
| 179 | |
| 180 | return $parsed_string; |
| 181 | } |
| 182 | } |
| 183 |