Skip to content

Commit

Permalink
Merge pull request #432 from magefan/6721-Magefan-Blog-Sitemap
Browse files Browse the repository at this point in the history
6721-Magefan-Blog-Sitemap
  • Loading branch information
magefan authored Jul 5, 2022
2 parents 71bb7b8 + a2225e3 commit 3fc01d1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
1 change: 0 additions & 1 deletion Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function getSitemapPath(): string

return rtrim($serverPath, '/') . $path;
}

return $path;
}
}
125 changes: 125 additions & 0 deletions Plugin/Magento/Sitemap/AmastySitemapPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/

namespace Magefan\Blog\Plugin\Magento\Sitemap;

use Magefan\Blog\Model\CategoryFactory;
use Magefan\Blog\Model\PostFactory;

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

/**
* @var CategoryFactory
*/
protected $categoryFactory;

/**
* @var PostFactory
*/
protected $postFactory;

/**
* @var mixed
*/
protected $config;

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

/**
* SitemapPlugin constructor.
* @param \Magefan\Blog\Model\SitemapFactory $sitemapFactory
* @param CategoryFactory $categoryFactory
* @param PostFactory $postFactory
* @param null|\Magefan\Blog\Model\Config config
*/
public function __construct(
\Magefan\Blog\Model\SitemapFactory $sitemapFactory,
CategoryFactory $categoryFactory,
PostFactory $postFactory,
\Magefan\Blog\Model\Config $config
) {
$this->postFactory = $postFactory;
$this->categoryFactory = $categoryFactory;
$this->sitemapFactory = $sitemapFactory;
$this->config = $config;
}

/**
* @param $subject
* @param $result
* @param $sitemap
* @return mixed
*/
public function afterGenerate($subject, $result, $sitemap) {

$sitemapId = $sitemap->getId() ?: 0;
if (in_array($sitemapId, $this->generated)) {
return $result;
}
$this->generated[] = $sitemapId;

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

if (!$blogSitemap->getSitemapId() && $sitemap->getId()) {
$blogSitemap->setSitemapId($sitemap->getId());
}

/* Fix for Amasty\XmlSitemap\Model\Sitemap */
if ($sitemap instanceof \Amasty\XmlSitemap\Api\SitemapInterface ) {
if ($sitemap->getFilePath()) {
$filepath = $sitemap->getFilePath();
$pathArray = explode('/', $filepath);
$filename = end($pathArray);
$blogFilepath = str_replace($filename, '', $filepath);
$blogFilepath = str_replace('pub/', '', $blogFilepath);
$blogSitemap->setSitemapFilename('blog_sitemap.xml');
$blogSitemap->setSitemapPath($blogFilepath);
}
$blogSitemap->generateXml();
}

return $result;
}

/**
* Deprecated
* @param \Magento\Framework\Model\AbstractModel $sitemap
* @param $result
* @return mixed
*/
public function afterCollectSitemapItems(\Magento\Framework\Model\AbstractModel $sitemap, $result)
{
return $result;
}

/**
* @param $sitemap
* @return mixed
*/
protected function isEnabled($sitemap)
{
return $this->config->isEnabled(
$sitemap->getStoreId()
);
}
}
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
type="Magefan\Blog\Plugin\Magento\Sitemap\SitemapPlugin" sortOrder="10"/>
</type>

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

<type name="Magento\AdminGws\Model\Models">
<plugin name="magefan_blog_plugin_admingws_model_models_plugin" type="\Magefan\Blog\Plugin\Magento\AdminGws\Model\ModelsPlugin" sortOrder="10"/>
</type>
Expand Down

0 comments on commit 3fc01d1

Please sign in to comment.