Skip to content

Commit f0abaa9

Browse files
authored
Merge pull request #223 from magefan/1694-sitemap-in-M2.3.0-v2
1694 sitemap in m2.3.0 v2
2 parents 8c4e196 + 2daf9cf commit f0abaa9

File tree

5 files changed

+185
-27
lines changed

5 files changed

+185
-27
lines changed

Api/SitemapConfigInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). 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+
9+
namespace Magefan\Blog\Api;
10+
11+
interface SitemapConfigInterface
12+
{
13+
const HOME_PAGE = 'index';
14+
const CATEGORIES_PAGE = 'category';
15+
const POSTS_PAGE = 'post';
16+
/**
17+
* @param $page
18+
* @return bool
19+
*/
20+
public function isEnabledSitemap($page);
21+
22+
/**
23+
* @param $page
24+
* @return string
25+
*/
26+
public function getFrequency($page);
27+
28+
/**
29+
* @param $page
30+
* @return float
31+
*/
32+
public function getPriority($page);
33+
34+
}

Model/Sitemap.php

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Magefan\Blog\Model;
1010

11+
use Magento\Framework\App\ProductMetadataInterface;
12+
use Magefan\Blog\Api\SitemapConfigInterface;
13+
1114
/**
1215
* Deprecated
1316
* Used for Magento 2.1.x only to create blog_sitemap.xml
@@ -22,32 +25,81 @@ class Sitemap extends \Magento\Sitemap\Model\Sitemap
2225
*/
2326
protected function _initSitemapItems()
2427
{
28+
29+
30+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
31+
32+
$megento = $objectManager->create(ProductMetadataInterface::class);
33+
$sitemapConfig = $objectManager->create(SitemapConfigInterface::class);
34+
35+
2536
parent::_initSitemapItems();
26-
$this->_sitemapItems = [];
27-
28-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
29-
[
30-
'changefreq' => 'weekly',
31-
'priority' => '0.25',
32-
'collection' => \Magento\Framework\App\ObjectManager::getInstance()->create(
33-
\Magefan\Blog\Model\Category::class
34-
)->getCollection($this->getStoreId())
35-
->addStoreFilter($this->getStoreId())
36-
->addActiveFilter(),
37-
]
38-
);
39-
40-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
41-
[
42-
'changefreq' => 'weekly',
43-
'priority' => '0.25',
44-
'collection' => \Magento\Framework\App\ObjectManager::getInstance()->create(
45-
\Magefan\Blog\Model\Post::class
46-
)->getCollection($this->getStoreId())
47-
->addStoreFilter($this->getStoreId())
48-
->addActiveFilter(),
49-
]
50-
);
37+
38+
$sitemapItems = [];
39+
if ($sitemapConfig->isEnabledSitemap(SitemapConfigInterface::HOME_PAGE)) {
40+
$sitemapItems[] = new \Magento\Framework\DataObject(
41+
[
42+
'changefreq' => $sitemapConfig->getFrequency(SitemapConfigInterface::HOME_PAGE),
43+
'priority' => $sitemapConfig->getPriority(SitemapConfigInterface::HOME_PAGE),
44+
'collection' => \Magento\Framework\App\ObjectManager::getInstance()->create(
45+
\Magento\Framework\Data\Collection::class
46+
)->addItem(
47+
\Magento\Framework\App\ObjectManager::getInstance()->create(
48+
\Magento\Framework\DataObject::class
49+
)->setData([
50+
'updated_at' => '',
51+
'url' => '',
52+
])
53+
)
54+
]
55+
);
56+
}
57+
58+
if ($sitemapConfig->isEnabledSitemap(SitemapConfigInterface::CATEGORIES_PAGE)) {
59+
$sitemapItems[] = new \Magento\Framework\DataObject(
60+
[
61+
'changefreq' => $sitemapConfig->getFrequency(SitemapConfigInterface::CATEGORIES_PAGE),
62+
'priority' => $sitemapConfig->getPriority(SitemapConfigInterface::CATEGORIES_PAGE),
63+
'collection' => \Magento\Framework\App\ObjectManager::getInstance()->create(
64+
\Magefan\Blog\Model\Category::class
65+
)->getCollection($this->getStoreId())
66+
->addStoreFilter($this->getStoreId())
67+
->addActiveFilter(),
68+
]
69+
);
70+
}
71+
72+
if ($sitemapConfig->isEnabledSitemap(SitemapConfigInterface::POSTS_PAGE)) {
73+
$sitemapItems[] = new \Magento\Framework\DataObject(
74+
[
75+
'changefreq' => $sitemapConfig->getFrequency(SitemapConfigInterface::POSTS_PAGE),
76+
'priority' => $sitemapConfig->getPriority(SitemapConfigInterface::POSTS_PAGE),
77+
'collection' => \Magento\Framework\App\ObjectManager::getInstance()->create(
78+
\Magefan\Blog\Model\Post::class
79+
)->getCollection($this->getStoreId())
80+
->addStoreFilter($this->getStoreId())
81+
->addActiveFilter(),
82+
]
83+
);
84+
}
85+
86+
if (version_compare($megento->getVersion(), '2.3.0', '<')) {
87+
$this->_sitemapItems = $sitemapItems;
88+
} else {
89+
$this->_sitemapItems = [];
90+
foreach ($sitemapItems as $sitemapItem) {
91+
foreach ($sitemapItem->getCollection() as $item) {
92+
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
93+
[
94+
'url' => $item->getUrl(),
95+
'updated_at' => '',
96+
'priority' => $sitemapItem->getData('priority'),
97+
'change_frequency' => $sitemapItem->getData('changefreq'),
98+
]
99+
);
100+
}
101+
}
102+
}
51103
}
52104

