| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The admin-facing functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://themeatelier.net |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package chat-help |
| 10 |
* @subpackage chat-help/Admin |
| 11 |
* @author ThemeAtelier<themeatelierbd@gmail.com> |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace ThemeAtelier\ChatHelp\Admin; |
| 15 |
|
| 16 |
if (! defined('ABSPATH')) { |
| 17 |
die; |
| 18 |
} // Cannot access directly. |
| 19 |
/** |
| 20 |
* The admin class |
| 21 |
*/ |
| 22 |
class DBUpdates |
| 23 |
{ |
| 24 |
/** |
| 25 |
* DB updates that need to be run |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private static $updates = array( |
| 30 |
'2.1.0' => 'updates/update-2.1.0.php', |
| 31 |
'2.2.9' => 'updates/update-2.2.9.php', |
| 32 |
'3.1.12' => 'updates/update-3.1.12.php', |
| 33 |
'3.2.0' => 'updates/update-3.2.0.php', |
| 34 |
); |
| 35 |
|
| 36 |
/** |
| 37 |
* The class constructor. |
| 38 |
* |
| 39 |
*/ |
| 40 |
function __construct() |
| 41 |
{ |
| 42 |
add_action('admin_init', array($this, 'perform_updates')); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if an update is needed. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function is_needs_update() |
| 51 |
{ |
| 52 |
$installed_version = get_option('chat_help_version'); |
| 53 |
$first_version = get_option('chat_help_first_version'); |
| 54 |
$activation_date = get_option('chat_help_activation_date'); |
| 55 |
|
| 56 |
if (false === $installed_version) { |
| 57 |
update_option('chat_help_version', CHAT_HELP_VERSION); |
| 58 |
update_option('chat_help_db_version', CHAT_HELP_VERSION); |
| 59 |
} |
| 60 |
if (false === $first_version) { |
| 61 |
update_option('chat_help_first_version', CHAT_HELP_VERSION); |
| 62 |
} |
| 63 |
if (false === $activation_date) { |
| 64 |
update_option('chat_help_activation_date', current_time('timestamp')); |
| 65 |
} |
| 66 |
|
| 67 |
if (version_compare($installed_version, CHAT_HELP_VERSION, '<')) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Perform all updates. |
| 76 |
* |
| 77 |
*/ |
| 78 |
public function perform_updates() |
| 79 |
{ |
| 80 |
if (!$this->is_needs_update()) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$installed_version = get_option('chat_help_version'); |
| 85 |
|
| 86 |
foreach (self::$updates as $version => $path) { |
| 87 |
if (version_compare($installed_version, $version, '<')) { |
| 88 |
include $path; |
| 89 |
update_option('chat_help_version', $version); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
update_option('chat_help_version', CHAT_HELP_VERSION); |
| 94 |
} |
| 95 |
} |
| 96 |
|