# hurrytimer/2.0.0/includes/Upgrade.php

HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress &amp; WooCommerce, version 2.0.0. 142 lines.

- Page: https://pluginprobe.com/plugins/hurrytimer/2.0.0/code/includes/Upgrade.php
- Raw: https://pluginprobe.com/plugins/hurrytimer/2.0.0/raw/includes/Upgrade.php
- Modified: 2019-04-05T20:43:02+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/hurrytimer/2.0.0/code/includes/Upgrade.php#L10-L20`.

```php
<?php

namespace Hurrytimer;

class Upgrade
{
    static $instance;


    /**
     * @return Upgrade
     */
    public static function getInstance()
    {
        if ( ! static::$instance) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    /**
     * Run upgrade
     *
     * @return void
     */
    public function run()
    {
        $this->upgradeDb();
        $this->upgradeCore();
    }

    public function upgradeCore()
    {
        $this->backwardCompat();
        $this->updatePluginVersion();
    }

    function buildCssFile(){
        Campaign::buildCss();
    }

    /**
     * Upgrade or created table.
     *
     * @return void
     */
    private function upgradeDb()
    {
        $ver = $this->getInstalledDbVersion();

        if ( ! $ver || $ver != HURRYT_DB_VERSION) {
            $this->createTables();
            $this->updateDbVersion();
        }
    }

    public function createTables()
    {
        global $wpdb;

        $table           = "{$wpdb->prefix}hurrytimer_evergreen";
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE {$table} (
              id bigint(20) NOT NULL AUTO_INCREMENT,
              countdown_id bigint(20) unsigned NOT NULL,
              client_ip_address varchar(50) NOT NULL,
              expired tinyint(1) unsigned DEFAULT NULL,
              client_expires_at bigint(20) unsigned NOT NULL,
              destroy_at timestamp NULL DEFAULT NULL,
              PRIMARY KEY (id)
            ) {$charset_collate}";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';

        dbDelta($sql);
    }

    /**
     * Returns installed plugin version.
     *
     * @return string
     */
    private function getInstalledPluginVersion()
    {
        return get_option('hurrytimer_version');
    }

    /**
     * Update plugin version.
     *
     * @return void
     */
    private function updatePluginVersion()
    {
        update_option('hurrytimer_version', HURRYT_VERSION);
    }

    /**
     * Returns installed db version.
     *
     * @return string
     */
    private function getInstalledDbVersion()
    {
        return get_option('hurrytimer_db_version');
    }

    /**
     * Save new db version.
     *
     * @return void
     */
    private function updateDbVersion()
    {
        update_option('hurrytimer_db_version', HURRYT_DB_VERSION);
    }

    /**
     * Run global backward compatibility if applicable.
     *
     * @return void
     */
    private function backwardCompat()
    {
        $ver = $this->getInstalledPluginVersion();

        // Clean up unused `_htmr_reset_cookie` option.
        if (version_compare($ver, '1.1.3', '<=')) {
            delete_option('_htmr_reset_cookie');
        }

        // Legacy code for some features.
        if (version_compare($ver, '2.0.0', '<')) {
            update_option('hurrytimer_legacy', 'yes');
            $this->buildCssFile();
        }

    }
}

```
