-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_classes.php
100 lines (74 loc) · 2.08 KB
/
core_classes.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
class PluginTables extends PluginUtilities
{
public $tables;
public $table_names;
function __construct(){
$args = func_get_args();
$this->parse_args($args[0]);
if($this->tables){
$this->assign_table_names();
}
}
function assign_table_names(){
//set array of all table names in full
global $wpdb;
foreach($this->tables as $table_name => $fields){
$this->table_names[] = $wpdb->prefix . $table_name;
}
}
function parse_args($args){
foreach($args as $k=>$v){
$this->$k = $v;
}
}
function activate(){
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
if(!is_array($this->tables)) wp_die('table argument for AdminDatabase must be an array');
$sql = array();
foreach($this->tables as $table_name => $fields){
$table_name = $wpdb->prefix . $table_name;
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){
//create table
$sql[$table_name] = "CREATE TABLE $table_name (id mediumint(9) NOT NULL AUTO_INCREMENT,";
//create fields
if(!is_array($fields)) wp_die("$table_name has no fields");
foreach ($fields as $k => $sql_command){
$sql[$table_name] .= $k." ".$sql_command.",";
}
$sql[$table_name] .= "UNIQUE KEY id (id));";
dbDelta($sql[$table_name]);
}
}
}
function deactivate(){
global $wpdb;
if(!is_array($this->tables)) wp_die('table argument for AdminDatabase must be an array');
foreach($this->tables as $table_name => $fields){
$table_name = $wpdb->prefix . $table_name;
$wpdb->query("DROP TABLE $table_name;");
}
}
}
class PluginUtilities{
function parse_args($args){
foreach($args as $k=>$v){
$this->$k = $v;
}
}
//fetch table row
function fetch_table($table_name, $where = false){
global $wpdb;
return $wpdb->get_results("SELECT * FROM $table_name ".($where ? $where : "").";");
}
//only processes call back if the page has POST set
function fetch_post($callback){
if(!isset($_POST) || count($_POST) < 1){
return false;
} else {
call_user_func($callback);
}
}
}
?>