diff --git a/config/page-methods.php b/config/page-methods.php index 0967bc7..97564c2 100644 --- a/config/page-methods.php +++ b/config/page-methods.php @@ -8,7 +8,51 @@ return PageMeta::of($this, $languageCode); }, - 'isIndexible' => function(){ - return Sitemap::isPageIndexible( $this ); + 'isIndexible' => function (): bool { + // pages have to pass a set of for being indexible. If any test + // fails, the page is excluded from index + + $templatesExclude = option('fabianmichael.meta.sitemap.templates.exclude', []); + $templatesExcludeRegex = '!^(?:' . implode('|', $templatesExclude) . ')$!i'; + + $templatesIncludeUnlisted = option('fabianmichael.meta.sitemap.templates.includeUnlisted', []); + $templatesIncludeUnlistedRegex = '!^(?:' . implode('|', $templatesIncludeUnlisted) . ')$!i'; + + $pagesExclude = option('fabianmichael.meta.sitemap.pages.exclude', []); + $pagesExcludeRegex = '!^(?:' . implode('|', $pagesExclude) . ')$!i'; + + $pagesIncludeUnlisted = option('fabianmichael.meta.sitemap.pages.includeUnlisted', []); + $pagesIncludeUnlistedRegex = '!^(?:' . implode('|', $pagesIncludeUnlisted) . ')$!i'; + + if ($this->isErrorPage()) { + // error page is always excluded from sitemap + return false; + } + + if ($this->isDraft()) { + // draft pages are never indexible + return false; + } + + if (! $this->isHomePage() && $this->status() === 'unlisted') { + if (preg_match($templatesIncludeUnlistedRegex, $this->intendedTemplate()->name()) !== 1 + && preg_match($pagesIncludeUnlistedRegex, $this->id()) !== 1) { + // unlisted pages are only indexible if exceptions are + // defined for them based on page id or template + return false; + } + } + + if (preg_match($templatesExcludeRegex, $this->intendedTemplate()->name()) === 1) { + // page is in exclude-list of templates + return false; + } + + if (preg_match($pagesExcludeRegex, $this->id()) === 1) { + // page is in exclude-list of page IDs + return false; + } + + return true; }, ]; diff --git a/config/views.php b/config/views.php index e6c3341..85e21d0 100644 --- a/config/views.php +++ b/config/views.php @@ -35,7 +35,7 @@ 'title' => $page->title()->value(), 'meta_title' => $meta->get('meta_title')->value(), 'icon' => $page->blueprint()->icon(), - 'is_indexible' => Sitemap::isPageIndexible($page), + 'is_indexible' => $page->isIndexible(), 'status' => $page->status(), 'id' => $page->id(), 'url' => $page->url(), diff --git a/src/PageMeta.php b/src/PageMeta.php index af0ce94..3a463f4 100644 --- a/src/PageMeta.php +++ b/src/PageMeta.php @@ -256,7 +256,7 @@ public function robots(?string $name = null): bool|string } // if page is not in sitemap, it will also not be indexible - if ($name === 'index' && ! Sitemap::isPageIndexible($this->page)) { + if ($name === 'index' && ! $this->page->isIndexible()) { return false; } @@ -485,7 +485,7 @@ public function panelTitlePlaceholder(): string public function reports(): array { - $isIndexible = Sitemap::isPageIndexible($this->page) && $this->robots('index'); + $isIndexible = $this->page->isIndexible() && $this->robots('index'); return [ [ diff --git a/src/Sitemap.php b/src/Sitemap.php index 66748cf..6b67847 100644 --- a/src/Sitemap.php +++ b/src/Sitemap.php @@ -59,11 +59,11 @@ public function generate(): string protected function urlsForPage( Page $page, DOMDocument $doc, - DOMElement $root): void - { + DOMElement $root + ): void { $meta = $page->meta(); - if (static::isPageIndexible($page) === false) { + if ($page->isIndexible() === false) { // Exclude page, if explicitly excluded in page settings // for global settings return; @@ -111,54 +111,4 @@ protected function urlsForPage( $root->appendChild($url); } } - - public static function isPageIndexible(Page $page): bool - { - // pages have to pass a set of for being indexible. If any test - // fails, the page is excluded from index - - $templatesExclude = option('fabianmichael.meta.sitemap.templates.exclude', []); - $templatesExcludeRegex = '!^(?:' . implode('|', $templatesExclude) . ')$!i'; - - $templatesIncludeUnlisted = option('fabianmichael.meta.sitemap.templates.includeUnlisted', []); - $templatesIncludeUnlistedRegex = '!^(?:' . implode('|', $templatesIncludeUnlisted) . ')$!i'; - - $pagesExclude = option('fabianmichael.meta.sitemap.pages.exclude', []); - $pagesExcludeRegex = '!^(?:' . implode('|', $pagesExclude) . ')$!i'; - - $pagesIncludeUnlisted = option('fabianmichael.meta.sitemap.pages.includeUnlisted', []); - $pagesIncludeUnlistedRegex = '!^(?:' . implode('|', $pagesIncludeUnlisted) . ')$!i'; - - if ($page->isErrorPage()) { - // error page is always excluded from sitemap - return false; - } - - - if (! $page->isHomePage() && $page->status() === 'unlisted') { - if (preg_match($templatesIncludeUnlistedRegex, $page->intendedTemplate()->name()) !== 1 - && preg_match($pagesIncludeUnlistedRegex, $page->id()) !== 1) { - // unlisted pages are only indexible, if exceptions are - // defined for them based on page id or template - return false; - } - } - - if (preg_match($templatesExcludeRegex, $page->intendedTemplate()->name()) === 1) { - // page is in exclude-list of templates - return false; - } - - if (preg_match($pagesExcludeRegex, $page->id()) === 1) { - // page is in exclude-list of page IDs - return false; - } - - if (! is_null($page->parent())) { - // test indexability of parent pages as well - return static::isPageIndexible($page->parent()); - } - - return true; - } }