' . "\n" . '' . "\n"; $count = 0; foreach ($pages as $page) { $alternates = ""; foreach ($languages as $code => $language) { $alternates .= ' ' . "\n"; } $alternates .= ' ' . "\n"; foreach ($languages as $language) { if (++$count > self::LIMIT) { break 2; } $xml .= " \n " . self::escape($host . self::url($language["dir"], $page["path"])) . "\n"; if ($page["lastmod"] !== "") { $xml .= " " . $page["lastmod"] . "\n"; } $xml .= $alternates . " \n"; } } return $xml . "\n"; } private static function pages(): array { $catalogId = Config::iblockId("catalog"); $pages = []; if ($catalogId <= 0 || !Loader::includeModule("iblock")) { return $pages; } $sectionCodes = []; foreach (Sections::all() as $code => $section) { $sectionCodes[$section["ID"]] = $code; } $now = new DateTime(); $wines = []; $latest = 0; $rows = ElementTable::getList([ "select" => ["CODE", "IBLOCK_SECTION_ID", "TIMESTAMP_X"], "filter" => [ "=IBLOCK_ID" => $catalogId, "=ACTIVE" => "Y", ["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]], ["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]], ], "order" => ["ID" => "ASC"], ]); while ($row = $rows->fetch()) { $section = $sectionCodes[(int)$row["IBLOCK_SECTION_ID"]] ?? ""; if ($section === "" || (string)$row["CODE"] === "") { continue; } $time = $row["TIMESTAMP_X"] instanceof DateTime ? $row["TIMESTAMP_X"]->getTimestamp() : 0; $latest = max($latest, $time); $wines[] = ["path" => "catalog/" . $section . "/" . rawurlencode((string)$row["CODE"]) . "/", "lastmod" => self::date($time)]; } $menu = MegaMenu::collect($catalogId, 1000); $lastmod = self::date($latest); $pages[] = ["path" => "", "lastmod" => $lastmod]; $pages[] = ["path" => "catalog/", "lastmod" => $lastmod]; foreach (["TYPES", "COUNTRIES", "BRANDS"] as $group) { foreach ($menu[$group] as $item) { $pages[] = ["path" => ltrim(self::relative((string)$item["URL"]), "/"), "lastmod" => $lastmod]; } } $pages[] = ["path" => "brands/", "lastmod" => $lastmod]; $pages[] = ["path" => "about/", "lastmod" => $lastmod]; $pages = array_merge($pages, self::section("news", "articles/", $now)); $pages = array_merge($pages, self::section("offers", "offers/", $now, static fn(int $id): bool => Offers::summary(Offers::discountIdsOf($id))["active"])); $pages = array_merge($pages, $wines); $pages[] = ["path" => "privacy/", "lastmod" => self::date(Consent::timestamp(Consent::POLICY_VERSION))]; $pages[] = ["path" => "consent/", "lastmod" => self::date(Consent::timestamp(Consent::VERSION))]; return $pages; } private static function section(string $iblock, string $folder, DateTime $now, ?callable $keep = null): array { $iblockId = Config::iblockId($iblock); if ($iblockId <= 0) { return []; } $items = []; $latest = 0; $rows = ElementTable::getList([ "select" => ["ID", "CODE", "TIMESTAMP_X"], "filter" => [ "=IBLOCK_ID" => $iblockId, "=ACTIVE" => "Y", ["LOGIC" => "OR", ["=ACTIVE_FROM" => false], ["<=ACTIVE_FROM" => $now]], ["LOGIC" => "OR", ["=ACTIVE_TO" => false], [">=ACTIVE_TO" => $now]], ], "order" => ["ACTIVE_FROM" => "DESC", "ID" => "DESC"], ]); while ($row = $rows->fetch()) { if ((string)$row["CODE"] === "" || ($keep !== null && !$keep((int)$row["ID"]))) { continue; } $time = $row["TIMESTAMP_X"] instanceof DateTime ? $row["TIMESTAMP_X"]->getTimestamp() : 0; $latest = max($latest, $time); $items[] = ["path" => $folder . rawurlencode((string)$row["CODE"]) . "/", "lastmod" => self::date($time)]; } return array_merge([["path" => $folder, "lastmod" => self::date($latest)]], $items); } private static function write(string $xml): void { $file = Application::getDocumentRoot() . self::FILE; $temporary = $file . ".tmp"; if (file_put_contents($temporary, $xml, LOCK_EX) !== false) { rename($temporary, $file); } } private static function relative(string $url): string { return SITE_DIR !== "/" && str_starts_with($url, SITE_DIR) ? "/" . substr($url, strlen(SITE_DIR)) : $url; } private static function url(string $directory, string $path): string { return rtrim($directory, "/") . "/" . $path; } private static function date(int $timestamp): string { return $timestamp > 0 ? date("Y-m-d", $timestamp) : ""; } private static function escape(string $value): string { return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, "UTF-8"); } }