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

Commit f44ab89

Browse files
authored
Merge pull request #21 from mageplaza/2.3-develop
2.3 develop
2 parents c726f5b + 0af2bf4 commit f44ab89

File tree

12 files changed

+400
-172
lines changed

12 files changed

+400
-172
lines changed

Block/Sitemap.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Magento\CatalogInventory\Helper\Stock;
3131
use Magento\Cms\Model\Page;
3232
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
33+
use Magento\Framework\Data\Tree\Node\Collection as TreeCollection;
3334
use Magento\Framework\Exception\NoSuchEntityException;
3435
use Magento\Framework\View\Element\Template;
3536
use Magento\Framework\View\Element\Template\Context;
@@ -129,6 +130,7 @@ public function __construct(
129130

130131
/**
131132
* Get product collection
133+
*
132134
* @return mixed
133135
*/
134136
public function getProductCollection()
@@ -150,15 +152,16 @@ public function getProductCollection()
150152

151153
/**
152154
* Get category collection
153-
* @return \Magento\Framework\Data\Tree\Node\Collection
155+
*
156+
* @return TreeCollection
154157
*/
155158
public function getCategoryCollection()
156159
{
157160
return $this->_categoryHelper->getStoreCategories(false, true);
158161
}
159162

160163
/**
161-
* @param $categoryId
164+
* @param int $categoryId
162165
*
163166
* @return string
164167
* @throws NoSuchEntityException
@@ -178,7 +181,7 @@ public function getPageCollection()
178181
{
179182
$excludePages = $this->_helper->getExcludePageListing();
180183
$pageCollection = $this->pageCollection->addFieldToFilter('is_active', Page::STATUS_ENABLED)
181-
->addStoreFilter($this->_storeManager->getStore());
184+
->addStoreFilter($this->_storeManager->getStore()->getId());
182185

183186
if ($this->_helper->isEnableExcludePage() && !empty($excludePages)) {
184187
$pageCollection->addFieldToFilter('identifier', [
@@ -191,6 +194,7 @@ public function getPageCollection()
191194

192195
/**
193196
* Get excluded pages
197+
*
194198
* @return array
195199
*/
196200
public function getExcludedPages()
@@ -200,6 +204,7 @@ public function getExcludedPages()
200204

201205
/**
202206
* Get addition link collection
207+
*
203208
* @return mixed
204209
*/
205210
public function getAdditionLinksCollection()
@@ -220,8 +225,8 @@ public function getAdditionLinksCollection()
220225
/**
221226
* Render link element
222227
*
223-
* @param $link
224-
* @param $title
228+
* @param string $link
229+
* @param string $title
225230
*
226231
* @return string
227232
*/
@@ -230,11 +235,12 @@ public function renderLinkElement($link, $title)
230235
return '<li><a href="' . $link . '">' . __($title) . '</a></li>';
231236
}
232237

238+
// phpcs:disable Generic.Metrics.NestingLevel
233239
/**
234-
* @param $section
235-
* @param $config
236-
* @param $title
237-
* @param $collection
240+
* @param string $section
241+
* @param bool $config
242+
* @param string $title
243+
* @param Object $collection
238244
*
239245
* @return string
240246
* @throws NoSuchEntityException
@@ -250,15 +256,25 @@ public function renderSection($section, $config, $title, $collection)
250256
foreach ($collection as $key => $item) {
251257
switch ($section) {
252258
case 'category':
253-
$html .= $this->renderLinkElement($this->getCategoryUrl($item->getId()), $item->getName());
259+
$category = $this->categoryRepository->get($item->getId());
260+
if (!$category->getData('mp_exclude_sitemap')) {
261+
$html .= $this->renderLinkElement(
262+
$this->getCategoryUrl($item->getId()),
263+
$item->getName()
264+
);
265+
}
254266
break;
255267
case 'page':
256-
if (in_array($item->getIdentifier(), $this->getExcludedPages())) {
268+
if (in_array($item->getIdentifier(), $this->getExcludedPages())
269+
|| $item->getData('mp_exclude_sitemap')) {
257270
continue 2;
258271
}
259272
$html .= $this->renderLinkElement($this->getUrl($item->getIdentifier()), $item->getTitle());
260273
break;
261274
case 'product':
275+
if ($item->getData('mp_exclude_sitemap')) {
276+
continue 2;
277+
}
262278
$html .= $this->renderLinkElement($this->getUrl($item->getProductUrl()), $item->getName());
263279
break;
264280
case 'link':
@@ -311,6 +327,7 @@ public function renderHtmlSitemap()
311327

312328
/**
313329
* Is enable html site map
330+
*
314331
* @return mixed
315332
*/
316333
public function isEnableHtmlSitemap()

