Yoast\WP\SEO\Helpers

Url_Helper::get_link_type()publicYoast 1.0

Returns the link type.

Метод класса: Url_Helper{}

Хуков нет.

Возвращает

Строку. The link type.

Использование

$Url_Helper = new Url_Helper();
$Url_Helper->get_link_type( $url, $home_url, $is_image );
$url(массив) (обязательный)
The URL, as parsed by wp_parse_url.
$home_url(массив|null)
The home URL, as parsed by wp_parse_url. Used to avoid reparsing the home_url.
По умолчанию: null
$is_image(true|false)
Whether or not the link is an image.
По умолчанию: false

Код Url_Helper::get_link_type() Yoast 22.3

public function get_link_type( $url, $home_url = null, $is_image = false ) {
	// If there is no scheme and no host the link is always internal.
	// Beware, checking just the scheme isn't enough as a link can be //yoast.com for instance.
	if ( empty( $url['scheme'] ) && empty( $url['host'] ) ) {
		return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
	}

	// If there is a scheme but it's not http(s) then the link is always external.
	if ( \array_key_exists( 'scheme', $url ) && ! \in_array( $url['scheme'], [ 'http', 'https' ], true ) ) {
		return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
	}

	if ( \is_null( $home_url ) ) {
		$home_url = \wp_parse_url( \home_url() );
	}

	// When the base host is equal to the host.
	if ( isset( $url['host'] ) && $url['host'] !== $home_url['host'] ) {
		return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
	}

	// There is no base path and thus all URLs of the same domain are internal.
	if ( empty( $home_url['path'] ) ) {
		return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
	}

	// When there is a path and it matches the start of the url.
	if ( isset( $url['path'] ) && \strpos( $url['path'], $home_url['path'] ) === 0 ) {
		return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
	}

	return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
}