|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// Exit if accessed directly |
|
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
|
5
|
|
|
|
|
6
|
|
|
abstract class Wpinv_DB { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The name of our database table |
|
10
|
|
|
* |
|
11
|
|
|
* @access public |
|
12
|
|
|
* @since 1.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
public $table_name; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The version of our database table |
|
18
|
|
|
* |
|
19
|
|
|
* @access public |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
public $version; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The name of the primary column |
|
26
|
|
|
* |
|
27
|
|
|
* @access public |
|
28
|
|
|
* @since 1.0.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
public $primary_key; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get things started |
|
34
|
|
|
* |
|
35
|
|
|
* @access public |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() {} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Whitelist of columns |
|
42
|
|
|
* |
|
43
|
|
|
* @access public |
|
44
|
|
|
* @since 1.0.0 |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get_columns() { |
|
48
|
|
|
return array(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Default column values |
|
53
|
|
|
* |
|
54
|
|
|
* @access public |
|
55
|
|
|
* @since 1.0.0 |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function get_column_defaults() { |
|
59
|
|
|
return array(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Retrieve a row by the primary key |
|
64
|
|
|
* |
|
65
|
|
|
* @access public |
|
66
|
|
|
* @since 1.0.0 |
|
67
|
|
|
* @return object |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get( $row_id ) { |
|
70
|
|
|
global $wpdb; |
|
71
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve a row by a specific column / value |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @since 1.0.0 |
|
79
|
|
|
* @return object |
|
80
|
|
|
*/ |
|
81
|
|
|
public function get_by( $column, $row_id ) { |
|
82
|
|
|
global $wpdb; |
|
83
|
|
|
$column = esc_sql( $column ); |
|
84
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Retrieve a specific column's value by the primary key |
|
89
|
|
|
* |
|
90
|
|
|
* @access public |
|
91
|
|
|
* @since 1.0.0 |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function get_column( $column, $row_id ) { |
|
95
|
|
|
global $wpdb; |
|
96
|
|
|
$column = esc_sql( $column ); |
|
97
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Retrieve a specific column's value by the the specified column / value |
|
102
|
|
|
* |
|
103
|
|
|
* @access public |
|
104
|
|
|
* @since 1.0.0 |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function get_column_by( $column, $column_where, $column_value ) { |
|
108
|
|
|
global $wpdb; |
|
109
|
|
|
$column_where = esc_sql( $column_where ); |
|
110
|
|
|
$column = esc_sql( $column ); |
|
111
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Insert a new row |
|
116
|
|
|
* |
|
117
|
|
|
* @access public |
|
118
|
|
|
* @since 1.0.0 |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
|
|
public function insert( $data, $type = '' ) { |
|
122
|
|
|
global $wpdb; |
|
123
|
|
|
|
|
124
|
|
|
// Set default values |
|
125
|
|
|
$data = wp_parse_args( $data, $this->get_column_defaults() ); |
|
126
|
|
|
|
|
127
|
|
|
do_action( 'wpinv_pre_insert_' . $type, $data ); |
|
128
|
|
|
|
|
129
|
|
|
// Initialise column format array |
|
130
|
|
|
$column_formats = $this->get_columns(); |
|
131
|
|
|
|
|
132
|
|
|
// Force fields to lower case |
|
133
|
|
|
$data = array_change_key_case( $data ); |
|
134
|
|
|
|
|
135
|
|
|
// White list columns |
|
136
|
|
|
$data = array_intersect_key( $data, $column_formats ); |
|
137
|
|
|
|
|
138
|
|
|
// Reorder $column_formats to match the order of columns given in $data |
|
139
|
|
|
$data_keys = array_keys( $data ); |
|
140
|
|
|
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
|
141
|
|
|
|
|
142
|
|
|
$wpdb->insert( $this->table_name, $data, $column_formats ); |
|
143
|
|
|
$wpdb_insert_id = $wpdb->insert_id; |
|
144
|
|
|
|
|
145
|
|
|
do_action( 'wpinv_post_insert_' . $type, $wpdb_insert_id, $data ); |
|
146
|
|
|
|
|
147
|
|
|
return $wpdb_insert_id; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Update a row |
|
152
|
|
|
* |
|
153
|
|
|
* @access public |
|
154
|
|
|
* @since 1.0.0 |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
public function update( $row_id, $data = array(), $where = '' ) { |
|
158
|
|
|
|
|
159
|
|
|
global $wpdb; |
|
160
|
|
|
|
|
161
|
|
|
// Row ID must be positive integer |
|
162
|
|
|
$row_id = absint( $row_id ); |
|
163
|
|
|
|
|
164
|
|
|
if( empty( $row_id ) ) { |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if( empty( $where ) ) { |
|
169
|
|
|
$where = $this->primary_key; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// Initialise column format array |
|
173
|
|
|
$column_formats = $this->get_columns(); |
|
174
|
|
|
|
|
175
|
|
|
// Force fields to lower case |
|
176
|
|
|
$data = array_change_key_case( $data ); |
|
177
|
|
|
|
|
178
|
|
|
// White list columns |
|
179
|
|
|
$data = array_intersect_key( $data, $column_formats ); |
|
180
|
|
|
|
|
181
|
|
|
// Reorder $column_formats to match the order of columns given in $data |
|
182
|
|
|
$data_keys = array_keys( $data ); |
|
183
|
|
|
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
|
184
|
|
|
|
|
185
|
|
|
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) { |
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Delete a row identified by the primary key |
|
194
|
|
|
* |
|
195
|
|
|
* @access public |
|
196
|
|
|
* @since 1.0.0 |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function delete( $row_id = 0 ) { |
|
200
|
|
|
|
|
201
|
|
|
global $wpdb; |
|
202
|
|
|
|
|
203
|
|
|
// Row ID must be positive integer |
|
204
|
|
|
$row_id = absint( $row_id ); |
|
205
|
|
|
|
|
206
|
|
|
if( empty( $row_id ) ) { |
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) { |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return true; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Check if the given table exists |
|
219
|
|
|
* |
|
220
|
|
|
* @since 2.4 |
|
221
|
|
|
* @param string $table The table name |
|
222
|
|
|
* @return bool If the table name exists |
|
223
|
|
|
*/ |
|
224
|
|
|
public function table_exists( $table ) { |
|
225
|
|
|
global $wpdb; |
|
226
|
|
|
$table = sanitize_text_field( $table ); |
|
227
|
|
|
|
|
228
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Check if the table was ever installed |
|
233
|
|
|
* |
|
234
|
|
|
* @since 2.4 |
|
235
|
|
|
* @return bool Returns if the customers table was installed and upgrade routine run |
|
236
|
|
|
*/ |
|
237
|
|
|
public function installed() { |
|
238
|
|
|
return $this->table_exists( $this->table_name ); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|