forked from getgrav/grav-plugin-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.php
149 lines (128 loc) · 3.74 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
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Common\Page\Page;
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' => ['onPluginsInitialized', 0],
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
/**
* Activate feed plugin only if feed was requested for the current page.
*
* Also disables debugger.
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
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->active = true;
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onCollectionProcessed' => ['onCollectionProcessed', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
}
/**
* Initialize feed configuration.
*/
public function onPageInitialized()
{
/** @var Page $page */
$page = $this->grav['page'];
if (isset($page->header()->feed)) {
$this->feed_config = array_merge($this->feed_config, $page->header()->feed);
}
// Overwrite regular content with feed config, so you can influence the collection processing with feed config
if (property_exists($page->header(), 'content')) {
$page->header()->content = array_merge($page->header()->content, $this->feed_config);
}
}
/**
* Feed consists of all sub-pages.
*
* @param Event $event
*/
public function onCollectionProcessed(Event $event)
{
/** @var Collection $collection */
$collection = $event['collection'];
foreach ($collection as $slug => $page) {
$header = $page->header();
if (isset($header->feed) && !empty($header->feed['skip'])) {
$collection->remove($page);
}
}
}
/**
* Set feed template as current twig template
*/
public function onTwigSiteVariables()
{
$this->grav['twig']->template = 'feed.' . $this->type . '.twig';
}
/**
* 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'];
if (!$inEvent && $blueprint->name == 'blog_list') {
$inEvent = true;
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('feed');
$blueprint->extend($extends, true);
$inEvent = false;
}
}
}