dataHelper = $dataHelper;
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->registry = $registry;
$this->page = $page;
$this->connectionDB = $connectionDB;
$this->urlFinder = $urlFinder;
parent::__construct($context, $data);
}
/**
* @param int $storeId
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getCurrentUrlHreflang($storeId)
{
return $this->processUrl($this->_storeManager->getStore($storeId)->getCurrentUrl(false));
}
/**
* @return int
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getStoreId()
{
return $this->_storeManager->getStore()->getId();
}
/**
* @return \Bss\HrefLang\Helper\Data
*/
public function getDataHelper()
{
return $this->dataHelper;
}
/**
* @return string
*/
public function getFullActionName()
{
return $this->getRequest()->getFullActionName();
}
/**
* @param string $type
* @param int $storeId
* @return bool|string
*/
public function getCategoryUrlHreflang($type, $storeId)
{
$categoryId = $this->getCurrentId($type);
try {
$category = $this->categoryRepository->create()->load($categoryId);
if ($category->getIsActive() != 1) {
return false;
}
$requestPath = $this->getCategoryRequestPath($categoryId, $storeId);
if ($requestPath) {
$url = $this->_storeManager->getStore($storeId)->getUrl($requestPath, ['_current' => false]);
$categoryUrl = $url . $this->insertParamsToHreflang();
return $this->processUrl($categoryUrl);
}
return false;
} catch (\Exception $e) {
return false;
}
}
/**
* @param string $type
* @param int $storeId
* @param int $websiteId
* @return bool|string
*/
public function getProductUrlHreflang($type, $storeId, $websiteId)
{
$productId = $this->getCurrentId($type);
try {
$product = $this->productRepository->getById($productId, false, $storeId);
$productWebsites = $product->getWebsiteIds();
if ((int)$product->getStatus() !== 1) {
return false;
}
if (!in_array($websiteId, $productWebsites)) {
return false;
}
$productUrl = $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true])
. $this->insertParamsToHreflang();
return $this->processUrl($productUrl);
} catch (\Exception $e) {
return false;
}
}
/**
* @param int $storeId
* @return bool|string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getCmsPageUrlHreflang($storeId)
{
$currentCmsPage = $this->getCurrentCmsPage();
if (!empty($currentCmsPage)) {
if (in_array(0, $currentCmsPage['store_id']) || in_array($storeId, $currentCmsPage['store_id'])) {
$storeCmsPage = $this->connectionDB->getCms($storeId, $currentCmsPage['id'], false);
if (empty($storeCmsPage)) {
$storeCmsPage = $this->connectionDB->getCms(false, $currentCmsPage['id'], false);
}
if (empty($storeCmsPage)) {
return false;
}
$storeCmsPageUrl = $storeCmsPage['request_path'];
} else {
$storeCmsPage = $this->connectionDB->getCms($storeId, false, $currentCmsPage['ident']);
if (empty($storeCmsPage) || $storeCmsPage['is_active'] == 0) {
return false;
}
$storeCmsPageUrl = $storeCmsPage['request_path'];
}
$cmsUrl = $this->_storeManager->getStore($storeId)->getUrl($storeCmsPageUrl, ['_current' => false])
. $this->insertParamsToHreflang();
return $this->processUrl($cmsUrl);
}
return false;
}
/**
* @param string $url
* @return string
*/
protected function processUrl($url)
{
if (!$url || $url === '' || $url === null) {
return '';
}
$urlObject = parse_url($url);
$hostUrl = $urlObject['host'];
$pathUrl = $urlObject['path'];
$schemeUrl = $urlObject['scheme'];
$queryUrl = (isset($urlObject['query'])) ? $urlObject['query'] : '';
$pathUrl = rtrim($pathUrl, '/');
//Process Query, Remove SID
$queryAfter = '';
if ($queryUrl) {
$queryObject = explode('&', $queryUrl);
foreach ($queryObject as $key => $value) {
if (strpos($value, 'SID') !== false) {
unset($queryObject[$key]);
}
}
$queryAfter = implode('&', $queryObject);
$queryStringAdd = ($queryAfter !== '') ? '?' : '';
$queryAfter = $queryStringAdd . $queryAfter;
}
return $schemeUrl . '://' . $hostUrl . $pathUrl . $queryAfter;
}
/**
* @return string
*/
protected function insertParamsToHreflang()
{
$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$currentUrl = $this->processUrl($currentUrl);
$urlObject = parse_url($currentUrl);
$queryUrl = (isset($urlObject['query'])) ? $urlObject['query'] : '';
if ($queryUrl !== '' && $queryUrl !== null) {
$queryUrl = '?' . $queryUrl;
}
return $queryUrl;
}
/**
* @return array
*/
protected function getCurrentCmsPage()
{
if (!empty($this->cms)) {
return $this->cms;
}
if ($this->page->getId()) {
$this->cms['id'] = $this->page->getId();
$this->cms['ident'] = $this->page->getIdentifier();
$this->cms['store_id'] = $this->page->getStoreId();
}
return $this->cms;
}
/**
* @param string $type
* @return int
*/
public function getCurrentId($type)
{
if ($this->id != 0) {
return $this->id;
}
$data = $this->registry->registry($type);
$this->id = $data->getId();
return $this->id;
}
/**
* @param int $storeId
* @return \Magento\Store\Api\Data\StoreInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getStoreFromId($storeId)
{
return $this->_storeManager->getStore($storeId);
}
/**
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getHrefLang()
{
//Url For HREFLANG
$storeId = $this->getStoreId();
$hreflangStore = $this->dataHelper->getStoreHreflang($storeId);
$hreflangTag = [];
$handle = $this->getFullActionName();
if (is_array($hreflangStore)) {
foreach ($hreflangStore as $value) {
$country = strtolower($value['country']);
$store = $this->getStoreFromId($value['store']);
if (!$store->isActive()) {
continue;
}
$websiteId = $store->getWebsiteId();
if ($handle == 'catalog_category_view') {
$hreflangUrl = $this->getCategoryUrlHreflang('current_category', $value['store']);
} elseif ($handle == 'catalog_product_view') {
$hreflangUrl = $this->getProductUrlHreflang('current_product', $value['store'], $websiteId);
} elseif ($handle == 'cms_page_view') {
$hreflangUrl = $this->getCmsPageUrlHreflang($value['store']);
} else {
$hreflangUrl = $this->getCurrentUrlHreflang($value['store']);
}
if (!$hreflangUrl) {
continue;
}
if ($country == 'not_assign') {
$hreflangTag[] =
'' . PHP_EOL;
} else {
$hreflangTag[] =
'' . PHP_EOL;
}
}
}
return $hreflangTag;
}
/**
* @param string $categoryId
* @param int $storeId
* @return bool|string
*/
protected function getCategoryRequestPath($categoryId, $storeId)
{
$rewrite = $this->urlFinder->findOneByData([
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $storeId,
]);
if ($rewrite) {
return $rewrite->getRequestPath();
}
return false;
}
}