Skip to content

Commit

Permalink
fix robots blueprint not generating for defaults on pages (#89), redi…
Browse files Browse the repository at this point in the history
…rect /sitemap (#84), fix disabling robots (#87)
  • Loading branch information
tobimori committed Mar 21, 2024
1 parent 9e40fce commit 6fd227e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
18 changes: 10 additions & 8 deletions blueprints/fields/robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
use tobimori\Seo\Meta;

return function (App $kirby) {
if (
!$kirby->option('tobimori.seo.robots.pageSettings', $kirby->option('tobimori.seo.robots.active', false))
|| !($page = Meta::currentPage())
) {
if (!$kirby->option('tobimori.seo.robots.active') || !$kirby->option('tobimori.seo.robots.pageSettings')) {
return [
'type' => 'hidden'
];
Expand All @@ -23,19 +20,24 @@
]
];

$page = Meta::currentPage();
foreach ($kirby->option('tobimori.seo.robots.types') as $robots) {
$upper = Str::ucfirst($robots);
$default = $page->metadata()->get("robots{$upper}", ['fields'])->toBool() ? t('yes') : t('no');

$fields["robots{$upper}"] = [
'label' => 'robots-' . $robots,
'label' => "robots-{$robots}",
'type' => 'toggles',
'help' => 'robots-' . $robots . '-help',
'help' => "robots-{$robots}-help",
'width' => '1/2',
'default' => 'default',
'required' => true,
'options' => [
'default' => A::join([t('default-select'), $default], ' '),
'default' => $page ?
A::join([
t('default-select'),
$page->metadata()->get("robots{$upper}", ['fields'])->toBool() ? t('yes') : t('no')
], ' ')
: t('default-select'),
'true' => t('yes'),
'false' => t('no'),
]
Expand Down
7 changes: 3 additions & 4 deletions blueprints/fields/site-robots.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use Kirby\Cms\App;
use Kirby\Toolkit\Str;

return function (App $kirby) {
if (!$kirby->option('tobimori.seo.robots.pageSettings', $kirby->option('tobimori.seo.robots.active', false))) {
if (!$kirby->option('tobimori.seo.robots.active') || !$kirby->option('tobimori.seo.robots.pageSettings')) {
return [
'type' => 'hidden'
];
Expand All @@ -25,9 +24,9 @@
}

$fields["robots{$robots}"] = [
'label' => 'robots-' . Str::lower($robots),
'label' => "robots-{$robots}",
'type' => 'toggles',
'help' => 'robots-' . Str::lower($robots) . '-help',
'help' => "robots-{$robots}-help",
'width' => '1/2',
'default' => 'default',
'required' => true,
Expand Down
11 changes: 11 additions & 0 deletions config/hooks.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<?php

use Kirby\Cms\Page;
use Kirby\Toolkit\Str;

return [
'page.update:after' => function (Page $newPage, Page $oldPage) {
foreach ($newPage->kirby()->option('tobimori.seo.robots.types') as $robots) {
$upper = Str::ucfirst($robots);
if ($newPage->content()->get("robots{$upper}")->value() === "") {
$newPage = $newPage->update([
"robots{$upper}" => 'default'
]);
}
}
},
'page.render:before' => function (string $contentType, array $data, Page $page) {
if (option('tobimori.seo.generateSchema')) {
$page->schema('WebSite')
Expand Down
1 change: 1 addition & 0 deletions config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
],
'sitemap' => [
'active' => true,
'redirect' => true, // redirect /sitemap to /sitemap.xml
'lang' => 'en',
'generator' => require __DIR__ . '/options/sitemap.php',
'changefreq' => 'weekly',
Expand Down
10 changes: 10 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
$this->next();
}
],
[
'pattern' => 'sitemap',
'action' => function () {
if (!option('tobimori.seo.sitemap.redirect', true) || !option('tobimori.seo.sitemap.active', true)) {
$this->next();
}

go('/sitemap.xml');
}
],
[
'pattern' => 'sitemap.xsl',
'action' => function () {
Expand Down

0 comments on commit 6fd227e

Please sign in to comment.