-
Notifications
You must be signed in to change notification settings - Fork 42
/
sitemap.php
356 lines (308 loc) · 13.3 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Cache;
use Grav\Common\Grav;
use Grav\Common\Data;
use Grav\Common\Language\Language;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
use Grav\Common\Uri;
use Grav\Common\Page\Pages;
use Grav\Common\Utils;
use Grav\Plugin\Sitemap\SitemapEntry;
use RocketTheme\Toolbox\Event\Event;
use Twig\TwigFunction;
class SitemapPlugin extends Plugin
{
/**
* @var array
*/
protected $sitemap = false;
protected $route_data = [];
protected $multilang_skiplang_prefix = null;
protected $multilang_include_fallbacks = false;
protected $multilang_enabled = true;
protected $datetime_format = null;
protected $include_change_freq = true;
protected $default_change_freq = null;
protected $include_priority = true;
protected $default_priority = null;
protected $ignores = null;
protected $ignore_external = true;
protected $ignore_protected = true;
protected $ignore_redirect = true;
protected $news_route = null;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100000], // TODO: Remove when plugin requires Grav >=1.7
['onPluginsInitialized', 0],
],
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
/**
* Composer autoload.
*is
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Enable sitemap only if url matches to the configuration.
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
/** @var Uri $uri */
$uri = $this->grav['uri'];
$route = $this->config()['route'];
$uri_route = $uri->route();
$news_page = false;
if ($this->config()['include_news_tags'] &&
$this->config()['standalone_sitemap_news'] &&
Utils::endsWith($uri->uri(), $this->config()['sitemap_news_path']) &&
in_array(dirname($uri->route()), $this->config()['news_enabled_paths'])) {
$this->news_route = dirname($uri->route());
}
if ($route === $uri->route() || !empty($this->news_route)) {
$this->enable([
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
}
/**
* Generate data for the sitemap.
*/
public function onPagesInitialized()
{
/** @var Cache $cache */
$cache = $this->grav['cache'];
/** @var Pages $pages */
$pages = $this->grav['pages'];
$cache_id = md5('sitemap-data-'.$pages->getPagesCacheId());
// $this->sitemap = $cache->fetch($cache_id);
if ($this->sitemap === false) {
$this->multilang_enabled = $this->config->get('plugins.sitemap.multilang_enabled');
/** @var Language $language */
$language = $this->grav['language'];
$default_lang = $language->getDefault() ?: 'en';
$active_lang = $language->getActive() ?? $default_lang;
$languages = $this->multilang_enabled && $language->enabled() ? $language->getLanguages() : [$default_lang];
$include_default_lang = $this->config->get('system.languages.include_default_lang');
$this->multilang_skiplang_prefix = $this->config->get('system.languages.include_default_lang') ? '' : $language->getDefault();
$this->multilang_include_fallbacks = $this->config->get('system.languages.pages_fallback_only') || !empty($this->config->get('system.languages.content_fallback'));
$this->datetime_format = $this->config->get('plugins.sitemap.short_date_format') ? 'Y-m-d' : 'Y-m-d\TH:i:sP';
$this->include_change_freq = $this->config->get('plugins.sitemap.include_changefreq');
$this->default_change_freq = $this->config->get('plugins.sitemap.changefreq');
$this->include_priority = $this->config->get('plugins.sitemap.include_priority');
$this->default_priority = $this->config->get('plugins.sitemap.priority');
$this->ignores = (array) $this->config->get('plugins.sitemap.ignores');
$this->ignore_external = $this->config->get('plugins.sitemap.ignore_external');
$this->ignore_protected = $this->config->get('plugins.sitemap.ignore_protected');
$this->ignore_redirect = $this->config->get('plugins.sitemap.ignore_redirect');
// Gather data for all languages
foreach ($languages as $lang) {
$language->init();
$language->setActive($lang);
$pages->reset();
$this->addRouteData($pages, $lang);
}
// Reset back to active language
if ($language->enabled() && $language->getActive() !== $active_lang) {
$language->init();
$language->setActive($active_lang);
$pages->reset();
}
// Build sitemap
foreach ($languages as $lang) {
foreach($this->route_data as $route => $route_data) {
if ($data = $route_data[$lang] ?? null) {
$entry = new SitemapEntry();
$entry->setData($data);
if ($language->enabled()) {
foreach ($route_data as $l => $l_data) {
$entry->addHreflangs(['hreflang' => $l, 'href' => $l_data['location']]);
if ($include_default_lang === false && $l == $default_lang) {
$entry->addHreflangs(['hreflang' => 'x-default', 'href' => $l_data['location']]);
}
}
}
$this->sitemap[$data['url']] = $entry;
}
}
}
$additions = (array) $this->config->get('plugins.sitemap.additions');
foreach ($additions as $addition) {
if (isset($addition['location'])) {
$location = Utils::url($addition['location'], true);
$entry = new SitemapEntry($location,$addition['lastmod'] ?? null,$addition['changefreq'] ?? null, $addition['priority'] ?? null);
$this->sitemap[$location] = $entry;
}
}
$cache->save($cache_id, $this->sitemap);
}
$this->grav->fireEvent('onSitemapProcessed', new Event(['sitemap' => &$this->sitemap]));
}
public function onPageInitialized($event)
{
$page = $event['page'] ?? null;
$route = $this->config->get('plugins.sitemap.route');
$uri = $this->grav['uri'];
$html_support = $this->config->get('plugins.sitemap.html_support', false);
$extension = $this->grav['uri']->extension() ?? ($html_support ? 'html': 'xml');
if (is_null($page) || $uri->route() === $route || !empty($this->news_route)) {
// set a dummy page
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/sitemap.md'));
$page->templateFormat($extension);
unset($this->grav['page']);
$this->grav['page'] = $page;
$twig = $this->grav['twig'];
if (!empty($this->news_route)) {
$header = $page->header();
$header->sitemap['news_route'] = $this->news_route;
$page->header($header);
$twig->template = "sitemap-news.$extension.twig";
} else {
$twig->template = "sitemap.$extension.twig";
}
}
}
// Access plugin events in this class
public function onTwigInitialized()
{
$this->grav['twig']->twig()->addFunction(
new TwigFunction('sort_sitemap_entries_by_language', [$this, 'sortSitemapEntriesByLanguage'])
);
$this->grav['twig']->twig()->addFunction(
new TwigFunction('timestamp_within_days', [$this, 'timestampWithinDays'])
);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Set needed variables to display the sitemap.
*/
public function onTwigSiteVariables()
{
$twig = $this->grav['twig'];
$twig->twig_vars['sitemap'] = $this->sitemap;
}
/**
* Extend page blueprints with feed configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
{
static $inEvent = false;
/** @var Data\Blueprint $blueprint */
$blueprint = $event['blueprint'];
if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
if (!in_array($blueprint->getFilename(), array_keys($this->grav['pages']->modularTypes()))) {
$inEvent = true;
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('sitemap');
$blueprint->extend($extends, true);
$inEvent = false;
}
}
}
public function sortSitemapEntriesByLanguage()
{
$entries = [];
foreach ((array) $this->sitemap as $route => $entry) {
$lang = $entry->getLang();
unset($entry->hreflangs);
unset($entry->image);
if ($lang === null) {
$lang = $this->grav['language']->getDefault() ?: 'en';
}
$entries[$lang][$route] = $entry;
}
return $entries;
}
public function timestampWithinDays(int $timestamp, int $days): bool
{
$now = time();
$days_ago = $now - ($days * 24 * 60 * 60);
return $timestamp >= $days_ago;
}
protected function addRouteData($pages, $lang)
{
$routes = array_unique($pages->routes());
ksort($routes);
foreach ($routes as $route => $path) {
/** @var PageInterface $page */
$page = $pages->get($path);
$rawroute = $page->rawRoute();
$header = $page->header();
$external_url = $this->ignore_external ? isset($header->external_url) : false;
$protected_page = $this->ignore_protected ? isset($header->access) : false;
$redirect_page = $this->ignore_redirect ? isset($header->redirect) : false;
$config_ignored = preg_match(sprintf("@^(%s)$@i", implode('|', $this->ignores)), $page->route());
$page_ignored = $protected_page || $external_url || $redirect_page || (isset($header->sitemap['ignore']) ? $header->sitemap['ignore'] : false);
if ($page->routable() && $page->published() && !$config_ignored && !$page_ignored) {
$page_languages = array_keys($page->translatedLanguages());
$include_lang = $this->multilang_skiplang_prefix !== $lang;
$location = $page->canonical($include_lang);
$url = $page->url(false, $include_lang);
$lastmod = !empty($header->sitemap['lastmod']) ? strtotime($header->sitemap['lastmod']) : $page->modified();
$lang_route = [
'title' => $page->title(),
'url' => $url,
'route' => $route,
'lang' => $lang,
'translated' => in_array($lang, $page_languages),
'location' => $location,
'lastmod' => date($this->datetime_format, $lastmod),
'longdate' => date('Y-m-d\TH:i:sP', $page->date()),
'shortdate' => date('Y-m-d', $page->date()),
'timestamp' => $page->date(),
'rawroute' => $page->rawRoute(),
];
if ($this->include_change_freq) {
$lang_route['changefreq'] = $header->sitemap['changefreq'] ?? $this->default_change_freq;
}
if ($this->include_priority) {
$lang_route['priority'] = $header->sitemap['priority'] ?? $this->default_priority;
}
// optional add image
$images = $header->sitemap['images'] ?? $this->config->get('plugins.sitemap.images') ?? [];
if (isset($images)) {
foreach ($images as $image => $values) {
if (isset($values['loc'])) {
$images[$image]['loc'] = $page->media()[$values['loc']]->url();
} else {
unset($images[$image]);
}
}
$lang_route['images'] = $images;
}
$this->route_data[$rawroute][$lang] = $lang_route;
}
}
}
}