diff --git a/app/code/Magento/Sitemap/Model/Sitemap.php b/app/code/Magento/Sitemap/Model/Sitemap.php index 6abc13c98c35e..2638caf52d130 100644 --- a/app/code/Magento/Sitemap/Model/Sitemap.php +++ b/app/code/Magento/Sitemap/Model/Sitemap.php @@ -7,8 +7,10 @@ // @codingStandardsIgnoreFile namespace Magento\Sitemap\Model; + use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; use Magento\Framework\App\ObjectManager; +use Magento\Framework\DataObject; /** * Sitemap model @@ -225,55 +227,78 @@ protected function _getStream() } /** - * Initialize sitemap items + * Add a sitemap item to the array of sitemap items + * + * @param DataObject $sitemapItem + * @return $this + */ + public function addSitemapItem(DataObject $sitemapItem) + { + $this->_sitemapItems[] = $sitemapItem; + + return $this; + } + + /** + * Collect all sitemap items * * @return void */ - protected function _initSitemapItems() + public function collectSitemapItems() { /** @var $helper \Magento\Sitemap\Helper\Data */ $helper = $this->_sitemapData; $storeId = $this->getStoreId(); - $this->_sitemapItems[] = new \Magento\Framework\DataObject( + $this->addSitemapItem(new DataObject( [ 'changefreq' => $helper->getCategoryChangefreq($storeId), 'priority' => $helper->getCategoryPriority($storeId), 'collection' => $this->_categoryFactory->create()->getCollection($storeId), ] - ); + )); - $this->_sitemapItems[] = new \Magento\Framework\DataObject( + $this->addSitemapItem(new DataObject( [ 'changefreq' => $helper->getProductChangefreq($storeId), 'priority' => $helper->getProductPriority($storeId), 'collection' => $this->_productFactory->create()->getCollection($storeId), ] - ); + )); - $this->_sitemapItems[] = new \Magento\Framework\DataObject( + $this->addSitemapItem(new DataObject( [ 'changefreq' => $helper->getPageChangefreq($storeId), 'priority' => $helper->getPagePriority($storeId), 'collection' => $this->_cmsFactory->create()->getCollection($storeId), ] - ); + )); + } + + /** + * Initialize sitemap + * + * @return void + */ + protected function _initSitemapItems() + { + $this->collectSitemapItems(); $this->_tags = [ self::TYPE_INDEX => [ self::OPEN_TAG_KEY => '' . - PHP_EOL . - '' . - PHP_EOL, + PHP_EOL . + '' . + PHP_EOL, self::CLOSE_TAG_KEY => '', ], self::TYPE_URL => [ self::OPEN_TAG_KEY => '' . - PHP_EOL . - '' . - PHP_EOL, + PHP_EOL . + '' . + PHP_EOL, self::CLOSE_TAG_KEY => '', ], ]; @@ -341,6 +366,7 @@ public function beforeSave() public function generateXml() { $this->_initSitemapItems(); + /** @var $sitemapItem \Magento\Framework\DataObject */ foreach ($this->_sitemapItems as $sitemapItem) { $changefreq = $sitemapItem->getChangefreq();