123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- /**
- * Mageplaza
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Mageplaza.com license that is
- * available through the world-wide-web at this URL:
- * https://www.mageplaza.com/LICENSE.txt
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade this extension to newer
- * version in the future.
- *
- * @category Mageplaza
- * @package Mageplaza_Core
- * @copyright Copyright (c) 2016-2018 Mageplaza (http://www.mageplaza.com/)
- * @license https://www.mageplaza.com/LICENSE.txt
- */
- namespace Mageplaza\Core\Helper;
- use Magento\Framework\App\Area;
- use Magento\Framework\App\Helper\AbstractHelper;
- use Magento\Framework\App\Helper\Context;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\App\ProductMetadataInterface;
- use Magento\Framework\Json\Helper\Data as JsonHelper;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\UrlInterface;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Store\Model\StoreManagerInterface;
- /**
- * Class AbstractData
- * @package Mageplaza\Core\Helper
- */
- class AbstractData extends AbstractHelper
- {
- const CONFIG_MODULE_PATH = 'mageplaza';
- /**
- * @type array
- */
- protected $_data = [];
- /**
- * @type \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManager;
- /**
- * @type \Magento\Framework\ObjectManagerInterface
- */
- protected $objectManager;
- /**
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Framework\ObjectManagerInterface $objectManager
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- */
- public function __construct(
- Context $context,
- ObjectManagerInterface $objectManager,
- StoreManagerInterface $storeManager
- )
- {
- $this->objectManager = $objectManager;
- $this->storeManager = $storeManager;
- parent::__construct($context);
- }
- /**
- * @param null $storeId
- * @return bool
- */
- public function isEnabled($storeId = null)
- {
- return $this->getConfigGeneral('enabled', $storeId);
- }
- /**
- * @param string $code
- * @param null $storeId
- * @return mixed
- */
- public function getConfigGeneral($code = '', $storeId = null)
- {
- $code = ($code !== '') ? '/' . $code : '';
- return $this->getConfigValue(static::CONFIG_MODULE_PATH . '/general' . $code, $storeId);
- }
- /**
- * @param string $field
- * @param null $storeId
- * @return mixed
- */
- public function getModuleConfig($field = '', $storeId = null)
- {
- $field = ($field !== '') ? '/' . $field : '';
- return $this->getConfigValue(static::CONFIG_MODULE_PATH . $field, $storeId);
- }
- /**
- * @param $field
- * @param null $storeId
- * @return mixed
- */
- public function getConfigValue($field, $storeId = null)
- {
- return $this->scopeConfig->getValue(
- $field,
- ScopeInterface::SCOPE_STORE,
- $storeId
- );
- }
- /**
- * @param $name
- * @return null
- */
- public function getData($name)
- {
- if (array_key_exists($name, $this->_data)) {
- return $this->_data[$name];
- }
- return null;
- }
- /**
- * @param $name
- * @param $value
- * @return $this
- */
- public function setData($name, $value)
- {
- $this->_data[$name] = $value;
- return $this;
- }
- /**
- * @return mixed
- */
- public function getCurrentUrl()
- {
- $model = $this->objectManager->get(UrlInterface::class);
- return $model->getCurrentUrl();
- }
- /**
- * @param $data
- * @return string
- * @throws \Zend_Serializer_Exception
- */
- public function serialize($data)
- {
- if ($this->versionCompare('2.2.0')) {
- return self::jsonEncode($data);
- }
- return $this->getSerializeClass()->serialize($data);
- }
- /**
- * @param $ver
- * @return mixed
- */
- public function versionCompare($ver)
- {
- $productMetadata = $this->objectManager->get(ProductMetadataInterface::class);
- $version = $productMetadata->getVersion(); //will return the magento version
- return version_compare($version, $ver, '>=');
- }
- /**
- * Encode the mixed $valueToEncode into the JSON format
- *
- * @param mixed $valueToEncode
- * @return string
- */
- public static function jsonEncode($valueToEncode)
- {
- try {
- $encodeValue = self::getJsonHelper()->jsonEncode($valueToEncode);
- } catch (\Exception $e) {
- $encodeValue = '{}';
- }
- return $encodeValue;
- }
- /**
- * @return \Magento\Framework\Json\Helper\Data|mixed
- */
- public static function getJsonHelper()
- {
- return ObjectManager::getInstance()->get(JsonHelper::class);
- }
- /**
- * @return \Zend_Serializer_Adapter_PhpSerialize|mixed
- */
- protected function getSerializeClass()
- {
- return $this->objectManager->get(\Zend_Serializer_Adapter_PhpSerialize::class);
- }
- /**
- * @param $string
- * @return mixed
- * @throws \Zend_Serializer_Exception
- */
- public function unserialize($string)
- {
- if ($this->versionCompare('2.2.0')) {
- return self::jsonDecode($string);
- }
- return $this->getSerializeClass()->unserialize($string);
- }
- /**
- * Decodes the given $encodedValue string which is
- * encoded in the JSON format
- *
- * @param string $encodedValue
- * @return mixed
- */
- public static function jsonDecode($encodedValue)
- {
- try {
- $decodeValue = self::getJsonHelper()->jsonDecode($encodedValue);
- } catch (\Exception $e) {
- $decodeValue = [];
- }
- return $decodeValue;
- }
- /**
- * Is Admin Store
- *
- * @return bool
- */
- public function isAdmin()
- {
- /** @var \Magento\Framework\App\State $state */
- $state = $this->objectManager->get('Magento\Framework\App\State');
- try {
- $areaCode = $state->getAreaCode();
- } catch (\Exception $e) {
- return false;
- }
- return $areaCode == Area::AREA_ADMINHTML;
- }
- /**
- * @param $path
- * @param array $arguments
- * @return mixed
- */
- public function createObject($path, $arguments = [])
- {
- return $this->objectManager->create($path, $arguments);
- }
- /**
- * @param $path
- * @return mixed
- */
- public function getObject($path)
- {
- return $this->objectManager->get($path);
- }
- }
|