forked from MattByName/Pico-RssMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRssMaker.php
132 lines (110 loc) · 3.68 KB
/
RssMaker.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
<?php
/**
* RssMaker - basic RSS feed generator for Pico
*
* @author Matt Barnard
* @link https://github.com/picocms/Pico/blob/master/plugins/DummyPlugin.php
* @license http://opensource.org/licenses/MIT The MIT License
* @version 1.0
*/
final class RssMaker extends AbstractPicoPlugin
{
//variable declarations
private $giveFeed = false; // boolean to determine if the user has typed example.com/feed
private $feedTitle = ''; // title of the feed will be the site title
private $baseURL = ''; // this will hold the base url
/**
* This plugin is enabled by default?
*
* @see AbstractPicoPlugin::$enabled
* @var boolean
*/
protected $enabled = false;
/**
* This plugin depends on ...
*
* @see AbstractPicoPlugin::$dependsOn
* @var string[]
*/
protected $dependsOn = array();
/**
* Triggered after Pico has loaded all available plugins
*
* This event is triggered nevertheless the plugin is enabled or not.
* It is NOT guaranteed that plugin dependencies are fulfilled!
*
* @see Pico::getPlugin()
* @see Pico::getPlugins()
* @param object[] &$plugins loaded plugin instances
* @return void
*/
public function onConfigLoaded(array &$config)
{
// Get site data
$this->feedTitle = $config['site_title'];
$this->baseURL = $config['base_url'];
}
/**
* Triggered after Pico has evaluated the request URL
*
* @see Pico::getRequestUrl()
* @param string &$url part of the URL describing the requested contents
* @return void
*/
public function onRequestUrl(&$url)
{
// If example.com/feed, then true
if ($url == 'feed') {
$this->giveFeed = true;
}
}
public function onPagesLoaded(
array &$pages,
array &$currentPage = null,
array &$previousPage = null,
array &$nextPage = null
)
{
// If this is the feed link, return RSS feed
if ($this->giveFeed) {
//Sitemap found, 200 OK
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
header("Content-Type: application/rss+xml; charset=UTF-8");
//RSS Start
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$rss .= '<channel><title>';
$rss .= $this->feedTitle;
$rss .= '</title>';
$rss .= '<atom:link href="{{ base_url }}/feed" rel="self" type="application/rss+xml" />';
//Reverse order like in a blog
$reverse_pages = array_reverse($pages);
//Page loop
foreach ($reverse_pages as $page) {
if (!empty($page['date'])) {
$rss .= '<item>';
$rss .= '<title>';
$rss .= $page['title'];
$rss .= '</title>';
$rss .= '<description>';
$rss .= $page['description'];
$rss .= '</description>';
$rss .= '<link>';
$rss .= $page['url'];
$rss .= '</link>';
$rss .= '<pubDate>';
$rss .= date('c', $page['time']);
$rss .= '</pubDate>';
$rss .= '<guid>';
$rss .= $page['url'];
$rss .= '</guid>';
$rss .= '</item>';
}
}
//RSS End
$rss .= '</channel></rss>';
//Show generated sitemap
die($rss);
}
}
}