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

Commit 6a88338

Browse files
Merge pull request #22 from mageplaza/2.3-develop
2.3 develop
2 parents f44ab89 + 66bd5e8 commit 6a88338

File tree

8 files changed

+615
-207
lines changed

8 files changed

+615
-207
lines changed

Block/Sitemap.php

Lines changed: 159 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121

2222
namespace Mageplaza\Sitemap\Block;
2323

24+
use Exception;
2425
use Magento\Catalog\Helper\Category;
2526
use Magento\Catalog\Model\CategoryRepository;
2627
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
2728
use Magento\Catalog\Model\ResourceModel\Category\Collection;
2829
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
29-
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
30+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
3031
use Magento\CatalogInventory\Helper\Stock;
3132
use Magento\Cms\Model\Page;
3233
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
33-
use Magento\Framework\Data\Tree\Node\Collection as TreeCollection;
34+
use Magento\Framework\Data\Collection\AbstractDb;
35+
use Magento\Framework\Exception\LocalizedException;
3436
use Magento\Framework\Exception\NoSuchEntityException;
3537
use Magento\Framework\View\Element\Template;
3638
use Magento\Framework\View\Element\Template\Context;
3739
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
40+
use Mageplaza\Sitemap\Model\Source\SortProduct;
3841

3942
/**
4043
* Class Sitemap
@@ -136,28 +139,160 @@ public function __construct(
136139
public function getProductCollection()
137140
{
138141
$limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
139-
$collection = $this->productCollection
142+
$collection = $this->productCollection->create()
140143
->setVisibility($this->productVisibility->getVisibleInCatalogIds())
141144
->addMinimalPrice()
142145
->addFinalPrice()
143146
->addTaxPercents()
144-
->setPageSize($limit)
145147
->addAttributeToSelect('*');
146-
if (!$this->_helper->getConfigValue('cataloginventory/options/show_out_of_stock')) {
148+
149+
$sortProductBy = $this->_helper->getHtmlSitemapConfig('product_sorting');
150+
$sortProductDir = $this->_helper->getHtmlSitemapConfig('product_sorting_dir');
151+
152+
switch ($sortProductBy) {
153+
case SortProduct::PRODUCT_NAME:
154+
$collection->setOrder('name', $sortProductDir);
155+
break;
156+
case SortProduct::PRICE:
157+
$collection->setOrder('minimal_price', $sortProductDir);
158+
break;
159+
default:
160+
$collection->setOrder('entity_id', $sortProductDir);
161+
break;
162+
}
163+
164+
if ($this->_helper->getHtmlSitemapConfig('out_of_stock_products')) {
147165
$this->_stockFilter->addInStockFilterToCollection($collection);
148166
}
149167

168+
$collection->setPageSize($limit);
169+
150170
return $collection;
151171
}
152172

153173
/**
154-
* Get category collection
155-
*
156-
* @return TreeCollection
174+
* @return Collection|AbstractDb
175+
* @throws NoSuchEntityException
176+
* @throws LocalizedException
157177
*/
158178
public function getCategoryCollection()
159179
{
160-
return $this->_categoryHelper->getStoreCategories(false, true);
180+
$storeRootCategoryId = $this->_storeManager->getStore()->getRootCategoryId();
181+
$storeRootCategory = $this->categoryRepository->get($storeRootCategoryId);
182+
$categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*')
183+
->addFieldToFilter('entity_id', ['in' => $storeRootCategory->getAllChildren(true)])
184+
->addFieldToFilter('is_active', 1)
185+
->addFieldToFilter('include_in_menu', 1)
186+
->addFieldToFilter('entity_id', ['nin' => [$storeRootCategoryId]]);
187+
188+
$excludeCategories = $this->_helper->getHtmlSitemapConfig('category_page');
189+
if (!empty($excludeCategories)) {
190+
$excludeCategories = array_map('trim', explode(
191+
"\n",
192+
$excludeCategories
193+
));
194+
195+
$allExcludeIds = '';
196+
foreach ($excludeCategories as $excludeCategory) {
197+
if (!empty($excludeCategory)) {
198+
try {
199+
$testRegex = preg_match($excludeCategory, '');
200+
if ($testRegex) {
201+
$allExcludeIds .= '-' . $this->filterCategoryWithRegex($excludeCategory);
202+
} else {
203+
$excludePath = $this->getExcludePath($excludeCategory);
204+
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
205+
}
206+
} catch (Exception $e) {
207+
$excludePath = $this->getExcludePath($excludeCategory);
208+
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
209+
}
210+
}
211+
}
212+
213+
$excludeIds = explode('-', $allExcludeIds);
214+
$categoryCollection->addFieldToFilter('entity_id', ['nin' => $excludeIds]);
215+
}
216+
217+
return $this->_categoryCollection->create()->addAttributeToSelect('*')
218+
->addFieldToFilter('entity_id', ['in' => $categoryCollection->getAllIds()])->setOrder('path');
219+
}
220+
221+
/**
222+
* @param $path
223+
* @param $categoryCollection
224+
*
225+
* @return string
226+
*/
227+
protected function filterCategoryWithPath($path, $categoryCollection)
228+
{
229+
$excludeIds = [];
230+
foreach ($categoryCollection as $category) {
231+
if ($this->isExcludeCategory($category, $path)) {
232+
$excludeIds[] = $category->getData('entity_id');
233+
}
234+
}
235+
236+
return implode('-', $excludeIds);
237+
}
238+
239+
/**
240+
* @param $category
241+
* @param $path
242+
*
243+
* @return bool
244+
*/
245+
public function isExcludeCategory($category, $path)
246+
{
247+
$filterPath = explode('/', $path);
248+
$categoryPath = $category->getUrlPath();
249+
$categoryPath = explode('/', $categoryPath);
250+
251+
foreach ($filterPath as $pathInfo) {
252+
if (!in_array($pathInfo, $categoryPath)) {
253+
return false;
254+
}
255+
}
256+
257+
return true;
258+
}
259+
260+
/**
261+
* @param $regex
262+
*
263+
* @return string
264+
* @throws LocalizedException
265+
* @throws NoSuchEntityException
266+
*/
267+
protected function filterCategoryWithRegex($regex)
268+
{
269+
$excludeCategoriesIds = [];
270+
$categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*')
271+
->setStoreId($this->_storeManager->getStore()->getId());
272+
foreach ($categoryCollection as $category) {
273+
if (!preg_match($regex, $category->getUrlPath())) {
274+
$excludeCategoriesIds[] = $category->getId();
275+
}
276+
}
277+
278+
return implode('-', $excludeCategoriesIds);
279+
}
280+
281+
/**
282+
* @param $excludeCategory
283+
*
284+
* @return string
285+
*/
286+
protected function getExcludePath($excludeCategory)
287+
{
288+
if ($excludeCategory[0] == '/') {
289+
$excludeCategory = substr($excludeCategory, 1);
290+
}
291+
if ($excludeCategory[-1] == '/') {
292+
$excludeCategory = substr($excludeCategory, 0, -1);
293+
}
294+
295+
return $excludeCategory;
161296
}
162297

