diff --git a/e107_plugins/gsitemap/e_url.php b/e107_plugins/gsitemap/e_url.php index 062a83ab9..830632b26 100644 --- a/e107_plugins/gsitemap/e_url.php +++ b/e107_plugins/gsitemap/e_url.php @@ -42,6 +42,14 @@ class gsitemap_url // plugin-folder + '_url' ); + $config['sitemaps'] = array( + 'alias' => 'sitemaps', + 'regex' => '^{alias}\.xml$', // matched against url, and if true, redirected to 'redirect' below. + 'sef' => '{alias}.xml', // used by e107::url(); to create a url from the db table. + 'redirect' => '{e_BASE}gsitemap.php?index=1', // file-path of what to load when the regex returns true. + + ); + $addons = e107::getAddonConfig('e_gsitemap', 'gsitemap'); foreach($addons as $plug => $item) diff --git a/gsitemap.php b/gsitemap.php index cd194f788..6e6bf13cb 100644 --- a/gsitemap.php +++ b/gsitemap.php @@ -47,6 +47,10 @@ class gsitemap_xml } } + elseif(!empty($_GET['index'])) // Sitemap index. + { + $this->renderSitemapIndex(); + } else // From Gsitemap Database Table. { $this->renderXML(); @@ -135,6 +139,62 @@ class gsitemap_xml } + + /** + * Generates a Sitemap Index containing references to multiple sitemaps. + * + * @param array $sitemaps An array of sitemaps, where each item is an associative array with keys: + * - 'loc' (string): The full URL of the sitemap. + * - 'lastmod' (int|null): A timestamp representing the last modification date of the sitemap (optional). + * @return void + */ + function renderSitemapIndex() + { + header('Content-type: application/xml', true); + + // Begin the sitemap index + $xml = "\n"; + $xml .= "\n"; + + $obj = e107::getAddon('gsitemap', 'e_url'); + + $sitemaps = []; + + if($items = e107::callMethod($obj, 'config')) + { + foreach($items as $key => $val) + { + if($key == 'sitemaps' || $key == 'index') + { + continue; + } + + $sitemaps[] = ['loc' => e107::url('gsitemap',$key, null, ['mode'=>'full']), 'lastmod' => (time() - 86400)]; + } + } + + + foreach($sitemaps as $sitemap) + { + $xml .= "\n"; + $xml .= "\t{$sitemap['loc']}\n"; + + + if(!empty($sitemap['lastmod'])) // Optional: Include the last modified date, if available + { + $xml .= "\t" . date('c', (int) $sitemap['lastmod']) . "\n"; + } + + $xml .= "\n"; + } + + + $xml .= ""; + + // Output the XML + echo $xml; + } + }