Skip to content

Commit

Permalink
Add support for alternate locales in sitemap (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
anik1ng authored Jul 27, 2021
1 parent fbeb109 commit 3333718
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions models/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ public function generateSitemap()
* Single item
*/
if (isset($itemInfo['url'])) {
$this->addItemToSet($item, $itemInfo['url'], array_get($itemInfo, 'mtime'));
$this->addItemToSet(
$item,
$itemInfo['url'],
array_get($itemInfo, 'mtime'),
$itemInfo['alternate_locale_urls'] ?? null
);
}

/*
Expand All @@ -128,7 +133,12 @@ public function generateSitemap()
{
foreach ($items as $item) {
if (isset($item['url'])) {
$this->addItemToSet($parentItem, $item['url'], array_get($item, 'mtime'));
$this->addItemToSet(
$parentItem,
$item['url'],
array_get($item, 'mtime'),
$itemInfo['alternate_locale_urls'] ?? null
);
}

if (isset($item['items'])) {
Expand Down Expand Up @@ -173,13 +183,14 @@ protected function makeUrlSet()
$xml = $this->makeXmlObject();
$urlSet = $xml->createElement('urlset');
$urlSet->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$urlSet->setAttribute('xmlns:xhtml', 'https://www.w3.org/1999/xhtml');
$urlSet->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$urlSet->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');

return $this->urlSet = $urlSet;
}

protected function addItemToSet($item, $url, $mtime = null)
protected function addItemToSet($item, $url, $mtime = null, $alternateLocaleUrls = null)
{
if ($mtime instanceof \DateTime) {
$mtime = $mtime->getTimestamp();
Expand All @@ -194,7 +205,8 @@ protected function addItemToSet($item, $url, $mtime = null)
$url,
$mtime,
$item->changefreq,
$item->priority
$item->priority,
$alternateLocaleUrls
);

if ($urlElement) {
Expand All @@ -204,7 +216,7 @@ protected function addItemToSet($item, $url, $mtime = null)
return $urlSet;
}

protected function makeUrlElement($xml, $pageUrl, $lastModified, $frequency, $priority)
protected function makeUrlElement($xml, $pageUrl, $lastModified, $frequency, $priority, $alternateLocaleUrls = null)
{
if ($this->urlCount >= self::MAX_URLS) {
return false;
Expand All @@ -218,6 +230,16 @@ protected function makeUrlElement($xml, $pageUrl, $lastModified, $frequency, $pr
$url->appendChild($xml->createElement('changefreq', $frequency));
$url->appendChild($xml->createElement('priority', $priority));

if ($alternateLocaleUrls) {
foreach ($alternateLocaleUrls as $locale => $localeUrl) {
$alternateUrl = $xml->createElement('xhtml:link');
$alternateUrl->setAttribute('rel', 'alternate');
$alternateUrl->setAttribute('hreflang', $locale);
$alternateUrl->setAttribute('href', $localeUrl);
$url->appendChild($alternateUrl);
}
}

return $url;
}
}

0 comments on commit 3333718

Please sign in to comment.