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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Admin */
/** * Represents the proxy for communicating with the database. */ class WPSEO_Database_Proxy {
/** * @var string */ protected $table_name;
/** * @var bool */ protected $suppress_errors = true;
/** * @var bool */ protected $is_multisite_table = false;
/** * @var bool */ protected $last_suppressed_state;
/** * @var wpdb */ protected $database;
/** * Sets the class attributes and registers the table. * * @param wpdb $database The database object. * @param string $table_name The table name that is represented. * @param bool $suppress_errors Should the errors be suppressed. * @param bool $is_multisite_table Should the table be global in multisite. */ public function __construct( $database, $table_name, $suppress_errors = true, $is_multisite_table = false ) { $this->table_name = $table_name; $this->suppress_errors = (bool) $suppress_errors; $this->is_multisite_table = (bool) $is_multisite_table; $this->database = $database;
// If the table prefix was provided, strip it as it's handled automatically. $table_prefix = $this->get_table_prefix(); if ( ! empty( $table_prefix ) && strpos( $this->table_name, $table_prefix ) === 0 ) { $this->table_prefix = substr( $this->table_name, strlen( $table_prefix ) ); }
if ( ! $this->is_table_registered() ) { $this->register_table(); } }
/** * Inserts data into the database. * * @param array $data Data to insert. * @param null $format Formats for the data. * * @return false|int Total amount of inserted rows or false on error. */ public function insert( array $data, $format = null ) { $this->pre_execution();
$result = $this->database->insert( $this->get_table_name(), $data, $format );
$this->post_execution();
return $result; }
/** * Updates data in the database. * * @param array $data Data to update on the table. * @param array $where Where condition as key => value array. * @param null $format Optional. data prepare format. * @param null $where_format Optional. Where prepare format. * * @return false|int False when the update request is invalid, int on number of rows changed. */ public function update( array $data, array $where, $format = null, $where_format = null ) { $this->pre_execution();
$result = $this->database->update( $this->get_table_name(), $data, $where, $format, $where_format );
$this->post_execution();
return $result; }
/** * Upserts data in the database. * * Performs an insert into and if key is duplicate it will update the existing record. * * @param array $data Data to update on the table. * @param array $where Unused. Where condition as key => value array. * @param null $format Optional. Data prepare format. * @param null $where_format Deprecated. Where prepare format. * * @return false|int False when the upsert request is invalid, int on number of rows changed. */ public function upsert( array $data, array $where = null, $format = null, $where_format = null ) { if ( $where_format !== null ) { _deprecated_argument( __METHOD__, '7.7.0', 'The where_format argument is deprecated' ); }
$this->pre_execution();
$update = array(); $keys = array(); $columns = array_keys( $data ); foreach ( $columns as $column ) { $keys[] = '`' . $column . '`'; $update[] = sprintf( '`%1$s` = VALUES(`%1$s`)', $column ); }
$query = sprintf( 'INSERT INTO `%1$s` (%2$s) VALUES ( %3$s ) ON DUPLICATE KEY UPDATE %4$s', $this->get_table_name(), implode( ', ', $keys ), implode( ', ', array_fill( 0, count( $data ), '%s' ) ), implode( ', ', $update ) );
$result = $this->database->query( $this->database->prepare( $query, array_values( $data ) ) );
$this->post_execution();
return $result; }
/** * Deletes a record from the database. * * @param array $where Where clauses for the query. * @param null|array $format Formats for the data. * * @return false|int */ public function delete( array $where, $format = null ) { $this->pre_execution();
$result = $this->database->delete( $this->get_table_name(), $where, $format );
$this->post_execution();
return $result; }
/** * Executes the given query and returns the results. * * @param string $query The query to execute. * * @return array|null|object The resultset */ public function get_results( $query ) { $this->pre_execution();
$results = $this->database->get_results( $query );
$this->post_execution();
return $results; }
/** * Creates a table to the database. * * @param array $columns The columns to create. * @param array $indexes The indexes to use. * * @return bool True when creation is successful. */ public function create_table( array $columns, array $indexes = array() ) { $create_table = sprintf( 'CREATE TABLE IF NOT EXISTS %1$s ( %2$s ) %3$s', $this->get_table_name(), implode( ',', array_merge( $columns, $indexes ) ), $this->database->get_charset_collate() );
$this->pre_execution();
$is_created = (bool) $this->database->query( $create_table );
$this->post_execution();
return $is_created; }
/** * Checks if there is an error. * * @return bool Returns true when there is an error. */ public function has_error() { return ( $this->database->last_error !== '' ); }
/** * Executed before a query will be ran. */ protected function pre_execution() { if ( $this->suppress_errors ) { $this->last_suppressed_state = $this->database->suppress_errors(); } }
/** * Executed after a query has been ran. */ protected function post_execution() { if ( $this->suppress_errors ) { $this->database->suppress_errors( $this->last_suppressed_state ); } }
/** * Returns the full table name. * * @return string Full table name including prefix. */ public function get_table_name() { return $this->get_table_prefix() . $this->table_name; }
/** * Returns the prefix to use for the table. * * @return string The table prefix depending on the database context. */ protected function get_table_prefix() { if ( $this->is_multisite_table ) { return $this->database->base_prefix; }
return $this->database->get_blog_prefix(); }
/** * Registers the table with WordPress. * * @return void */ protected function register_table() { $table_name = $this->table_name; $full_table_name = $this->get_table_name();
$this->database->$table_name = $full_table_name;
if ( $this->is_multisite_table ) { $this->database->ms_global_tables[] = $table_name; return; }
$this->database->tables[] = $table_name; }
/** * Checks if the table has been registered with WordPress. * * @return bool True if the table is registered, false otherwise. */ protected function is_table_registered() { if ( $this->is_multisite_table ) { return in_array( $this->table_name, $this->database->ms_global_tables, true ); }
return in_array( $this->table_name, $this->database->tables, true ); } }
|