get_settings('site', null); $stored = (string) ($settings['canonical_scheme'] ?? ''); } /** * Filter the scheme ThinkRank publishes its own URLs with. * * @since 2.7.0 * * @param string $mode One of Url_Scheme::MODES. */ $mode = (string) apply_filters('thinkrank_canonical_scheme', $stored); self::$mode = in_array($mode, self::MODES, true) ? $mode : self::AUTOMATIC; } return self::$mode; } /** * Apply the site's scheme preference to one URL. * * @since 2.7.0 * @param string $url Absolute URL, or anything else (returned untouched). * @return string */ public static function apply(string $url): string { $mode = self::preference(); if (self::AUTOMATIC === $mode || '' === $url) { return $url; } $scheme = wp_parse_url($url, PHP_URL_SCHEME); // No scheme means relative or protocol-relative: not ours to decide. if (!is_string($scheme) || '' === $scheme) { return $url; } $scheme = strtolower($scheme); if ('http' !== $scheme && 'https' !== $scheme) { return $url; } if ($scheme === $mode || !self::is_own_url($url)) { return $url; } return set_url_scheme($url, $mode); } /** * Apply the preference to every string in a nested structure. * * The schema graph is the reason this exists: its `@id` and `url` values * come from a dozen different producers, some deriving from WordPress and * some from stored settings, and normalizing at the point of serialization * is what stops one graph carrying both schemes at once. * * @since 2.7.0 * @param mixed $value Array, string, or anything else. * @return mixed Same shape, schemes normalized. */ public static function apply_deep($value) { if (self::AUTOMATIC === self::preference()) { return $value; } if (is_string($value)) { return self::apply($value); } if (!is_array($value)) { return $value; } foreach ($value as $key => $item) { $value[$key] = self::apply_deep($item); } return $value; } /** * Whether a URL belongs to this site. * * Host comparison only. A subdomain is a different site as far as a * canonical is concerned, and a port or path difference is still us. * * @since 2.7.0 * @param string $url Absolute URL. * @return bool */ private static function is_own_url(string $url): bool { $host = wp_parse_url($url, PHP_URL_HOST); if (!is_string($host) || '' === $host) { return false; } return strtolower($host) === self::host(); } /** * This site's host, lowercased and memoised. * * @since 2.7.0 * @return string */ private static function host(): string { if (null === self::$host) { $host = wp_parse_url(home_url('/'), PHP_URL_HOST); self::$host = is_string($host) ? strtolower($host) : ''; } return self::$host; } /** * Forget the memoised preference and host. * * @since 2.7.0 * @return void */ public static function reset(): void { self::$mode = null; self::$host = null; } }