Automattic\WooCommerce\Gateways\PayPal

Request::normalize_url_for_paypalprivateWC 1.0

Normalize a URL for PayPal. PayPal requires absolute URLs with protocol.

Method of the class: Request{}

No Hooks.

Returns

String. Normalized URL.

Usage

// private - for code of main (parent) class only
$result = $this->normalize_url_for_paypal( $url ): string;
$url(string) (required)
The URL to check.

Request::normalize_url_for_paypal() code WC 10.9.4

private function normalize_url_for_paypal( string $url ): string {
	// Replace encoded ampersand with actual ampersand.
	// In some cases, the URL may contain encoded ampersand but PayPal expects the actual ampersand.
	// PayPal request fails if the URL contains encoded ampersand.
	$url = str_replace( '&', '&', $url );

	// If the URL is already the home URL, return it.
	if ( strpos( $url, home_url() ) === 0 ) {
		return esc_url_raw( $url );
	}

	// Return the URL if it is already absolute (contains ://).
	if ( strpos( $url, '://' ) !== false ) {
		return esc_url_raw( $url );
	}

	$home_url = untrailingslashit( home_url() );

	// If the URL is relative (starts with /), prepend the home URL.
	if ( strpos( $url, '/' ) === 0 ) {
		return esc_url_raw( $home_url . $url );
	}

	// Prepend home URL with a slash.
	return esc_url_raw( $home_url . '/' . $url );
}