Added a sitemap generator

This commit is contained in:
Belle Aerni 2023-01-13 23:46:17 -08:00
parent ca54a56eaa
commit eaca96fd6b
3 changed files with 59 additions and 3 deletions

View file

@ -8,7 +8,4 @@ RewriteRule ^Themes/[^/]+/Assets/.+$ - [L]
# Allow access to the robots.txt file
RewriteRule ^robots\.txt$ - [L]
# Allow access to the sitemap.xml file
RewriteRule ^sitemap\.xml$ - [L]
RewriteRule ^(.+)$ index.php [L,QSA]

View file

@ -0,0 +1,54 @@
<?php
use AntCMS\AntPlugin;
use AntCMS\AntPages;
use AntCMS\AntConfig;
use AntCMS\AntTools;
class SitemapPlugin extends AntPlugin
{
public function handlePluginRoute(array $route)
{
$protocol = AntConfig::currentConfig('forceHTTPS') ? 'https' : 'http';
$baseURL = AntConfig::currentConfig('baseURL');
$pages = AntPages::getPages();
if (extension_loaded('dom')) {
$doc = new DOMDocument();
$doc->formatOutput = true;
$root = $doc->createElement('urlset');
$root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$doc->appendChild($root);
$urls = array();
foreach ($pages as $key => $value) {
$urls[$key]['url'] = $value['functionalPagePath'];
$urls[$key]['lastchange'] = date('Y-m-d', filemtime($value['fullPagePath']));
}
foreach ($urls as $url) {
$element = $doc->createElement('url');
$loc = $doc->createElement('loc', $protocol . '://' . AntTools::repairURL($baseURL . $url['url']));
$element->appendChild($loc);
$lastmod = $doc->createElement('lastmod', $url['lastchange']);
$element->appendChild($lastmod);
$root->appendChild($element);
}
header('Content-Type: application/xml');
echo $doc->saveXML();
exit;
} else {
die("AntCMS is unable to generate a sitemap without having the DOM extension loadded in PHP.");
}
}
public function getName()
{
return 'Sitemap';
}
}

View file

@ -54,6 +54,11 @@ if ($segments[0] === 'themes' && $segments[2] === 'assets') {
exit;
}
if($segments[0] == 'sitemap.xml'){
$segments[0] = 'plugin';
$segments[1] = 'sitemap';
}
if ($segments[0] === 'plugin') {
$pluginName = $segments[1];
$pluginLoader = new AntPluginLoader();