Skip to content
wiki.fftac.org

Linklocalizer - Source Excerpt 01

Back to Linklocalizer

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/LinkLocalizer.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/LinkLocalizer.php

<?php

namespace UAIXLocaleRouter;

use UAIXLocaleRouter\Support\Path;


if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class LinkLocalizer {
	/**
	 * Guard against recursive canonical-link resolution.
	 *
	 * @var int
	 */
	private static $canonical_query_resolution_depth = 0;

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public static function register() {
		add_filter( 'home_url', array( __CLASS__, 'filter_home_url' ), 20, 4 );
		add_filter( 'page_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'post_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'post_type_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'term_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'author_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'get_pagenum_link', array( __CLASS__, 'filter_localized_url' ), 20 );
		add_filter( 'nav_menu_link_attributes', array( __CLASS__, 'filter_menu_link_attributes' ), 20, 4 );
		add_filter( 'the_content', array( __CLASS__, 'filter_content_links' ), 20 );
		add_action( 'wp_head', array( __CLASS__, 'render_seo_tags' ), 1 );
	}

	/**
	 * Localize home_url output.
	 *
	 * @param string      $url Localized URL.
	 * @param string      $path Requested path.
	 * @param string|null $orig_scheme Original scheme.
	 * @param int|null    $blog_id Blog ID.
	 * @return string
	 */
	public static function filter_home_url( $url, $path, $orig_scheme, $blog_id ) {
		unset( $orig_scheme, $blog_id );

		if ( self::should_bypass_localization_context() ) {
			return $url;
		}

		return self::localize_url( $url, (string) $path );
	}

	/**
	 * Localize a generic URL.
	 *
	 * @param string $url URL input.
	 * @return string
	 */
	public static function filter_localized_url( $url ) {
		if ( self::should_bypass_localization_context() ) {
			return $url;
		}

		return self::localize_url( $url );
	}

	/**
	 * Localize menu item hrefs.
	 *
	 * @param array $atts Menu link attributes.
	 * @return array
	 */
	public static function filter_menu_link_attributes( $atts ) {
		if ( self::should_bypass_localization_context() ) {
			return $atts;
		}

		if ( isset( $atts['href'] ) && is_string( $atts['href'] ) ) {
			$atts['href'] = self::localize_url( $atts['href'] );
		}

		return $atts;
	}

	/**
	 * Rewrite internal href attributes inside post content.
	 *
	 * @param string $content Content HTML.
	 * @return string
	 */
	public static function filter_content_links( $content ) {
		$settings = Plugin::settings();

		if ( self::should_bypass_localization_context() || empty( $settings['content_link_rewriting'] ) || ! is_string( $content ) || '' === $content ) {
			return $content;
		}

		if ( false === stripos( $content, 'href=' ) ) {
			return $content;
		}

		// Guard the regex pass so unusually large generated pages are not rewritten on the hot path.
		$max_bytes = (int) apply_filters( 'uaixlr_content_link_rewrite_max_bytes', 1048576 );

		if ( $max_bytes > 0 && strlen( $content ) > $max_bytes ) {
			return $content;
		}

		return preg_replace_callback(
			'#href=("|\')(.*?)\1#i',
			static function ( $matches ) {
				$quote = isset( $matches[1] ) ? $matches[1] : '"';
				$url   = isset( $matches[2] ) ? $matches[2] : '';

				return 'href=' . $quote . esc_url( self::localize_url( $url ) ) . $quote;
			},
			$content
		);
	}

	/**
	 * Output canonical and hreflang tags.
	 *
	 * @return void
	 */
	public static function render_seo_tags() {
		if ( self::should_bypass_localization_context() || Router::is_excluded_path( Router::get_original_request_path() ) ) {
			return;
		}

		$current_tag  = Router::get_current_url_tag();
		$current_path = Router::get_stripped_request_path();
		$query_args   = Path::current_query_args();
		$current_url  = Path::build_front_url( Path::localize_path( $current_path, $current_tag ), $query_args );

		echo '<link rel="canonical" href="' . esc_url( $current_url ) . "\" />\n";

		foreach ( LocaleRepository::get_enabled_locales() as $locale ) {
			$href = Path::build_front_url( Path::localize_path( $current_path, $locale['urlTag'] ), $query_args );
			echo '<link rel="alternate" hreflang="' . esc_attr( $locale['tag'] ) . '" href="' . esc_url( $href ) . "\" />\n";
		}

		$x_default = Path::build_front_url( Path::localize_path( $current_path, LocaleRepository::get_default_url_tag() ), $query_args );
		echo '<link rel="alternate" hreflang="x-default" href="' . esc_url( $x_default ) . "\" />\n";
	}

	/**
	 * Localize an internal URL.
	 *
	 * @param string $url URL input.
	 * @param string $path_hint Optional path hint.
	 * @return string
	 */
	public static function localize_url( $url, $path_hint = '' ) {
		$url = (string) $url;

		if ( self::$canonical_query_resolution_depth > 0 ) {
			return $url;
		}

		if ( '' === $url || self::should_bypass_localization_context() || self::is_excluded_url( $url ) ) {
			return $url;
		}

		$site_host = wp_parse_url( (string) get_option( 'home' ), PHP_URL_HOST );
		$host      = wp_parse_url( $url, PHP_URL_HOST );

		if ( is_string( $host ) && '' !== $host && is_string( $site_host ) && strtolower( $host ) !== strtolower( $site_host ) ) {
			return $url;
		}

		$canonical_url = self::canonicalize_internal_query_url( $url );

		if ( '' !== $canonical_url && $canonical_url !== $url ) {
			$url       = $canonical_url;
			$path_hint = '';
		}

		$path = '' !== $path_hint ? (string) $path_hint : (string) wp_parse_url( $url, PHP_URL_PATH );

		if ( '' === $path ) {
			$path = '/';
		}

		if ( self::has_explicit_locale_prefix( $path ) ) {
			return $url;
		}

		if ( Router::is_excluded_path( $path ) ) {
			return $url;
		}

		$query = array();
		parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );

		$fragment         = (string) wp_parse_url( $url, PHP_URL_FRAGMENT );
		$url_tag          = Router::get_current_url_tag();
		$localized_path   = Path::localize_path( $path, $url_tag );
		$is_root_relative = '/' === substr( $url, 0, 1 ) && '//' !== substr( $url, 0, 2 );

		if ( $is_root_relative ) {
			$localized_url = $localized_path;

			if ( ! empty( $query ) ) {
				$localized_url = add_query_arg( $query, $localized_url );
			}

			if ( '' !== $fragment ) {
				$localized_url .= '#' . ltrim( $fragment, '#' );
			}

			return $localized_url;
		}

		return Path::build_front_url( $localized_path, $query, $fragment );
	}

	/**
	 * Determine whether a URL path already pins an enabled locale explicitly.
	 *
	 * @param string $path Path input.
	 * @return bool
	 */
	private static function has_explicit_locale_prefix( $path ) {
		$segments = Path::segments( $path );

		if ( empty( $segments ) ) {
			return false;
		}

		return LocaleRepository::is_enabled_url_tag( LocaleRepository::normalize_url_tag( $segments[0] ) );
	}

	/**
	 * Resolve internal query-string links to canonical pretty URLs.
	 *
	 * This lets legacy stored links such as ?page_id=76 keep rendering as
	 * canonical public paths until the underlying content is resaved.
	 *
	 * @param string $url URL input.
	 * @return string
	 */
	private static function canonicalize_internal_query_url( $url ) {
		if ( function_exists( 'uaix_authority_canonicalize_internal_public_url' ) ) {
			$theme_canonical_url = uaix_authority_canonicalize_internal_public_url( $url );

			if ( is_string( $theme_canonical_url ) && '' !== $theme_canonical_url && $theme_canonical_url !== $url ) {
				return $theme_canonical_url;
			}
		}

		$query_string = (string) wp_parse_url( $url, PHP_URL_QUERY );

		if ( '' === $query_string ) {
			return $url;
		}

		$query = array();
		parse_str( $query_string, $query );

		if ( empty( $query ) || ! is_array( $query ) ) {
			return $url;
		}

		$canonical_target = '';

		self::$canonical_query_resolution_depth++;

		try {
			if ( isset( $query['page_id'] ) && absint( $query['page_id'] ) > 0 ) {
				$canonical_target = get_permalink( absint( $query['page_id'] ) );
				unset( $query['page_id'] );
			} elseif ( isset( $query['p'] ) && absint( $query['p'] ) > 0 ) {
				$canonical_target = get_permalink( absint( $query['p'] ) );
				unset( $query['p'] );
			} elseif ( isset( $query['attachment_id'] ) && absint( $query['attachment_id'] ) > 0 ) {
				$canonical_target = get_attachment_link( absint( $query['attachment_id'] ) );
				unset( $query['attachment_id'] );
			} elseif ( isset( $query['s'] ) && is_scalar( $query['s'] ) ) {
				$search_term = trim( (string) $query['s'] );

				if ( '' !== $search_term ) {
					$canonical_target = get_search_link( $search_term );
					unset( $query['s'] );
				}
			}
		} finally {
			self::$canonical_query_resolution_depth--;
		}