Skip to content

Commit

Permalink
Updated SitemapXMLController to apply filters to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-cyber-duck authored and ccordina committed Jan 21, 2022
1 parent f0545d8 commit 0117991
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Controller/SitemapXMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\ErrorPage\ErrorPage;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Subsites\Model\Subsite;

/**
* SitemapXML_Controller
Expand All @@ -32,7 +34,7 @@ public function init()
{
parent::init();

$this->response->addHeader("Content-Type", "application/xml");
$this->response->addHeader("Content-Type", "application/xml");
}

/**
Expand Down Expand Up @@ -87,18 +89,32 @@ public function EncodedValue($value)
public function getSitemapPages()
{
$pages = ArrayList::create();

$objects = (array) Config::inst()->get(SitemapGenerator::class, 'objects');

if(!empty($objects)) {
foreach($objects as $name => $values) {
foreach($name::get() as $page) {
if(!$page->SitemapHide) {
foreach($name::get()->filter($this->getSitemapFilters()) as $page) {
$pages->push($page);
}
}
}
}
return $pages;
}
}

/**
* @return array
*/
private function getSitemapFilters()
{
$filters = [
'ClassName:not' => ErrorPage::class,
'Robots:not' => ['noindex,nofollow', 'noindex,follow'],
'SitemapHide' => 0
];
if(class_exists('SubSite')) {
$filters['SubsiteID'] = Subsite::currentSubSiteID();
}
return $filters;
}
}

0 comments on commit 0117991

Please sign in to comment.