Skip to content

Commit

Permalink
6721-Magefan-Blog-Sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
KozakSerhii committed Jul 1, 2022
1 parent de62e64 commit 81c4cbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 89 deletions.
76 changes: 0 additions & 76 deletions Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,6 @@
*/
class Sitemap extends \Magento\Sitemap\Model\Sitemap
{
private $state;

public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Escaper $escaper,
\Magento\Sitemap\Helper\Data $sitemapData,
\Magento\Framework\Filesystem $filesystem,
\Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory $categoryFactory,
\Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory $productFactory,
\Magento\Sitemap\Model\ResourceModel\Cms\PageFactory $cmsFactory,
\Magento\Framework\Stdlib\DateTime\DateTime $modelDate,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\App\State $state,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null,
ItemProviderInterface $itemProvider = null,
SitemapConfigReaderInterface $configReader = null,
\Magento\Sitemap\Model\SitemapItemInterfaceFactory $sitemapItemFactory = null
) {
parent::__construct($context, $registry, $escaper, $sitemapData, $filesystem, $categoryFactory, $productFactory, $cmsFactory, $modelDate, $storeManager, $request, $dateTime, $resource, $resourceCollection, $data, $documentRoot, $itemProvider, $configReader, $sitemapItemFactory);
$this->state = $state;
}

/**
* Initialize sitemap items
*
Expand Down Expand Up @@ -175,52 +147,4 @@ public function getSitemapPath(): string
}
return $path;
}

public function generateXml()
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
$this->_initSitemapItems();

/** @var $item SitemapItemInterface */
foreach ($this->_sitemapItems as $item) {
$xml = $this->_getSitemapRow(
$item->getUrl(),
$item->getUpdatedAt(),
$item->getChangeFrequency(),
$item->getPriority(),
$item->getImages()
);

if ($this->_isSplitRequired($xml) && $this->_sitemapIncrement > 0) {
$this->_finalizeSitemap();
}

if (!$this->_fileSize) {
$this->_createSitemap();
}

$this->_writeSitemapRow($xml);
// Increase counters
$this->_lineCount++;
$this->_fileSize += strlen($xml);
}

$this->_finalizeSitemap();

if ($this->_sitemapIncrement == 1) {
// In case when only one increment file was created use it as default sitemap
$path = rtrim($this->getSitemapPath(), '/')
. '/'
. $this->_getCurrentSitemapFilename($this->_sitemapIncrement);
$this->setSitemapPath($path);
} else {
// Otherwise create index file with list of generated sitemaps
$this->_createSitemapIndex();
}

$this->setSitemapTime($this->_dateModel->gmtDate('Y-m-d H:i:s'));
$this->save();

return $this;
}
}
20 changes: 11 additions & 9 deletions Plugin/Magento/Sitemap/AmastySitemapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

use Magefan\Blog\Model\CategoryFactory;
use Magefan\Blog\Model\PostFactory;
use Magento\Framework\DataObject;
use Magento\Sitemap\Model\Sitemap;

/**
* Plugin for sitemap generation
Expand Down Expand Up @@ -55,17 +53,21 @@ public function __construct(
\Magefan\Blog\Model\SitemapFactory $sitemapFactory,
CategoryFactory $categoryFactory,
PostFactory $postFactory,
$config = null
\Magefan\Blog\Model\Config $config
) {
$this->postFactory = $postFactory;
$this->categoryFactory = $categoryFactory;
$this->sitemapFactory = $sitemapFactory;

$this->config = $config ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magefan\Blog\Model\Config::class);
$this->config = $config;
}

public function afterGenerate(\Amasty\XmlSitemap\Model\XmlGenerator $subject, $result, $sitemap) {
/**
* @param $subject
* @param $result
* @param $sitemap
* @return mixed
*/
public function afterGenerate($subject, $result, $sitemap) {

$sitemapId = $sitemap->getId() ?: 0;
if (in_array($sitemapId, $this->generated)) {
Expand All @@ -83,7 +85,7 @@ public function afterGenerate(\Amasty\XmlSitemap\Model\XmlGenerator $subject, $r
}

/* Fix for Amasty\XmlSitemap\Model\Sitemap */
if (get_class($sitemap) === 'Amasty\XmlSitemap\Model\Sitemap') {
if ($sitemap instanceof \Amasty\XmlSitemap\Api\SitemapInterface ) {
if ($sitemap->getFilePath()) {
$filepath = $sitemap->getFilePath();
$pathArray = explode('/', $filepath);
Expand All @@ -93,8 +95,8 @@ public function afterGenerate(\Amasty\XmlSitemap\Model\XmlGenerator $subject, $r
$blogSitemap->setSitemapFilename('blog_sitemap.xml');
$blogSitemap->setSitemapPath($blogFilepath);
}
$blogSitemap->generateXml();
}
$blogSitemap->generateXml();

return $result;
}
Expand Down
8 changes: 4 additions & 4 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
type="Magefan\Blog\Plugin\Magento\Sitemap\SitemapPlugin" sortOrder="10"/>
</type>

<!-- <type name="Amasty\XmlSitemap\Model\Sitemap">-->
<!-- <plugin name="amasty_sitemap_model_sitemap_magefan_blog"-->
<!-- type="Magefan\Blog\Plugin\Magento\Sitemap\SitemapPlugin" sortOrder="10"/>-->
<!-- </type>-->
<type name="Amasty\XmlSitemap\Model\Sitemap">
<plugin name="amasty_sitemap_model_sitemap_magefan_blog"
type="Magefan\Blog\Plugin\Magento\Sitemap\SitemapPlugin" sortOrder="10"/>
</type>

<type name="Amasty\XmlSitemap\Model\XmlGenerator">
<plugin name="amasty_sitemap_model_sitemap_magefan_blog"
Expand Down

0 comments on commit 81c4cbc

Please sign in to comment.