| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class Upgrade |
| 6 |
{ |
| 7 |
static $instance; |
| 8 |
|
| 9 |
/** |
| 10 |
* @return Upgrade |
| 11 |
*/ |
| 12 |
public static function getInstance() |
| 13 |
{ |
| 14 |
if (!static::$instance) { |
| 15 |
static::$instance = new static(); |
| 16 |
} |
| 17 |
|
| 18 |
return static::$instance; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Run upgrade. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function run() |
| 27 |
{ |
| 28 |
$this->before(); |
| 29 |
$this->upgradeDb(); |
| 30 |
$this->upgradeCore(); |
| 31 |
$this->buildCssFile(); |
| 32 |
$this->after(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Run before upgrade. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function before() |
| 41 |
{ |
| 42 |
if (!$this->getInstalledPluginVersion()) { |
| 43 |
Activator::activate(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* After upgrade |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function after() |
| 53 |
{ |
| 54 |
$this->updatePluginVersion(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Upgrade code features. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function upgradeCore() |
| 63 |
{ |
| 64 |
$this->backwardCompat(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Re-build public CSS file. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function buildCssFile() |
| 73 |
{ |
| 74 |
if (!$this->isCssBuilt()) { |
| 75 |
Campaign::buildCss(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns true if the public CSS file exits already. |
| 81 |
* |
| 82 |
* @return boolean |
| 83 |
*/ |
| 84 |
public function isCssBuilt() |
| 85 |
{ |
| 86 |
return file_exists(HURRYT_DIR . 'assets/css/hurrytimer.css'); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Upgrade or created table. |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
private function upgradeDb() |
| 96 |
{ |
| 97 |
$ver = $this->getInstalledDbVersion(); |
| 98 |
|
| 99 |
if (!$ver || $ver != HURRYT_DB_VERSION) { |
| 100 |
$this->createTables(); |
| 101 |
$this->updateDbVersion(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Create custom db tables. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function createTables() |
| 111 |
{ |
| 112 |
global $wpdb; |
| 113 |
|
| 114 |
$table = "{$wpdb->prefix}hurrytimer_evergreen"; |
| 115 |
$charset_collate = $wpdb->get_charset_collate(); |
| 116 |
|
| 117 |
$sql = "CREATE TABLE {$table} ( |
| 118 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 119 |
countdown_id bigint(20) unsigned NOT NULL, |
| 120 |
client_ip_address varchar(50) NOT NULL, |
| 121 |
expired tinyint(1) unsigned DEFAULT NULL, |
| 122 |
client_expires_at bigint(20) unsigned NOT NULL, |
| 123 |
destroy_at timestamp NULL DEFAULT NULL, |
| 124 |
PRIMARY KEY (id) |
| 125 |
) {$charset_collate}"; |
| 126 |
|
| 127 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 128 |
|
| 129 |
dbDelta($sql); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns installed plugin version. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
private function getInstalledPluginVersion() |
| 138 |
{ |
| 139 |
return get_option(HURRYT_OPTION_VERSION_KEY); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Update plugin version. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
private function updatePluginVersion() |
| 148 |
{ |
| 149 |
update_option(HURRYT_OPTION_VERSION_KEY, HURRYT_VERSION); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns installed db version. |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
private function getInstalledDbVersion() |
| 158 |
{ |
| 159 |
return get_option('hurrytimer_db_version'); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Save new db version. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
private function updateDbVersion() |
| 168 |
{ |
| 169 |
update_option('hurrytimer_db_version', HURRYT_DB_VERSION); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Run global backward compatibility if applicable. |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
private function backwardCompat() |
| 178 |
{ |
| 179 |
$ver = $this->getInstalledPluginVersion(); |
| 180 |
|
| 181 |
if (version_compare($ver, '2.0.0', '<')) { |
| 182 |
$this->buildCssFile(); |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
} |
| 187 |
|