Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 6ed480b

Browse files
Merge pull request #6 from mageplaza/develop
Develop
2 parents c9b4316 + 772db30 commit 6ed480b

File tree

14 files changed

+173
-79
lines changed

14 files changed

+173
-79
lines changed

Block/Sitemap.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
2929
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
3030
use Magento\CatalogInventory\Helper\Stock;
31+
use Magento\Cms\Model\Page;
3132
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
33+
use Magento\Framework\Exception\NoSuchEntityException;
3234
use Magento\Framework\View\Element\Template;
3335
use Magento\Framework\View\Element\Template\Context;
3436
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
@@ -42,22 +44,22 @@ class Sitemap extends Template
4244
const DEFAULT_PRODUCT_LIMIT = 100;
4345

4446
/**
45-
* @var \Magento\Catalog\Helper\Category
47+
* @var Category
4648
*/
4749
protected $_categoryHelper;
4850

4951
/**
50-
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
52+
* @var CollectionFactory
5153
*/
5254
protected $_categoryCollection;
5355

5456
/**
55-
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
57+
* @var Collection
5658
*/
5759
protected $collection;
5860

5961
/**
60-
* @var \Magento\Catalog\Model\CategoryRepository
62+
* @var CategoryRepository
6163
*/
6264
protected $categoryRepository;
6365

@@ -67,22 +69,22 @@ class Sitemap extends Template
6769
protected $_helper;
6870

6971
/**
70-
* @var \Magento\CatalogInventory\Helper\Stock
72+
* @var Stock
7173
*/
7274
protected $_stockFilter;
7375

7476
/**
75-
* @var \Magento\Catalog\Model\Product\Visibility
77+
* @var ProductVisibility
7678
*/
7779
protected $productVisibility;
7880

7981
/**
80-
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
82+
* @var ProductCollection
8183
*/
8284
protected $productCollection;
8385

8486
/**
85-
* @var \Magento\Cms\Model\ResourceModel\Page\Collection
87+
* @var PageCollection
8688
*/
8789
protected $pageCollection;
8890

