tribe_build_url( mixed $url, mixed $parts = array(), int $flags = HTTP_URL_REPLACE, array $new_url = array() )

Build a URL.

The parts of the second URL will be merged into the first according to the flags argument.


Parameters #

$url

(mixed) (Required) (part(s) of) an URL in form of a string or associative array like parse_url() returns.

$parts

(mixed) (Optional) Same as the first argument.

Default value: array()

$flags

(int) (Optional) A bitmask of binary ordered HTTP_URL constants; HTTP_URL_REPLACE is the default.

Default value: HTTP_URL_REPLACE

$new_url

(array) (Optional) if set, it will be filled with the parts of the composed url like parse_url() would return.

Default value: array()


Top ↑

Return #

(string) The URL built from its parts.


Top ↑

Source #

File: src/functions/url.php

	function tribe_build_url( $url, $parts = [], $flags = HTTP_URL_REPLACE, &$new_url = [] ) {
		is_array( $url ) || $url = parse_url( $url );
		is_array( $parts ) || $parts = parse_url( $parts );

		( isset( $url['query'] ) && is_string( $url['query'] ) ) || $url['query'] = null;
		( isset( $parts['query'] ) && is_string( $parts['query'] ) ) || $parts['query'] = null;

		$keys = [ 'user', 'pass', 'port', 'path', 'query', 'fragment' ];

		// HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags.
		if ( $flags & HTTP_URL_STRIP_ALL ) {
			$flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS
			          | HTTP_URL_STRIP_PORT | HTTP_URL_STRIP_PATH
			          | HTTP_URL_STRIP_QUERY | HTTP_URL_STRIP_FRAGMENT;
		} elseif ( $flags & HTTP_URL_STRIP_AUTH ) {
			$flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS;
		}

		// Schema and host are alwasy replaced
		foreach ( [ 'scheme', 'host' ] as $part ) {
			if ( isset( $parts[ $part ] ) ) {
				$url[ $part ] = $parts[ $part ];
			}
		}

		if ( $flags & HTTP_URL_REPLACE ) {
			foreach ( $keys as $key ) {
				if ( isset( $parts[ $key ] ) ) {
					$url[ $key ] = $parts[ $key ];
				}
			}
		} else {
			if ( isset( $parts['path'] ) && ( $flags & HTTP_URL_JOIN_PATH ) ) {
				if ( isset( $url['path'] ) && substr( $parts['path'], 0, 1 ) !== '/' ) {
					// Workaround for trailing slashes
					$url['path'] .= 'a';
					$url['path'] = rtrim(
						               str_replace( basename( $url['path'] ), '', $url['path'] ),
						               '/'
					               ) . '/' . ltrim( $parts['path'], '/' );
				} else {
					$url['path'] = $parts['path'];
				}
			}

			if ( isset( $parts['query'] ) && ( $flags & HTTP_URL_JOIN_QUERY ) ) {
				if ( isset( $url['query'] ) ) {
					parse_str( $url['query'], $url_query );
					parse_str( $parts['query'], $parts_query );

					$url['query'] = http_build_query(
						array_replace_recursive(
							$url_query,
							$parts_query
						)
					);
				} else {
					$url['query'] = $parts['query'];
				}
			}
		}

		if ( isset( $url['path'] ) && $url['path'] !== '' && substr( $url['path'], 0, 1 ) !== '/' ) {
			$url['path'] = '/' . $url['path'];
		}

		foreach ( $keys as $key ) {
			$strip = 'HTTP_URL_STRIP_' . strtoupper( $key );
			if ( $flags & constant( $strip ) ) {
				unset( $url[ $key ] );
			}
		}

		$parsed_string = '';

		if ( ! empty( $url['scheme'] ) ) {
			$parsed_string .= $url['scheme'] . '://';
		}

		if ( ! empty( $url['user'] ) ) {
			$parsed_string .= $url['user'];

			if ( isset( $url['pass'] ) ) {
				$parsed_string .= ':' . $url['pass'];
			}

			$parsed_string .= '@';
		}

		if ( ! empty( $url['host'] ) ) {
			$parsed_string .= $url['host'];
		}

		if ( ! empty( $url['port'] ) ) {
			$parsed_string .= ':' . $url['port'];
		}

		if ( ! empty( $url['path'] ) ) {
			$parsed_string .= $url['path'];
		}

		if ( ! empty( $url['query'] ) ) {
			$parsed_string .= '?' . $url['query'];
		}

		if ( ! empty( $url['fragment'] ) ) {
			$parsed_string .= '#' . $url['fragment'];
		}

		$new_url = $url;

		return $parsed_string;
	}

Top ↑

Changelog #

Changelog
Version Description
4.9.11 Introduced.