163298
/**
@@ -223,16 +358,15 @@ public function getAdditionLinksCollection()
223358
}
224359

225360
/**
226-
* Render link element
227-
*
228-
* @param string $link
229-
* @param string $title
361+
* @param $link
362+
* @param $title
363+
* @param $level
230364
*
231365
* @return string
232366
*/
233-
public function renderLinkElement($link, $title)
367+
public function renderLinkElement($link, $title, $level = null)
234368
{
235-
return '<li><a href="' . $link . '">' . __($title) . '</a></li>';
369+
return '<li><a class="level-' . $level . '" href="' . $link . '">' . __($title) . '</a></li>';
236370
}
237371

238372
// phpcs:disable Generic.Metrics.NestingLevel
@@ -260,7 +394,8 @@ public function renderSection($section, $config, $title, $collection)
260394
if (!$category->getData('mp_exclude_sitemap')) {
261395
$html .= $this->renderLinkElement(
262396
$this->getCategoryUrl($item->getId()),
263-
$item->getName()
397+
$item->getName(),
398+
$item->getLevel()
264399
);
265400
}
266401
break;
@@ -334,4 +469,12 @@ public function isEnableHtmlSitemap()
334469
{
335470
return $this->_helper->isEnableHtmlSiteMap();
336471
}
472+
473+
/**
474+
* @return array|bool|mixed
475+
*/
476+
public function getCategoryDisplayType()
477+
{
478+
return $this->_helper->getHtmlSitemapConfig('display_type');
479+
}
337480
}

Model/Source/DisplayType.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Option\ArrayInterface;
25+
26+
/**
27+
* Class DisplayType
28+
* @package Mageplaza\Sitemap\Model\Config\Source
29+
*/
30+
class DisplayType implements ArrayInterface
31+
{
32+
const LIST = 'list';
33+
const DROPDOWN = 'dropdown';
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toOptionArray()
39+
{
40+
$options = [];
41+
foreach ($this->toArray() as $value => $label) {
42+
$options[] = [
43+
'value' => $value,
44+
'label' => $label
45+
];
46+
}
47+
48+
return $options;
49+
}
50+
51+
/**
52+
* Get options in "key-value" format
53+
*
54+
* @return array
55+
*/
56+
public function toArray()
57+
{
58+
return [
59+
self::LIST => __('List'),
60+
self::DROPDOWN => __('Dropdown'),
61+
];
62+
}
63+
}

Model/Source/SortDirection.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Option\ArrayInterface;
25+
26+
/**
27+
* Class SortDirection
28+
* @package Mageplaza\Sitemap\Model\Config\Source
29+
*/
30+
class SortDirection implements ArrayInterface
31+
{
32+
const ASC = 'asc';
33+
const DESC = 'desc';
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toOptionArray()
39+
{
40+
$options = [];
41+
foreach ($this->toArray() as $value => $label) {
42+
$options[] = [
43+
'value' => $value,
44+
'label' => $label
45+
];
46+
}
47+
48+
return $options;
49+
}
50+
51+
/**
52+
* Get options in "key-value" format
53+
*
54+
* @return array
55+
*/
56+
public function toArray()
57+
{
58+
return [
59+
self::ASC => __('Ascending'),
60+
self::DESC => __('Descending'),
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)