|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ManiyaTech |
| 4 | + * |
| 5 | + * @author Milan Maniya |
| 6 | + * @package ManiyaTech_Seo |
| 7 | + */ |
| 8 | + |
| 9 | +namespace ManiyaTech\Seo\Helper; |
| 10 | + |
| 11 | +use Magento\Framework\App\Helper\Context; |
| 12 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 13 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\App\Helper\AbstractHelper; |
| 16 | +use Magento\Framework\Model\AbstractModel; |
| 17 | +use Magento\Store\Model\ScopeInterface; |
| 18 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 19 | + |
| 20 | +class Data extends AbstractHelper |
| 21 | +{ |
| 22 | + private const XML_PATH_MODULE_ENABLED = 'seo_config/general/enabled'; |
| 23 | + private const XML_PATH_PRODUCT_MODULE = 'seo_config/product/enabled'; |
| 24 | + private const CATEGORY_TAGS = ['CL1', 'CL2', 'CL3', 'CL4', 'CL5', 'CL6']; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var ScopeConfigInterface |
| 28 | + */ |
| 29 | + protected $scopeConfig; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ProductRepositoryInterface |
| 33 | + */ |
| 34 | + protected $productRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CategoryRepositoryInterface |
| 38 | + */ |
| 39 | + protected $categoryRepository; |
| 40 | + |
| 41 | + /** |
| 42 | + * AbstractData constructor. |
| 43 | + * |
| 44 | + * @param Context $context |
| 45 | + * @param ScopeConfigInterface $scopeConfig |
| 46 | + * @param ProductRepositoryInterface $productRepository |
| 47 | + * @param CategoryRepositoryInterface $categoryRepository |
| 48 | + */ |
| 49 | + public function __construct( |
| 50 | + Context $context, |
| 51 | + ScopeConfigInterface $scopeConfig, |
| 52 | + ProductRepositoryInterface $productRepository, |
| 53 | + CategoryRepositoryInterface $categoryRepository |
| 54 | + ) { |
| 55 | + $this->scopeConfig = $scopeConfig; |
| 56 | + $this->productRepository = $productRepository; |
| 57 | + $this->categoryRepository = $categoryRepository; |
| 58 | + parent::__construct($context); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check if the SEO module is enabled in system configuration. |
| 63 | + * |
| 64 | + * @return bool |
| 65 | + */ |
| 66 | + public function isModuleEnabled(): bool |
| 67 | + { |
| 68 | + return (bool) $this->scopeConfig->getValue(self::XML_PATH_MODULE_ENABLED, ScopeInterface::SCOPE_STORE); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check if product meta updates are enabled. |
| 73 | + * |
| 74 | + * @return bool |
| 75 | + */ |
| 76 | + public function isProductMetaEnabled(): bool |
| 77 | + { |
| 78 | + return (bool) $this->scopeConfig->getValue(self::XML_PATH_PRODUCT_MODULE, ScopeInterface::SCOPE_STORE); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if category meta updates are enabled for a specific level. |
| 83 | + * |
| 84 | + * @param int|string $level Category depth level. |
| 85 | + * @return bool |
| 86 | + */ |
| 87 | + public function isCategoryMetaEnabled($level): bool |
| 88 | + { |
| 89 | + return (bool) $this->scopeConfig->getValue( |
| 90 | + 'seo_config/category' . $level . '/enabled', |
| 91 | + ScopeInterface::SCOPE_STORE |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieve the configured meta value for a category and resolve dynamic shortcodes. |
| 97 | + * |
| 98 | + * @param string $level Category level (e.g. "1", "2", etc.) |
| 99 | + * @param string $field Meta field key (e.g. "meta_title", "meta_description") |
| 100 | + * @param AbstractModel $category Category model instance |
| 101 | + * @return string|null |
| 102 | + */ |
| 103 | + public function getCategoryMeta(string $level, string $field, AbstractModel $category): ?string |
| 104 | + { |
| 105 | + $value = $this->scopeConfig->getValue("seo_config/category{$level}/{$field}", ScopeInterface::SCOPE_STORE); |
| 106 | + return !empty($value) ? $this->shortcode($value, $category) : null; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Retrieve the configured meta value for a product and resolve dynamic shortcodes. |
| 111 | + * |
| 112 | + * @param string $field Meta field key (e.g. "meta_title", "meta_description") |
| 113 | + * @param AbstractModel $product Product model instance |
| 114 | + * @return string|null |
| 115 | + */ |
| 116 | + public function getProductMeta(string $field, AbstractModel $product): ?string |
| 117 | + { |
| 118 | + $value = $this->scopeConfig->getValue("seo_config/product/{$field}", ScopeInterface::SCOPE_STORE); |
| 119 | + return !empty($value) ? $this->shortcode($value, $product) : null; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Parse template string and replace shortcodes like [name], [sku], [CL1], etc. |
| 124 | + * |
| 125 | + * @param string $template The template string containing placeholders |
| 126 | + * @param AbstractModel $entity Product or Category instance |
| 127 | + * @return string |
| 128 | + */ |
| 129 | + public function shortcode(string $template, AbstractModel $entity): string |
| 130 | + { |
| 131 | + preg_match_all('/\[(.*?)\]/', $template, $matches); |
| 132 | + |
| 133 | + if (empty($matches[1])) { |
| 134 | + return $template; |
| 135 | + } |
| 136 | + |
| 137 | + $isProduct = method_exists($entity, 'getTypeId') && !empty($entity->getTypeId()); |
| 138 | + |
| 139 | + foreach ($matches[1] as $i => $tag) { |
| 140 | + $replacement = ''; |
| 141 | + |
| 142 | + if ($isProduct) { |
| 143 | + try { |
| 144 | + $product = $this->productRepository->getById((int) $entity->getId()); |
| 145 | + $replacement = (string) $product->getData($tag); |
| 146 | + } catch (NoSuchEntityException $e) { |
| 147 | + $replacement = ''; |
| 148 | + } |
| 149 | + } else { |
| 150 | + $replacement = $this->resolveCategoryTag($tag, $entity); |
| 151 | + } |
| 152 | + |
| 153 | + $template = str_replace($matches[0][$i], $replacement, $template); |
| 154 | + } |
| 155 | + |
| 156 | + return $template; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Resolve category-specific shortcodes like [CL1], [CL2], etc. |
| 161 | + * |
| 162 | + * @param string $tag Tag placeholder to resolve |
| 163 | + * @param AbstractModel $category Category entity |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + private function resolveCategoryTag(string $tag, AbstractModel $category): string |
| 167 | + { |
| 168 | + $path = $category->getPath() ?? ''; |
| 169 | + $pathArray = explode('/', $path); |
| 170 | + |
| 171 | + if (in_array($tag, self::CATEGORY_TAGS, true)) { |
| 172 | + $index = (int) substr($tag, -1) + 1; |
| 173 | + |
| 174 | + if (isset($pathArray[$index])) { |
| 175 | + try { |
| 176 | + $parentCategory = $this->categoryRepository->get((int) $pathArray[$index]); |
| 177 | + return $parentCategory->getName() ?? ''; |
| 178 | + } catch (NoSuchEntityException $e) { |
| 179 | + return ''; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return (string) $category->getData($tag); |
| 185 | + } |
| 186 | +} |
0 commit comments