Model/Sitemap.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
namespace Mageplaza\Sitemap\Model;
2323

24-
use Exception;
2524
use Magento\Catalog\Model\CategoryFactory;
2625
use Magento\Catalog\Model\ProductFactory;
2726
use Magento\CatalogInventory\Model\Stock\Item;
@@ -31,7 +30,10 @@
3130
use Magento\Framework\Data\Collection\AbstractDb;
3231
use Magento\Framework\DataObject;
3332
use Magento\Framework\Escaper;
33+
use Magento\Framework\Exception\FileSystemException;
3434
use Magento\Framework\Exception\LocalizedException;
35+
use Magento\Framework\Exception\NoSuchEntityException;
36+
use Magento\Framework\Exception\ValidatorException;
3537
use Magento\Framework\Filesystem;
3638
use Magento\Framework\Model\Context;
3739
use Magento\Framework\Model\ResourceModel\AbstractResource;
@@ -45,6 +47,7 @@
4547
use Magento\Sitemap\Model\Sitemap as CoreSitemap;
4648
use Magento\Store\Model\StoreManagerInterface;
4749
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
50+
use Zend_Db_Statement_Exception;
4851

4952
/**
5053
* Class Sitemap
@@ -202,24 +205,25 @@ public function _initSitemapItems()
202205
}
203206

204207
/**
205-
* @return $this
206-
* @throws Exception
208+
* @return $this|CoreSitemap
207209
* @throws LocalizedException
210+
* @throws FileSystemException
211+
* @throws ValidatorException
208212
*/
209213
public function generateXml()
210214
{
211215
$this->_initSitemapItems();
212216
/** @var $sitemapItem DataObject */
213217
foreach ($this->_sitemapItems as $item) {
214-
$changefreq = $item->getChangefreq();
218+
$changeFreq = $item->getChangefreq();
215219
$priority = $item->getPriority();
216220
$urlType = $item->getUrlType();
217221
foreach ($item->getCollection() as $itemChild) {
218222
$xml = $this->getSitemapRow(
219223
$itemChild->getUrl(),
220224
$urlType,
221225
$itemChild->getUpdatedAt(),
222-
$changefreq,
226+
$changeFreq,
223227
$priority,
224228
$itemChild->getImages()
225229
);
@@ -258,10 +262,10 @@ public function generateXml()
258262
/**
259263
* Get site map row
260264
*
261-
* @param $url
262-
* @param $urlType
263-
* @param null $lastmod
264-
* @param null $changefreq
265+
* @param string $url
266+
* @param int $urlType
267+
* @param null $lastMod
268+
* @param null $changeFreq
265269
* @param null $priority
266270
* @param null $images
267271
*
@@ -270,8 +274,8 @@ public function generateXml()
270274
protected function getSitemapRow(
271275
$url,
272276
$urlType,
273-
$lastmod = null,
274-
$changefreq = null,
277+
$lastMod = null,
278+
$changeFreq = null,
275279
$priority = null,
276280
$images = null
277281
) {
@@ -281,11 +285,11 @@ protected function getSitemapRow(
281285
$url = $this->convertUrl($url);
282286
}
283287
$row = '<loc>' . htmlspecialchars($url) . '</loc>';
284-
if ($lastmod) {
285-
$row .= '<lastmod>' . $this->_getFormattedLastmodDate($lastmod) . '</lastmod>';
288+
if ($lastMod) {
289+
$row .= '<lastmod>' . $this->_getFormattedLastmodDate($lastMod) . '</lastmod>';
286290
}
287-
if ($changefreq) {
288-
$row .= '<changefreq>' . $changefreq . '</changefreq>';
291+
if ($changeFreq) {
292+
$row .= '<changefreq>' . $changeFreq . '</changefreq>';
289293
}
290294
if ($priority) {
291295
$row .= sprintf('<priority>%.1f</priority>', $priority);
@@ -315,7 +319,7 @@ protected function getSitemapRow(
315319
/**
316320
* Get link collection added by config Additional Links
317321
*
318-
* @param $storeId
322+
* @param int $storeId
319323
*
320324
* @return array
321325
*/
@@ -339,7 +343,7 @@ public function getLinkCollectionAdded($storeId)
339343
/**
340344
* Get category collection
341345
*
342-
* @param $storeId
346+
* @param int $storeId
343347
*
344348
* @return array
345349
*/
@@ -360,7 +364,7 @@ public function _getCategoryCollection($storeId)
360364
/**
361365
* Get page collection
362366
*
363-
* @param $storeId
367+
* @param int $storeId
364368
*
365369
* @return array
366370
*/
@@ -382,7 +386,7 @@ public function _getPageCollection($storeId)
382386
/**
383387
* Get product Collection
384388
*
385-
* @param $storeId
389+
* @param int $storeId
386390
*
387391
* @return array
388392
*/
@@ -405,25 +409,24 @@ public function _getProductCollection($storeId)
405409
/**
406410
* Convert Url
407411
*
408-
* @param $url
412+
* @param string $url
409413
*
410414
* @return string
411415
*/
412416
public function convertUrl($url)
413417
{
414-
if (preg_match(self::PATTERN, $url)
415-
|| strpos($url, 'http://') !== false
416-
|| strpos($url, 'https://') !== false) {
418+
if (preg_match('@^http://@i', $url) || preg_match('@^https://@i', $url)) {
417419
return $url;
418420
}
421+
419422
return 'http://' . $url;
420423
}
421424

422425
/**
423426
* Remove the link of the CMS page using for homepage.
424427
*
425-
* @param $storeId
426-
* @param $page
428+
* @param int $storeId
429+
* @param Object $page
427430
*
428431
* @return bool
429432
*/

Model/Source/Boolean.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Mageplaza
17+
* @package Mageplaza_Sitemap
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
namespace Mageplaza\Sitemap\Model\Source;
23+
24+
use Magento\Framework\Data\OptionSourceInterface;
25+
26+
/**
27+
* Class Boolean
28+
* @package Mageplaza\Sitemap\Model\Source
29+
*/
30+
class Boolean extends \Magento\Eav\Model\Entity\Attribute\Source\Boolean implements OptionSourceInterface
31+
{
32+
/**
33+
* @return array|null
34+
*/
35+
public function getAllOptions()
36+
{
37+
if ($this->_options === null) {
38+
$this->_options = [
39+
['label' => __('No'), 'value' => self::VALUE_NO],
40+
['label' => __('Yes'), 'value' => self::VALUE_YES]
41+
];
42+
}
43+
return $this->_options;
44+
}
45+
}

Model/Source/Page.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public function toOptionArray()
5757
];
5858

5959
/** @var Collection $collection */
60-
$collection = $this->_pageCollectionFactory->create();
60+
$collection = $this->_pageCollectionFactory->create()->addFieldToFilter('is_active', 1);
61+
6162
foreach ($collection as $item) {
6263
$options[] = ['value' => $item->getIdentifier(), 'label' => $item->getTitle()];
6364
}

0 commit comments

Comments
 (0)