Skip to content

Commit

Permalink
feat: IPET-2213 Mask category urls on search result page
Browse files Browse the repository at this point in the history
  • Loading branch information
Leone committed Oct 10, 2023
1 parent e6fe3a0 commit d7a0b42
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 148 deletions.
27 changes: 8 additions & 19 deletions Helper/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@

class Category
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
*/
protected $categoryRepository;

/**
* @var \MageSuite\SeoLinkMasking\Helper\Configuration
*/
protected $configuration;
protected \Magento\Store\Model\StoreManagerInterface $storeManager;
protected \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository;
protected \MageSuite\SeoLinkMasking\Helper\Page $pageHelper;

public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\MageSuite\SeoLinkMasking\Helper\Configuration $configuration
\MageSuite\SeoLinkMasking\Helper\Page $pageHelper
) {
$this->storeManager = $storeManager;
$this->categoryRepository = $categoryRepository;
$this->configuration = $configuration;
$this->pageHelper = $pageHelper;
}

public function getCategoryEntityForSearchResultPage($category)
Expand All @@ -35,9 +24,9 @@ public function getCategoryEntityForSearchResultPage($category)
return $category;
}

if ($this->configuration->isSearchResultPageAjaxFilterCall() ||
$this->configuration->isSearchResultPage() ||
$this->configuration->isBrandsIndexPage()
if ($this->pageHelper->isSearchResultPageAjaxFilterCall() ||
$this->pageHelper->isSearchResultPage() ||
$this->pageHelper->isBrandsIndexPage()
) {
$rootCategoryId = $this->storeManager->getStore()->getRootCategoryId();
return $this->categoryRepository->get($rootCategoryId);
Expand Down
120 changes: 44 additions & 76 deletions Helper/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,92 @@

namespace MageSuite\SeoLinkMasking\Helper;

class Configuration extends \Magento\Framework\App\Helper\AbstractHelper
class Configuration
{
const XML_PATH_SEO_LINK_MASKING_CONFIGURATION = 'seo/link_masking';
const XML_PATH_SEO_LINK_MASKING_SPACE_REPLACEMENT_CHAR = 'seo/link_masking/space_replacement_character';
const XML_PATH_SEO_LINK_MASKING_EXCLUDED_CHARACTERS = 'seo/link_masking/excluded_characters';
const LINK_MASKING_PARAMETER_REGISTRY_KEY = 'link_masking_parameters';
public const LINK_MASKING_PARAMETER_REGISTRY_KEY = 'link_masking_parameters';

const SEARCH_RESULT_PAGE_FULL_ACTION_NAME = 'catalogsearch_result_index';
const BRAND_PAGE_FULL_ACTION_NAME = 'brands_index_index';
public const XML_PATH_SEO_LINK_MASKING_IS_ENABLED = 'seo/link_masking/is_enabled';
public const XML_PATH_SEO_LINK_MASKING_DEFAULT_MASKING_STATE = 'seo/link_masking/default_masking_state';
public const XML_PATH_SEO_LINK_MASKING_ONLY_ONE_FILTER_DEMASKED = 'seo/link_masking/only_one_filter_demasked';
public const XML_PATH_SEO_LINK_MASKING_MASK_CATEGORY_URL_ON_SEARCH_PAGE = 'seo/link_masking/mask_category_url_on_search_page';
public const XML_PATH_SEO_LINK_MASKING_IS_SHORT_FILTER_URL_ENABLED = 'seo/link_masking/is_short_filter_url_enabled';
public const XML_PATH_SEO_LINK_MASKING_IS_DISPLAYING_WARNING_ABOUT_DUPLICATED_OPTIONS_ENABLED = 'seo/link_masking/is_displaying_warning_about_duplicated_options_enabled';
public const XML_PATH_SEO_LINK_MASKING_CACHE_LENGTH_FOR_WARNING_ABOUT_DUPLICATED_OPTIONS = 'seo/link_masking/cache_length_for_warning_about_duplicated_options';
public const XML_PATH_SEO_LINK_MASKING_ENABLE_FILTER_PARAMS_IN_CANONICAL = 'seo/link_masking/enable_filter_params_in_canonical';
public const XML_PATH_SEO_LINK_MASKING_SPACE_REPLACEMENT_CHAR = 'seo/link_masking/space_replacement_character';
public const XML_PATH_SEO_LINK_MASKING_MULTISELECT_OPTION_SEPARATOR = 'seo/link_masking/multiselect_option_separator';
public const XML_PATH_SEO_LINK_MASKING_IS_UTF_FRIENDLY_MODE_ENABLED = 'seo/link_masking/is_utf_friendly_mode_enabled';
public const XML_PATH_SEO_LINK_MASKING_EXCLUDED_CHARACTERS = 'seo/link_masking/excluded_characters';

const AJAX_FILTER_FULL_ACTION_NAME = 'catalog_navigation_filter_ajax';
public const XML_PATH_SHOW_SWATCH_TOOLTIP = 'catalog/frontend/show_swatch_tooltip';

const XML_PATH_SHOW_SWATCH_TOOLTIP = 'catalog/frontend/show_swatch_tooltip';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;

protected $config = null;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface,
\Magento\Framework\App\Request\Http $request
) {
parent::__construct($context);
protected \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig;

public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface)
{
$this->scopeConfig = $scopeConfigInterface;
$this->request = $request;
}

public function getDefaultMaskingState()
public function isLinkMaskingEnabled($storeId = null): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_IS_ENABLED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function getDefaultMaskingState($storeId = null): bool
{
if (!$this->isLinkMaskingEnabled()) {
return false;
}

return (bool)$this->getConfig()->getDefaultMaskingState();
return (bool)$this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_DEFAULT_MASKING_STATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function isLinkMaskingEnabled()
public function onlyOneFilterDemasked($storeId = null): bool
{
return (bool)$this->getConfig()->getIsEnabled();
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_ONLY_ONE_FILTER_DEMASKED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function onlyOneFilterDemasked()
public function maskCategoryUrlOnSearchPage($storeId = null): bool
{
return (bool)$this->getConfig()->getOnlyOneFilterDemasked();
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_MASK_CATEGORY_URL_ON_SEARCH_PAGE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function isShortFilterUrlEnabled()
public function isShortFilterUrlEnabled($storeId = null): bool
{
return (bool)$this->getConfig()->getIsShortFilterUrlEnabled();
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_IS_SHORT_FILTER_URL_ENABLED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function isDisplayingWarningAboutDuplicatedOptionsEnabled(): bool
{
return (bool)$this->getConfig()->getIsDisplayingWarningAboutDuplicatedOptionsEnabled();
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_IS_DISPLAYING_WARNING_ABOUT_DUPLICATED_OPTIONS_ENABLED);
}

public function getCacheLengthForWarningAboutDuplicatedOptions(): int
{
return (int)$this->getConfig()->getCacheLengthForWarningAboutDuplicatedOptions();
}

public function areFilterParamsInCanonicalEnabled()
{
return (bool)$this->getConfig()->getEnableFilterParamsInCanonical();
return (int)$this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_CACHE_LENGTH_FOR_WARNING_ABOUT_DUPLICATED_OPTIONS);
}

public function getSpaceReplacementCharacter()
public function areFilterParamsInCanonicalEnabled($storeId = null): bool
{
return $this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_SPACE_REPLACEMENT_CHAR);
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_ENABLE_FILTER_PARAMS_IN_CANONICAL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function getMultiselectOptionSeparator()
public function getSpaceReplacementCharacter(): string
{
return $this->getConfig()->getMultiselectOptionSeparator();
return (string)$this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_SPACE_REPLACEMENT_CHAR);
}

public function isSearchResultPage()
public function getMultiselectOptionSeparator($storeId = null): string
{
return $this->request->getFullActionName() == self::SEARCH_RESULT_PAGE_FULL_ACTION_NAME;
return (string)$this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_MULTISELECT_OPTION_SEPARATOR, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function isBrandsIndexPage()
public function isUtfFriendlyModeEnabled($storeId = null): bool
{
return $this->request->getFullActionName() == self::BRAND_PAGE_FULL_ACTION_NAME;
return $this->scopeConfig->isSetFlag(self::XML_PATH_SEO_LINK_MASKING_IS_UTF_FRIENDLY_MODE_ENABLED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}

public function isSearchResultPageAjaxFilterCall()
{
return !$this->request->getParam('cat') && ($this->request->getFullActionName() == self::AJAX_FILTER_FULL_ACTION_NAME);
}

public function isUtfFriendlyModeEnabled()
{
return (bool)$this->getConfig()->getIsUtfFriendlyModeEnabled();
}

public function getExcludedCharacters()
public function getExcludedCharacters(): array
{
$excludedCharacters = $this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_EXCLUDED_CHARACTERS);

Expand All @@ -122,19 +101,8 @@ public function getExcludedCharacters()
);
}

protected function getConfig()
{
if ($this->config === null) {
$this->config = new \Magento\Framework\DataObject(
$this->scopeConfig->getValue(self::XML_PATH_SEO_LINK_MASKING_CONFIGURATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
);
}

return $this->config;
}

public function canShowSwatchTooltip()
public function canShowSwatchTooltip($storeId = null): bool
{
return $this->scopeConfig->getValue(self::XML_PATH_SHOW_SWATCH_TOOLTIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
return $this->scopeConfig->isSetFlag(self::XML_PATH_SHOW_SWATCH_TOOLTIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
}
}
32 changes: 32 additions & 0 deletions Helper/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace MageSuite\SeoLinkMasking\Helper;

class Page
{
const SEARCH_RESULT_PAGE_FULL_ACTION_NAME = 'catalogsearch_result_index';
const BRAND_PAGE_FULL_ACTION_NAME = 'brands_index_index';
const AJAX_FILTER_FULL_ACTION_NAME = 'catalog_navigation_filter_ajax';

protected \Magento\Framework\App\Request\Http $request;

public function __construct(\Magento\Framework\App\Request\Http $request)
{
$this->request = $request;
}

public function isSearchResultPage(): bool
{
return $this->request->getFullActionName() == self::SEARCH_RESULT_PAGE_FULL_ACTION_NAME;
}

public function isBrandsIndexPage(): bool
{
return $this->request->getFullActionName() == self::BRAND_PAGE_FULL_ACTION_NAME;
}

public function isSearchResultPageAjaxFilterCall(): bool
{
return !$this->request->getParam('cat') && ($this->request->getFullActionName() == self::AJAX_FILTER_FULL_ACTION_NAME);
}
}
2 changes: 1 addition & 1 deletion Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function encodeValue($value)

$spaceReplacementCharacter = $this->configuration->getSpaceReplacementCharacter();

if (!$spaceReplacementCharacter || $spaceReplacementCharacter == self::DEFAULT_SPACE_REPLACEMENT_CHAR) {
if (empty($spaceReplacementCharacter) || $spaceReplacementCharacter == self::DEFAULT_SPACE_REPLACEMENT_CHAR) {
return $value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function aroundGetData(\Magento\Catalog\Model\Layer\Filter\AbstractFilter

protected function getCategory($subject)
{
if ($this->request->getFullActionName() == \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME) {
if ($this->request->getFullActionName() == \MageSuite\SeoLinkMasking\Helper\Page::AJAX_FILTER_FULL_ACTION_NAME) {
return $subject->getLayer()->getCurrentCategory();
}

Expand Down
56 changes: 25 additions & 31 deletions Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,25 @@ class AdjustFilterItemUrl
{
const CATEGORY_FILTER_CODE = 'cat';

/**
* @var \MageSuite\SeoLinkMasking\Helper\Configuration
*/
protected $configuration;

/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $request;

/**
* @var \Magento\Framework\Registry
*/
protected $registry;

/**
* @var \Magento\Framework\UrlInterface
*/
protected $url;

/**
* @var \Magento\Framework\Data\Helper\PostHelper
*/
protected $postHelper;

/**
* @var \MageSuite\SeoLinkMasking\Service\FilterItemUrlProcessor
*/
protected $filterItemUrlProcessor;
protected \MageSuite\SeoLinkMasking\Helper\Configuration $configuration;
protected \MageSuite\SeoLinkMasking\Helper\Page $pageHelper;
protected \Magento\Framework\App\RequestInterface $request;
protected \Magento\Framework\Registry $registry;
protected \Magento\Framework\UrlInterface $url;
protected \Magento\Framework\Data\Helper\PostHelper $postHelper;
protected \MageSuite\SeoLinkMasking\Service\FilterItemUrlProcessor $filterItemUrlProcessor;

public function __construct(
\MageSuite\SeoLinkMasking\Helper\Configuration $configuration,
\MageSuite\SeoLinkMasking\Helper\Page $pageHelper,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Registry $registry,
\Magento\Framework\UrlInterface $url,
\Magento\Framework\Data\Helper\PostHelper $postHelper,
\MageSuite\SeoLinkMasking\Service\FilterItemUrlProcessor $filterItemUrlProcessor
) {
$this->configuration = $configuration;
$this->pageHelper = $pageHelper;
$this->request = $request;
$this->registry = $registry;
$this->url = $url;
Expand All @@ -59,7 +39,15 @@ public function aroundGetUrl(\Magento\Catalog\Model\Layer\Filter\Item $subject,

$maskingEnabled = $this->configuration->isShortFilterUrlEnabled() || $filter->getIsLinkMaskingEnabled();

if (!$maskingEnabled || $this->isCategoryFilter($filter->getRequestVar())) {
if (!$maskingEnabled) {
return $proceed();
}

if ($this->isCategoryFilter($filter->getRequestVar())) {
if ($this->configuration->maskCategoryUrlOnSearchPage() && $this->pageHelper->isSearchResultPage()) {
return $this->maskCategoryUrlOnSearchPage($proceed());
}

return $proceed();
}

Expand All @@ -69,7 +57,7 @@ public function aroundGetUrl(\Magento\Catalog\Model\Layer\Filter\Item $subject,
$url = $this->filterItemUrlProcessor->prepareItemUrl($filter, $category, $subject->getValue());
}

if ($this->request->getFullActionName() != \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME) {
if ($this->request->getFullActionName() != \MageSuite\SeoLinkMasking\Helper\Page::AJAX_FILTER_FULL_ACTION_NAME) {
return $url;
}

Expand Down Expand Up @@ -105,4 +93,10 @@ protected function getCategory()

return ($category && $category->getId()) ? $category : null;
}

protected function maskCategoryUrlOnSearchPage($url): string
{
$linkMaskingUrl = $this->url->getUrl(\MageSuite\SeoLinkMasking\Plugin\Smile\ElasticsuiteCatalog\Block\Navigation\Renderer\Attribute\AddLinkMaskingToFilterData::LINK_MASKING_ENDPOINT);
return $this->postHelper->getPostData($linkMaskingUrl, ['url' => $url]);
}
}
2 changes: 1 addition & 1 deletion Plugin/Theme/Block/Html/AddLinkMaskingToPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getLinkMaskedPagerUrl(\Magento\Theme\Block\Html\Pager $pager,
$page = isset($params[$paginationParam]) ? $params[$paginationParam] : null;

$query = [];
$parsedUrl = parse_url($url, PHP_URL_QUERY) ?? '';
$parsedUrl = parse_url($url, PHP_URL_QUERY) ?? ''; //phpcs:ignore
//phpcs:ignore Magento2.Functions.DiscouragedFunction
parse_str($parsedUrl, $query);

Expand Down
2 changes: 1 addition & 1 deletion Service/FilterItemUrlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function getCategoryUrlCacheKey($categoryId)

public function getUrl($category, $requestParameters)
{
if ($this->request->getFullActionName() != \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME) {
if ($this->request->getFullActionName() != \MageSuite\SeoLinkMasking\Helper\Page::AJAX_FILTER_FULL_ACTION_NAME) {
return $this->url->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $requestParameters]);
}

Expand Down
Loading

0 comments on commit d7a0b42

Please sign in to comment.