Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #213 from EmicoEcommerce/feat/feature-whitelist-fi…
Browse files Browse the repository at this point in the history
…lter-values

Feat/feature whitelist filter values
  • Loading branch information
Hnto authored Dec 8, 2021
2 parents 77a84f9 + 259f0f7 commit 9231b2c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
31 changes: 31 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,37 @@ public function getFilterWhitelist(Store $store = null)
return explode(',', $filterList) ?: [];
}

/**
* @param Store|null $store
* @return array
*/
public function getFilterValuesWhitelist(Store $store = null): array
{
$filterList = $this->getStoreConfig('tweakwise/seo/filter_values_whitelist', $store);

if (empty($filterList)) {
return [];
}

$filterList = trim($filterList);

$filterListExploded = explode(',', $filterList) ?: [];
if (empty($filterListExploded)) {
return [];
}

$return = [];
foreach ($filterListExploded as $listItem) {
$item = explode('=', trim($listItem)) ?: null;
if ($item === null) {
continue;
}
$return[$item[0]][] = $item[1];
}

return $return;
}

/**
* @param Store|null $store
* @return int
Expand Down
53 changes: 51 additions & 2 deletions Model/Seo/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Emico\Tweakwise\Model\Client\Type\FacetType\SettingsType;
use Emico\Tweakwise\Model\Config;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Product;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;

class FilterHelper
{
Expand Down Expand Up @@ -41,7 +44,11 @@ class FilterHelper
* @param Tweakwise $filterList
* @param Config $config
*/
public function __construct(Resolver $layerResolver, Tweakwise $filterList, Config $config)
public function __construct(
Resolver $layerResolver,
Tweakwise $filterList,
Config $config
)
{
$this->layerResolver = $layerResolver;
$this->tweakwiseFilterList = $filterList;
Expand All @@ -62,7 +69,10 @@ public function shouldFilterBeIndexable(Item $item): bool
return true;
}

if (!$this->exceedsMaxAllowedFacets() && $this->isFilterItemInWhiteList($item)) {
if (!$this->exceedsMaxAllowedFacets() &&
$this->isFilterItemInWhiteList($item) &&
$this->isFilterValueItemInWhiteList($item)
) {
return true;
}

Expand Down Expand Up @@ -104,6 +114,15 @@ protected function getAttributeCodeFromFilterItem(Item $item)
->getUrlKey();
}

/**
* @param Item $item
* @return string|null
*/
protected function getAttributeValueFromFilterItem(Item $item): ?string
{
return $item->getAttribute()->getTitle();
}

/**
* @return bool
*/
Expand Down Expand Up @@ -132,6 +151,36 @@ protected function isFilterItemInWhiteList(Item $item): bool
return \in_array($attributeCode, $filterWhiteList, true);
}

/**
* @param Item $item
* @return bool
*/
protected function isFilterValueItemInWhiteList(Item $item): bool
{
$filterValuesWhiteList = $this->config->getFilterValuesWhitelist();
$attributeValue = $this->getAttributeValueFromFilterItem($item);

if (empty($filterValuesWhiteList)) {
return true;
}

$attributeCode = $this->getAttributeCodeFromFilterItem($item);

if (!array_key_exists($attributeCode, $filterValuesWhiteList)) {
return true;
}

if ($attributeValue === null) {
return false;
}

return \in_array(
strtolower($attributeValue),
array_map('strtolower', $filterValuesWhiteList[$attributeCode]),
true
);
}

/**
* @param bool $includeCategoryFilter
* @return Item[]
Expand Down
13 changes: 12 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="max_allowed_facets" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="filter_values_whitelist" translate="label" type="textarea" sortOrder="30" showInStore="1" showInWebsite="1" showInDefault="1">
<label>Filter Values Whitelist</label>
<comment>
Attributes whose filter values should be indexable.
Add the filter and its value (as seen in navigator) comma separated.
For example: size=xs,size=s,size=m
</comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="max_allowed_facets" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Max allowed selected filter</label>
<comment>Max allowed filters before rendering filters as not indexable</comment>
<validate>validate-number</validate>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Emico_Tweakwise" setup_version="2.0.1">
<module name="Emico_Tweakwise" setup_version="2.0.2">
<sequence>
<module name="Magento_CatalogSearch"/>
<module name="Magento_Search"/>
Expand Down

0 comments on commit 9231b2c

Please sign in to comment.