Skip to content
Snippets Groups Projects
knowledge-space-database.php 1.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • $SPACES_TABLE = ks_get_table_name("spaces");
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
    global $ks_db_version;
    
    $ks_db_version = '0.1';
    
    function ks_get_table_name($table)
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    {
    	global $wpdb;
    	return $wpdb->prefix . "ks_" . $table;
    }
    
    register_activation_hook(__FILE__, 'ks_install');
    function ks_install()
    {
    	global $wpdb;
    	global $ks_db_version;
    
    	$installed_ver = get_option("ks_db_version");
    	if ($installed_ver == $ks_db_version) {
    		return;
    	}
    
    	$charset_collate = $wpdb->get_charset_collate();
    
    	require_once(__DIR__ . '/../../../../wp-admin/includes/upgrade.php');
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    	global $SPACES_TABLE;
    	$sql = "CREATE TABLE $SPACES_TABLE (
    		space_id int(11) NOT NULL AUTO_INCREMENT,
    		name text NOT NULL,
    
    		graph text NOT NULL,
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    		PRIMARY KEY  (space_id)
    	) $charset_collate;";
    	dbDelta($sql);	//! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins
    
    	add_option('ks_db_version', $ks_db_version);
    }
    
    // Handling upgrade
    function ks_update_db_check()
    {
    	global $ks_db_version;
    	if (get_site_option('ks_db_version') != $ks_db_version) {
    		ks_install();
    	}
    }
    add_action('plugins_loaded', 'ks_update_db_check');