This commit is contained in:
Michael RICOIS 2018-03-19 11:47:59 +01:00
parent 2207ae3cb8
commit 3fa1118554

View File

@ -2,11 +2,13 @@
if (!defined('_PS_VERSION_'))
exit;
class GoogleSitemap extends Module {
class GoogleSitemap extends Module
{
private $_html = '';
private $_postErrors = array();
public function __construct() {
public function __construct()
{
$this->name = 'googlesitemap';
$this->tab = 'seo';
$this->version = '1.2';
@ -18,17 +20,18 @@ class GoogleSitemap extends Module {
$this->displayName = $this->l('Google sitemap with private sales support');
$this->description = $this->l('Generate your Google sitemap file');
if(!defined('GSITEMAP_FILE')) {
if (!defined('GSITEMAP_FILE')) {
define('GSITEMAP_FILE', dirname(__FILE__).'/../../sitemap.xml');
}
}
public function install() {
if(!parent::install()) {
public function install()
{
if (!parent::install()) {
return FALSE;
}
if(Module::isInstalled('privatesales')) {
if (Module::isInstalled('privatesales')) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_module`
VALUES (
@ -41,26 +44,29 @@ class GoogleSitemap extends Module {
return TRUE;
}
public function uninstall() {
public function uninstall()
{
file_put_contents(GSITEMAP_FILE, '');
if(Module::isInstalled('privatesales')) {
if (Module::isInstalled('privatesales')) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_module` WHERE `modulename` = "googlesitemap"');
}
return parent::uninstall();
}
private function _postValidation() {
private function _postValidation()
{
file_put_contents(GSITEMAP_FILE, '');
if(!($fp = fopen(GSITEMAP_FILE, 'w'))) {
if (!($fp = fopen(GSITEMAP_FILE, 'w'))) {
$this->_postErrors[] = $this->l('Cannot create').' '.realpath(dirname(__FILE__.'/../..')).'/'.$this->l('sitemap.xml file.');
} else {
fclose($fp);
}
}
public function generateSitemap() {
public function generateSitemap()
{
global $cookie;
$link = new Link();
$langs = Language::getLanguages();
@ -75,7 +81,7 @@ XML;
$xml = new SimpleXMLElement($xmlString);
if(Configuration::get('PS_REWRITING_SETTINGS') && sizeof($langs) > 1) {
if (Configuration::get('PS_REWRITING_SETTINGS') && sizeof($langs) > 1) {
foreach($langs as $lang) {
$this->_addSitemapNode($xml, Tools::getShopDomain(true, true).__PS_BASE_URI__.$lang['iso_code'].'/', '1.00', 'daily', date('Y-m-d'));
}
@ -84,7 +90,7 @@ XML;
}
/* CMS Generator */
if(Configuration::get('GSITEMAP_ALL_CMS') || !Module::isInstalled('blockcms')) {
if (Configuration::get('GSITEMAP_ALL_CMS') || !Module::isInstalled('blockcms')) {
$sql_cms = '
SELECT DISTINCT '.(Configuration::get('PS_REWRITING_SETTINGS')? 'cl.id_cms, cl.link_rewrite, cl.id_lang': 'cl.id_cms').'
FROM `'._DB_PREFIX_.'cms_lang` cl
@ -114,21 +120,21 @@ XML;
/* Categories Generator */
$exclude_categories = array();
if(Module::isInstalled('privatesales')) {
if (Module::isInstalled('privatesales')) {
include(dirname(__FILE__).'/../privatesales/Sale.php');
$has_trailers = file_exists(dirname(__FILE__).'/../privatesales/trailer.php');
$sales = Sale::getSales(TRUE, NULL, NULL, 'not_ended');
$sales = Sale::getSales(true, null, null, 'not_ended');
foreach($sales as $sale) {
$exclude_categories = array_merge($exclude_categories, Sale::getCategoriesFromCache($sale->id));
if($has_trailers) {
if ($has_trailers) {
$trailers_i18n = array(
'fr' => 'bande-annonce',
'en' => 'trailer',
);
if(Configuration::get('PS_REWRITING_SETTINGS')) {
if(count($langs) > 1) {
if (Configuration::get('PS_REWRITING_SETTINGS')) {
if (count($langs) > 1) {
$this->_addSitemapNode($xml, Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.$iso_code.'/'.(isset($trailers_i18n[$iso_code])? $trailers_i18n[$iso_code]: $trailers_i18n['en']).'/'.$sale->id.'-'.Tools::str2url($sale->title[$cookie->id_lang]), '0.8', 'weekly');
} else {
$this->_addSitemapNode($xml, Tools::getShopDomain(TRUE, TRUE).__PS_BASE_URI__.(isset($trailers_i18n[$iso_code])? $trailers_i18n[$iso_code]: $trailers_i18n['en']).'/'.$sale->id.'-'.Tools::str2url($sale->title[$cookie->id_lang]), '0.8', 'weekly');
@ -140,8 +146,8 @@ XML;
}
$ordering = (int) Configuration::get('PRIVATESALES_FEATURED_ORDER');
if($ordering == 0) {
if(Module::isInstalled('privatesales_brands')) {
if ($ordering == 0) {
if (Module::isInstalled('privatesales_brands')) {
$sales = Sale::getSales(TRUE, (int) Configuration::get('PRIVATESALES_FEATURED_IGNORE') == 1? NULL: !((bool) Configuration::get('PRIVATESALES_GUESTLIST')), TRUE, (bool) Configuration::get('PRIVATESALES_FEATURED_CURRENT'), TRUE);
$brands = array();
@ -175,8 +181,8 @@ XML;
'en' => 'brands',
);
foreach($manufacturers as $manufacturer) {
if(Configuration::get('PS_REWRITING_SETTINGS')) {
if(count($langs) > 1) {
if (Configuration::get('PS_REWRITING_SETTINGS')) {
if (count($langs) > 1) {
$this->_addSitemapNode($xml, Tools::getShopDomain(true, true).__PS_BASE_URI__.$iso_code.'/'.(isset($brands_i18n[$iso_code])? $brands_i18n[$iso_code]: $brands_i18n['en']).'/'.$manufacturer['id_manufacturer'].'-'.Tools::str2url($manufacturer['name']), '0.8', 'weekly');
} else {
$this->_addSitemapNode($xml, Tools::getShopDomain(true, true).__PS_BASE_URI__.(isset($brands_i18n[$iso_code])? $brands_i18n[$iso_code]: $brands_i18n['en']).'/'.$manufacturer['id_manufacturer'].'-'.Tools::str2url($manufacturer['name']), '0.8', 'weekly');
@ -188,7 +194,7 @@ XML;
}
}
if(Configuration::get('PS_REWRITING_SETTINGS')) {
/*if (Configuration::get('PS_REWRITING_SETTINGS')) {
$categories = Db::getInstance()->ExecuteS('
SELECT c.id_category, c.level_depth, link_rewrite, DATE_FORMAT(IF(date_upd,date_upd,date_add), \'%Y-%m-%d\') AS date_upd, cl.id_lang
FROM '._DB_PREFIX_.'category c
@ -209,10 +215,10 @@ XML;
'.(count($exclude_categories) > 0? ' AND c.id_category NOT IN ('.implode(', ', $exclude_categories).')': '').'
ORDER BY c.id_category ASC
');
}
}*/
foreach($categories as $category) {
if(($priority = 0.9 - ($category['level_depth'] / 10)) < 0.1) {
if (($priority = 0.9 - ($category['level_depth'] / 10)) < 0.1) {
$priority = 0.1;
}
@ -253,7 +259,7 @@ XML;
$tmp = NULL;
$res = NULL;
foreach($products as $product) {
if($tmp == $product['id_product']) {
if ($tmp == $product['id_product']) {
$res[$tmp]['images'][] = array('id_image' => $product['id_image'], 'legend_image' => $product['legend_image']);
} else {
$tmp = $product['id_product'];
@ -264,7 +270,7 @@ XML;
}
foreach($res as $product) {
if(($priority = 0.7 - ($product['level_depth'] / 10)) < 0.1) {
if (($priority = 0.7 - ($product['level_depth'] / 10)) < 0.1) {
$priority = 0.1;
}
@ -275,37 +281,42 @@ XML;
/* Add classic pages (contact, best sales, new products...) */
$pages = array(
'authentication' => TRUE,
'best-sales' => FALSE,
'contact-form' => TRUE,
'discount' => FALSE,
'index' => FALSE,
'manufacturer' => FALSE,
'new-products' => FALSE,
'prices-drop' => FALSE,
'supplier' => FALSE,
'store' => FALSE,
'authentication' => true,
'best-sales' => false,
'contact-form' => true,
'discount' => false,
'index' => false,
'manufacturer' => false,
'new-products' => false,
'prices-drop' => false,
'supplier' => false,
'store' => false,
);
// Don't show suppliers and manufacturers if they are disallowed
if(!Module::getInstanceByName('blockmanufacturer')->id && !Configuration::get('PS_DISPLAY_SUPPLIERS')) {
if (!Module::getInstanceByName('blockmanufacturer')->id && !Configuration::get('PS_DISPLAY_SUPPLIERS')) {
unset($pages['manufacturer']);
}
if(!Module::getInstanceByName('blocksupplier')->id && !Configuration::get('PS_DISPLAY_SUPPLIERS')) {
if (!Module::getInstanceByName('blocksupplier')->id && !Configuration::get('PS_DISPLAY_SUPPLIERS')) {
unset($pages['supplier']);
}
// Generate nodes for pages
if(Configuration::get('PS_REWRITING_SETTINGS')) {
foreach($pages as $page => $ssl) {
foreach($langs as $lang) {
$this->_addSitemapNode($xml, $link->getPageLink($page.'.php', $ssl, $lang['id_lang']), '0.5', 'monthly');
$ssl = true;
if (Configuration::get('PS_REWRITING_SETTINGS')) {
foreach($pages as $page => $enable) {
if ($enable === true) {
foreach($langs as $lang) {
$this->_addSitemapNode($xml, $link->getPageLink($page.'.php', $ssl, $lang['id_lang']), '0.5', 'monthly');
}
}
}
} else {
foreach($pages as $page => $ssl) {
$this->_addSitemapNode($xml, $link->getPageLink($page.'.php', $ssl), '0.5', 'monthly');
foreach($pages as $page => $enable) {
if ($enable === true) {
$this->_addSitemapNode($xml, $link->getPageLink($page.'.php', $ssl), '0.5', 'monthly');
}
}
}
@ -317,7 +328,8 @@ XML;
}
private function _postProcess() {
private function _postProcess()
{
Configuration::updateValue('GSITEMAP_ALL_CMS', (int) Tools::getValue('GSITEMAP_ALL_CMS'));
Configuration::updateValue('GSITEMAP_ALL_PRODUCTS', (int) Tools::getValue('GSITEMAP_ALL_PRODUCTS'));
@ -329,28 +341,31 @@ XML;
$this->_html .= '</h3>';
}
private function getUrlWith($url, $key, $value) {
if(empty($value)) {
private function getUrlWith($url, $key, $value)
{
if (empty($value)) {
return $url;
}
if(strpos($url, '?') !== FALSE) {
if (strpos($url, '?') !== FALSE) {
return $url.'&'.$key.'='.$value;
}
return $url.'?'.$key.'='.$value;
}
private function _addSitemapNode($xml, $loc, $priority, $change_freq, $last_mod=NULL) {
private function _addSitemapNode($xml, $loc, $priority, $change_freq, $last_mod=NULL)
{
$sitemap = $xml->addChild('url');
$sitemap->addChild('loc', $loc);
$sitemap->addChild('priority', $priority);
if($last_mod) {
if ($last_mod) {
$sitemap->addChild('lastmod', $last_mod);
}
$sitemap->addChild('changefreq', $change_freq);
return $sitemap;
}
private function _addSitemapNodeImage($xml, $product) {
private function _addSitemapNodeImage($xml, $product)
{
foreach($product['images'] as $img) {
$link = new Link();
$image = $xml->addChild('image', NULL, 'http://www.google.com/schemas/sitemap-image/1.1');
@ -362,8 +377,9 @@ XML;
}
}
private function _displaySitemap() {
if(file_exists(GSITEMAP_FILE) && filesize(GSITEMAP_FILE)) {
private function _displaySitemap()
{
if (file_exists(GSITEMAP_FILE) && filesize(GSITEMAP_FILE)) {
$fp = fopen(GSITEMAP_FILE, 'r');
$fstat = fstat($fp);
fclose($fp);
@ -380,7 +396,8 @@ XML;
}
}
private function _displayForm() {
private function _displayForm()
{
$this->_html .=
'<form action="'.Tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']).'" method="post">
<div style="margin:0 0 20px 0;">
@ -394,11 +411,12 @@ XML;
</form>';
}
public function getContent() {
public function getContent()
{
$this->_html .= '<h2>'.$this->l('Search Engine Optimization').'</h2>
'.$this->l('See').' <a href="https://www.google.com/webmasters/tools/docs/en/about.html" style="font-weight:bold;text-decoration:underline;" target="_blank">
'.$this->l('this page').'</a> '.$this->l('for more information').'<br /><br />';
if(Tools::isSubmit('btnSubmit')) {
if (Tools::isSubmit('btnSubmit')) {
$this->_postValidation();
if (!sizeof($this->_postErrors)) {
$this->_postProcess();