| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid; |
| 4 |
|
| 5 |
class VersionControl { |
| 6 |
|
| 7 |
/** @var string */ |
| 8 |
protected $pluginVersion = ''; |
| 9 |
|
| 10 |
/** @var string */ |
| 11 |
protected $dbVersion = ''; |
| 12 |
|
| 13 |
/** @var bool */ |
| 14 |
protected $needUpgrade = false; |
| 15 |
|
| 16 |
public function __construct(){ |
| 17 |
|
| 18 |
$settings = getwid()->settings(); |
| 19 |
|
| 20 |
$this->pluginVersion = $settings->getVersion(); |
| 21 |
|
| 22 |
$this->checkVersion(); |
| 23 |
$this->addActions(); |
| 24 |
} |
| 25 |
|
| 26 |
protected function checkVersion() |
| 27 |
{ |
| 28 |
$this->dbVersion = $this->getCurrentDatabaseVersion(); |
| 29 |
|
| 30 |
if (version_compare($this->pluginVersion, $this->dbVersion, '>')) { |
| 31 |
$this->needUpgrade = true; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
protected function addActions() |
| 36 |
{ |
| 37 |
if ($this->needUpgrade) { |
| 38 |
add_action('init', [$this, 'upgrade']); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function upgrade() |
| 43 |
{ |
| 44 |
// Nothing to do at the moment |
| 45 |
$this->afterUpgrade(); |
| 46 |
} |
| 47 |
|
| 48 |
protected function afterUpgrade() |
| 49 |
{ |
| 50 |
$this->setCurrentDatabaseVersion($this->pluginVersion); |
| 51 |
|
| 52 |
if (version_compare($this->pluginVersion, $this->dbVersion, '!=')) { |
| 53 |
$this->addVersionToHistory($this->pluginVersion); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
protected function getCurrentDatabaseVersion() |
| 58 |
{ |
| 59 |
return get_option('getwid_db_version', '0.0.0'); |
| 60 |
} |
| 61 |
|
| 62 |
protected function setCurrentDatabaseVersion($version) |
| 63 |
{ |
| 64 |
update_option('getwid_db_version', $version); |
| 65 |
} |
| 66 |
|
| 67 |
protected function addVersionToHistory($version) |
| 68 |
{ |
| 69 |
$versionHistory = get_option('getwid_db_version_history', []); |
| 70 |
|
| 71 |
if (!in_array($version, $versionHistory)) { |
| 72 |
$versionHistory[] = $version; |
| 73 |
update_option('getwid_db_version_history', $versionHistory); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|