SitemapPlugin.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Plugin\Magento\Sitemap;
  9. /**
  10. * Plugin for sitemap generation
  11. */
  12. class SitemapPlugin
  13. {
  14. /**
  15. * @var \Magefan\Blog\Model\SitemapFactory
  16. */
  17. protected $sitemapFactory;
  18. /**
  19. * Generated sitemaps
  20. * @var array
  21. */
  22. protected $generated = [];
  23. public function __construct(
  24. \Magefan\Blog\Model\SitemapFactory $sitemapFactory
  25. ) {
  26. $this->sitemapFactory = $sitemapFactory;
  27. }
  28. /**
  29. * Add magefan blog actions to allowed list
  30. * @param \Magento\Sitemap\Model\Sitemap $sitemap
  31. * @return array
  32. */
  33. public function afterGenerateXml(\Magento\Sitemap\Model\Sitemap $sitemap, $result)
  34. {
  35. $sitemapId = $sitemap->getId() ?: 0;
  36. if (in_array($sitemapId, $this->generated)) {
  37. return $result;
  38. }
  39. $this->generated[] = $sitemapId;
  40. $blogSitemap = $this->sitemapFactory->create();
  41. $blogSitemap->setData(
  42. $sitemap->getData()
  43. );
  44. $blogSitemap->setSitemapFilename(
  45. 'blog_' . $sitemap->getSitemapFilename()
  46. );
  47. $blogSitemap->generateXml();
  48. return $result;
  49. }
  50. }