-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33aef1a
commit 681d36d
Showing
21 changed files
with
1,092 additions
and
861 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoXTemplatesGraphQl\Model\Category; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
|
||
/** | ||
* Modify category SEO params for category - crop dynamic variables | ||
*/ | ||
class CategoryModifier | ||
{ | ||
/** | ||
* @var \MageWorx\SeoXTemplates\Model\Observer\CategoryDataModifier | ||
*/ | ||
protected $categoryDataModifier; | ||
|
||
/** | ||
* @var \MageWorx\SeoXTemplates\Model\DynamicRenderer\Category | ||
*/ | ||
protected $categoryDynamicRenderer; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
protected $collectionFactory; | ||
|
||
/** | ||
* @var \MageWorx\SeoXTemplatesGraphQl\Model\RequestedFilterArgsStorage | ||
*/ | ||
protected $requestedFilterArgsStorage; | ||
|
||
/** | ||
* @var CategoryTemplateRendererAdapter | ||
*/ | ||
protected $templateRendererAdapter; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $attributes; | ||
|
||
/** | ||
* @var \Magento\Framework\Event\ManagerInterface | ||
*/ | ||
protected $eventManager; | ||
|
||
public function __construct( | ||
CollectionFactory $collectionFactory, | ||
\MageWorx\SeoXTemplates\Model\DynamicRenderer\Category $categoryDynamicRenderer, | ||
\MageWorx\SeoXTemplatesGraphQl\Model\RequestedFilterArgsStorage $requestedFilterArgsStorage, | ||
\MageWorx\SeoXTemplatesGraphQl\Model\Category\CategoryTemplateRendererAdapter $categoryTemplateRendererAdapter, | ||
\Magento\Framework\Event\ManagerInterface $eventManager, | ||
array $attributes = [] | ||
) { | ||
$this->categoryDynamicRenderer = $categoryDynamicRenderer; | ||
$this->collectionFactory = $collectionFactory; | ||
$this->requestedFilterArgsStorage = $requestedFilterArgsStorage; | ||
$this->templateRendererAdapter = $categoryTemplateRendererAdapter; | ||
$this->eventManager = $eventManager; | ||
$this->attributes = $attributes; | ||
} | ||
|
||
/** | ||
* @param mixed|Value $resolvedValue | ||
* @param ResolveInfo $info | ||
* @param array|null $args | ||
* @return mixed | ||
*/ | ||
public function modify( | ||
&$resolvedValue, | ||
ResolveInfo $info, | ||
array $args = null | ||
) { | ||
$fieldSelection = $info->getFieldSelection(1); | ||
$requestedAttributes = array_keys($fieldSelection['items']); | ||
$attributes = array_intersect($requestedAttributes, $this->attributes); | ||
|
||
if ($attributes) { | ||
$this->requestedFilterArgsStorage->disable(); | ||
|
||
$categoryIds = array_column($resolvedValue['items'], 'id'); | ||
$collection = $this->getCategoryCollection($categoryIds, $attributes); | ||
|
||
foreach ($resolvedValue['items'] as $key => $categoryData) { | ||
|
||
foreach ($attributes as $attribute) { | ||
|
||
if (empty($categoryData[$attribute])) { | ||
continue; | ||
} | ||
|
||
$category = $collection->getItemById($categoryData['id']); | ||
|
||
if ($category) { | ||
$category->setData($categoryData); | ||
$renderedValue = $this->templateRendererAdapter->getRenderedValue( | ||
$attribute, | ||
$category | ||
); | ||
$resolvedValue['items'][$key][$attribute] = $renderedValue; | ||
} | ||
} | ||
} | ||
|
||
$this->requestedFilterArgsStorage->enable(); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve loaded category collection | ||
* | ||
* @param array $ids | ||
* @param array $attributes | ||
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
protected function getCategoryCollection(array $ids, array $attributes) | ||
{ | ||
$collection = $this->collectionFactory->create(); | ||
$collection->addIdFilter($ids); | ||
$collection->addAttributeToSelect($attributes); | ||
|
||
$this->eventManager->dispatch( | ||
'mw_seoxtemplates_category_modifier_collection_load_before', | ||
['collection' => $collection] | ||
); | ||
|
||
$collection->addAttributeToSelect($this->getTemplateVariables()); | ||
|
||
$this->eventManager->dispatch( | ||
'mw_seoxtemplates_category_modifier_collection_load_after', | ||
['collection' => $collection] | ||
); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @todo we need to retrieve parsed SEO-template's variables | ||
* @return string[] | ||
*/ | ||
protected function getTemplateVariables(): array | ||
{ | ||
return ['name']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoXTemplatesGraphQl\Model\Category; | ||
|
||
class CategoryTemplateRendererAdapter | ||
{ | ||
/** | ||
* @var \MageWorx\SeoXTemplates\Model\DynamicRenderer\Category | ||
*/ | ||
protected $categoryDynamicRenderer; | ||
|
||
public function __construct( | ||
\MageWorx\SeoXTemplates\Model\DynamicRenderer\Category $categoryDynamicRenderer | ||
) { | ||
$this->categoryDynamicRenderer = $categoryDynamicRenderer; | ||
} | ||
|
||
/** | ||
* @param string $attribute | ||
* @param \Magento\Catalog\Model\Category $category | ||
* @return string | ||
*/ | ||
public function getRenderedValue($attribute, $category) | ||
{ | ||
$attributeValue = ''; | ||
|
||
if ('meta_title' === $attribute) { | ||
$this->categoryDynamicRenderer->modifyCategoryTitle($category, $attributeValue, true); | ||
} elseif ('meta_description' === $attribute) { | ||
$this->categoryDynamicRenderer->modifyCategoryMetaDescription($category, $attributeValue, true); | ||
} elseif ('meta_keywords' === $attribute) { | ||
$this->categoryDynamicRenderer->modifyCategoryMetaKeywords($category, $attributeValue, true); | ||
} elseif ('description' === $attribute) { | ||
$this->categoryDynamicRenderer->modifyCategoryDescription($category, $attributeValue, true); | ||
} elseif ('category_seo_name' === $attribute) { | ||
$attributeValue = $this->categoryDynamicRenderer->getModifiedCategorySeoName( | ||
$category, | ||
$category->getCategorySeoName(), | ||
true | ||
); | ||
} | ||
|
||
return $attributeValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoXTemplatesGraphQl\Model\Category\Products; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Adds category SEO params for products - replacing dynamic variables in category SEO-attributes using products filter | ||
*/ | ||
class CategoryDataFiller | ||
{ | ||
/** | ||
* @var \MageWorx\SeoXTemplates\Model\Observer\CategoryDataModifier | ||
*/ | ||
protected $categoryDataModifier; | ||
|
||
/** | ||
* @var \MageWorx\SeoXTemplates\Model\DynamicRenderer\Category | ||
*/ | ||
protected $categoryDynamicRenderer; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
protected $collectionFactory; | ||
|
||
/** | ||
* @var \MageWorx\SeoXTemplatesGraphQl\Model\RequestedFilterArgsStorage | ||
*/ | ||
protected $requestedFilterArgsStorage; | ||
|
||
/** | ||
* @var \MageWorx\SeoXTemplatesGraphQl\Model\Category\CategoryTemplateRendererAdapter | ||
*/ | ||
protected $categoryTemplateRendererAdapter; | ||
|
||
/** | ||
* @var \Magento\Framework\Event\ManagerInterface | ||
*/ | ||
protected $eventManager; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $attributes; | ||
|
||
public function __construct( | ||
CollectionFactory $collectionFactory, | ||
\MageWorx\SeoXTemplates\Model\DynamicRenderer\Category $categoryDynamicRenderer, | ||
\MageWorx\SeoXTemplatesGraphQl\Model\RequestedFilterArgsStorage $requestedFilterArgsStorage, | ||
\MageWorx\SeoXTemplatesGraphQl\Model\Category\CategoryTemplateRendererAdapter $categoryTemplateRendererAdapter, | ||
\Magento\Framework\Event\ManagerInterface $eventManager, | ||
array $attributes = [] | ||
) { | ||
$this->categoryDynamicRenderer = $categoryDynamicRenderer; | ||
$this->collectionFactory = $collectionFactory; | ||
$this->requestedFilterArgsStorage = $requestedFilterArgsStorage; | ||
$this->categoryTemplateRendererAdapter = $categoryTemplateRendererAdapter; | ||
$this->eventManager = $eventManager; | ||
$this->attributes = $attributes; | ||
} | ||
|
||
/** | ||
* @param mixed|Value $resolvedValue | ||
* @param ResolveInfo $info | ||
* @param array|null $args | ||
* @return mixed | ||
*/ | ||
public function modify( | ||
&$resolvedValue, | ||
ResolveInfo $info, | ||
array $args = null | ||
) { | ||
$fieldSelection = $info->getFieldSelection(1); | ||
|
||
if (!empty($fieldSelection['mw_seo_category_data']) | ||
&& !empty($resolvedValue['categories']) | ||
&& count($resolvedValue['categories']) === 1 | ||
&& $resolvedValue['layer_type'] === 'category' | ||
) { | ||
$requestedAttributes = array_keys($fieldSelection['mw_seo_category_data']); | ||
|
||
$attributes = array_intersect($requestedAttributes, $this->attributes); | ||
|
||
if ($attributes) { | ||
|
||
$this->requestedFilterArgsStorage->set($args['filter']); | ||
$collection = $this->getCategoryCollection([$resolvedValue['categories'][0]], $attributes); | ||
/** @var \Magento\Catalog\Model\Category $category */ | ||
$category = $collection->getFirstItem(); | ||
|
||
if ($category->getId()) { | ||
$resolvedValue['mw_seo_category_data'] = $fieldSelection['mw_seo_category_data']; | ||
|
||
foreach ($attributes as $attribute) { | ||
$renderedValue = $this->categoryTemplateRendererAdapter->getRenderedValue( | ||
$attribute, | ||
$category | ||
); | ||
|
||
$resolvedValue['mw_seo_category_data'][$attribute] = $renderedValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $resolvedValue; | ||
} | ||
|
||
/** | ||
* Retrieve loaded category collection | ||
* | ||
* @param array $ids | ||
* @param array $attributes | ||
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
protected function getCategoryCollection(array $ids, array $attributes) | ||
{ | ||
$collection = $this->collectionFactory->create(); | ||
$collection->addIdFilter($ids); | ||
$collection->addAttributeToSelect($attributes); | ||
$collection->addAttributeToSelect($this->getTemplateVariables()); | ||
|
||
$this->eventManager->dispatch( | ||
'mw_seoxtemplates_category_data_filler_collection_load_before', | ||
['collection' => $collection] | ||
); | ||
|
||
$collection->load(); | ||
|
||
$this->eventManager->dispatch( | ||
'mw_seoxtemplates_category_data_filler_collection_load_after', | ||
['collection' => $collection] | ||
); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* @todo we need to retrieve parsed SEO-template's variables | ||
* @return string[] | ||
*/ | ||
protected function getTemplateVariables() | ||
{ | ||
return ['name']; | ||
} | ||
} |
Oops, something went wrong.