-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
128 lines (102 loc) · 4.34 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php declare(strict_types=1);
use Kirby\Cms\App as Kirby;
use Kirby\Cms\Language;
use Kirby\Data\Yaml;
use Kirby\Filesystem\F;
use Kirby\Http\Response;
use Kirby\Http\Url;
use Kirby\Toolkit\Xml;
use PresProg\KirbyMeta\PageMeta;
Kirby::plugin('presprog/seo', [
'options' => [
'meta' => [
'appendSiteTitle' => true,
'titleSeparator' => ' | ',
],
'sitemap' => [
'templatesInclude' => [],
'pagesInclude' => [],
'pagesExclude' => [],
],
],
'blueprints' => [
'seo/tabs/site' => __DIR__ . '/blueprints/tabs/site.yml',
'seo/tabs/page' => __DIR__ . '/blueprints/tabs/page.yml',
'seo/fields/robotsGroup' => __DIR__ . '/blueprints/fields/robotsGroup.yml',
'seo/fields/robotsToggle' => __DIR__ . '/blueprints/fields/robotsToggle.yml',
],
'pageMethods' => [
'meta' => function () {
return PageMeta::for($this);
},
],
'snippets' => [
'seo/head' => __DIR__ . '/snippets/seo/head.php',
],
'sections' => [
'sharePreview' => __DIR__ .'/sections/sharePreview.php',
],
'routes' => [
[
'pattern' => 'robots.txt',
'method' => 'ALL',
'action' => function () {
$robots = 'User-agent: *' . PHP_EOL;
$robots .= 'Allow: /' . PHP_EOL;
if (kirby()->multilang()) {
/** @var Language $language */
foreach (kirby()->languages() as $language) {
$robots .= Url::makeAbsolute('/sitemap.xml', $language->url()) . PHP_EOL;
}
} else {
$robots .= 'Sitemap: ' . url('sitemap.xml');
}
return kirby()
->response()
->type('text')
->body($robots);
},
], [
'pattern' => 'sitemap.xml',
'language' => '*',
'action' => function () {
$templatesIncludeList = option('presprog.seo.sitemap.templatesInclude', []);
$pagesIncludeList = option('presprog.seo.sitemap.pagesInclude', []);
$pagesExcludeList = option('presprog.seo.sitemap.pagesExclude', []);
$errorPage = kirby()->site()->errorPageId();
$pagesExcludeList = array_values([... $pagesExcludeList, $errorPage]);
$excludeListPattern = '!^(?:' . implode('|', $pagesExcludeList) . ')$!i';
$cache = kirby()->cache('pages');
$cacheId = (kirby()->multilang()) ? 'sitemap.' . kirby()->language()->code() . '.xml' : 'sitemap.xml';
if (!$sitemap = $cache->get($cacheId, [])) {
$sitemap[] = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap[] = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach (site()->index() as $item) {
if (in_array($item->intendedTemplate()->name(), $templatesIncludeList) === false && in_array($item->id(), $pagesIncludeList) === false) {
continue;
}
if (preg_match($excludeListPattern, $item->id())) {
continue;
}
if (method_exists($item, 'excludeFromSitemap') && $item->excludeFromSitemap()) {
continue;
}
$meta = $item->meta();
$sitemap[] = '<url>';
$sitemap[] = ' <loc>' . Xml::encode($item->url()) . '</loc>';
$sitemap[] = ' <priority>' . number_format($meta->priority(), 1, '.', '') . '</priority>';
$sitemap[] = '</url>';
}
$sitemap[] = '</urlset>';
$sitemap = implode(PHP_EOL, $sitemap);
$cache->set($cacheId, $sitemap);
}
return new Response($sitemap, 'application/xml');
},
],
],
'translations' => [
'en' => Yaml::decode(F::read(__DIR__ . '/translations/en.yml')),
'de' => Yaml::decode(F::read(__DIR__ . '/translations/de.yml')),
],
]);