bludit/bl-plugins/sitemap/plugin.php

114 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2016-01-08 00:43:09 +01:00
<?php
class pluginSitemap extends Plugin {
2018-03-24 11:31:41 +01:00
public function init()
{
$this->formButtons = false;
}
2017-10-13 23:40:23 +02:00
// Method called on the settings of the plugin on the admin area
public function form()
{
global $Language;
$html = '<div>';
$html .= '<label>'.$Language->get('Sitemap URL').'</label>';
$html .= '<a href="'.Theme::sitemapUrl().'">'.Theme::sitemapUrl().'</a>';
$html .= '</div>';
return $html;
}
2016-01-08 00:43:09 +01:00
private function createXML()
{
global $Site;
global $dbPages;
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$xml .= '<url>';
$xml .= '<loc>'.$Site->url().'</loc>';
$xml .= '</url>';
// Get DB
$pageNumber = 1;
$amountOfItems = -1;
$onlyPublished = true;
2017-12-26 17:45:02 +01:00
$pages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
2017-12-26 17:45:02 +01:00
foreach($pages as $pageKey) {
// Create the page object from the page key
$page = buildPage($pageKey);
$xml .= '<url>';
$xml .= '<loc>'.$page->permalink().'</loc>';
$xml .= '<lastmod>'.$page->dateRaw(SITEMAP_DATE_FORMAT).'</lastmod>';
$xml .= '<changefreq>daily</changefreq>';
$xml .= '</url>';
2016-01-08 00:43:09 +01:00
}
$xml .= '</urlset>';
2016-01-08 00:43:09 +01:00
// New DOM document
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($xml);
$doc->save($this->workspace().'sitemap.xml');
2016-01-08 00:43:09 +01:00
}
public function install($position=0)
2016-01-08 00:43:09 +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-08 00:43:09 +01:00
public function afterPageCreate()
{
$this->createXML();
}
public function afterPageModify()
{
$this->createXML();
}
public function afterPageDelete()
{
$this->createXML();
}
2017-07-29 01:20:47 +02:00
public function beforeAll()
2016-01-08 00:43:09 +01:00
{
2017-07-29 01:20:47 +02:00
$webhook = 'sitemap.xml';
if( $this->webhook($webhook) ) {
2016-01-08 00:43:09 +01:00
// Send XML header
header('Content-type: text/xml');
$doc = new DOMDocument();
// Workaround for a bug https://bugs.php.net/bug.php?id=62577
libxml_disable_entity_loader(false);
// Load XML
$doc->load($this->workspace().'sitemap.xml');
libxml_disable_entity_loader(true);
2016-01-08 00:43:09 +01:00
// Print the XML
echo $doc->saveXML();
// Terminate the run successfully
exit(0);
2016-01-08 00:43:09 +01:00
}
}
}