53105
/**

Model/Sitemap/SitemapConfig.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). 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+
9+
namespace Magefan\Blog\Model\Sitemap;
10+
11+
use Magefan\Blog\Api\SitemapConfigInterface;
12+
13+
class SitemapConfig implements SitemapConfigInterface
14+
{
15+
16+
/**
17+
* @param $page
18+
* @return bool
19+
*/
20+
public function isEnabledSitemap($page)
21+
{
22+
return true;
23+
}
24+
25+
/**
26+
* @param $page
27+
* @return string
28+
*/
29+
public function getFrequency($page)
30+
{
31+
switch ($page) {
32+
case 'index':
33+
$frequency = 'Dailly';
34+
break;
35+
case 'category':
36+
$frequency = 'Dailly';
37+
break;
38+
case 'post':
39+
$frequency = 'Dailly';
40+
break;
41+
default:
42+
$frequency = 'Dailly';
43+
}
44+
return $frequency;
45+
}
46+
47+
/**
48+
* @param $page
49+
* @return float
50+
*/
51+
public function getPriority($page)
52+
{
53+
54+
switch ($page) {
55+
case 'index':
56+
$priority = 1;
57+
break;
58+
case 'category':
59+
$priority = 0.75;
60+
break;
61+
case 'post':
62+
$priority = 0.5;
63+
break;
64+
default:
65+
$priority = 0.25;
66+
}
67+
return $priority;
68+
}
69+
}

Plugin/Magento/Sitemap/SitemapPlugin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(
7575
public function afterGenerateXml(\Magento\Framework\Model\AbstractModel $sitemap, $result)
7676
{
7777
if ($this->isEnabled($sitemap)) {
78-
if ($this->isMageWorxXmlSitemap($sitemap) || !method_exists($sitemap, 'collectSitemapItems')) {
78+
/* if ($this->isMageWorxXmlSitemap($sitemap) || !method_exists($sitemap, 'collectSitemapItems')) { */
7979
$sitemapId = $sitemap->getId() ?: 0;
8080
if (in_array($sitemapId, $this->generated)) {
8181
return $result;
@@ -92,7 +92,7 @@ public function afterGenerateXml(\Magento\Framework\Model\AbstractModel $sitemap
9292
);
9393

9494
$blogSitemap->generateXml();
95-
}
95+
/*}*/
9696
}
9797
return $result;
9898
}
@@ -102,6 +102,7 @@ public function afterGenerateXml(\Magento\Framework\Model\AbstractModel $sitemap
102102
* @param $result
103103
* @return mixed
104104
*/
105+
/* Deprecated
105106
public function afterCollectSitemapItems(\Magento\Framework\Model\AbstractModel $sitemap, $result)
106107
{
107108
if ($this->isEnabled($sitemap) && !$this->isMageWorxXmlSitemap($sitemap)) {
@@ -132,6 +133,7 @@ public function afterCollectSitemapItems(\Magento\Framework\Model\AbstractModel
132133
133134
return $result;
134135
}
136+
*/
135137

136138
/**
137139
* @param $sitemap

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-->
1010
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1111

12+
<preference for="Magefan\Blog\Api\SitemapConfigInterface" type="Magefan\Blog\Model\Sitemap\SitemapConfig" />
1213
<preference for="Magefan\Blog\Api\PostManagementInterface" type="Magefan\Blog\Model\PostManagement" />
1314
<preference for="Magefan\Blog\Api\CategoryManagementInterface" type="Magefan\Blog\Model\CategoryManagement" />
1415
<!-- deprecated <preference for="Magento\Sitemap\Model\Sitemap" type="Magefan\Blog\Model\Sitemap" /> -->

0 commit comments

Comments
 (0)