-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedteasers.php
More file actions
213 lines (182 loc) · 6.55 KB
/
Copy pathfeedteasers.php
File metadata and controls
213 lines (182 loc) · 6.55 KB
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
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Plugin\FeedTeasers\FeedParser;
use RocketTheme\Toolbox\Event\Event;
require_once __DIR__ . '/classes/FeedParser.php';
class FeedteasersPlugin extends Plugin
{
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized(): void
{
if ($this->isAdmin()) {
$this->enable([
'onGetPageTemplates' => ['onGetPageTemplates', 0],
]);
return;
}
$this->enable([
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageContentProcessed' => ['onPageContentProcessed', 0],
]);
}
/**
* Ersetzt [feedteasers] (optional mit Parametern, z.B.
* [feedteasers show_tabs=false items_per_feed=3]) im bereits aus
* Markdown gerenderten Seiteninhalt durch die fertige Teaser-Ausgabe.
* Dadurch muss die Seite selbst keine Twig-Verarbeitung aktiviert haben.
*/
public function onPageContentProcessed(Event $event): void
{
$page = $event['page'];
$content = $page->getRawContent();
if (stripos($content, '[feedteasers') === false) {
return;
}
$content = preg_replace_callback(
'/\[feedteasers(\s+[^\]]*)?\]/i',
function (array $matches): string {
$overrides = $this->parseShortcodeAttributes($matches[1] ?? '');
return $this->renderFeedTeasers($overrides);
},
$content
);
$page->setRawContent($content);
}
/**
* Parst einfache key="value" / key=value Paare aus dem Shortcode-Tag,
* z.B. 'show_tabs=false items_per_feed=3' -> ['show_tabs' => false, 'items_per_feed' => 3]
*/
private function parseShortcodeAttributes(string $attrString): array
{
$attrs = [];
if (trim($attrString) === '') {
return $attrs;
}
if (preg_match_all('/(\w+)\s*=\s*"([^"]*)"|(\w+)\s*=\s*(\S+)/', $attrString, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if ($match[1] !== '') {
$key = $match[1];
$value = $match[2];
} else {
$key = $match[3];
$value = $match[4];
}
$attrs[$key] = $this->castShortcodeValue($value);
}
}
return $attrs;
}
private function castShortcodeValue(string $value)
{
if ($value === 'true') {
return true;
}
if ($value === 'false') {
return false;
}
if (is_numeric($value)) {
return $value + 0;
}
return $value;
}
public function onGetPageTemplates(Event $event): void
{
// Platzhalter fuer zukuenftige eigene Seitentemplates, falls gewuenscht.
}
/**
* Registriert den templates/-Ordner des Plugins bei Twig, damit
* partials/feedteasers.html.twig ueberhaupt gefunden werden kann
* (sowohl beim internen render() als auch bei einem manuellen
* {% include %} aus einem Theme-Template heraus).
*/
public function onTwigTemplatePaths(): void
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigInitialized(): void
{
$this->grav['twig']->twig()->addFunction(
new \Twig\TwigFunction(
'feed_teasers',
[$this, 'renderFeedTeasers'],
['is_safe' => ['html']]
)
);
}
public function onTwigSiteVariables(): void
{
/** @var \Grav\Common\Assets $assets */
$assets = $this->grav['assets'];
$assets->addCss('plugin://feedteasers/assets/feedteasers.css');
$assets->addJs('plugin://feedteasers/assets/feedteasers.js', ['group' => 'bottom', 'loading' => 'defer']);
}
/**
* Twig-Funktion: {{ feed_teasers() }} oder {{ feed_teasers({'show_tabs': false}) }}
*/
public function renderFeedTeasers(array $overrides = []): string
{
/** @var \Grav\Common\Config\Config $gravConfig */
$gravConfig = $this->grav['config'];
$config = (array) $gravConfig->get('plugins.feedteasers', []);
$config = array_replace_recursive($config, $overrides);
$feeds = $config['feeds'] ?? [];
$groups = [];
foreach ($feeds as $index => $feedConfig) {
$name = $feedConfig['name'] ?? ('Feed ' . ($index + 1));
$url = $feedConfig['url'] ?? '';
if ($url === '') {
continue;
}
$items = $this->getFeedItems($url, (int) $config['cache_time'], (int) $config['request_timeout']);
$items = array_slice($items, 0, (int) $config['items_per_feed']);
$groups[] = [
'id' => 'feedteasers-' . self::slugify($name . '-' . $index),
'name' => $name,
'items' => $items,
'error' => $items === null,
];
}
return $this->grav['twig']->twig()->render('partials/feedteasers.html.twig', [
'groups' => $groups,
'config' => $config,
]);
}
/**
* Holt Feed-Items aus dem Cache oder laedt/parst den Feed neu.
* Gibt bei Fehlern ein leeres Array zurueck, damit ein einzelner
* kaputter Feed nicht die ganze Seite zum Absturz bringt.
*/
private function getFeedItems(string $url, int $cacheTime, int $timeout): array
{
/** @var \Grav\Common\Cache $cache */
$cache = $this->grav['cache'];
$cacheKey = 'feedteasers-' . md5($url);
$cached = $cache->fetch($cacheKey);
if ($cached !== false) {
return $cached;
}
try {
$items = FeedParser::fetchAndParse($url, $timeout);
} catch (\Throwable $e) {
$this->grav['log']->warning('[feedteasers] Feed konnte nicht geladen werden (' . $url . '): ' . $e->getMessage());
$items = [];
}
$cache->save($cacheKey, $items, $cacheTime);
return $items;
}
private static function slugify(string $text): string
{
$text = preg_replace('/[^a-zA-Z0-9]+/', '-', $text);
$text = trim($text, '-');
return strtolower($text) ?: 'feed';
}
}