Extensions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Mageplaza
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Mageplaza.com license that is
  8. * available through the world-wide-web at this URL:
  9. * https://www.mageplaza.com/LICENSE.txt
  10. *
  11. * DISCLAIMER
  12. *
  13. * Do not edit or add to this file if you wish to upgrade this extension to newer
  14. * version in the future.
  15. *
  16. * @category Mageplaza
  17. * @package Mageplaza_Core
  18. * @copyright Copyright (c) 2016-2018 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\Core\Block\Adminhtml;
  22. use Magento\Framework\App\Cache\Type\Config;
  23. use Magento\Framework\Module\FullModuleList;
  24. use Magento\Framework\View\Element\Template\Context;
  25. /**
  26. * Class Extensions
  27. */
  28. class Extensions extends \Magento\Framework\View\Element\Template
  29. {
  30. /**
  31. * Cache group Tag
  32. */
  33. const CACHE_GROUP = Config::TYPE_IDENTIFIER;
  34. /**
  35. * Prefix for cache key of block
  36. */
  37. const CACHE_KEY_PREFIX = 'MAGEPLAZA_';
  38. /**
  39. * Cache tag
  40. */
  41. const CACHE_TAG = 'extensions';
  42. /**
  43. * Mageplaza api url to get extension json
  44. */
  45. const API_URL = 'https://www.mageplaza.com/api/getVersions.json';
  46. /**
  47. * @var string
  48. */
  49. protected $_template = 'extensions.phtml';
  50. /**
  51. * @var \Magento\Framework\Module\FullModuleList
  52. */
  53. private $moduleList;
  54. /**
  55. * Extensions constructor.
  56. * @param \Magento\Framework\View\Element\Template\Context $context
  57. * @param \Magento\Framework\Module\FullModuleList $moduleList
  58. * @param array $data
  59. */
  60. public function __construct(
  61. Context $context,
  62. FullModuleList $moduleList,
  63. array $data = []
  64. )
  65. {
  66. parent::__construct($context, $data);
  67. $this->moduleList = $moduleList;
  68. $this->addData(
  69. ['cache_lifetime' => 86400, 'cache_tags' => [self::CACHE_TAG]]
  70. );
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getInstalledModules()
  76. {
  77. $mageplza_modules = [];
  78. foreach ($this->moduleList->getAll() as $moduleName => $info) {
  79. if (strpos($moduleName, 'Mageplaza') !== false) {
  80. $mageplza_modules[$moduleName] = $info['setup_version'];
  81. }
  82. }
  83. return $mageplza_modules;
  84. }
  85. /**
  86. * @return bool|mixed|string
  87. */
  88. public function getAvailableModules()
  89. {
  90. $result = $this->_loadCache();
  91. if (!$result) {
  92. try {
  93. $result = file_get_contents(self::API_URL);
  94. $this->_saveCache($result);
  95. } catch (\Exception $e) {
  96. return false;
  97. }
  98. }
  99. $result = json_decode($result, true); //true return array otherwise object
  100. return $result;
  101. }
  102. }