Skip to content

Commit

Permalink
feat: move indexible status to page method
Browse files Browse the repository at this point in the history
Closes #61
  • Loading branch information
fabianmichael committed Mar 22, 2024
1 parent b232337 commit 3da36dc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
48 changes: 46 additions & 2 deletions config/page-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
];
2 changes: 1 addition & 1 deletion config/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/PageMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 [
[
Expand Down
56 changes: 3 additions & 53 deletions src/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3da36dc

Please sign in to comment.