123 lines
4.8 KiB
PHP
123 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca;
|
|
|
|
final class Seo
|
|
{
|
|
public static function currentPath(): string
|
|
{
|
|
global $APPLICATION;
|
|
$path = (string)$APPLICATION->GetCurPage(false);
|
|
if (substr($path, -9) === "index.php") {
|
|
$path = substr($path, 0, -9);
|
|
}
|
|
|
|
return $path === "" ? "/" : $path;
|
|
}
|
|
|
|
public static function description(string $text, int $limit = 155): string
|
|
{
|
|
$text = trim((string)preg_replace('/\s+/u', " ", $text));
|
|
if (mb_strlen($text) <= $limit) {
|
|
return $text;
|
|
}
|
|
$cut = mb_substr($text, 0, $limit + 1);
|
|
$sentence = max((int)mb_strrpos($cut, ". "), (int)mb_strrpos($cut, "! "), (int)mb_strrpos($cut, "? "));
|
|
if ($sentence >= 60) {
|
|
return mb_substr($cut, 0, $sentence + 1);
|
|
}
|
|
$space = (int)mb_strrpos($cut, " ");
|
|
|
|
return rtrim(mb_substr($cut, 0, $space > 0 ? $space : $limit), " ,;:-");
|
|
}
|
|
|
|
public static function fitTitle(array $candidates, int $limit = 60): string
|
|
{
|
|
foreach ($candidates as $candidate) {
|
|
if (mb_strlen($candidate) <= $limit) {
|
|
return $candidate;
|
|
}
|
|
}
|
|
|
|
return (string)end($candidates);
|
|
}
|
|
|
|
public static function fitDescription(string $base, array $tails, int $min = 120, int $max = 160): string
|
|
{
|
|
$base = trim($base);
|
|
if (mb_strlen($base) >= $min) {
|
|
return $base;
|
|
}
|
|
foreach ($tails as $tail) {
|
|
$candidate = $base . " " . $tail;
|
|
if (mb_strlen($candidate) >= $min && mb_strlen($candidate) <= $max) {
|
|
return $candidate;
|
|
}
|
|
}
|
|
|
|
return $base;
|
|
}
|
|
|
|
public static function openGraph(string $canonical): string
|
|
{
|
|
global $APPLICATION;
|
|
if ((string)$APPLICATION->GetPageProperty("vino_alternates") === "N") {
|
|
return "";
|
|
}
|
|
$tag = static fn(string $attribute, string $name, string $value): string => ' <meta ' . $attribute . '="' . $name . '" content="' . htmlspecialcharsbx($value) . '">' . "\n";
|
|
$languages = Config::languages();
|
|
$html = $tag("property", "og:site_name", "Vinoteca") . $tag("property", "og:locale", (string)$languages[LANGUAGE_ID]["locale"]);
|
|
foreach ($languages as $code => $language) {
|
|
if ($code !== LANGUAGE_ID) {
|
|
$html .= $tag("property", "og:locale:alternate", (string)$language["locale"]);
|
|
}
|
|
}
|
|
$image = (string)$APPLICATION->GetProperty("og_image");
|
|
|
|
return $html
|
|
. $tag("property", "og:type", (string)$APPLICATION->GetProperty("og_type") ?: "website")
|
|
. $tag("property", "og:url", Config::host() . self::canonicalPath($canonical))
|
|
. $tag("property", "og:title", (string)$APPLICATION->GetTitle(false))
|
|
. $tag("property", "og:description", (string)$APPLICATION->GetProperty("description"))
|
|
. $tag("property", "og:image", $image !== "" ? $image : Config::host() . SITE_TEMPLATE_PATH . "/assets/media/og-home.jpg")
|
|
. $tag("name", "twitter:card", "summary_large_image");
|
|
}
|
|
|
|
public static function canonicalPath(string $default): string
|
|
{
|
|
global $APPLICATION;
|
|
$path = (string)$APPLICATION->GetPageProperty("vino_canonical");
|
|
|
|
return $path !== "" ? $path : $default;
|
|
}
|
|
|
|
public static function headLinks(string $default, string $host, array $languages, string $defaultLanguage): string
|
|
{
|
|
if (defined("ERROR_404") && ERROR_404 === "Y") {
|
|
return "";
|
|
}
|
|
$path = self::canonicalPath($default);
|
|
$relative = $path;
|
|
if (SITE_DIR !== "/" && strpos($path, SITE_DIR) === 0) {
|
|
$relative = "/" . substr($path, strlen(SITE_DIR));
|
|
}
|
|
$ready = array_values(array_filter(array_map("trim", explode(",", (string)$GLOBALS["APPLICATION"]->GetPageProperty("vino_langs")))));
|
|
if ($ready) {
|
|
$languages = array_intersect_key($languages, array_flip($ready));
|
|
$defaultLanguage = isset($languages[$defaultLanguage]) ? $defaultLanguage : (string)array_key_first($languages);
|
|
}
|
|
$html = '<link rel="canonical" href="' . htmlspecialcharsbx($host . $path) . '">' . "\n";
|
|
if ((string)$GLOBALS["APPLICATION"]->GetPageProperty("vino_alternates") === "N") {
|
|
return $html;
|
|
}
|
|
foreach ($languages as $language => $settings) {
|
|
$html .= ' <link rel="alternate" hreflang="' . $language . '" href="' . htmlspecialcharsbx($host . rtrim($settings["dir"], "/") . $relative) . '">' . "\n";
|
|
}
|
|
$html .= ' <link rel="alternate" hreflang="x-default" href="' . htmlspecialcharsbx($host . rtrim($languages[$defaultLanguage]["dir"], "/") . $relative) . '">' . "\n";
|
|
|
|
return $html;
|
|
}
|
|
}
|