-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyandex-turbo.php
212 lines (177 loc) · 6.3 KB
/
yandex-turbo.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
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
use Grav\Common\Grav;
use Grav\Common\Page\Pages;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Collection;
use Grav\Common\Uri;
use RocketTheme\Toolbox\Event\Event;
class YandexTurboPlugin extends Plugin
{
/** @var array */
protected $items = [];
/**
* Subscribe to required events
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
/**
* Enable turbo RSS only if url matches to the configuration.
*
* @return void
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
/** @var Uri $uri */
$uri = $this->grav['uri'];
$route = $this->config()['route'];
if ($route && $route == $uri->path()) {
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
}
/**
* Add current directory to twig lookup paths.
*
* @return void
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Generate data for the turbo pages.
*
* @return void
*/
public function onPagesInitialized()
{
if ($this->config()['enable_cache']) {
$cache = Grav::instance()['cache'];
$cache_id = md5('yandex_turbo_plugin');
if ($data = $cache->fetch($cache_id)) {
$this->items = $data;
}
}
if (empty($this->items)) {
$content = (array) $this->config()['content'];
if (empty($content)) {
/** @var Pages $pages */
$pages = $this->grav['pages'];
$collection = $pages->all();
} else {
$collection = new Collection();
$page = Grav::instance()['page'];
foreach ($content as $route) {
if ($route === '/') {
$collection->append($page->evaluate(['@page.self' => $route, '@page' => $route]));
} else {
$collection->append($page->evaluate(['@page.self' => $route, '@page.children' => $route]));
}
}
}
$by = $this->config()['sort_by'] ?? 'date';
$dir = $this->config()['sort_dir'] ?? 'desc';
$options = [$by, $dir];
if ($by === 'manual' && ! empty($content)) {
$options = array_merge($options, [$content, SORT_NUMERIC]);
}
$collection = $collection->published()->routable()->order(...$options)->slice(0, 1000);
foreach ($collection as $page) {
$header = $page->header();
if (isset($header->external_url) || isset($header->access))
continue;
$entry = new \stdClass();
$entry->title = $page->title();
$entry->link = $page->canonical();
$date = $by == 'modified' ? $page->modified() : $page->date();
if ($by == 'publish_date' && ! empty($page->publishDate()))
$date = $page->publishDate();
$entry->date = date(DATE_RFC822, $date);
if (! empty($header->metadata['author'])) {
$entry->author = $header->metadata['author'];
} elseif (! empty($header->author['name'])) {
$entry->author = $header->author['name'];
}
$entry->media = $page->media()->images();
if ($this->config()['sort_by'] === 'desc') {
$entry->content = ! empty($header->metadata['description']) ? $header->metadata['description'] : $page->summary();
} else {
$entry->content = $page->content();
}
$entry->active = ! isset($header->yandex_turbo['active']) || $header->yandex_turbo['active'];
if (! empty($entry->content))
$this->items[$page->route()] = $entry;
}
}
if (! empty($cache_id) && ! empty($cache)) {
$cache->save($cache_id, $this->items);
}
$this->grav->fireEvent('onYandexTurboProcessed', new Event(['turbo' => &$this->items]));
}
/**
* Define twig-template and md-page for turbo RSS
*
* @return void
*/
public function onPageInitialized(Event $event)
{
$page = $event['page'] ?? null;
$route = $this->config()['route'];
if (is_null($page) || $page->route() !== $route) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/turbo.md'));
unset($this->grav['page']);
$this->grav['page'] = $page;
$twig = $this->grav['twig'];
$twig->template = 'turbo.rss.twig';
}
}
/**
* Set needed variables to display the turbo RSS.
*
* @return void
*/
public function onTwigSiteVariables()
{
$twig = $this->grav['twig'];
$twig->twig_vars['items'] = $this->items;
}
/**
* 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('yandex_turbo');
$blueprint->extend($extends, true);
$inEvent = false;
}
}
}
}