diff --git a/Controller/Adminhtml/Index/Activate.php b/Controller/Adminhtml/Index/Activate.php index 39f8933..a5d59fe 100644 --- a/Controller/Adminhtml/Index/Activate.php +++ b/Controller/Adminhtml/Index/Activate.php @@ -1,224 +1,224 @@ -activateFactory = $activateFactory; - $this->resourceConfig = $resourceConfig; - $this->_appConfig = $config; - $this->_coreHelper = $helper; - - parent::__construct($context); - } - - /** - * Execute view action - * - * @return ResultInterface - */ - public function execute() - { - $params = $this->getRequest()->getPost()->toArray(); - if (!isset($params['extension'])) { - return $this->jsonResponse([ - 'success' => false, - 'message' => __('Invalid data.') - ]); - } - - if ($path = $this->_coreHelper->getConfigModulePath($params['extension'])) { - $this->_moduleConfigPath = $path; - } - - $activateModel = $this->activateFactory->create(); - $result = $activateModel->activate($params); - if ($result['success']) { - $result['active'] = true; - - $configSave = ['active' => 1]; - if (isset($result['key']) && $result['key']) { - $configSave['product_key'] = $result['key']; - } - - if ($this->_coreHelper->getModuleType($params['extension']) == '1') { - $freeInfo = [ - 'email' => $params['email'], - 'name' => $params['name'], - 'create' => $params['create'], - 'subscribe' => $params['subscribe'] - ]; - foreach ($freeInfo as $code => $value) { - $this->saveConfig('free/module/' . $code, $value, true); - } - - $configSave += $freeInfo; - } - - $this->saveConfig($configSave); - $this->_appConfig->reinit(); - } - - return $this->jsonResponse($result); - } - - /** - * @param $result - * - * @return mixed - */ - protected function jsonResponse($result) - { - return $this->getResponse()->representJson( - Validate::jsonEncode($result) - ); - } - - /** - * @param $pathId - * @param null $value - * @param bool $isFullPath - * @param string $scope - * @param int $scopeId - * - * @return $this - */ - protected function saveConfig($pathId, $value = null, $isFullPath = false, $scope = 'default', $scopeId = 0) - { - if (is_array($pathId)) { - foreach ($pathId as $path => $pathValue) { - $this->saveConfig($path, $pathValue, $isFullPath, $scope, $scopeId); - } - - return $this; - } - - $this->resourceConfig->saveConfig( - $isFullPath ? $pathId : $this->buildConfigPath($pathId), - $value, - $scope, - $scopeId - ); - - return $this; - } - - /** - * @param $pathId - * - * @return string - */ - protected function buildConfigPath($pathId) - { - return $this->_moduleConfigPath . '/module/' . $pathId; - } - - /** - * @param $pathId - * @param bool $isFullPath - * @param string $scope - * @param int $scopeId - * - * @return $this - */ - protected function deleteConfig($pathId, $isFullPath = false, $scope = 'default', $scopeId = 0) - { - if (is_array($pathId)) { - foreach ($pathId as $path) { - $this->deleteConfig($path, $isFullPath, $scope, $scopeId); - } - - return $this; - } - - $this->resourceConfig->deleteConfig( - $isFullPath ? $pathId : $this->buildConfigPath($pathId), - $scope, - $scopeId - ); - - return $this; - } -} +activateFactory = $activateFactory; + $this->resourceConfig = $resourceConfig; + $this->_appConfig = $config; + $this->_coreHelper = $helper; + + parent::__construct($context); + } + + /** + * Execute view action + * + * @return ResultInterface + */ + public function execute() + { + $params = $this->getRequest()->getPost()->toArray(); + if (!isset($params['extension'])) { + return $this->jsonResponse([ + 'success' => false, + 'message' => __('Invalid data.') + ]); + } + + if ($path = $this->_coreHelper->getConfigModulePath($params['extension'])) { + $this->_moduleConfigPath = $path; + } + + $activateModel = $this->activateFactory->create(); + $result = $activateModel->activate($params); + if ($result['success']) { + $result['active'] = true; + + $configSave = ['active' => 1]; + if (isset($result['key']) && $result['key']) { + $configSave['product_key'] = $result['key']; + } + + if ($this->_coreHelper->getModuleType($params['extension']) == '1') { + $freeInfo = [ + 'email' => $params['email'], + 'name' => $params['name'], + 'create' => $params['create'], + 'subscribe' => $params['subscribe'] + ]; + foreach ($freeInfo as $code => $value) { + $this->saveConfig('free/module/' . $code, $value, true); + } + + $configSave += $freeInfo; + } + + $this->saveConfig($configSave); + $this->_appConfig->reinit(); + } + + return $this->jsonResponse($result); + } + + /** + * @param $result + * + * @return mixed + */ + protected function jsonResponse($result) + { + return $this->getResponse()->representJson( + Validate::jsonEncode($result) + ); + } + + /** + * @param $pathId + * @param null $value + * @param bool $isFullPath + * @param string $scope + * @param int $scopeId + * + * @return $this + */ + protected function saveConfig($pathId, $value = null, $isFullPath = false, $scope = 'default', $scopeId = 0) + { + if (is_array($pathId)) { + foreach ($pathId as $path => $pathValue) { + $this->saveConfig($path, $pathValue, $isFullPath, $scope, $scopeId); + } + + return $this; + } + + $this->resourceConfig->saveConfig( + $isFullPath ? $pathId : $this->buildConfigPath($pathId), + $value, + $scope, + $scopeId + ); + + return $this; + } + + /** + * @param $pathId + * + * @return string + */ + protected function buildConfigPath($pathId) + { + return $this->_moduleConfigPath . '/module/' . $pathId; + } + + /** + * @param $pathId + * @param bool $isFullPath + * @param string $scope + * @param int $scopeId + * + * @return $this + */ + protected function deleteConfig($pathId, $isFullPath = false, $scope = 'default', $scopeId = 0) + { + if (is_array($pathId)) { + foreach ($pathId as $path) { + $this->deleteConfig($path, $isFullPath, $scope, $scopeId); + } + + return $this; + } + + $this->resourceConfig->deleteConfig( + $isFullPath ? $pathId : $this->buildConfigPath($pathId), + $scope, + $scopeId + ); + + return $this; + } +} diff --git a/Cron/GetUpdate.php b/Cron/GetUpdate.php new file mode 100755 index 0000000..4ba8a47 --- /dev/null +++ b/Cron/GetUpdate.php @@ -0,0 +1,140 @@ +helperValidate = $helperValidate; + $this->componentRegistrar = $componentRegistrar; + $this->readFactory = $readFactory; + $this->curlFactory = $curlFactory; + $this->notifierPool = $notifierPool; + $this->logger = $logger; + } + + /** + * @return $this + */ + public function execute() + { + if (!$this->helperValidate->isEnabledNotificationUpdate()) { + return $this; + } + + $moduleList = $this->helperValidate->getModuleList(); + $edition = $this->helperValidate->getEdition(); + + $modules = []; + foreach ($moduleList as $moduleName){ + if ($moduleName === 'Mageplaza_Core') { + continue; + } + + try{ + $path = $this->componentRegistrar->getPath( + ComponentRegistrar::MODULE, + $moduleName + ); + $directoryRead = $this->readFactory->create($path); + $composerJsonData = $directoryRead->readFile('composer.json'); + $data = json_decode($composerJsonData, true); + $modules[$data['name']] = $data['version']; + }catch (\Exception $exception){ + continue; + } + } + + try { + $curl = $this->curlFactory->create(); + $curl->post(self::CHECK_VERSION_URL, ['magento_edition' => $edition, 'modules' => $modules]); + $response = $curl->getBody(); + + if ($response) { + $response = Validate::jsonDecode($response); + if (isset($response['is_update']) && $response['is_update']) { + $this->notifierPool->addNotice('Mageplaza Notice', $response['message'],self::DASHBOARD_URL); + } + } + + } catch (\Exception $e) { + $this->logger->critical($e->getMessage()); + } + } +} diff --git a/Helper/AbstractData.php b/Helper/AbstractData.php old mode 100644 new mode 100755 index 6c98276..c56a053 --- a/Helper/AbstractData.php +++ b/Helper/AbstractData.php @@ -1,329 +1,355 @@ -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 $scopeValue - * @param string $scopeType - * - * @return array|mixed - */ - public function getConfigValue($field, $scopeValue = null, $scopeType = ScopeInterface::SCOPE_STORE) - { - if ($scopeValue === null && !$this->isArea()) { - /** @var Config $backendConfig */ - if (!$this->backendConfig) { - $this->backendConfig = $this->objectManager->get(\Magento\Backend\App\ConfigInterface::class); - } - - return $this->backendConfig->getValue($field); - } - - return $this->scopeConfig->getValue($field, $scopeType, $scopeValue); - } - - /** - * @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 $ver - * @param string $operator - * - * @return mixed - */ - public function versionCompare($ver, $operator = '>=') - { - $productMetadata = $this->objectManager->get(ProductMetadataInterface::class); - $version = $productMetadata->getVersion(); //will return the magento version - - return version_compare($version, $ver, $operator); - } - - /** - * @param $data - * - * @return string - */ - public function serialize($data) - { - if ($this->versionCompare('2.2.0')) { - return self::jsonEncode($data); - } - - return $this->getSerializeClass()->serialize($data); - } - - /** - * @param $string - * - * @return mixed - */ - public function unserialize($string) - { - if ($this->versionCompare('2.2.0')) { - return self::jsonDecode($string); - } - - return $this->getSerializeClass()->unserialize($string); - } - - /** - * 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; - } - - /** - * 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() - { - return $this->isArea(Area::AREA_ADMINHTML); - } - - /** - * @param string $area - * - * @return mixed - */ - public function isArea($area = Area::AREA_FRONTEND) - { - if (!isset($this->isArea[$area])) { - /** @var State $state */ - $state = $this->objectManager->get(\Magento\Framework\App\State::class); - - try { - $this->isArea[$area] = ($state->getAreaCode() == $area); - } catch (Exception $e) { - $this->isArea[$area] = false; - } - } - - return $this->isArea[$area]; - } - - /** - * @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); - } - - /** - * @return JsonHelper|mixed - */ - public static function getJsonHelper() - { - return ObjectManager::getInstance()->get(JsonHelper::class); - } - - /** - * @return mixed - */ - protected function getSerializeClass() - { - return $this->objectManager->get('Zend_Serializer_Adapter_PhpSerialize'); - } -} +objectManager = $objectManager; + $this->storeManager = $storeManager; + + parent::__construct($context); + } + + /** + * @param null $storeId + * + * @return bool + */ + public function isEnabled($storeId = null) + { + return $this->getConfigGeneral('enabled', $storeId); + } + + /** + * @param null $storeId + * + * @return bool + */ + public function isEnabledNotificationUpdate($storeId = null) + { + $isEnable = $this->getConfigGeneral('notice_enable', $storeId); + $noticeType = $this->getConfigGeneral('notice_type', $storeId); + if ($noticeType) { + $noticeType = explode(',', $noticeType); + $noticeType = in_array(NoticeType::TYPE_NEWUPDATE, $noticeType); + } + + return $isEnable && $noticeType; + } + + /** + * @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 $scopeValue + * @param string $scopeType + * + * @return array|mixed + */ + public function getConfigValue($field, $scopeValue = null, $scopeType = ScopeInterface::SCOPE_STORE) + { + if ($scopeValue === null && !$this->isArea()) { + /** @var Config $backendConfig */ + if (!$this->backendConfig) { + $this->backendConfig = $this->objectManager->get(\Magento\Backend\App\ConfigInterface::class); + } + + return $this->backendConfig->getValue($field); + } + + return $this->scopeConfig->getValue($field, $scopeType, $scopeValue); + } + + /** + * @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 $ver + * @param string $operator + * + * @return mixed + */ + public function versionCompare($ver, $operator = '>=') + { + $productMetadata = $this->objectManager->get(ProductMetadataInterface::class); + $version = $productMetadata->getVersion(); //will return the magento version + + return version_compare($version, $ver, $operator); + } + + /** + * @param $data + * + * @return string + */ + public function serialize($data) + { + if ($this->versionCompare('2.2.0')) { + return self::jsonEncode($data); + } + + return $this->getSerializeClass()->serialize($data); + } + + /** + * @param $string + * + * @return mixed + */ + public function unserialize($string) + { + if ($this->versionCompare('2.2.0')) { + return self::jsonDecode($string); + } + + return $this->getSerializeClass()->unserialize($string); + } + + /** + * 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; + } + + /** + * 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() + { + return $this->isArea(Area::AREA_ADMINHTML); + } + + /** + * @param string $area + * + * @return mixed + */ + public function isArea($area = Area::AREA_FRONTEND) + { + if (!isset($this->isArea[$area])) { + /** @var State $state */ + $state = $this->objectManager->get(\Magento\Framework\App\State::class); + + try { + $this->isArea[$area] = ($state->getAreaCode() == $area); + } catch (Exception $e) { + $this->isArea[$area] = false; + } + } + + return $this->isArea[$area]; + } + + /** + * @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); + } + + /** + * @return JsonHelper|mixed + */ + public static function getJsonHelper() + { + return ObjectManager::getInstance()->get(JsonHelper::class); + } + + /** + * @return mixed + */ + protected function getSerializeClass() + { + return $this->objectManager->get('Zend_Serializer_Adapter_PhpSerialize'); + } + + /** + * @return mixed + */ + public function getEdition() + { + return $this->objectManager->get(ProductMetadataInterface::class)->getEdition(); + } +} diff --git a/Helper/Media.php b/Helper/Media.php index bcb79f0..95e0558 100644 --- a/Helper/Media.php +++ b/Helper/Media.php @@ -1,292 +1,292 @@ -mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); - $this->uploaderFactory = $uploaderFactory; - $this->imageFactory = $imageFactory; - - parent::__construct($context, $objectManager, $storeManager); - } - - /** - * @param $data - * @param string $fileName - * @param string $type - * @param null $oldImage - * - * @return $this - */ - public function uploadImage(&$data, $fileName = 'image', $type = '', $oldImage = null) - { - if (isset($data[$fileName]['delete']) && $data[$fileName]['delete']) { - if ($oldImage) { - try { - $this->removeImage($oldImage, $type); - } catch (Exception $e) { - $this->_logger->critical($e->getMessage()); - } - } - $data[$fileName] = ''; - } else { - try { - $uploader = $this->uploaderFactory->create(['fileId' => $fileName]); - $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']); - $uploader->setAllowRenameFiles(true); - $uploader->setFilesDispersion(true); - $uploader->setAllowCreateFolders(true); - - $path = $this->getBaseMediaPath($type); - - $image = $uploader->save( - $this->mediaDirectory->getAbsolutePath($path) - ); - - if ($oldImage) { - $this->removeImage($oldImage, $type); - } - - $data[$fileName] = $this->_prepareFile($image['file']); - } catch (Exception $e) { - $data[$fileName] = isset($data[$fileName]['value']) ? $data[$fileName]['value'] : ''; - } - } - - return $this; - } - - /** - * @param $file - * @param $type - * - * @return $this - * @throws FileSystemException - */ - public function removeImage($file, $type) - { - $image = $this->getMediaPath($file, $type); - if ($this->mediaDirectory->isFile($image)) { - $this->mediaDirectory->delete($image); - } - - return $this; - } - - /** - * @param $file - * @param string $type - * - * @return string - */ - public function getMediaPath($file, $type = '') - { - return $this->getBaseMediaPath($type) . '/' . $this->_prepareFile($file); - } - - /** - * @param string $type - * - * @return string - */ - public function getBaseMediaPath($type = '') - { - return trim(static::TEMPLATE_MEDIA_PATH . '/' . $type, '/'); - } - - /** - * @param string $file - * - * @return string - */ - protected function _prepareFile($file) - { - return ltrim(str_replace('\\', '/', $file), '/'); - } - - /** - * @param $file - * @param $size - * @param string $type - * @param bool $keepRatio - * - * @return string - * @throws NoSuchEntityException - */ - public function resizeImage($file, $size, $type = '', $keepRatio = true) - { - $image = $this->getMediaPath($file, $type); - if (!($imageSize = $this->correctImageSize($size))) { - return $this->getMediaUrl($image); - } - list($width, $height) = $imageSize; - - $resizeImage = $this->getMediaPath($file, ($type ? $type . '/' : '') . 'resize/' . $width . 'x' . $height); - - /** @var WriteInterface $mediaDirectory */ - $mediaDirectory = $this->getMediaDirectory(); - if ($mediaDirectory->isFile($resizeImage)) { - $image = $resizeImage; - } else { - $imageResize = $this->imageFactory->create(); - $imageResize->open($mediaDirectory->getAbsolutePath($image)); - $imageResize->constrainOnly(true); - $imageResize->keepTransparency(true); - $imageResize->keepFrame(false); - $imageResize->keepAspectRatio($keepRatio); - $imageResize->resize($width, $height); - - try { - $imageResize->save($mediaDirectory->getAbsolutePath($resizeImage)); - - $image = $resizeImage; - } catch (Exception $e) { - $this->_logger->critical($e->getMessage()); - } - } - - return $this->getMediaUrl($image); - } - - /** - * @param $size - * - * @return array|bool - */ - protected function correctImageSize($size) - { - if (!$size) { - return false; - } - - if (strpos($size, 'x') === false) { - $width = $height = (int) $size; - } else { - list($width, $height) = explode('x', $size); - } - - if (!$width && !$height) { - return false; - } - - return [(int) $width ?: null, (int) $height ?: null]; - } - - /** - * @param $file - * - * @return string - * @throws NoSuchEntityException - */ - public function getMediaUrl($file) - { - return $this->getBaseMediaUrl() . '/' . $this->_prepareFile($file); - } - - /** - * @return string - * @throws NoSuchEntityException - */ - public function getBaseMediaUrl() - { - return rtrim($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA), '/'); - } - - /** - * @return WriteInterface - */ - public function getMediaDirectory() - { - return $this->mediaDirectory; - } - - /** - * @param $path - * - * @return $this - * @throws FileSystemException - */ - public function removePath($path) - { - $pathMedia = $this->mediaDirectory->getRelativePath($path); - if ($this->mediaDirectory->isDirectory($pathMedia)) { - $this->mediaDirectory->delete($path); - } - - return $this; - } -} +mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); + $this->uploaderFactory = $uploaderFactory; + $this->imageFactory = $imageFactory; + + parent::__construct($context, $objectManager, $storeManager); + } + + /** + * @param $data + * @param string $fileName + * @param string $type + * @param null $oldImage + * + * @return $this + */ + public function uploadImage(&$data, $fileName = 'image', $type = '', $oldImage = null) + { + if (isset($data[$fileName]['delete']) && $data[$fileName]['delete']) { + if ($oldImage) { + try { + $this->removeImage($oldImage, $type); + } catch (Exception $e) { + $this->_logger->critical($e->getMessage()); + } + } + $data[$fileName] = ''; + } else { + try { + $uploader = $this->uploaderFactory->create(['fileId' => $fileName]); + $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png', 'svg']); + $uploader->setAllowRenameFiles(true); + $uploader->setFilesDispersion(true); + $uploader->setAllowCreateFolders(true); + + $path = $this->getBaseMediaPath($type); + + $image = $uploader->save( + $this->mediaDirectory->getAbsolutePath($path) + ); + + if ($oldImage) { + $this->removeImage($oldImage, $type); + } + + $data[$fileName] = $this->_prepareFile($image['file']); + } catch (Exception $e) { + $data[$fileName] = isset($data[$fileName]['value']) ? $data[$fileName]['value'] : ''; + } + } + + return $this; + } + + /** + * @param $file + * @param $type + * + * @return $this + * @throws FileSystemException + */ + public function removeImage($file, $type) + { + $image = $this->getMediaPath($file, $type); + if ($this->mediaDirectory->isFile($image)) { + $this->mediaDirectory->delete($image); + } + + return $this; + } + + /** + * @param $file + * @param string $type + * + * @return string + */ + public function getMediaPath($file, $type = '') + { + return $this->getBaseMediaPath($type) . '/' . $this->_prepareFile($file); + } + + /** + * @param string $type + * + * @return string + */ + public function getBaseMediaPath($type = '') + { + return trim(static::TEMPLATE_MEDIA_PATH . '/' . $type, '/'); + } + + /** + * @param string $file + * + * @return string + */ + protected function _prepareFile($file) + { + return ltrim(str_replace('\\', '/', $file), '/'); + } + + /** + * @param $file + * @param $size + * @param string $type + * @param bool $keepRatio + * + * @return string + * @throws NoSuchEntityException + */ + public function resizeImage($file, $size, $type = '', $keepRatio = true) + { + $image = $this->getMediaPath($file, $type); + if (!($imageSize = $this->correctImageSize($size))) { + return $this->getMediaUrl($image); + } + list($width, $height) = $imageSize; + + $resizeImage = $this->getMediaPath($file, ($type ? $type . '/' : '') . 'resize/' . $width . 'x' . $height); + + /** @var WriteInterface $mediaDirectory */ + $mediaDirectory = $this->getMediaDirectory(); + if ($mediaDirectory->isFile($resizeImage)) { + $image = $resizeImage; + } elseif (!$mediaDirectory->isExist($mediaDirectory->getAbsolutePath($image))) { + $imageResize = $this->imageFactory->create(); + $imageResize->open($mediaDirectory->getAbsolutePath($image)); + $imageResize->constrainOnly(true); + $imageResize->keepTransparency(true); + $imageResize->keepFrame(false); + $imageResize->keepAspectRatio($keepRatio); + $imageResize->resize($width, $height); + + try { + $imageResize->save($mediaDirectory->getAbsolutePath($resizeImage)); + + $image = $resizeImage; + } catch (Exception $e) { + $this->_logger->critical($e->getMessage()); + } + } + + return $this->getMediaUrl($image); + } + + /** + * @param $size + * + * @return array|bool + */ + protected function correctImageSize($size) + { + if (!$size) { + return false; + } + + if (strpos($size, 'x') === false) { + $width = $height = (int) $size; + } else { + list($width, $height) = explode('x', $size); + } + + if (!$width && !$height) { + return false; + } + + return [(int) $width ?: null, (int) $height ?: null]; + } + + /** + * @param $file + * + * @return string + * @throws NoSuchEntityException + */ + public function getMediaUrl($file) + { + return $this->getBaseMediaUrl() . '/' . $this->_prepareFile($file); + } + + /** + * @return string + * @throws NoSuchEntityException + */ + public function getBaseMediaUrl() + { + return rtrim($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA), '/'); + } + + /** + * @return WriteInterface + */ + public function getMediaDirectory() + { + return $this->mediaDirectory; + } + + /** + * @param $path + * + * @return $this + * @throws FileSystemException + */ + public function removePath($path) + { + $pathMedia = $this->mediaDirectory->getRelativePath($path); + if ($this->mediaDirectory->isDirectory($pathMedia)) { + $this->mediaDirectory->delete($path); + } + + return $this; + } +} diff --git a/Helper/Validate.php b/Helper/Validate.php old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json index 55a5813..a463ee5 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,23 @@ -{ - "name": "mageplaza/module-core", - "description": "Mageplaza Core for Magento 2", - "type": "magento2-module", - "version": "1.4.12", - "license": "proprietary", - "authors": [ - { - "name": "Mageplaza", - "email": "support@mageplaza.com", - "homepage": "https://www.mageplaza.com", - "role": "Technical Support" - } - ], - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Mageplaza\\Core\\": "" - } - } -} +{ + "name": "mageplaza/module-core", + "description": "Mageplaza Core for Magento 2", + "type": "magento2-module", + "version": "1.5.0", + "license": "proprietary", + "authors": [ + { + "name": "Mageplaza", + "email": "support@mageplaza.com", + "homepage": "https://www.mageplaza.com", + "role": "Technical Support" + } + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Mageplaza\\Core\\": "" + } + } +} diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100755 index 0000000..8de5c6d --- /dev/null +++ b/etc/crontab.xml @@ -0,0 +1,29 @@ + + + + + + 0 2 * * 0 + + + diff --git a/view/base/requirejs-config.js b/view/base/requirejs-config.js index 0261e5f..5cbbe89 100644 --- a/view/base/requirejs-config.js +++ b/view/base/requirejs-config.js @@ -1,38 +1,38 @@ -/** - * 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) Mageplaza (https://www.mageplaza.com/) - * @license https://www.mageplaza.com/LICENSE.txt - */ - -var config = { - paths: { - 'mageplaza/core/jquery/popup': 'Mageplaza_Core/js/jquery.magnific-popup.min', - 'mageplaza/core/owl.carousel': 'Mageplaza_Core/js/owl.carousel.min', - 'mageplaza/core/bootstrap': 'Mageplaza_Core/js/bootstrap.min', - mpIonRangeSlider: 'Mageplaza_Core/js/ion.rangeSlider.min', - touchPunch: 'Mageplaza_Core/js/jquery.ui.touch-punch.min', - mpDevbridgeAutocomplete: 'Mageplaza_Core/js/jquery.autocomplete.min' - }, - shim: { - "mageplaza/core/jquery/popup": ["jquery"], - "mageplaza/core/owl.carousel": ["jquery"], - "mageplaza/core/bootstrap": ["jquery"], - mpIonRangeSlider: ["jquery"], - mpDevbridgeAutocomplete: ["jquery"], - touchPunch: ['jquery', 'jquery/ui'] - } -}; +/** + * 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) Mageplaza (https://www.mageplaza.com/) + * @license https://www.mageplaza.com/LICENSE.txt + */ + +var config = { + paths: { + 'mageplaza/core/jquery/popup': 'Mageplaza_Core/js/jquery.magnific-popup.min', + 'mageplaza/core/owl.carousel': 'Mageplaza_Core/js/owl.carousel.min', + 'mageplaza/core/bootstrap': 'Mageplaza_Core/js/bootstrap.min', + mpIonRangeSlider: 'Mageplaza_Core/js/ion.rangeSlider.min', + touchPunch: 'Mageplaza_Core/js/jquery.ui.touch-punch.min', + mpDevbridgeAutocomplete: 'Mageplaza_Core/js/jquery.autocomplete.min' + }, + shim: { + "mageplaza/core/jquery/popup": ["jquery"], + "mageplaza/core/owl.carousel": ["jquery"], + "mageplaza/core/bootstrap": ["jquery"], + mpIonRangeSlider: ["jquery"], + mpDevbridgeAutocomplete: ["jquery"], + touchPunch: ['jquery', 'jquery-ui-modules/core', 'jquery-ui-modules/mouse', 'jquery-ui-modules/widget'] + } +};