-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBModelBase.php
More file actions
250 lines (203 loc) · 6.08 KB
/
DBModelBase.php
File metadata and controls
250 lines (203 loc) · 6.08 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* This handy class originated from Pippin's Easy Digital Downloads.
* https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/class-edd-db.php
*
* Sub-classes should define $table_name, $version, and $primary_key in __construct() method.
*
* @package EDD
* @subpackage Classes/EDD DB
* @copyright Copyright (c) 2015, Pippin Williamson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 2.1
*/
abstract class DB_Model_Base {
/**
* The name of our database table
*/
public $table_name;
/**
* The name of the primary column
*/
public $primary_key;
public function __construct() {}
/**
* Whitelist of columns
*/
public function get_column_ids() {
return array_map(function ($column) {
return $column->id;
}, self::get_columns());
}
/**
* Default column values
*/
public function get_column_defaults() {
$defaults = [];
foreach (self::get_columns() as $column) {
$defaults[$column->id] = $column->default;
}
return $defaults;
}
/**
* Retrieve a row by the primary key
*/
public function get( $row_id ) {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
$this->fromDB($row);
return $row;
}
/**
* Retrieve a row by a specific column / value
*/
public function get_by( $column, $row_id ) {
global $wpdb;
$column = esc_sql( $column );
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
$this->fromDB($row);
return $row;
}
/**
* Retrieve a specific column's value by the primary key
*/
public function get_column( $column, $row_id ) {
global $wpdb;
$column = esc_sql( $column );
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
}
/**
* Retrieve a specific column's value by the the specified column / value
*/
public function get_column_by( $column, $column_where, $column_value ) {
global $wpdb;
$column_where = esc_sql( $column_where );
$column = esc_sql( $column );
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
}
public function fromDB(array $dbValues) {
foreach ($dbValues as $columnName => $value) {
if (property_exists($this, $columnName)) {
$this->$columnName = $value;
}
}
}
public function toDB() {
$dbValues = [];
$whitelist = $this->get_column_ids();
foreach ($this as $property => $value) {
if(in_array($property, $whitelist)){
$dbValues[$property] = $value;
}
}
return $dbValues;
}
/**
* Insert a new row
*/
public function insert( ) {
global $wpdb;
// Set default values
$data = wp_parse_args( $this->toDB(), $this->get_column_defaults() );
// Initialise column format array
$column_formats = $this->get_column_ids();
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
$wpdb->insert( $this->table_name, $data, $column_formats );
$wpdb_insert_id = $wpdb->insert_id;
return $wpdb_insert_id;
}
/**
* Update a row
*/
public function update( $row_id, $data = null, $where = '' ) {
global $wpdb;
$data = is_null($data) ? $this->toDB() : $data;
// Row ID must be positive integer
$row_id = absint( $row_id );
if( empty( $row_id ) ) {
return false;
}
if( empty( $where ) ) {
$where = $this->primary_key;
}
// Initialise column format array
$column_formats = $this->get_column_ids();
// Force fields to lower case
$data = array_change_key_case( $data );
// White list columns
$data = array_intersect_key( $data, $column_formats );
// Reorder $column_formats to match the order of columns given in $data
$data_keys = array_keys( $data );
$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
return false;
}
return true;
}
/**
* Delete a row identified by the primary key
*/
public function delete( $row_id = 0 ) {
global $wpdb;
// Row ID must be positive integer
$row_id = absint( $row_id );
if( empty( $row_id ) ) {
return false;
}
if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
return false;
}
return true;
}
/**
* Check if the given table exists
*/
public function table_exists( $table ) {
global $wpdb;
$table = sanitize_text_field( $table );
return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
}
/**
* Check if the table was ever installed
*/
public function installed() {
return $this->table_exists( $this->table_name );
}
/**
* Install the table
*/
public function install(){
global $wpdb;
$table_name = $wpdb->prefix . $this->table_name;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (";
foreach ($this::get_columns() as $column) {
$sql = $sql . " $column->id $column->type$column->typeAddendum,";
}
$sql = $sql . " PRIMARY KEY ($this->primary_key)) $charset_collate;";
dbDelta( $sql );
}
abstract public static function get_columns();
}
class DB_Column{
public $id;
public $type;
public $typeAddendum;
public $label;
public $default;
public function __construct($id, $label, $type, $typeAddendum = "", $default = null) {
$this->id = $id;
$this->type = $type;
$this->typeAddendum = $typeAddendum;
$this->label = $label;
$this->default = $default;
}
}