EventManager.php
2 weeks ago
LogManager.php
1 month ago
OptionManager.php
1 month ago
PolicyManager.php
5 days ago
VersionUpdateManager.php
5 days ago
VersionUpdateManager.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Version manager. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Managers |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Managers; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Kirki\Framework\Constants\OptionKeys; |
| 14 | use Kirki\Framework\Contracts\Executor; |
| 15 | use Kirki\Framework\Supports\Facades\Option; |
| 16 | use function Kirki\Framework\app; |
| 17 | class VersionUpdateManager implements Executor |
| 18 | { |
| 19 | /** |
| 20 | * The instance of the VersionManager. |
| 21 | * |
| 22 | * @var static |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | protected static $instance; |
| 27 | /** |
| 28 | * The version details. |
| 29 | * |
| 30 | * @var array |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | protected $version_details = []; |
| 35 | /** |
| 36 | * The installed version key. |
| 37 | * |
| 38 | * @var string |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | protected $installed_version_key = ''; |
| 43 | /** |
| 44 | * Constructor. |
| 45 | * |
| 46 | * @return void |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function __construct() |
| 51 | { |
| 52 | $this->installed_version_key = \sprintf('%s%s', app()->prefix(), OptionKeys::INSTALLED_VERSION); |
| 53 | } |
| 54 | /** |
| 55 | * Get the instance of the VersionManager. |
| 56 | * |
| 57 | * @return VersionUpdateManager |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public static function get_instance() |
| 62 | { |
| 63 | if (static::$instance) { |
| 64 | return static::$instance; |
| 65 | } |
| 66 | static::$instance = new static(); |
| 67 | return static::$instance; |
| 68 | } |
| 69 | /** |
| 70 | * Execute the version manager. |
| 71 | * |
| 72 | * @return void |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function run() |
| 77 | { |
| 78 | if ($this->is_current_version_already_installed()) { |
| 79 | return; |
| 80 | } |
| 81 | $updates_path = app()->config_path('version-updates.php'); |
| 82 | if (!\file_exists($updates_path)) { |
| 83 | return; |
| 84 | } |
| 85 | $plugin_updates = (include $updates_path); |
| 86 | $plugin_update_keys = \array_keys($plugin_updates); |
| 87 | \usort($plugin_update_keys, function ($first, $second) { |
| 88 | return \version_compare($first, $second); |
| 89 | }); |
| 90 | $installed_versions = $this->get_installed_versions(); |
| 91 | foreach ($plugin_update_keys as $version) { |
| 92 | if (\version_compare($version, app()->version(), '<=') && !$this->is_already_installed($version)) { |
| 93 | $callback = $plugin_updates[$version]; |
| 94 | $callback(); |
| 95 | $installed_versions[] = $version; |
| 96 | } |
| 97 | } |
| 98 | $this->update_installed_version($installed_versions); |
| 99 | } |
| 100 | /** |
| 101 | * Get the installed version details. |
| 102 | * |
| 103 | * @return array |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | protected function get_installed_version_details() |
| 108 | { |
| 109 | if (!empty($this->version_details)) { |
| 110 | return $this->version_details; |
| 111 | } |
| 112 | $default = ['current_version' => '0.0.0', 'installed_versions' => []]; |
| 113 | $version_details = Option::get($this->installed_version_key, $default); |
| 114 | // For backward compatibility check. If the option is not set or is not an array, set it to the default. |
| 115 | if (!\is_array($version_details) || !isset($version_details['installed_versions'], $version_details['current_version'])) { |
| 116 | $version_details = $default; |
| 117 | } |
| 118 | $this->version_details = $version_details; |
| 119 | return $this->version_details; |
| 120 | } |
| 121 | /** |
| 122 | * Get the current installed version. |
| 123 | * |
| 124 | * @return string |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | */ |
| 128 | protected function get_current_installed_version() |
| 129 | { |
| 130 | return $this->get_installed_version_details()['current_version']; |
| 131 | } |
| 132 | /** |
| 133 | * Get the installed versions. |
| 134 | * |
| 135 | * @return array |
| 136 | * |
| 137 | * @since 1.0.0 |
| 138 | */ |
| 139 | protected function get_installed_versions() |
| 140 | { |
| 141 | return $this->get_installed_version_details()['installed_versions']; |
| 142 | } |
| 143 | /** |
| 144 | * Update the installed version. |
| 145 | * |
| 146 | * @param array $installed_versions The installed versions. |
| 147 | * |
| 148 | * @return bool |
| 149 | * |
| 150 | * @since 1.0.0 |
| 151 | */ |
| 152 | protected function update_installed_version($installed_versions = []) |
| 153 | { |
| 154 | $installed_versions = \array_unique($installed_versions); |
| 155 | \usort($installed_versions, function ($first, $second) { |
| 156 | return \version_compare($first, $second); |
| 157 | }); |
| 158 | $version_details = ['current_version' => app()->version(), 'installed_versions' => $installed_versions]; |
| 159 | $is_updated = Option::set($this->installed_version_key, $version_details, \true); |
| 160 | if (!empty($is_updated)) { |
| 161 | $this->version_details = $version_details; |
| 162 | return \true; |
| 163 | } |
| 164 | return \false; |
| 165 | } |
| 166 | /** |
| 167 | * Check if the current version is already installed. |
| 168 | * |
| 169 | * @return bool |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | */ |
| 173 | protected function is_current_version_already_installed() |
| 174 | { |
| 175 | return (bool) \version_compare($this->get_current_installed_version(), app()->version(), '>='); |
| 176 | } |
| 177 | /** |
| 178 | * Check if the version is already installed. |
| 179 | * |
| 180 | * @param string $version The version to check. |
| 181 | * |
| 182 | * @return bool |
| 183 | * |
| 184 | * @since 1.0.0 |
| 185 | */ |
| 186 | protected function is_already_installed(string $version) |
| 187 | { |
| 188 | $installed_versions = $this->get_installed_versions(); |
| 189 | return \in_array($version, $installed_versions, \true); |
| 190 | } |
| 191 | } |
| 192 |