bludit/bl-plugins/rss/plugin.php

142 lines
3.2 KiB
PHP
Raw Normal View History

2016-01-11 23:51:00 +01:00
<?php
class pluginRSS extends Plugin {
public function init()
{
// Fields and default values for the database of this plugin
$this->dbFields = array(
'amountOfItems'=>5
);
}
// Method called on the settings of the plugin on the admin area
public function form()
{
global $language;
2018-07-02 00:24:53 +02:00
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$html .= '<div>';
$html .= '<label>'.$language->get('RSS URL').'</label>';
$html .= '<a href="'.Theme::rssUrl().'">'.Theme::rssUrl().'</a>';
$html .= '</div>';
$html .= '<div>';
$html .= '<label>'.$language->get('Amount of items').'</label>';
$html .= '<input id="jsamountOfItems" name="amountOfItems" type="text" value="'.$this->getValue('amountOfItems').'">';
$html .= '<span class="tip">'.$language->get('Amount of items to show on the feed').'</span>';
$html .= '</div>';
return $html;
}
2016-01-11 23:51:00 +01:00
private function createXML()
{
global $site;
2018-08-03 18:59:23 +02:00
global $pages;
global $url;
2016-01-11 23:51:00 +01:00
// Amount of pages to show
$amountOfItems = $this->getValue('amountOfItems');
// Page number the first one
$pageNumber = 1;
// Only published pages
$onlyPublished = true;
// Get the list of pages
2018-08-03 18:59:23 +02:00
$list = $pages->getList($pageNumber, $amountOfItems, $onlyPublished, true);
2016-01-11 23:51:00 +01:00
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<rss version="2.0">';
$xml .= '<channel>';
$xml .= '<title>'.$site->title().'</title>';
$xml .= '<link>'.$site->url().'</link>';
$xml .= '<description>'.$site->description().'</description>';
2016-01-11 23:51:00 +01:00
// Get keys of pages
foreach($list as $pageKey) {
try {
// Create the page object from the page key
2018-08-02 17:06:53 +02:00
$page = new Page($pageKey);
$xml .= '<item>';
$xml .= '<title>'.$page->title().'</title>';
$xml .= '<link>'.$page->permalink().'</link>';
$xml .= '<description>'.Sanitize::html($page->contentBreak()).'</description>';
$xml .= '<pubDate>'.$page->dateRaw('r').'</pubDate>';
$xml .= '<guid isPermaLink="false">'.$page->uuid().'</guid>';
$xml .= '</item>';
} catch (Exception $e) {
// Continue
}
2016-01-11 23:51:00 +01:00
}
$xml .= '</channel></rss>';
// New DOM document
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($xml);
$doc->save($this->workspace().'rss.xml');
2016-01-11 23:51:00 +01:00
}
public function install($position=0)
2016-01-11 23:51:00 +01:00
{
parent::install($position);
$this->createXML();
}
2018-02-28 11:08:35 +01:00
public function post()
{
// Call the method
parent::post();
// After POST request
$this->createXML();
}
2016-01-11 23:51:00 +01:00
public function afterPageCreate()
{
$this->createXML();
}
public function afterPageModify()
{
$this->createXML();
}
public function afterPageDelete()
{
$this->createXML();
}
2016-01-17 23:05:46 +01:00
public function siteHead()
{
return '<link rel="alternate" type="application/rss+xml" href="'.DOMAIN_BASE.'rss.xml" title="RSS Feed">'.PHP_EOL;
2016-01-17 23:05:46 +01:00
}
2017-07-29 01:20:47 +02:00
public function beforeAll()
2016-01-11 23:51:00 +01:00
{
2017-07-29 01:20:47 +02:00
$webhook = 'rss.xml';
2018-07-02 00:24:53 +02:00
if ($this->webhook($webhook)) {
2016-01-11 23:51:00 +01:00
// Send XML header
header('Content-type: text/xml');
$doc = new DOMDocument();
// Load XML
libxml_disable_entity_loader(false);
$doc->load($this->workspace().'rss.xml');
libxml_disable_entity_loader(true);
2016-01-11 23:51:00 +01:00
// Print the XML
echo $doc->saveXML();
2018-07-02 00:24:53 +02:00
// Stop Bludit execution
exit(0);
2016-01-11 23:51:00 +01:00
}
}
}