HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ubuntu-8gb-hel1-1 6.8.0-55-generic #57-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 12 23:42:21 UTC 2025 x86_64
User: www-data (33)
PHP: 8.1.32
Disabled: NONE
Upload Files
File: /var/www/agighana.org_backup/Google_URL_Normalizer.php
<?php
/**
 * Class Google\Site_Kit\Core\Util\Google_URL_Normalizer
 *
 * @package   Google\Site_Kit
 * @copyright 2021 Google LLC
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link      https://sitekit.withgoogle.com
 */

namespace Google\Site_Kit\Core\Util;

/**
 * Class handling URL normalization for comparisons and API requests.
 *
 * @since 1.18.0
 * @access private
 * @ignore
 */
final class Google_URL_Normalizer {

	/**
	 * Normalizes a URL by converting to all lowercase, converting Unicode characters
	 * to punycode, and removing bidirectional control characters.
	 *
	 * @since 1.18.0
	 *
	 * @param string $url The URL or domain to normalize.
	 * @return string The normalized URL or domain.
	 */
	public function normalize_url( $url ) {
		// Remove bidirectional control characters.
		$url = preg_replace( array( '/\xe2\x80\xac/', '/\xe2\x80\xab/' ), '', $url );
		$url = $this->decode_unicode_url_or_domain( $url );
		$url = strtolower( $url );

		return $url;
	}

	/**
	 * Returns the Punycode version of a Unicode URL or domain name.
	 *
	 * @since 1.18.0
	 *
	 * @param string $url The URL or domain name to decode.
	 */
	protected function decode_unicode_url_or_domain( $url ) {
		$encoder_class = class_exists( '\WpOrg\Requests\IdnaEncoder' ) ? '\WpOrg\Requests\IdnaEncoder' : '\Requests_IDNAEncoder';

		$parts = URL::parse( $url );
		if ( ! $parts || ! isset( $parts['host'] ) || '' === $parts['host'] ) {
			return $encoder_class::encode( $url );
		}
		$decoded_host = $encoder_class::encode( $parts['host'] );
		return str_replace( $parts['host'], $decoded_host, $url );
	}
}