-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPlugin.php
112 lines (96 loc) · 3.76 KB
/
Plugin.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
<?php namespace SoBoRed\Rss;
use DB;
use DateTime;
use File;
use Backend;
use Controller;
use Event;
use Markdown;
use System\Classes\PluginBase;
use SoBoRed\Rss\Models\Settings;
class Plugin extends PluginBase
{
/**
* @var array Plugin dependencies
*/
public $require = ['RainLab.Blog'];
public function pluginDetails()
{
return [
'name' => 'Blog RSS Feed',
'description' => 'An RSS feed generator for the RainLab Blog plugin',
'author' => 'Josh Hall',
'icon' => 'icon-rss'
];
}
public function registerComponents()
{
return [
'SoBoRed\Rss\Components\Link' => 'rssLink'
];
}
public function boot()
{
// Event Listeners for RainLab Blog
Event::listen('eloquent.created: *\Blog\Models\Post', function($model) {
$this->createRss();
});
Event::listen('eloquent.saved: *\Blog\Models\Post', function($model) {
$this->createRss();
});
Event::listen('eloquent.deleted: *\Blog\Models\Post', function($model) {
$this->createRss();
});
// Event Listeners for SoBoRed settings
Event::listen('eloquent.saved: SoBoRed\Rss\Models\Settings', function($model) {
$this->createRss();
});
}
public function registerSettings()
{
return [
'settings' => [
'label' => 'Blog RSS Settings',
'description' => 'Manage the Blog RSS settings.',
'category' => 'Blog',
'icon' => 'icon-rss',
'class' => 'SoBoRed\Rss\Models\Settings',
'order' => 100
]
];
}
protected function createRss()
{
$fileContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" .
"<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n".
"\t<channel>\n".
"\t\t<title>" . Settings::get('title') . "</title>\n" .
"\t\t<link>" . Settings::get('link') . "</link>\n" .
"\t\t<description>" . Settings::get('description') . "</description>\n".
"\t\t<atom:link href=\"" . Settings::get('link') . "/rss.xml\" rel=\"self\" type=\"application/rss+xml\" />\n\n";
foreach($this->loadPosts() as $post)
{
$published = DateTime::createFromFormat('Y-m-d H:i:s', $post->published_at);
$description = Settings::get('showFullPostContent') ? $post->content : $post->excerpt;
$description = Markdown::parse(trim($description));
$fileContents .= "\t\t<item>\n" .
"\t\t\t<title>" . htmlspecialchars($post->title, ENT_QUOTES, 'UTF-8') . "</title>\n" .
"\t\t\t<link>" . Settings::get('link') . Settings::get('postPage') . "/" . $post->slug . "</link>\n" .
"\t\t\t<guid>" . Settings::get('link') . Settings::get('postPage') . "/" . $post->slug . "</guid>\n" .
"\t\t\t<pubDate>" . $published->format(DateTime::RFC2822) . "</pubDate>\n" .
"\t\t\t<description>" . htmlspecialchars($description, ENT_QUOTES, 'UTF-8') . "</description>\n" .
"\t\t</item>\n";
}
$fileContents .= "\t</channel>\n";
$fileContents .= "</rss>\n";
$file = File::put('rss.xml', $fileContents);
return $file;
}
protected function loadPosts()
{
$posts = \RainLab\Blog\Models\Post::isPublished()
->orderBy('published_at', 'desc')
->get();
return $posts;
}
}