forked from scribu/wp-scb-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.php
56 lines (41 loc) · 1.45 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
// Takes care of creating, updating and deleting database tables
class scbTable {
protected $name;
protected $columns;
protected $upgrade_method;
function __construct( $name, $file, $columns, $upgrade_method = 'dbDelta' ) {
global $wpdb;
$this->name = $name;
$this->columns = $columns;
$this->upgrade_method = $upgrade_method;
$wpdb->tables[] = $name;
$wpdb->$name = $wpdb->prefix . $name;
scbUtil::add_activation_hook( $file, array( $this, 'install' ) );
scbUtil::add_uninstall_hook( $file, array( $this, 'uninstall' ) );
}
function install() {
global $wpdb;
$full_table_name = $wpdb->prefix . $this->name;
$charset_collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE $wpdb->collate";
}
if ( 'dbDelta' == $this->upgrade_method ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( "CREATE TABLE $full_table_name ( $this->columns ) $charset_collate" );
return;
}
if ( 'delete_first' == $this->upgrade_method )
$wpdb->query( "DROP TABLE IF EXISTS $full_table_name;" );
$wpdb->query( "CREATE TABLE IF NOT EXISTS $full_table_name ( $this->columns ) $charset_collate;" );
}
function uninstall() {
global $wpdb;
$full_table_name = $wpdb->prefix . $this->name;
$wpdb->query( "DROP TABLE IF EXISTS $full_table_name" );
}
}