@@ -131,7 +133,7 @@ public function __construct(
131133
*/
132134
public function getProductCollection()
133135
{
134-
$limit = $this->_helper->getProductLimit() ? $this->_helper->getProductLimit() : self::DEFAULT_PRODUCT_LIMIT;
136+
$limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
135137
$collection = $this->productCollection
136138
->setVisibility($this->productVisibility->getVisibleInCatalogIds())
137139
->addMinimalPrice()
@@ -150,14 +152,14 @@ public function getProductCollection()
150152
*/
151153
public function getCategoryCollection()
152154
{
153-
return $this->_categoryHelper->getStoreCategories(false, true, true);
155+
return $this->_categoryHelper->getStoreCategories(false, true);
154156
}
155157

156158
/**
157159
* @param $categoryId
158160
*
159161
* @return string
160-
* @throws \Magento\Framework\Exception\NoSuchEntityException
162+
* @throws NoSuchEntityException
161163
*/
162164
public function getCategoryUrl($categoryId)
163165
{
@@ -170,7 +172,7 @@ public function getCategoryUrl($categoryId)
170172
*/
171173
public function getPageCollection()
172174
{
173-
return $this->pageCollection->addFieldToFilter('is_active', \Magento\Cms\Model\Page::STATUS_ENABLED)
175+
return $this->pageCollection->addFieldToFilter('is_active', Page::STATUS_ENABLED)
174176
->addFieldToFilter('identifier', [
175177
'nin' => $this->getExcludedPages()
176178
]);
@@ -228,7 +230,7 @@ public function renderLinkElement($link, $title)
228230
* @param $collection
229231
*
230232
* @return string
231-
* @throws \Magento\Framework\Exception\NoSuchEntityException
233+
* @throws NoSuchEntityException
232234
*/
233235
public function renderSection($section, $config, $title, $collection)
234236
{
@@ -245,7 +247,7 @@ public function renderSection($section, $config, $title, $collection)
245247
break;
246248
case 'page':
247249
if (in_array($item->getIdentifier(), $this->getExcludedPages())) {
248-
continue;
250+
continue 2;
249251
}
250252
$html .= $this->renderLinkElement($this->getUrl($item->getIdentifier()), $item->getTitle());
251253
break;
@@ -267,15 +269,35 @@ public function renderSection($section, $config, $title, $collection)
267269

268270
/**
269271
* @return string
270-
* @throws \Magento\Framework\Exception\NoSuchEntityException
272+
* @throws NoSuchEntityException
271273
*/
272274
public function renderHtmlSitemap()
273275
{
274276
$htmlSitemap = '';
275-
$htmlSitemap .= $this->renderSection('category', $this->_helper->isEnableCategorySitemap(), 'Categories', $this->getCategoryCollection());
276-
$htmlSitemap .= $this->renderSection('page', $this->_helper->iisEnablePageSitemap(), 'Pages', $this->getPageCollection());
277-
$htmlSitemap .= $this->renderSection('product', $this->_helper->isEnableProductSitemap(), 'Products', $this->getProductCollection());
278-
$htmlSitemap .= $this->renderSection('link', $this->_helper->isEnableAddLinksSitemap(), 'Additional links', $this->getAdditionLinksCollection());
277+
$htmlSitemap .= $this->renderSection(
278+
'category',
279+
$this->_helper->isEnableCategorySitemap(),
280+
'Categories',
281+
$this->getCategoryCollection()
282+
);
283+
$htmlSitemap .= $this->renderSection(
284+
'page',
285+
$this->_helper->isEnablePageSitemap(),
286+
'Pages',
287+
$this->getPageCollection()
288+
);
289+
$htmlSitemap .= $this->renderSection(
290+
'product',
291+
$this->_helper->isEnableProductSitemap(),
292+
'Products',
293+
$this->getProductCollection()
294+
);
295+
$htmlSitemap .= $this->renderSection(
296+
'link',
297+
$this->_helper->isEnableAddLinksSitemap(),
298+
'Additional links',
299+
$this->getAdditionLinksCollection()
300+
);
279301

280302
return $htmlSitemap;
281303
}

Controller/Index/Index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Magento\Framework\App\Action\Action;
2525
use Magento\Framework\App\Action\Context;
2626
use Magento\Framework\Exception\NotFoundException;
27+
use Magento\Framework\View\Result\Page;
2728
use Magento\Framework\View\Result\PageFactory;
2829
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
2930

@@ -34,7 +35,7 @@
3435
class Index extends Action
3536
{
3637
/**
37-
* @var \Magento\Framework\View\Result\PageFactory
38+
* @var PageFactory
3839
*/
3940
protected $pageFactory;
4041

@@ -59,8 +60,8 @@ public function __construct(Context $context, PageFactory $pageFactory, HelperCo
5960
}
6061

6162
/**
62-
* @return \Magento\Framework\View\Result\Page
63-
* @throws \Magento\Framework\Exception\NotFoundException
63+
* @return Page
64+
* @throws NotFoundException
6465
*/
6566
public function execute()
6667
{

Helper/Data.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function isEnableHtmlSiteMap($storeId = null)
5252
*/
5353
public function getHtmlSitemapConfig($code, $storeId = null)
5454
{
55-
return $this->getConfigValue(self::CONFIG_MODULE_PATH . '/' . self::HTML_SITEMAP_CONFIGUARATION . $code, $storeId);
55+
return $this->getConfigValue(
56+
self::CONFIG_MODULE_PATH . '/' . self::HTML_SITEMAP_CONFIGUARATION . $code,
57+
$storeId
58+
);
5659
}
5760

5861
/**
@@ -68,7 +71,7 @@ public function isEnableCategorySitemap()
6871
* Is enable page site map
6972
* @return mixed
7073
*/
71-
public function iisEnablePageSitemap()
74+
public function isEnablePageSitemap()
7275
{
7376
return $this->getHtmlSitemapConfig('page');
7477
}
@@ -136,7 +139,10 @@ public function getProductLimit()
136139
*/
137140
public function getXmlSitemapConfig($code, $storeId = null)
138141
{
139-
return $this->getConfigValue(self::CONFIG_MODULE_PATH . '/' . self::XML_SITEMAP_CONFIGUARATION . $code, $storeId);
142+
return $this->getConfigValue(
143+
self::CONFIG_MODULE_PATH . '/' . self::XML_SITEMAP_CONFIGUARATION . $code,
144+
$storeId
145+
);
140146
}
141147

142148
/**

Model/Sitemap.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121

2222
namespace Mageplaza\Sitemap\Model;
2323

24+
use Exception;
2425
use Magento\Catalog\Model\CategoryFactory;
2526
use Magento\Catalog\Model\ProductFactory;
2627
use Magento\CatalogInventory\Model\Stock\Item;
2728
use Magento\Cms\Model\PageFactory;
2829
use Magento\Framework\App\ObjectManager;
2930
use Magento\Framework\App\RequestInterface;
3031
use Magento\Framework\Data\Collection\AbstractDb;
32+
use Magento\Framework\DataObject;
3133
use Magento\Framework\Escaper;
34+
use Magento\Framework\Exception\LocalizedException;
3235
use Magento\Framework\Filesystem;
3336
use Magento\Framework\Model\Context;
3437
use Magento\Framework\Model\ResourceModel\AbstractResource;
@@ -55,17 +58,17 @@ class Sitemap extends CoreSitemap
5558
const HOMEPAGE_PATH = 'web/default/cms_home_page';
5659

5760
/**
58-
* @var \Magento\Catalog\Model\CategoryFactory
61+
* @var CategoryFactory
5962
*/
6063
protected $_coreCategoryFactory;
6164

6265
/**
63-
* @var \Magento\Catalog\Model\ProductFactory
66+
* @var ProductFactory
6467
*/
6568
protected $_coreProductFactory;
6669

6770
/**
68-
* @var \Magento\Cms\Model\PageFactory
71+
* @var PageFactory
6972
*/
7073
protected $_corePageFactory;
7174

@@ -75,7 +78,7 @@ class Sitemap extends CoreSitemap
7578
protected $helperConfig;
7679

7780
/**
78-
* @var \Magento\CatalogInventory\Model\Stock\Item
81+
* @var Item
7982
*/
8083
protected $stockItem;
8184

@@ -159,7 +162,7 @@ public function _initSitemapItems()
159162
$helper = $this->_sitemapData;
160163
$storeId = $this->getStoreId();
161164
$this->_sitemapItems = null;
162-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
165+
$this->_sitemapItems[] = new DataObject(
163166
[
164167
'changefreq' => $helper->getCategoryChangefreq($storeId),
165168
'priority' => $helper->getCategoryPriority($storeId),
@@ -168,7 +171,7 @@ public function _initSitemapItems()
168171
]
169172
);
170173

171-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
174+
$this->_sitemapItems[] = new DataObject(
172175
[
173176
'changefreq' => $helper->getProductChangefreq($storeId),
174177
'priority' => $helper->getProductPriority($storeId),
@@ -177,7 +180,7 @@ public function _initSitemapItems()
177180
]
178181
);
179182

180-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
183+
$this->_sitemapItems[] = new DataObject(
181184
[
182185
'changefreq' => $helper->getPageChangefreq($storeId),
183186
'priority' => $helper->getPagePriority($storeId),
@@ -187,7 +190,7 @@ public function _initSitemapItems()
187190
);
188191

189192
if ($this->helperConfig->isEnableAdditionalLinks($storeId)) {
190-
$this->_sitemapItems[] = new \Magento\Framework\DataObject(
193+
$this->_sitemapItems[] = new DataObject(
191194
[
192195
'changefreq' => $this->helperConfig->getFrequency($storeId),
193196
'priority' => $this->helperConfig->getPriority($storeId),
@@ -200,13 +203,13 @@ public function _initSitemapItems()
200203

201204
/**
202205
* @return $this
203-
* @throws \Exception
204-
* @throws \Magento\Framework\Exception\LocalizedException
206+
* @throws Exception
207+
* @throws LocalizedException
205208
*/
206209
public function generateXml()
207210
{
208211
$this->_initSitemapItems();
209-
/** @var $sitemapItem \Magento\Framework\DataObject */
212+
/** @var $sitemapItem DataObject */
210213
foreach ($this->_sitemapItems as $item) {
211214
$changefreq = $item->getChangefreq();
212215
$priority = $item->getPriority();
@@ -236,12 +239,8 @@ public function generateXml()
236239

237240
if ($this->_sitemapIncrement == 1) {
238241
// In case when only one increment file was created use it as default sitemap
239-
$path = rtrim(
240-
$this->getSitemapPath(),
241-
'/'
242-
) . '/' . $this->_getCurrentSitemapFilename(
243-
$this->_sitemapIncrement
244-
);
242+
$path = rtrim($this->getSitemapPath(), '/') . '/'
243+
. $this->_getCurrentSitemapFilename($this->_sitemapIncrement);
245244
$destination = rtrim($this->getSitemapPath(), '/') . '/' . $this->getSitemapFilename();
246245

247246
$this->_directory->renameFile($path, $destination);
@@ -273,8 +272,14 @@ public function generateXml()
273272
*
274273
* @return string
275274
*/
276-
protected function getSitemapRow($url, $urlType, $lastmod = null, $changefreq = null, $priority = null, $images = null)
277-
{
275+
protected function getSitemapRow(
276+
$url,
277+
$urlType,
278+
$lastmod = null,
279+
$changefreq = null,
280+
$priority = null,
281+
$images = null
282+
) {
278283
if ($urlType == self::URL) {
279284
$url = $this->_getUrl($url);
280285
} else {
@@ -304,9 +309,8 @@ protected function getSitemapRow($url, $urlType, $lastmod = null, $changefreq =
304309
// Add PageMap image for Google web search
305310
$row .= '<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
306311
$row .= '<Attribute name="name" value="' . htmlspecialchars($images->getTitle()) . '"/>';
307-
$row .= '<Attribute name="src" value="' . htmlspecialchars(
308-
$this->_getMediaUrl($images->getThumbnail())
309-
) . '"/>';
312+
$row .= '<Attribute name="src" value="' . htmlspecialchars($this->_getMediaUrl($images->getThumbnail()))
313+
. '"/>';
310314
$row .= '</DataObject></PageMap>';
311315
}
312316

@@ -325,8 +329,8 @@ public function getLinkCollectionAdded($storeId)
325329
$id = 1;
326330
$collection = [];
327331
foreach ($this->helperConfig->getXmlAdditionalLinks($storeId) as $item) {
328-
if ($item != null) {
329-
$obj = ObjectManager::getInstance()->create('\Magento\Framework\DataObject');
332+
if ($item !== null) {
333+
$obj = ObjectManager::getInstance()->create(\Magento\Framework\DataObject::class);
330334
$obj->setData('id', $id++);
331335
$obj->setData('url', $item);
332336
$obj->setData('updated_at', $this->getSitemapTime());
@@ -347,6 +351,7 @@ public function getLinkCollectionAdded($storeId)
347351
public function _getCategoryCollection($storeId)
348352
{
349353
$collection = [];
354+
350355
foreach ($this->_categoryFactory->create()->getCollection($storeId) as $item) {
351356
if ($this->_coreCategoryFactory->create()->load($item->getId())->getData('mp_exclude_sitemap') == 1) {
352357
continue;
@@ -428,12 +433,7 @@ public function convertUrl($url)
428433
*/
429434
public function optimizeHomepage($storeId, $page)
430435
{
431-
if ($this->helperConfig->isEnableHomepageOptimization($storeId) == 1) {
432-
if ($this->helperConfig->getConfigValue(self::HOMEPAGE_PATH, $storeId) == $page->getUrl()) {
433-
return true;
434-
}
435-
}
436-
437-
return false;
436+
return $this->helperConfig->isEnableHomepageOptimization($storeId) == 1
437+
&& $this->helperConfig->getConfigValue(self::HOMEPAGE_PATH, $storeId) == $page->getUrl();
438438
}
439439
}

0 commit comments

Comments
 (0)