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

Commit

Permalink
Merge pull request #22 from mageplaza/2.3-develop
Browse files Browse the repository at this point in the history
2.3 develop
  • Loading branch information
haitv282 committed Jun 24, 2022
2 parents f44ab89 + 66bd5e8 commit 6a88338
Show file tree
Hide file tree
Showing 8 changed files with 615 additions and 207 deletions.
175 changes: 159 additions & 16 deletions Block/Sitemap.php
Expand Up @@ -21,20 +21,23 @@

namespace Mageplaza\Sitemap\Block;

use Exception;
use Magento\Catalog\Helper\Category;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
use Magento\CatalogInventory\Helper\Stock;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection;
use Magento\Framework\Data\Tree\Node\Collection as TreeCollection;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Mageplaza\Sitemap\Helper\Data as HelperConfig;
use Mageplaza\Sitemap\Model\Source\SortProduct;

/**
* Class Sitemap
Expand Down Expand Up @@ -136,28 +139,160 @@ public function __construct(
public function getProductCollection()
{
$limit = $this->_helper->getProductLimit() ?: self::DEFAULT_PRODUCT_LIMIT;
$collection = $this->productCollection
$collection = $this->productCollection->create()
->setVisibility($this->productVisibility->getVisibleInCatalogIds())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->setPageSize($limit)
->addAttributeToSelect('*');
if (!$this->_helper->getConfigValue('cataloginventory/options/show_out_of_stock')) {

$sortProductBy = $this->_helper->getHtmlSitemapConfig('product_sorting');
$sortProductDir = $this->_helper->getHtmlSitemapConfig('product_sorting_dir');

switch ($sortProductBy) {
case SortProduct::PRODUCT_NAME:
$collection->setOrder('name', $sortProductDir);
break;
case SortProduct::PRICE:
$collection->setOrder('minimal_price', $sortProductDir);
break;
default:
$collection->setOrder('entity_id', $sortProductDir);
break;
}

if ($this->_helper->getHtmlSitemapConfig('out_of_stock_products')) {
$this->_stockFilter->addInStockFilterToCollection($collection);
}

$collection->setPageSize($limit);

return $collection;
}

/**
* Get category collection
*
* @return TreeCollection
* @return Collection|AbstractDb
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getCategoryCollection()
{
return $this->_categoryHelper->getStoreCategories(false, true);
$storeRootCategoryId = $this->_storeManager->getStore()->getRootCategoryId();
$storeRootCategory = $this->categoryRepository->get($storeRootCategoryId);
$categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*')
->addFieldToFilter('entity_id', ['in' => $storeRootCategory->getAllChildren(true)])
->addFieldToFilter('is_active', 1)
->addFieldToFilter('include_in_menu', 1)
->addFieldToFilter('entity_id', ['nin' => [$storeRootCategoryId]]);

$excludeCategories = $this->_helper->getHtmlSitemapConfig('category_page');
if (!empty($excludeCategories)) {
$excludeCategories = array_map('trim', explode(
"\n",
$excludeCategories
));

$allExcludeIds = '';
foreach ($excludeCategories as $excludeCategory) {
if (!empty($excludeCategory)) {
try {
$testRegex = preg_match($excludeCategory, '');
if ($testRegex) {
$allExcludeIds .= '-' . $this->filterCategoryWithRegex($excludeCategory);
} else {
$excludePath = $this->getExcludePath($excludeCategory);
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
}
} catch (Exception $e) {
$excludePath = $this->getExcludePath($excludeCategory);
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
}
}
}

$excludeIds = explode('-', $allExcludeIds);
$categoryCollection->addFieldToFilter('entity_id', ['nin' => $excludeIds]);
}

return $this->_categoryCollection->create()->addAttributeToSelect('*')
->addFieldToFilter('entity_id', ['in' => $categoryCollection->getAllIds()])->setOrder('path');
}

/**
* @param $path
* @param $categoryCollection
*
* @return string
*/
protected function filterCategoryWithPath($path, $categoryCollection)
{
$excludeIds = [];
foreach ($categoryCollection as $category) {
if ($this->isExcludeCategory($category, $path)) {
$excludeIds[] = $category->getData('entity_id');
}
}

return implode('-', $excludeIds);
}

