| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use Hurrytimer\Traits\Singleton; |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* |
| 10 |
* @since 2.3.0 |
| 11 |
* @package Hurrytimer |
| 12 |
* @subpackage Hurrytimer/includes |
| 13 |
* @author Nabil Lemsieh <contact@nabillemsieh.com> |
| 14 |
*/ |
| 15 |
class Installer |
| 16 |
{ |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
public function upgrade() |
| 20 |
{ |
| 21 |
|
| 22 |
// Update table structure if available. |
| 23 |
$this->create_or_upgrade_evergreen_timers_tracking_table(); |
| 24 |
|
| 25 |
// Preserve compatibility with versions prior 2.3.0 |
| 26 |
$installed_version = get_option( 'hurrytimer_version' ); |
| 27 |
|
| 28 |
// A workaround to find installed version. |
| 29 |
if ( !$installed_version ) { |
| 30 |
|
| 31 |
$campaigns = get_posts( [ 'post_type' => HURRYT_POST_TYPE, 'post_status' => 'any', 'fields' => 'ids' ] ); |
| 32 |
if ( !empty( $campaigns ) ) { |
| 33 |
$installed_version = '2.2.28'; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( $installed_version && version_compare( $installed_version, '2.2.28', '<=' ) ) { |
| 38 |
$this->upgrade_2_2_28(); |
| 39 |
} |
| 40 |
|
| 41 |
// Update the current version. |
| 42 |
update_option( 'hurrytimer_version', HURRYT_VERSION ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Should we preserve compatibility with 2.2.28 and prior? |
| 47 |
* |
| 48 |
* @return bool|mixed |
| 49 |
*/ |
| 50 |
public function should_backward_compat_2_2_28_and_prior() |
| 51 |
{ |
| 52 |
if ( version_compare( get_option( 'hurrytimer_version' ), '2.2.28', '<=' ) ) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
return filter_var( get_option( 'hurrytimer_upgraded_2_2_28' ), FILTER_VALIDATE_BOOLEAN ); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Upgraded from 2.2.28? |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
public function has_upgraded_from_2_2_28_or_prior() |
| 66 |
{ |
| 67 |
return filter_var( get_option( 'hurrytimer_upgraded_2_2_28' ), FILTER_VALIDATE_BOOLEAN ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Backward compatibility with versions <= 2.2.28 |
| 72 |
*/ |
| 73 |
public function upgrade_2_2_28() |
| 74 |
{ |
| 75 |
add_option( 'hurrytimer_upgraded_2_2_28', '1' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Plugin activation hook |
| 80 |
* |
| 81 |
* @since 2.3.0 |
| 82 |
*/ |
| 83 |
public function activate() |
| 84 |
{ |
| 85 |
|
| 86 |
$this->create_or_upgrade_evergreen_timers_tracking_table(); |
| 87 |
|
| 88 |
// Add the current version. |
| 89 |
add_option( 'hurrytimer_version', HURRYT_VERSION ); |
| 90 |
|
| 91 |
// Rebuild css |
| 92 |
try { |
| 93 |
CSS_Builder::get_instance()->generate_css(); |
| 94 |
} catch ( \Exception $e ) { |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
private function create_or_upgrade_evergreen_timers_tracking_table() |
| 101 |
{ |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
$table = "{$wpdb->prefix}hurrytimer_evergreen"; |
| 106 |
$charset_collate = $wpdb->get_charset_collate(); |
| 107 |
|
| 108 |
$sql = "CREATE TABLE {$table} ( |
| 109 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 110 |
countdown_id bigint(20) unsigned NOT NULL, |
| 111 |
client_ip_address varchar(50) NOT NULL, |
| 112 |
expired tinyint(1) unsigned DEFAULT NULL, |
| 113 |
client_expires_at bigint(20) unsigned NOT NULL, |
| 114 |
reset_token varchar(20) NULL, |
| 115 |
destroy_at timestamp NULL DEFAULT NULL, |
| 116 |
PRIMARY KEY (id) |
| 117 |
) {$charset_collate}"; |
| 118 |
|
| 119 |
try{ |
| 120 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 121 |
dbDelta( $sql ); |
| 122 |
}catch(\Exception $e){ |
| 123 |
@$wpdb->query($sql); |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|