_registry = $registry; $this->_url = $url; $this->_storeManager = $storeManager; $this->_scopeConfig = $scopeConfig; } /** * Retrieve permalink type * @return string */ public function getPermalinkType() { return $this->_getConfig('type'); } /** * Retrieve route name by controller * @param string $controllerName * @param boolean $skip * @return string || null */ public function getRoute($controllerName = null, $skip = true) { if ($controllerName) { $controllerName .= '_'; } if ($route = $this->_getConfig($controllerName . 'route')) { return $route; } else { return $skip ? $controllerName : null; } } /** * Retrieve controller name by route * @param string $route * @param boolean $skip * @return string || null */ public function getControllerName($route, $skip = true) { foreach([ self::CONTROLLER_POST, self::CONTROLLER_CATEGORY, self::CONTROLLER_ARCHIVE, self::CONTROLLER_AUTHOR, self::CONTROLLER_TAG, self::CONTROLLER_SEARCH ] as $controllerName) { if ($this->getRoute($controllerName) == $route) { return $controllerName; } } return $skip ? $route : null; } /** * Retrieve blog base url * @return string */ public function getBaseUrl() { return $this->_url->getUrl($this->getRoute()); } /** * Retrieve blog page url * @param string $identifier * @param string $controllerName * @return string */ public function getUrl($identifier, $controllerName) { $url = $this->_url->getUrl('', [ '_direct' => $this->getUrlPath($identifier, $controllerName) ]); return $url; } /** * Retrieve canonical url * @param \Magento\Framework\Model\AbstractModel $object * @return string */ public function getCanonicalUrl(\Magento\Framework\Model\AbstractModel $object) { $storeIds = $object->getStoreIds(); $useDefaultStore = false; $currentStore = $this->_storeManager->getStore($object->getStoreId()); if (is_array($storeIds)) { if (0 == array_values($storeIds)[0]) { $useDefaultStore = true; } elseif (count($storeIds > 1)) { foreach ($storeIds as $storeId) { if ($storeId != $currentStore->getId()) { $store = $this->_storeManager->getStore($storeId); if ($store->getGroupId() == $currentStore->getGroupId()) { $useDefaultStore = true; break; } } } } } $storeChanged = false; if ($useDefaultStore) { $newStore = $currentStore->getGroup()->getDefaultStore(); $origStore = $this->_url->getScope(); if ($newStore->getId() != $origStore->getId()) { $this->_url->setScope($newStore); $storeChanged = true; } } $url = $this->getUrl($object->getIdentifier(), $object->getControllerName()); if ($storeChanged) { $this->_url->setScope($origStore); } return $url; } /** * Retrieve blog url path * @param string $identifier * @param string $controllerName * @return string */ public function getUrlPath($identifier, $controllerName) { $identifier = $this->getExpandedItentifier($identifier); switch ($this->getPermalinkType()) { case self::PERMALINK_TYPE_DEFAULT : $path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : ''); break; case self::PERMALINK_TYPE_SHORT : if ($controllerName == self::CONTROLLER_SEARCH || $controllerName == self::CONTROLLER_AUTHOR || $controllerName == self::CONTROLLER_TAG ) { $path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : ''); } else { $path = $this->getRoute() . '/' . $identifier . ( $identifier ? '/' : ''); } break; } $path = $this->addUrlSufix($path, $controllerName); return $path; } /** * Retrieve itentifier what include parent categories itentifier * @param \Magento\Framework\Model\AbstractModel || string $identifier * @return string */ protected function getExpandedItentifier($identifier) { if (is_object($identifier)) { $object = $identifier; $identifier = $identifier->getIdentifier(); $controllerName = $object->getControllerName(); if ($this->_getConfig($controllerName . '_use_categories') ) { if ($parentCategory = $object->getParentCategory()) { if ($parentIdentifier = $this->getExpandedItentifier($parentCategory)) { $identifier = $parentIdentifier . '/' . $identifier; } } } } return $identifier; } /** * Add url sufix * @param string $url * @param string $controllerName * @return string */ protected function addUrlSufix($url, $controllerName) { if (in_array($controllerName, [self::CONTROLLER_POST, self::CONTROLLER_CATEGORY])) { if ($sufix = $this->getUrlSufix($controllerName)) { $char = false; foreach(['#', '?'] as $ch) { if (false !== strpos($url, $ch)) { $char = $ch; } } if ($char) { $data = explode($char, $url); $data[0] = trim($data[0], '/') . $sufix; $url = implode($char, $url); } else { $url = trim($url, '/') . $sufix; } } } return $url; } /** * Retrieve trimmed url without sufix * @param string $identifier * @param string $sufix * @return string */ public function trimSufix($identifier, $sufix) { if ($sufix) { $p = mb_strrpos($identifier, $sufix); if (false !== $p) { $li = mb_strlen($identifier); $ls = mb_strlen($sufix); if ($p + $ls == $li) { $identifier = mb_substr($identifier, 0, $p); } } } return $identifier; } /** * Retrieve post url sufix * @return string */ public function getUrlSufix($controllerName) { return trim($this->_getConfig($controllerName . '_sufix')); } /** * Retrieve media url * @param string $file * @return string */ public function getMediaUrl($file) { return $this->_storeManager->getStore() ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $file; } /** * Retrieve blog permalink config value * @param string $key * @return string || null || int */ protected function _getConfig($key) { return $this->_scopeConfig->getValue( 'mfblog/permalink/'.$key, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->storeId ); } }