/**
* @param $category
* @param $path
*
* @return bool
*/
public function isExcludeCategory($category, $path)
{
$filterPath = explode('/', $path);
$categoryPath = $category->getUrlPath();
$categoryPath = explode('/', $categoryPath);

foreach ($filterPath as $pathInfo) {
if (!in_array($pathInfo, $categoryPath)) {
return false;
}
}

return true;
}

/**
* @param $regex
*
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
*/
protected function filterCategoryWithRegex($regex)
{
$excludeCategoriesIds = [];
$categoryCollection = $this->_categoryCollection->create()->addAttributeToSelect('*')
->setStoreId($this->_storeManager->getStore()->getId());
foreach ($categoryCollection as $category) {
if (!preg_match($regex, $category->getUrlPath())) {
$excludeCategoriesIds[] = $category->getId();
}
}

return implode('-', $excludeCategoriesIds);
}

/**
* @param $excludeCategory
*
* @return string
*/
protected function getExcludePath($excludeCategory)
{
if ($excludeCategory[0] == '/') {
$excludeCategory = substr($excludeCategory, 1);
}
if ($excludeCategory[-1] == '/') {
$excludeCategory = substr($excludeCategory, 0, -1);
}

return $excludeCategory;
}

/**
Expand Down Expand Up @@ -223,16 +358,15 @@ public function getAdditionLinksCollection()
}

/**
* Render link element
*
* @param string $link
* @param string $title
* @param $link
* @param $title
* @param $level
*
* @return string
*/
public function renderLinkElement($link, $title)
public function renderLinkElement($link, $title, $level = null)
{
return '<li><a href="' . $link . '">' . __($title) . '</a></li>';
return '<li><a class="level-' . $level . '" href="' . $link . '">' . __($title) . '</a></li>';
}

// phpcs:disable Generic.Metrics.NestingLevel
Expand Down Expand Up @@ -260,7 +394,8 @@ public function renderSection($section, $config, $title, $collection)
if (!$category->getData('mp_exclude_sitemap')) {
$html .= $this->renderLinkElement(
$this->getCategoryUrl($item->getId()),
$item->getName()
$item->getName(),
$item->getLevel()
);
}
break;
Expand Down Expand Up @@ -334,4 +469,12 @@ public function isEnableHtmlSitemap()
{
return $this->_helper->isEnableHtmlSiteMap();
}

/**
* @return array|bool|mixed
*/
public function getCategoryDisplayType()
{
return $this->_helper->getHtmlSitemapConfig('display_type');
}
}
63 changes: 63 additions & 0 deletions Model/Source/DisplayType.php
@@ -0,0 +1,63 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Sitemap
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Sitemap\Model\Source;

use Magento\Framework\Option\ArrayInterface;

/**
* Class DisplayType
* @package Mageplaza\Sitemap\Model\Config\Source
*/
class DisplayType implements ArrayInterface
{
const LIST = 'list';
const DROPDOWN = 'dropdown';

/**
* @return array
*/
public function toOptionArray()
{
$options = [];
foreach ($this->toArray() as $value => $label) {
$options[] = [
'value' => $value,
'label' => $label
];
}

return $options;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
self::LIST => __('List'),
self::DROPDOWN => __('Dropdown'),
];
}
}
63 changes: 63 additions & 0 deletions Model/Source/SortDirection.php
@@ -0,0 +1,63 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Sitemap
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Sitemap\Model\Source;

use Magento\Framework\Option\ArrayInterface;

/**
* Class SortDirection
* @package Mageplaza\Sitemap\Model\Config\Source
*/
class SortDirection implements ArrayInterface
{
const ASC = 'asc';
const DESC = 'desc';

/**
* @return array
*/
public function toOptionArray()
{
$options = [];
foreach ($this->toArray() as $value => $label) {
$options[] = [
'value' => $value,
'label' => $label
];
}

return $options;
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
self::ASC => __('Ascending'),
self::DESC => __('Descending'),
];
}
}

0 comments on commit 6a88338

Please sign in to comment.