Skip to content

Commit

Permalink
Remove Sitemap class override, added plugin instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Jul 2, 2017
1 parent f423b0b commit 8b2fda4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Sitemap extends \Magento\Sitemap\Model\Sitemap
protected function _initSitemapItems()
{
parent::_initSitemapItems();
$this->_sitemapItems = [];

$this->_sitemapItems[] = new \Magento\Framework\DataObject(
[
Expand All @@ -47,4 +48,13 @@ protected function _initSitemapItems()
);
}

/**
* Disable save action
* @return sekf

This comment has been minimized.

Copy link
@Koc

Koc Jul 10, 2017

Contributor

typo. Also better use return $this when returns same instance.

*/
public function save()
{
return $this;
}

}
60 changes: 60 additions & 0 deletions Plugin/Magento/Sitemap/SitemapPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/

namespace Magefan\Blog\Plugin\Magento\Sitemap;

/**
* Plugin for sitemap generation
*/
class SitemapPlugin
{
/**
* @var \Magefan\Blog\Model\SitemapFactory
*/
protected $sitemapFactory;

/**
* Generated sitemaps
* @var array
*/
protected $generated = [];

public function __construct(
\Magefan\Blog\Model\SitemapFactory $sitemapFactory
) {
$this->sitemapFactory = $sitemapFactory;
}

/**
* Add magefan blog actions to allowed list
* @param \Magento\Sitemap\Model\Sitemap $sitemap
* @return array
*/
public function afterGenerateXml(\Magento\Sitemap\Model\Sitemap $sitemap, $result)
{
$sitemapId = $sitemap->getId() ?: 0;
if (in_array($sitemapId, $this->generated)) {
return $result;
}
$this->generated[] = $sitemapId;

$blogSitemap = $this->sitemapFactory->create();
$blogSitemap->setData(
$sitemap->getData()
);

$blogSitemap->setSitemapFilename(
'blog_' . $sitemap->getSitemapFilename()
);

$blogSitemap->generateXml();

return $result;
}

}
7 changes: 6 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magefan\Blog\Api\PostManagementInterface" type="Magefan\Blog\Model\PostManagement" />
<preference for="Magefan\Blog\Api\CategoryManagementInterface" type="Magefan\Blog\Model\CategoryManagement" />
<preference for="Magento\Sitemap\Model\Sitemap" type="Magefan\Blog\Model\Sitemap" />
<!-- deprecated <preference for="Magento\Sitemap\Model\Sitemap" type="Magefan\Blog\Model\Sitemap" /> -->
<virtualType name="Magefan\Blog\ImageUpload" type="Magento\Catalog\Model\ImageUploader">
<arguments>
<argument name="baseTmpPath" xsi:type="string">magefan_blog/tmp</argument>
Expand Down Expand Up @@ -47,4 +47,9 @@
<plugin name="plumrocket_amp_source_page_magefan_blog"
type="Magefan\Blog\Plugin\Plumrocket\Amp\SourcePagePlugin" sortOrder="10"/>
</type>

<type name="Magento\Sitemap\Model\Sitemap">
<plugin name="magento_sitemap_model_sitemap_magefan_blog"
type="Magefan\Blog\Plugin\Magento\Sitemap\SitemapPlugin" sortOrder="10"/>
</type>
</config>

1 comment on commit 8b2fda4

@Koc
Copy link
Contributor

@Koc Koc commented on 8b2fda4 Jul 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.