SimpleHistory::check_for_upgrade PHP Method

check_for_upgrade() public method

Check if plugin version have changed, i.e. has been upgraded If upgrade is detected then maybe modify database and so on for that version
public check_for_upgrade ( )
    function check_for_upgrade()
    {
        global $wpdb;
        $db_version = get_option("simple_history_db_version");
        $table_name = $wpdb->prefix . SimpleHistory::DBTABLE;
        $table_name_contexts = $wpdb->prefix . SimpleHistory::DBTABLE_CONTEXTS;
        $first_install = false;
        // If no db_version is set then this
        // is a version of Simple History < 0.4
        // or it's a first install
        // Fix database not using UTF-8
        if (false === $db_version || intval($db_version) == 0) {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            // Table creation, used to be in register_activation_hook
            // We change the varchar size to add one num just to force update of encoding. dbdelta didn't see it otherwise.
            $sql = "CREATE TABLE " . $table_name . " (\n\t\t\t  id bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t  date datetime NOT NULL,\n\t\t\t  PRIMARY KEY  (id)\n\t\t\t) CHARACTER SET=utf8;";
            // Upgrade db / fix utf for varchars
            dbDelta($sql);
            // Fix UTF-8 for table
            $sql = sprintf('alter table %1$s charset=utf8;', $table_name);
            $wpdb->query($sql);
            $db_version_prev = $db_version;
            $db_version = 1;
            update_option("simple_history_db_version", $db_version);
            // We are not 100% sure that this is a first install,
            // but it is at least a very old version that is being updated
            $first_install = true;
        }
        // done pre db ver 1 things
        // If db version is 1 then upgrade to 2
        // Version 2 added the action_description column
        if (1 == intval($db_version)) {
            // V2 used to add column "action_description"
            // but it's not used any more so don't do i
            $db_version_prev = $db_version;
            $db_version = 2;
            update_option("simple_history_db_version", $db_version);
        }
        // Check that all options we use are set to their defaults, if they miss value
        // Each option that is missing a value will make a sql call otherwise = unnecessary
        $arr_options = array(array("name" => "simple_history_show_as_page", "default_value" => 1), array("name" => "simple_history_show_on_dashboard", "default_value" => 1));
        foreach ($arr_options as $one_option) {
            if (false === ($option_value = get_option($one_option["name"]))) {
                // Value is not set in db, so set it to a default
                update_option($one_option["name"], $one_option["default_value"]);
            }
        }
        /**
         * If db_version is 2 then upgrade to 3:
         * - Add some fields to existing table wp_simple_history_contexts
         * - Add all new table wp_simple_history_contexts
         *
         * @since 2.0
         */
        if (2 == intval($db_version)) {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            // Update old table
            $sql = "\n\t\t\t\tCREATE TABLE {$table_name} (\n\t\t\t\t  id bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\t  date datetime NOT NULL,\n\t\t\t\t  logger varchar(30) DEFAULT NULL,\n\t\t\t\t  level varchar(20) DEFAULT NULL,\n\t\t\t\t  message varchar(255) DEFAULT NULL,\n\t\t\t\t  occasionsID varchar(32) DEFAULT NULL,\n\t\t\t\t  initiator varchar(16) DEFAULT NULL,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY date (date),\n\t\t\t\t  KEY loggerdate (logger,date)\n\t\t\t\t) CHARSET=utf8;";
            dbDelta($sql);
            // Add context table
            $sql = "\n\t\t\t\tCREATE TABLE IF NOT EXISTS {$table_name_contexts} (\n\t\t\t\t  context_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t  history_id bigint(20) unsigned NOT NULL,\n\t\t\t\t  `key` varchar(255) DEFAULT NULL,\n\t\t\t\t  value longtext,\n\t\t\t\t  PRIMARY KEY  (context_id),\n\t\t\t\t  KEY history_id (history_id),\n\t\t\t\t  KEY `key` (`key`)\n\t\t\t\t) CHARSET=utf8;\n\t\t\t";
            $wpdb->query($sql);
            $db_version_prev = $db_version;
            $db_version = 3;
            update_option("simple_history_db_version", $db_version);
            // Update possible old items to use SimpleLegacyLogger
            $sql = sprintf('
					UPDATE %1$s
					SET
						logger = "SimpleLegacyLogger",
						level = "info"
					WHERE logger IS NULL
				', $table_name);
            $wpdb->query($sql);
            // Say welcome, however loggers are not added this early so we need to
            // use a filter to load it later
            add_action("simple_history/loggers_loaded", array($this, "addWelcomeLogMessage"));
        }
        // db version 2 » 3
        /**
         * If db version = 3
         * then we need to update database to allow null values for some old columns
         * that used to work in pre wp 4.1 beta, but since 4.1 wp uses STRICT_ALL_TABLES
         * WordPress Commit: https://github.com/WordPress/WordPress/commit/f17d168a0f72211a9bfd9d3fa680713069871bb6
         *
         * @since 2.0
         */
        if (3 == intval($db_version)) {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            // If old columns exist = this is an old install, then modify the columns so we still can keep them
            // we want to keep them because user may have logged items that they want to keep
            $db_cools = $wpdb->get_col("DESCRIBE {$table_name}");
            if (in_array("action", $db_cools)) {
                $sql = sprintf('
						ALTER TABLE %1$s
						MODIFY `action` varchar(255) NULL,
						MODIFY `object_type` varchar(255) NULL,
						MODIFY `object_subtype` varchar(255) NULL,
						MODIFY `user_id` int(10) NULL,
						MODIFY `object_id` int(10) NULL,
						MODIFY `object_name` varchar(255) NULL
					', $table_name);
                $wpdb->query($sql);
            }
            $db_version_prev = $db_version;
            $db_version = 4;
            update_option("simple_history_db_version", $db_version);
        }
        // end db version 3 » 4
        // Some installs on 2.2.2 got failed installs
        // We detect these by checking for db_version and then running the install stuff again
        if (4 == intval($db_version)) {
            if (!$this->does_database_have_data()) {
                // not ok, decrease db number so installs will run again and hopefully fix things
                $db_version = 0;
            } else {
                // all looks ok, upgrade to db version 5, so this part is not done again
                $db_version = 5;
            }
            update_option("simple_history_db_version", $db_version);
        }
    }
SimpleHistory