From f42d448aa0e91ee862932a669e8d75f74af01b22 Mon Sep 17 00:00:00 2001 From: ShaiMagal <8518736+ShaiMagal@users.noreply.github.com> Date: Tue, 1 Sep 2026 15:51:49 +0200 Subject: [PATCH] Declare the sitemaps of every shop in robots.txt on multistore --- config.xml | 2 +- gsitemap.php | 57 +++++++++++++++++++++++++++++++++++++-- upgrade/upgrade-5.1.0.php | 33 +++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 upgrade/upgrade-5.1.0.php diff --git a/config.xml b/config.xml index 4d213ad..585dce1 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ gsitemap - + diff --git a/gsitemap.php b/gsitemap.php index b558ea7..2a6db58 100755 --- a/gsitemap.php +++ b/gsitemap.php @@ -60,7 +60,7 @@ public function __construct() { $this->name = 'gsitemap'; $this->tab = 'checkout'; - $this->version = '5.0.0'; + $this->version = '5.1.0'; $this->author = 'PrestaShop'; $this->need_instance = 0; $this->bootstrap = true; @@ -190,7 +190,8 @@ public function install() } return parent::install() - && Db::getInstance()->Execute('CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'gsitemap_sitemap` (`link` varchar(255) DEFAULT NULL, `id_shop` int(11) DEFAULT 0) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;') && $this->installHook(); + && Db::getInstance()->Execute('CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'gsitemap_sitemap` (`link` varchar(255) DEFAULT NULL, `id_shop` int(11) DEFAULT 0) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;') && $this->installHook() + && $this->registerHook('actionAdminMetaAfterWriteRobotsFile'); } /** @@ -320,6 +321,58 @@ public function getContent() return $this->display(__FILE__, 'views/templates/admin/configuration.tpl'); } + /** + * Declares the sitemaps of the remaining shops in robots.txt. + * + * The core writes a Sitemap line for the shop currently in context only, while a + * multistore installation serves one robots.txt file to every domain. Without this, + * the sitemaps of the other shops are not declared anywhere. + * + * @param array $params + * + * @return void + */ + public function hookActionAdminMetaAfterWriteRobotsFile($params) + { + if (!Shop::isFeatureActive() || empty($params['write_fd']) || !is_resource($params['write_fd'])) { + return; + } + + $idShopInContext = (int) $this->context->shop->id; + + foreach (array_keys(Shop::getShops(true)) as $idShop) { + $idShop = (int) $idShop; + + // The core already declared this one. + if ($idShop === $idShopInContext) { + continue; + } + + $filename = $idShop . '_index_sitemap.xml'; + $sitemapFile = $this->normalizeDirectory(_PS_ROOT_DIR_) . $filename; + if (!file_exists($sitemapFile) || !filesize($sitemapFile)) { + continue; + } + + $shop = new Shop($idShop); + if (!Validate::isLoadedObject($shop)) { + continue; + } + + // Each shop has its own domain and can have its own SSL setting. + $useSsl = (bool) Configuration::get('PS_SSL_ENABLED', null, null, $idShop); + $domain = $useSsl ? $shop->domain_ssl : $shop->domain; + if (empty($domain)) { + continue; + } + + fwrite( + $params['write_fd'], + 'Sitemap: ' . Tools::getProtocol($useSsl) . $domain . $shop->getBaseURI() . $filename . PHP_EOL + ); + } + } + /** * Delete all the generated sitemap files from the files system and the database. * diff --git a/upgrade/upgrade-5.1.0.php b/upgrade/upgrade-5.1.0.php new file mode 100644 index 0000000..54e173b --- /dev/null +++ b/upgrade/upgrade-5.1.0.php @@ -0,0 +1,33 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +if (!defined('_PS_VERSION_')) { + exit; +} + +function upgrade_module_5_1_0($object) +{ + return $object->registerHook('actionAdminMetaAfterWriteRobotsFile'); +}