-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsitemap.php
90 lines (69 loc) · 2.75 KB
/
sitemap.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
<?php
kirby()->set('snippet', 'sitemap.page', __DIR__ . '/snippets/page.php');
kirby()->set('snippet', 'sitemap.image', __DIR__ . '/snippets/image.php');
kirby()->set('route', [
'pattern' => 'sitemap.xsl',
'method' => 'GET',
'action' => function() {
$stylesheet = f::read(__DIR__ . DS . 'sitemap.xsl');
return new response($stylesheet, 'xsl');
}
]);
kirby()->set('route', [
'pattern' => 'sitemap.xml',
'method' => 'GET',
'action' => function() {
if (cache::exists('sitemap')) {
return new response(cache::get('sitemap'), 'xml');
}
$includeInvisibles = c::get('sitemap.include.invisible', false);
$ignoredPages = c::get('sitemap.ignored.pages', []);
$ignoredTemplates = c::get('sitemap.ignored.templates', []);
$languages = site()->languages();
$pages = site()->index();
if (! $includeInvisibles) {
$pages = $pages->visible();
}
$pages = $pages
->not($ignoredPages)
->filterBy('intendedTemplate', 'not in', $ignoredTemplates)
->map('sitemapProcessAttributes');
$transform = c::get('sitemap.transform', null);
if (is_callable($transform)) {
$pages = $transform($pages);
if (! is_a($pages, 'Collection')) throw new Exception($pages . ' is not a Collection.');
} elseif (! is_null($transform)) {
throw new Exception($transform . ' is not callable.');
}
$sitemap = tpl::load(__DIR__ . DS . 'sitemap.html.php', compact('languages', 'pages'));
cache::set('sitemap', $sitemap);
return new response($sitemap, 'xml');
}
]);
function sitemapPriority($page) {
return $page->isHomePage() ? 1 : number_format(1.6 / ($page->depth() + 1), 1);
}
function sitemapFrequency($page) {
$priority = sitemapPriority($page);
switch (true) {
case $priority === 1 : $frequency = 'daily'; break;
case $priority >= 0.5 : $frequency = 'weekly'; break;
default : $frequency = 'monthly';
}
return $frequency;
}
function sitemapProcessAttributes($page) {
$frequency = c::get('sitemap.frequency', false);
$priority = c::get('sitemap.priority', false);
if ($frequency) {
$frequency = is_bool($frequency) ? 'sitemapFrequency' : $frequency;
if (! is_callable($frequency)) throw new Exception($frequency . ' is not callable.');
$page->frequency = $frequency($page);
}
if ($priority) {
$priority = is_bool($priority) ? 'sitemapPriority' : $priority;
if (! is_callable($priority)) throw new Exception($priority . ' is not callable.');
$page->priority = $priority($page);
}
return $page;
}