Automattic\WooCommerce\Internal\Utilities

URL::preprocess()privateWC 1.0

Makes all slashes forward slashes, converts filepaths to file:// URLs, and other processing to help with comprehension of filepaths.

Method of the class: URL{}

No Hooks.

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->preprocess();

URL::preprocess() code WC 7.1.1

private function preprocess() {
	// For consistency, all slashes should be forward slashes.
	$this->url = str_replace( '\\', '/', $this->url );

	// Windows: capture the drive letter if provided.
	if ( preg_match( '#^(file://)?([a-z]):/(?!/).*#i', $this->url, $matches ) ) {
		$this->components['drive'] = $matches[2];
	}

	/*
	 * If there is no scheme, assume and prepend "file://". An exception is made for cases where the URL simply
	 * starts with exactly two forward slashes, which indicates 'any scheme' (most commonly, that is used when
	 * there is freedom to switch between 'http' and 'https').
	 */
	if ( ! preg_match( '#^[a-z]+://#i', $this->url ) && ! preg_match( '#^//(?!/)#', $this->url ) ) {
		$this->url = 'file://' . $this->url;
	}

	$parsed_components = wp_parse_url( $this->url );

	// If we received a really badly formed URL, let's go no further.
	if ( false === $parsed_components ) {
		throw new URLException(
			sprintf(
			/* translators: %s is the URL. */
				__( '%s is not a valid URL.', 'woocommerce' ),
				$this->url
			)
		);
	}

	$this->components = array_merge( $this->components, $parsed_components );

	// File URLs cannot have a host. However, the initial path segment *or* the Windows drive letter
	// (if present) may be incorrectly be interpreted as the host name.
	if ( 'file' === $this->components['scheme'] && ! empty( $this->components['host'] ) ) {
		// If we do not have a drive letter, then simply merge the host and the path together.
		if ( null === $this->components['drive'] ) {
			$this->components['path'] = $this->components['host'] . ( $this->components['path'] ?? '' );
		}

		// Restore the host to null in this situation.
		$this->components['host'] = null;
	}
}