2016-01-08 00:43:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class pluginSitemap extends Plugin {
|
|
|
|
|
2018-03-24 11:31:41 +01:00
|
|
|
public function init()
|
|
|
|
{
|
2018-09-18 19:46:10 +02:00
|
|
|
$this->dbFields = array(
|
|
|
|
'pingGoogle'=>false,
|
|
|
|
'pingBing'=>false
|
|
|
|
);
|
2018-03-24 11:31:41 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 23:40:23 +02:00
|
|
|
// Method called on the settings of the plugin on the admin area
|
|
|
|
public function form()
|
|
|
|
{
|
2018-08-05 17:54:20 +02:00
|
|
|
global $L;
|
2017-10-13 23:40:23 +02:00
|
|
|
|
2018-08-16 14:19:21 +02:00
|
|
|
$html = '<div class="alert alert-primary" role="alert">';
|
|
|
|
$html .= $this->description();
|
|
|
|
$html .= '</div>';
|
|
|
|
|
|
|
|
$html .= '<div>';
|
2018-08-05 17:54:20 +02:00
|
|
|
$html .= '<label>'.$L->get('Sitemap URL').'</label>';
|
2017-10-13 23:40:23 +02:00
|
|
|
$html .= '<a href="'.Theme::sitemapUrl().'">'.Theme::sitemapUrl().'</a>';
|
|
|
|
$html .= '</div>';
|
|
|
|
|
2018-09-18 19:46:10 +02:00
|
|
|
$html .= '<div>';
|
|
|
|
$html .= '<label>Ping Google</label>';
|
|
|
|
$html .= '<select name="pingGoogle">';
|
|
|
|
$html .= '<option value="true" '.($this->getValue('pingGoogle')===true?'selected':'').'>'.$L->get('Enabled').'</option>';
|
|
|
|
$html .= '<option value="false" '.($this->getValue('pingGoogle')===false?'selected':'').'>'.$L->get('Disabled').'</option>';
|
|
|
|
$html .= '</select>';
|
|
|
|
$html .= '<span class="tip">'.$L->get('notifies-google-when-you-created').'</span>';
|
|
|
|
$html .= '</div>';
|
|
|
|
|
|
|
|
$html .= '<div>';
|
|
|
|
$html .= '<label>Ping Bing</label>';
|
|
|
|
$html .= '<select name="pingBing">';
|
|
|
|
$html .= '<option value="true" '.($this->getValue('pingBing')===true?'selected':'').'>'.$L->get('Enabled').'</option>';
|
|
|
|
$html .= '<option value="false" '.($this->getValue('pingBing')===false?'selected':'').'>'.$L->get('Disabled').'</option>';
|
|
|
|
$html .= '</select>';
|
|
|
|
$html .= '<span class="tip">'.$L->get('notifies-bing-when-you-created').'</span>';
|
|
|
|
$html .= '</div>';
|
|
|
|
|
2017-10-13 23:40:23 +02:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
private function createXML()
|
|
|
|
{
|
2018-07-17 19:13:01 +02:00
|
|
|
global $site;
|
2018-08-03 18:59:23 +02:00
|
|
|
global $pages;
|
2016-01-08 00:43:09 +01:00
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
|
|
|
|
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
|
|
|
|
|
|
|
$xml .= '<url>';
|
2018-07-17 19:13:01 +02:00
|
|
|
$xml .= '<loc>'.$site->url().'</loc>';
|
2017-06-04 21:08:31 +02:00
|
|
|
$xml .= '</url>';
|
|
|
|
|
2019-02-28 19:51:30 +01:00
|
|
|
$list = $pages->getList($pageNumber=1, $numberOfItems=-1, $published=true, $static=true, $sticky=true, $draft=false, $scheduled=false);
|
2018-09-18 19:46:10 +02:00
|
|
|
foreach ($list as $pageKey) {
|
2018-07-18 21:48:37 +02:00
|
|
|
try {
|
|
|
|
// Create the page object from the page key
|
2018-08-02 17:06:53 +02:00
|
|
|
$page = new Page($pageKey);
|
2019-06-21 10:26:38 +02:00
|
|
|
if (!$page->noindex() && !$page->noarchive()) {
|
|
|
|
$xml .= '<url>';
|
|
|
|
$xml .= '<loc>'.$page->permalink().'</loc>';
|
|
|
|
$xml .= '<lastmod>'.$page->date(SITEMAP_DATE_FORMAT).'</lastmod>';
|
|
|
|
$xml .= '<changefreq>daily</changefreq>';
|
|
|
|
$xml .= '</url>';
|
|
|
|
}
|
2018-07-18 21:48:37 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Continue
|
|
|
|
}
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
$xml .= '</urlset>';
|
2016-01-08 00:43:09 +01:00
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
// New DOM document
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->formatOutput = true;
|
|
|
|
$doc->loadXML($xml);
|
2018-09-05 22:55:14 +02:00
|
|
|
return $doc->save($this->workspace().'sitemap.xml');
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:46:10 +02:00
|
|
|
private function ping()
|
|
|
|
{
|
|
|
|
if ($this->getValue('pingGoogle')) {
|
|
|
|
$url = 'https://www.google.com/webmasters/sitemaps/ping?sitemap='.Theme::sitemapUrl();
|
|
|
|
TCP::http($url, 'GET', true, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getValue('pingBing')) {
|
|
|
|
$url = 'https://www.bing.com/webmaster/ping.aspx?sitemap='.Theme::sitemapUrl();
|
|
|
|
TCP::http($url, 'GET', true, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
public function install($position=0)
|
2016-01-08 00:43:09 +01:00
|
|
|
{
|
|
|
|
parent::install($position);
|
2018-09-05 22:55:14 +02:00
|
|
|
return $this->createXML();
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:08:35 +01:00
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
parent::post();
|
2019-01-28 22:12:33 +01:00
|
|
|
return $this->createXML();
|
2018-02-28 11:08:35 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
public function afterPageCreate()
|
|
|
|
{
|
|
|
|
$this->createXML();
|
2018-09-18 19:46:10 +02:00
|
|
|
$this->ping();
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function afterPageModify()
|
|
|
|
{
|
|
|
|
$this->createXML();
|
2018-09-18 19:46:10 +02:00
|
|
|
$this->ping();
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function afterPageDelete()
|
|
|
|
{
|
|
|
|
$this->createXML();
|
2018-09-18 19:46:10 +02:00
|
|
|
$this->ping();
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
|
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) ) {
|
2019-05-12 14:39:22 +02:00
|
|
|
$sitemapFile = $this->workspace().'sitemap.xml';
|
|
|
|
$sitemapSize = filesize($sitemapFile);
|
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
// Send XML header
|
|
|
|
header('Content-type: text/xml');
|
2019-05-12 14:39:22 +02:00
|
|
|
header('Content-length: '.$sitemapSize);
|
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
// Workaround for a bug https://bugs.php.net/bug.php?id=62577
|
2016-10-01 21:12:40 +02:00
|
|
|
libxml_disable_entity_loader(false);
|
2017-06-04 21:08:31 +02:00
|
|
|
|
|
|
|
// Load XML
|
|
|
|
$doc->load($this->workspace().'sitemap.xml');
|
|
|
|
|
2016-10-01 21:12:40 +02:00
|
|
|
libxml_disable_entity_loader(true);
|
2016-01-08 00:43:09 +01:00
|
|
|
|
|
|
|
// Print the XML
|
|
|
|
echo $doc->saveXML();
|
|
|
|
|
2017-06-04 21:08:31 +02:00
|
|
|
// Terminate the run successfully
|
|
|
|
exit(0);
|
2016-01-08 00:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 14:19:21 +02:00
|
|
|
}
|