-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfeed.php
188 lines (160 loc) · 4.92 KB
/
feed.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
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Data;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use RocketTheme\Toolbox\Event\Event;
class FeedPlugin extends Plugin
{
/**
* @var bool
*/
protected $active = false;
/**
* @var string
*/
protected $type;
/**
* @var array
*/
protected $feed_config;
/**
* @var array
*/
protected $valid_types = array('rss','atom');
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100000],
['onPluginsInitialized', 0],
],
'onBlueprintCreated' => ['onBlueprintCreated', 0],
'onPageHeaders' => ['onPageHeaders', 0]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Activate feed plugin only if feed was requested for the current page.
*
* Also disables debugger.
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
return;
}
$this->feed_config = (array) $this->config->get('plugins.feed');
if ($this->feed_config['enable_json_feed']) {
$this->valid_types[] = 'json';
}
/** @var Uri $uri */
$uri = $this->grav['uri'];
$this->type = $uri->extension();
if ($this->type && in_array($this->type, $this->valid_types)) {
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
}
}
/**
* Initialize feed configuration.
*/
public function onPageInitialized()
{
$page = $this->grav['page'];
// Overwrite regular content with feed config, so you can influence the collection processing with feed config
if (property_exists($page->header(), 'content') || property_exists($page->header(), 'feed')) {
// Set default template.
$template = "feed";
if (property_exists($page->header(), 'feed')) {
$this->feed_config = array_merge($this->feed_config, $page->header()->feed);
// Look for feed type override,
if (isset($this->feed_config['template'][$this->type])) {
$template = $this->feed_config['template'][$this->type];
}
}
if (property_exists($page->header(), 'content')) {
$page->header()->content = array_merge($page->header()->content ?? [], $this->feed_config);
$this->enable([
'onCollectionProcessed' => ['onCollectionProcessed', 0],
]);
}
// Set page template.
$this->grav['twig']->template = $template . "." . $this->type . '.twig';
}
}
/**
* Feed consists of all sub-pages.
*
* @param Event $event
*/
public function onCollectionProcessed(Event $event)
{
/** @var Collection $collection */
$collection = $event['collection']->nonModular();
foreach ($collection as $page) {
$header = $page->header();
if (isset($header->feed) && !empty($header->feed['skip'])) {
$collection->remove($page);
}
}
$this->grav->fireEvent('onFeedCollectionProcessed', $event);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* 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'];
$form = $blueprint->form();
$blog_tab_exists = isset($form['fields']['tabs']['fields']['blog']);
if (!$inEvent && $blog_tab_exists) {
$inEvent = true;
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('feed');
$blueprint->extend($extends, true);
$inEvent = false;
}
}
/**
* Force UTF-8 char-set encoding via `Content-Type` header
*
* @param Event $e
* @return void
*/
public function onPageHeaders(Event $e)
{
$headers = $e['headers'];
$content_type = $headers->{'Content-Type'} ?? null;
if ($content_type) {
$headers->{'Content-Type'} = "$content_type; charset=utf-8";
}
}
}