ping Google and Bing when sitemap changed

This commit is contained in:
Diego Najar 2018-09-18 19:46:10 +02:00
parent 389a400443
commit 01f4415ac3
4 changed files with 66 additions and 21 deletions

View File

@ -22,8 +22,7 @@ class TCP {
Log::set('Curl error: '.curl_error($ch)); Log::set('Curl error: '.curl_error($ch));
} }
curl_close($ch); curl_close($ch);
} } else {
else {
$options = array( $options = array(
'http'=>array( 'http'=>array(
'method'=>$method, 'method'=>$method,

View File

@ -108,24 +108,31 @@ class pluginRemoteContent extends Plugin {
// For each page inside the pages directory // For each page inside the pages directory
// Parse the page and add to the database // Parse the page and add to the database
if (Filesystem::directoryExists($root.DS.'pages')) { if (Filesystem::directoryExists($root.DS.'pages')) {
$pageList = Filesystem::listDirectories($root.DS.'pages'.DS); $parentList = Filesystem::listDirectories($root.DS.'pages'.DS);
foreach ($pageList as $directory) { foreach ($parentList as $parentDirectory) {
if (Filesystem::fileExists($directory.DS.'index.md')) { $parentKey = basename($parentDirectory);
// Parse the page from the file if (Filesystem::fileExists($parentDirectory.DS.'index.md')) {
$row = $this->parsePage($directory.DS.'index.md'); $row = $this->parsePage($parentDirectory.DS.'index.md');
$row['slug'] = $parentKey;
// Add the page to the database
$pages->add($row); $pages->add($row);
}
$childList = Filesystem::listDirectories($parentDirectory.DS);
foreach ($childList as $childDirectory) {
$childKey = basename($childDirectory);
if (Filesystem::fileExists($childDirectory.DS.'index.md')) {
$row = $this->parsePage($childDirectory.DS.'index.md');
$row['slug'] = $childKey;
$row['parent'] = $parentKey;
$pages->add($row);
}
}
}
// Call the plugins after page created
Theme::plugins('afterPageCreate'); Theme::plugins('afterPageCreate');
// Reindex databases
reindexCategories(); reindexCategories();
reindextags(); reindextags();
} }
}
}
return true; return true;
} }

View File

@ -4,5 +4,7 @@
"name": "Sitemap", "name": "Sitemap",
"description": "This plugin generates a sitemap.xml file, which provides the list of pages on your website, this helps search engines organize and filter content from their website." "description": "This plugin generates a sitemap.xml file, which provides the list of pages on your website, this helps search engines organize and filter content from their website."
}, },
"sitemap-url": "Sitemap URL" "sitemap-url": "Sitemap URL",
"notifies-google-when-you-created": "Notifies Google when you created, modified or deleted content from your site.",
"notifies-bing-when-you-created": "Notifies Bing when you created, modified or deleted content from your site."
} }

View File

@ -4,7 +4,10 @@ class pluginSitemap extends Plugin {
public function init() public function init()
{ {
$this->formButtons = false; $this->dbFields = array(
'pingGoogle'=>false,
'pingBing'=>false
);
} }
// Method called on the settings of the plugin on the admin area // Method called on the settings of the plugin on the admin area
@ -21,6 +24,24 @@ class pluginSitemap extends Plugin {
$html .= '<a href="'.Theme::sitemapUrl().'">'.Theme::sitemapUrl().'</a>'; $html .= '<a href="'.Theme::sitemapUrl().'">'.Theme::sitemapUrl().'</a>';
$html .= '</div>'; $html .= '</div>';
$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>';
return $html; return $html;
} }
@ -65,6 +86,19 @@ class pluginSitemap extends Plugin {
return $doc->save($this->workspace().'sitemap.xml'); return $doc->save($this->workspace().'sitemap.xml');
} }
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);
}
}
public function install($position=0) public function install($position=0)
{ {
parent::install($position); parent::install($position);
@ -83,16 +117,19 @@ class pluginSitemap extends Plugin {
public function afterPageCreate() public function afterPageCreate()
{ {
$this->createXML(); $this->createXML();
$this->ping();
} }
public function afterPageModify() public function afterPageModify()
{ {
$this->createXML(); $this->createXML();
$this->ping();
} }
public function afterPageDelete() public function afterPageDelete()
{ {
$this->createXML(); $this->createXML();
$this->ping();
} }
public function beforeAll() public function beforeAll()