author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
16 | 3 |
* WordPress database access abstraction class |
0 | 4 |
* |
5 |
* Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)} |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Database |
|
9 |
* @since 0.71 |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* @since 0.71 |
|
14 |
*/ |
|
15 |
define( 'EZSQL_VERSION', 'WP1.25' ); |
|
16 |
||
17 |
/** |
|
18 |
* @since 0.71 |
|
19 |
*/ |
|
5 | 20 |
define( 'OBJECT', 'OBJECT' ); |
9 | 21 |
// phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase |
5 | 22 |
define( 'object', 'OBJECT' ); // Back compat. |
0 | 23 |
|
24 |
/** |
|
25 |
* @since 2.5.0 |
|
26 |
*/ |
|
27 |
define( 'OBJECT_K', 'OBJECT_K' ); |
|
28 |
||
29 |
/** |
|
30 |
* @since 0.71 |
|
31 |
*/ |
|
32 |
define( 'ARRAY_A', 'ARRAY_A' ); |
|
33 |
||
34 |
/** |
|
35 |
* @since 0.71 |
|
36 |
*/ |
|
37 |
define( 'ARRAY_N', 'ARRAY_N' ); |
|
38 |
||
39 |
/** |
|
16 | 40 |
* WordPress database access abstraction class. |
41 |
* |
|
42 |
* This class is used to interact with a database without needing to use raw SQL statements. |
|
43 |
* By default, WordPress uses this class to instantiate the global $wpdb object, providing |
|
44 |
* access to the WordPress database. |
|
0 | 45 |
* |
16 | 46 |
* It is possible to replace this class with your own by setting the $wpdb global variable |
47 |
* in wp-content/db.php file to your class. The wpdb class will still be included, so you can |
|
48 |
* extend it or simply use your own. |
|
0 | 49 |
* |
16 | 50 |
* @link https://developer.wordpress.org/reference/classes/wpdb/ |
0 | 51 |
* |
52 |
* @since 0.71 |
|
53 |
*/ |
|
54 |
class wpdb { |
|
55 |
||
56 |
/** |
|
5 | 57 |
* Whether to show SQL/DB errors. |
58 |
* |
|
16 | 59 |
* Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true. |
0 | 60 |
* |
61 |
* @since 0.71 |
|
62 |
* @var bool |
|
63 |
*/ |
|
64 |
var $show_errors = false; |
|
65 |
||
66 |
/** |
|
16 | 67 |
* Whether to suppress errors during the DB bootstrapping. Default false. |
0 | 68 |
* |
69 |
* @since 2.5.0 |
|
70 |
* @var bool |
|
71 |
*/ |
|
72 |
var $suppress_errors = false; |
|
73 |
||
74 |
/** |
|
16 | 75 |
* The error encountered during the last query. |
0 | 76 |
* |
77 |
* @since 2.5.0 |
|
78 |
* @var string |
|
79 |
*/ |
|
5 | 80 |
public $last_error = ''; |
0 | 81 |
|
82 |
/** |
|
16 | 83 |
* The number of queries made. |
0 | 84 |
* |
85 |
* @since 1.2.0 |
|
86 |
* @var int |
|
87 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
public $num_queries = 0; |
0 | 89 |
|
90 |
/** |
|
16 | 91 |
* Count of rows returned by the last query. |
0 | 92 |
* |
93 |
* @since 0.71 |
|
94 |
* @var int |
|
95 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
public $num_rows = 0; |
0 | 97 |
|
98 |
/** |
|
16 | 99 |
* Count of rows affected by the last query. |
0 | 100 |
* |
101 |
* @since 0.71 |
|
102 |
* @var int |
|
103 |
*/ |
|
104 |
var $rows_affected = 0; |
|
105 |
||
106 |
/** |
|
16 | 107 |
* The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT). |
0 | 108 |
* |
109 |
* @since 0.71 |
|
110 |
* @var int |
|
111 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
public $insert_id = 0; |
0 | 113 |
|
114 |
/** |
|
16 | 115 |
* The last query made. |
0 | 116 |
* |
117 |
* @since 0.71 |
|
16 | 118 |
* @var string |
0 | 119 |
*/ |
120 |
var $last_query; |
|
121 |
||
122 |
/** |
|
16 | 123 |
* Results of the last query. |
0 | 124 |
* |
125 |
* @since 0.71 |
|
126 |
* @var array|null |
|
127 |
*/ |
|
128 |
var $last_result; |
|
129 |
||
130 |
/** |
|
131 |
* MySQL result, which is either a resource or boolean. |
|
132 |
* |
|
133 |
* @since 0.71 |
|
134 |
* @var mixed |
|
135 |
*/ |
|
136 |
protected $result; |
|
137 |
||
138 |
/** |
|
16 | 139 |
* Cached column info, for sanity checking data before inserting. |
5 | 140 |
* |
141 |
* @since 4.2.0 |
|
142 |
* @var array |
|
143 |
*/ |
|
144 |
protected $col_meta = array(); |
|
145 |
||
146 |
/** |
|
16 | 147 |
* Calculated character sets on tables. |
5 | 148 |
* |
149 |
* @since 4.2.0 |
|
150 |
* @var array |
|
151 |
*/ |
|
152 |
protected $table_charset = array(); |
|
153 |
||
154 |
/** |
|
16 | 155 |
* Whether text fields in the current query need to be sanity checked. Default false. |
5 | 156 |
* |
157 |
* @since 4.2.0 |
|
158 |
* @var bool |
|
159 |
*/ |
|
160 |
protected $check_current_query = true; |
|
161 |
||
162 |
/** |
|
163 |
* Flag to ensure we don't run into recursion problems when checking the collation. |
|
164 |
* |
|
165 |
* @since 4.2.0 |
|
166 |
* @see wpdb::check_safe_collation() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @var bool |
5 | 168 |
*/ |
169 |
private $checking_collation = false; |
|
170 |
||
171 |
/** |
|
16 | 172 |
* Saved info on the table column. |
0 | 173 |
* |
174 |
* @since 0.71 |
|
175 |
* @var array |
|
176 |
*/ |
|
177 |
protected $col_info; |
|
178 |
||
179 |
/** |
|
9 | 180 |
* Log of queries that were executed, for debugging purposes. |
0 | 181 |
* |
182 |
* @since 1.5.0 |
|
9 | 183 |
* @since 2.5.0 The third element in each query log was added to record the calling functions. |
184 |
* @since 5.1.0 The fourth element in each query log was added to record the start time. |
|
16 | 185 |
* @since 5.3.0 The fifth element in each query log was added to record custom data. |
9 | 186 |
* |
187 |
* @var array[] { |
|
188 |
* Array of queries that were executed. |
|
189 |
* |
|
190 |
* @type array ...$0 { |
|
191 |
* Data for each query. |
|
192 |
* |
|
193 |
* @type string $0 The query's SQL. |
|
194 |
* @type float $1 Total time spent on the query, in seconds. |
|
16 | 195 |
* @type string $2 Comma-separated list of the calling functions. |
9 | 196 |
* @type float $3 Unix timestamp of the time at the start of the query. |
16 | 197 |
* @type array $4 Custom query data. |
9 | 198 |
* } |
199 |
* } |
|
0 | 200 |
*/ |
201 |
var $queries; |
|
202 |
||
203 |
/** |
|
16 | 204 |
* The number of times to retry reconnecting before dying. Default 5. |
5 | 205 |
* |
206 |
* @since 3.9.0 |
|
207 |
* @see wpdb::check_connection() |
|
208 |
* @var int |
|
209 |
*/ |
|
210 |
protected $reconnect_retries = 5; |
|
211 |
||
212 |
/** |
|
0 | 213 |
* WordPress table prefix |
214 |
* |
|
16 | 215 |
* You can set this to have multiple WordPress installations in a single database. |
216 |
* The second reason is for possible security precautions. |
|
0 | 217 |
* |
218 |
* @since 2.5.0 |
|
219 |
* @var string |
|
220 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
public $prefix = ''; |
0 | 222 |
|
223 |
/** |
|
5 | 224 |
* WordPress base table prefix. |
225 |
* |
|
226 |
* @since 3.0.0 |
|
227 |
* @var string |
|
228 |
*/ |
|
9 | 229 |
public $base_prefix; |
5 | 230 |
|
231 |
/** |
|
0 | 232 |
* Whether the database queries are ready to start executing. |
233 |
* |
|
234 |
* @since 2.3.2 |
|
235 |
* @var bool |
|
236 |
*/ |
|
237 |
var $ready = false; |
|
238 |
||
239 |
/** |
|
5 | 240 |
* Blog ID. |
0 | 241 |
* |
242 |
* @since 3.0.0 |
|
243 |
* @var int |
|
244 |
*/ |
|
5 | 245 |
public $blogid = 0; |
0 | 246 |
|
247 |
/** |
|
5 | 248 |
* Site ID. |
0 | 249 |
* |
250 |
* @since 3.0.0 |
|
251 |
* @var int |
|
252 |
*/ |
|
5 | 253 |
public $siteid = 0; |
0 | 254 |
|
255 |
/** |
|
16 | 256 |
* List of WordPress per-blog tables. |
0 | 257 |
* |
258 |
* @since 2.5.0 |
|
259 |
* @see wpdb::tables() |
|
260 |
* @var array |
|
261 |
*/ |
|
9 | 262 |
var $tables = array( |
263 |
'posts', |
|
264 |
'comments', |
|
265 |
'links', |
|
266 |
'options', |
|
267 |
'postmeta', |
|
268 |
'terms', |
|
269 |
'term_taxonomy', |
|
270 |
'term_relationships', |
|
271 |
'termmeta', |
|
272 |
'commentmeta', |
|
273 |
); |
|
0 | 274 |
|
275 |
/** |
|
16 | 276 |
* List of deprecated WordPress tables. |
0 | 277 |
* |
16 | 278 |
* 'categories', 'post2cat', and 'link2cat' were deprecated in 2.3.0, db version 5539. |
0 | 279 |
* |
280 |
* @since 2.9.0 |
|
281 |
* @see wpdb::tables() |
|
282 |
* @var array |
|
283 |
*/ |
|
284 |
var $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
|
285 |
||
286 |
/** |
|
16 | 287 |
* List of WordPress global tables. |
0 | 288 |
* |
289 |
* @since 3.0.0 |
|
290 |
* @see wpdb::tables() |
|
291 |
* @var array |
|
292 |
*/ |
|
293 |
var $global_tables = array( 'users', 'usermeta' ); |
|
294 |
||
295 |
/** |
|
16 | 296 |
* List of Multisite global tables. |
0 | 297 |
* |
298 |
* @since 3.0.0 |
|
299 |
* @see wpdb::tables() |
|
300 |
* @var array |
|
301 |
*/ |
|
9 | 302 |
var $ms_global_tables = array( |
303 |
'blogs', |
|
304 |
'blogmeta', |
|
305 |
'signups', |
|
306 |
'site', |
|
307 |
'sitemeta', |
|
308 |
'sitecategories', |
|
309 |
'registration_log', |
|
310 |
); |
|
0 | 311 |
|
312 |
/** |
|
16 | 313 |
* WordPress Comments table. |
0 | 314 |
* |
315 |
* @since 1.5.0 |
|
316 |
* @var string |
|
317 |
*/ |
|
5 | 318 |
public $comments; |
0 | 319 |
|
320 |
/** |
|
16 | 321 |
* WordPress Comment Metadata table. |
0 | 322 |
* |
323 |
* @since 2.9.0 |
|
324 |
* @var string |
|
325 |
*/ |
|
5 | 326 |
public $commentmeta; |
0 | 327 |
|
328 |
/** |
|
16 | 329 |
* WordPress Links table. |
0 | 330 |
* |
331 |
* @since 1.5.0 |
|
332 |
* @var string |
|
333 |
*/ |
|
5 | 334 |
public $links; |
0 | 335 |
|
336 |
/** |
|
16 | 337 |
* WordPress Options table. |
0 | 338 |
* |
339 |
* @since 1.5.0 |
|
340 |
* @var string |
|
341 |
*/ |
|
5 | 342 |
public $options; |
0 | 343 |
|
344 |
/** |
|
16 | 345 |
* WordPress Post Metadata table. |
0 | 346 |
* |
347 |
* @since 1.5.0 |
|
348 |
* @var string |
|
349 |
*/ |
|
5 | 350 |
public $postmeta; |
0 | 351 |
|
352 |
/** |
|
16 | 353 |
* WordPress Posts table. |
0 | 354 |
* |
355 |
* @since 1.5.0 |
|
356 |
* @var string |
|
357 |
*/ |
|
5 | 358 |
public $posts; |
0 | 359 |
|
360 |
/** |
|
16 | 361 |
* WordPress Terms table. |
0 | 362 |
* |
363 |
* @since 2.3.0 |
|
364 |
* @var string |
|
365 |
*/ |
|
5 | 366 |
public $terms; |
0 | 367 |
|
368 |
/** |
|
16 | 369 |
* WordPress Term Relationships table. |
0 | 370 |
* |
371 |
* @since 2.3.0 |
|
372 |
* @var string |
|
373 |
*/ |
|
5 | 374 |
public $term_relationships; |
0 | 375 |
|
376 |
/** |
|
16 | 377 |
* WordPress Term Taxonomy table. |
0 | 378 |
* |
379 |
* @since 2.3.0 |
|
380 |
* @var string |
|
381 |
*/ |
|
5 | 382 |
public $term_taxonomy; |
0 | 383 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
* WordPress Term Meta table. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
* @var string |
0 | 389 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
public $termmeta; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
// |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
// Global and Multisite tables |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
// |
0 | 395 |
|
396 |
/** |
|
16 | 397 |
* WordPress User Metadata table. |
0 | 398 |
* |
399 |
* @since 2.3.0 |
|
400 |
* @var string |
|
401 |
*/ |
|
5 | 402 |
public $usermeta; |
0 | 403 |
|
404 |
/** |
|
16 | 405 |
* WordPress Users table. |
0 | 406 |
* |
407 |
* @since 1.5.0 |
|
408 |
* @var string |
|
409 |
*/ |
|
5 | 410 |
public $users; |
0 | 411 |
|
412 |
/** |
|
16 | 413 |
* Multisite Blogs table. |
0 | 414 |
* |
415 |
* @since 3.0.0 |
|
416 |
* @var string |
|
417 |
*/ |
|
5 | 418 |
public $blogs; |
0 | 419 |
|
420 |
/** |
|
16 | 421 |
* Multisite Blog Metadata table. |
9 | 422 |
* |
423 |
* @since 5.1.0 |
|
424 |
* @var string |
|
425 |
*/ |
|
426 |
public $blogmeta; |
|
427 |
||
428 |
/** |
|
16 | 429 |
* Multisite Registration Log table. |
0 | 430 |
* |
431 |
* @since 3.0.0 |
|
432 |
* @var string |
|
433 |
*/ |
|
5 | 434 |
public $registration_log; |
0 | 435 |
|
436 |
/** |
|
16 | 437 |
* Multisite Signups table. |
0 | 438 |
* |
439 |
* @since 3.0.0 |
|
440 |
* @var string |
|
441 |
*/ |
|
5 | 442 |
public $signups; |
0 | 443 |
|
444 |
/** |
|
16 | 445 |
* Multisite Sites table. |
0 | 446 |
* |
447 |
* @since 3.0.0 |
|
448 |
* @var string |
|
449 |
*/ |
|
5 | 450 |
public $site; |
0 | 451 |
|
452 |
/** |
|
16 | 453 |
* Multisite Sitewide Terms table. |
0 | 454 |
* |
455 |
* @since 3.0.0 |
|
456 |
* @var string |
|
457 |
*/ |
|
5 | 458 |
public $sitecategories; |
0 | 459 |
|
460 |
/** |
|
16 | 461 |
* Multisite Site Metadata table. |
0 | 462 |
* |
463 |
* @since 3.0.0 |
|
464 |
* @var string |
|
465 |
*/ |
|
5 | 466 |
public $sitemeta; |
0 | 467 |
|
468 |
/** |
|
16 | 469 |
* Format specifiers for DB columns. |
0 | 470 |
* |
16 | 471 |
* Columns not listed here default to %s. Initialized during WP load. |
472 |
* Keys are column names, values are format types: 'ID' => '%d'. |
|
0 | 473 |
* |
474 |
* @since 2.8.0 |
|
475 |
* @see wpdb::prepare() |
|
476 |
* @see wpdb::insert() |
|
477 |
* @see wpdb::update() |
|
478 |
* @see wpdb::delete() |
|
479 |
* @see wp_set_wpdb_vars() |
|
480 |
* @var array |
|
481 |
*/ |
|
5 | 482 |
public $field_types = array(); |
0 | 483 |
|
484 |
/** |
|
16 | 485 |
* Database table columns charset. |
0 | 486 |
* |
487 |
* @since 2.2.0 |
|
488 |
* @var string |
|
489 |
*/ |
|
5 | 490 |
public $charset; |
0 | 491 |
|
492 |
/** |
|
16 | 493 |
* Database table columns collate. |
0 | 494 |
* |
495 |
* @since 2.2.0 |
|
496 |
* @var string |
|
497 |
*/ |
|
5 | 498 |
public $collate; |
0 | 499 |
|
500 |
/** |
|
16 | 501 |
* Database Username. |
0 | 502 |
* |
503 |
* @since 2.9.0 |
|
504 |
* @var string |
|
505 |
*/ |
|
506 |
protected $dbuser; |
|
507 |
||
508 |
/** |
|
16 | 509 |
* Database Password. |
0 | 510 |
* |
511 |
* @since 3.1.0 |
|
512 |
* @var string |
|
513 |
*/ |
|
514 |
protected $dbpassword; |
|
515 |
||
516 |
/** |
|
16 | 517 |
* Database Name. |
0 | 518 |
* |
519 |
* @since 3.1.0 |
|
520 |
* @var string |
|
521 |
*/ |
|
522 |
protected $dbname; |
|
523 |
||
524 |
/** |
|
16 | 525 |
* Database Host. |
0 | 526 |
* |
527 |
* @since 3.1.0 |
|
528 |
* @var string |
|
529 |
*/ |
|
530 |
protected $dbhost; |
|
531 |
||
532 |
/** |
|
16 | 533 |
* Database Handle. |
0 | 534 |
* |
535 |
* @since 0.71 |
|
536 |
* @var string |
|
537 |
*/ |
|
538 |
protected $dbh; |
|
539 |
||
540 |
/** |
|
16 | 541 |
* A textual description of the last query/get_row/get_var call. |
0 | 542 |
* |
543 |
* @since 3.0.0 |
|
544 |
* @var string |
|
545 |
*/ |
|
5 | 546 |
public $func_call; |
0 | 547 |
|
548 |
/** |
|
549 |
* Whether MySQL is used as the database engine. |
|
550 |
* |
|
16 | 551 |
* Set in wpdb::db_connect() to true, by default. This is used when checking |
0 | 552 |
* against the required MySQL version for WordPress. Normally, a replacement |
553 |
* database drop-in (db.php) will skip these checks, but setting this to true |
|
554 |
* will force the checks to occur. |
|
555 |
* |
|
556 |
* @since 3.3.0 |
|
557 |
* @var bool |
|
558 |
*/ |
|
559 |
public $is_mysql = null; |
|
560 |
||
561 |
/** |
|
5 | 562 |
* A list of incompatible SQL modes. |
563 |
* |
|
564 |
* @since 3.9.0 |
|
565 |
* @var array |
|
566 |
*/ |
|
9 | 567 |
protected $incompatible_modes = array( |
568 |
'NO_ZERO_DATE', |
|
569 |
'ONLY_FULL_GROUP_BY', |
|
570 |
'STRICT_TRANS_TABLES', |
|
571 |
'STRICT_ALL_TABLES', |
|
572 |
'TRADITIONAL', |
|
16 | 573 |
'ANSI', |
9 | 574 |
); |
5 | 575 |
|
576 |
/** |
|
16 | 577 |
* Whether to use mysqli over mysql. Default false. |
5 | 578 |
* |
579 |
* @since 3.9.0 |
|
580 |
* @var bool |
|
581 |
*/ |
|
582 |
private $use_mysqli = false; |
|
583 |
||
584 |
/** |
|
16 | 585 |
* Whether we've managed to successfully connect at some point. |
5 | 586 |
* |
587 |
* @since 3.9.0 |
|
588 |
* @var bool |
|
589 |
*/ |
|
590 |
private $has_connected = false; |
|
591 |
||
592 |
/** |
|
16 | 593 |
* Connects to the database server and selects a database. |
0 | 594 |
* |
16 | 595 |
* PHP5 style constructor for compatibility with PHP5. Does the actual setting up |
596 |
* of the class properties and connection to the database. |
|
0 | 597 |
* |
598 |
* @since 2.0.8 |
|
599 |
* |
|
16 | 600 |
* @link https://core.trac.wordpress.org/ticket/3354 |
601 |
* @global string $wp_version The WordPress version string. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* |
16 | 603 |
* @param string $dbuser MySQL database user. |
604 |
* @param string $dbpassword MySQL database password. |
|
605 |
* @param string $dbname MySQL database name. |
|
606 |
* @param string $dbhost MySQL database host. |
|
0 | 607 |
*/ |
5 | 608 |
public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
9 | 609 |
if ( WP_DEBUG && WP_DEBUG_DISPLAY ) { |
0 | 610 |
$this->show_errors(); |
9 | 611 |
} |
0 | 612 |
|
16 | 613 |
// Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true. |
5 | 614 |
if ( function_exists( 'mysqli_connect' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
$this->use_mysqli = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
|
5 | 617 |
if ( defined( 'WP_USE_EXT_MYSQL' ) ) { |
618 |
$this->use_mysqli = ! WP_USE_EXT_MYSQL; |
|
619 |
} |
|
620 |
} |
|
0 | 621 |
|
9 | 622 |
$this->dbuser = $dbuser; |
0 | 623 |
$this->dbpassword = $dbpassword; |
9 | 624 |
$this->dbname = $dbname; |
625 |
$this->dbhost = $dbhost; |
|
0 | 626 |
|
5 | 627 |
// wp-config.php creation will manually connect when ready. |
628 |
if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
629 |
return; |
|
630 |
} |
|
631 |
||
0 | 632 |
$this->db_connect(); |
633 |
} |
|
634 |
||
635 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
* Makes private properties readable for backward compatibility. |
0 | 637 |
* |
638 |
* @since 3.5.0 |
|
639 |
* |
|
16 | 640 |
* @param string $name The private member to get, and optionally process. |
641 |
* @return mixed The private member. |
|
0 | 642 |
*/ |
5 | 643 |
public function __get( $name ) { |
9 | 644 |
if ( 'col_info' === $name ) { |
0 | 645 |
$this->load_col_info(); |
9 | 646 |
} |
0 | 647 |
|
648 |
return $this->$name; |
|
649 |
} |
|
650 |
||
651 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* Makes private properties settable for backward compatibility. |
0 | 653 |
* |
654 |
* @since 3.5.0 |
|
655 |
* |
|
16 | 656 |
* @param string $name The private member to set. |
657 |
* @param mixed $value The value to set. |
|
0 | 658 |
*/ |
5 | 659 |
public function __set( $name, $value ) { |
660 |
$protected_members = array( |
|
661 |
'col_meta', |
|
662 |
'table_charset', |
|
663 |
'check_current_query', |
|
664 |
); |
|
9 | 665 |
if ( in_array( $name, $protected_members, true ) ) { |
5 | 666 |
return; |
667 |
} |
|
0 | 668 |
$this->$name = $value; |
669 |
} |
|
670 |
||
671 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* Makes private properties check-able for backward compatibility. |
0 | 673 |
* |
674 |
* @since 3.5.0 |
|
675 |
* |
|
16 | 676 |
* @param string $name The private member to check. |
677 |
* @return bool If the member is set or not. |
|
0 | 678 |
*/ |
5 | 679 |
public function __isset( $name ) { |
0 | 680 |
return isset( $this->$name ); |
681 |
} |
|
682 |
||
683 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
* Makes private properties un-settable for backward compatibility. |
0 | 685 |
* |
686 |
* @since 3.5.0 |
|
687 |
* |
|
688 |
* @param string $name The private member to unset |
|
689 |
*/ |
|
5 | 690 |
public function __unset( $name ) { |
0 | 691 |
unset( $this->$name ); |
692 |
} |
|
693 |
||
694 |
/** |
|
16 | 695 |
* Sets $this->charset and $this->collate. |
0 | 696 |
* |
697 |
* @since 3.1.0 |
|
698 |
*/ |
|
5 | 699 |
public function init_charset() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
$charset = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
$collate = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
|
9 | 703 |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
$charset = 'utf8'; |
5 | 705 |
if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
706 |
$collate = DB_COLLATE; |
5 | 707 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
$collate = 'utf8_general_ci'; |
5 | 709 |
} |
0 | 710 |
} elseif ( defined( 'DB_COLLATE' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
$collate = DB_COLLATE; |
0 | 712 |
} |
713 |
||
5 | 714 |
if ( defined( 'DB_CHARSET' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
$charset = DB_CHARSET; |
5 | 716 |
} |
717 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
$charset_collate = $this->determine_charset( $charset, $collate ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
720 |
$this->charset = $charset_collate['charset']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
$this->collate = $charset_collate['collate']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
722 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
* Determines the best charset and collation to use given a charset and collation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
* For example, when able, utf8mb4 should be used instead of utf8. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* @param string $charset The character set to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
* @param string $collate The collation to check. |
16 | 733 |
* @return array { |
734 |
* The most appropriate character set and collation to use. |
|
735 |
* |
|
736 |
* @type string $charset Character set. |
|
737 |
* @type string $collate Collation. |
|
738 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
public function determine_charset( $charset, $collate ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
return compact( 'charset', 'collate' ); |
5 | 743 |
} |
744 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
if ( 'utf8' === $charset && $this->has_cap( 'utf8mb4' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
$charset = 'utf8mb4'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
$charset = 'utf8'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
$collate = str_replace( 'utf8mb4_', 'utf8_', $collate ); |
5 | 752 |
} |
753 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
if ( 'utf8mb4' === $charset ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
// _general_ is outdated, so we can upgrade it to _unicode_, instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
if ( ! $collate || 'utf8_general_ci' === $collate ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
$collate = 'utf8mb4_unicode_ci'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
$collate = str_replace( 'utf8_', 'utf8mb4_', $collate ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
760 |
} |
5 | 761 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
// _unicode_520_ is a better collation, we should use that when it's available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
$collate = 'utf8mb4_unicode_520_ci'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
return compact( 'charset', 'collate' ); |
0 | 769 |
} |
770 |
||
771 |
/** |
|
772 |
* Sets the connection's character set. |
|
773 |
* |
|
774 |
* @since 3.1.0 |
|
775 |
* |
|
16 | 776 |
* @param resource $dbh The resource given by mysql_connect. |
5 | 777 |
* @param string $charset Optional. The character set. Default null. |
778 |
* @param string $collate Optional. The collation. Default null. |
|
0 | 779 |
*/ |
5 | 780 |
public function set_charset( $dbh, $charset = null, $collate = null ) { |
9 | 781 |
if ( ! isset( $charset ) ) { |
0 | 782 |
$charset = $this->charset; |
9 | 783 |
} |
784 |
if ( ! isset( $collate ) ) { |
|
0 | 785 |
$collate = $this->collate; |
9 | 786 |
} |
0 | 787 |
if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
$set_charset_succeeded = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
|
5 | 790 |
if ( $this->use_mysqli ) { |
791 |
if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
$set_charset_succeeded = mysqli_set_charset( $dbh, $charset ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
if ( $set_charset_succeeded ) { |
5 | 796 |
$query = $this->prepare( 'SET NAMES %s', $charset ); |
9 | 797 |
if ( ! empty( $collate ) ) { |
5 | 798 |
$query .= $this->prepare( ' COLLATE %s', $collate ); |
9 | 799 |
} |
5 | 800 |
mysqli_query( $dbh, $query ); |
801 |
} |
|
802 |
} else { |
|
803 |
if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
$set_charset_succeeded = mysql_set_charset( $charset, $dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
if ( $set_charset_succeeded ) { |
5 | 807 |
$query = $this->prepare( 'SET NAMES %s', $charset ); |
9 | 808 |
if ( ! empty( $collate ) ) { |
5 | 809 |
$query .= $this->prepare( ' COLLATE %s', $collate ); |
9 | 810 |
} |
5 | 811 |
mysql_query( $query, $dbh ); |
812 |
} |
|
813 |
} |
|
814 |
} |
|
815 |
} |
|
816 |
||
817 |
/** |
|
16 | 818 |
* Changes the current SQL mode, and ensures its WordPress compatibility. |
5 | 819 |
* |
16 | 820 |
* If no modes are passed, it will ensure the current MySQL server modes are compatible. |
5 | 821 |
* |
822 |
* @since 3.9.0 |
|
823 |
* |
|
16 | 824 |
* @param array $modes Optional. A list of SQL modes to set. Default empty array. |
5 | 825 |
*/ |
826 |
public function set_sql_mode( $modes = array() ) { |
|
827 |
if ( empty( $modes ) ) { |
|
828 |
if ( $this->use_mysqli ) { |
|
829 |
$res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' ); |
|
0 | 830 |
} else { |
5 | 831 |
$res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh ); |
832 |
} |
|
833 |
||
834 |
if ( empty( $res ) ) { |
|
835 |
return; |
|
836 |
} |
|
837 |
||
838 |
if ( $this->use_mysqli ) { |
|
839 |
$modes_array = mysqli_fetch_array( $res ); |
|
840 |
if ( empty( $modes_array[0] ) ) { |
|
841 |
return; |
|
842 |
} |
|
843 |
$modes_str = $modes_array[0]; |
|
844 |
} else { |
|
845 |
$modes_str = mysql_result( $res, 0 ); |
|
846 |
} |
|
847 |
||
848 |
if ( empty( $modes_str ) ) { |
|
849 |
return; |
|
0 | 850 |
} |
5 | 851 |
|
852 |
$modes = explode( ',', $modes_str ); |
|
853 |
} |
|
854 |
||
855 |
$modes = array_change_key_case( $modes, CASE_UPPER ); |
|
856 |
||
857 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
* Filters the list of incompatible SQL modes to exclude. |
5 | 859 |
* |
860 |
* @since 3.9.0 |
|
861 |
* |
|
862 |
* @param array $incompatible_modes An array of incompatible modes. |
|
863 |
*/ |
|
864 |
$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes ); |
|
865 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
foreach ( $modes as $i => $mode ) { |
16 | 867 |
if ( in_array( $mode, $incompatible_modes, true ) ) { |
5 | 868 |
unset( $modes[ $i ] ); |
869 |
} |
|
870 |
} |
|
871 |
||
872 |
$modes_str = implode( ',', $modes ); |
|
873 |
||
874 |
if ( $this->use_mysqli ) { |
|
875 |
mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" ); |
|
876 |
} else { |
|
877 |
mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh ); |
|
0 | 878 |
} |
879 |
} |
|
880 |
||
881 |
/** |
|
882 |
* Sets the table prefix for the WordPress tables. |
|
883 |
* |
|
884 |
* @since 2.5.0 |
|
885 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
886 |
* @param string $prefix Alphanumeric name for the new prefix. |
16 | 887 |
* @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, |
888 |
* should be updated or not. Default true. |
|
889 |
* @return string|WP_Error Old prefix or WP_Error on error. |
|
0 | 890 |
*/ |
5 | 891 |
public function set_prefix( $prefix, $set_table_names = true ) { |
0 | 892 |
|
9 | 893 |
if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) { |
894 |
return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' ); |
|
895 |
} |
|
0 | 896 |
|
897 |
$old_prefix = is_multisite() ? '' : $prefix; |
|
898 |
||
9 | 899 |
if ( isset( $this->base_prefix ) ) { |
0 | 900 |
$old_prefix = $this->base_prefix; |
9 | 901 |
} |
0 | 902 |
|
903 |
$this->base_prefix = $prefix; |
|
904 |
||
905 |
if ( $set_table_names ) { |
|
9 | 906 |
foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) { |
0 | 907 |
$this->$table = $prefixed_table; |
9 | 908 |
} |
909 |
||
910 |
if ( is_multisite() && empty( $this->blogid ) ) { |
|
0 | 911 |
return $old_prefix; |
9 | 912 |
} |
0 | 913 |
|
914 |
$this->prefix = $this->get_blog_prefix(); |
|
915 |
||
9 | 916 |
foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) { |
0 | 917 |
$this->$table = $prefixed_table; |
9 | 918 |
} |
919 |
||
920 |
foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) { |
|
0 | 921 |
$this->$table = $prefixed_table; |
9 | 922 |
} |
0 | 923 |
} |
924 |
return $old_prefix; |
|
925 |
} |
|
926 |
||
927 |
/** |
|
16 | 928 |
* Sets blog ID. |
0 | 929 |
* |
930 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
* |
0 | 932 |
* @param int $blog_id |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
* @param int $network_id Optional. |
16 | 934 |
* @return int Previous blog ID. |
0 | 935 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
public function set_blog_id( $blog_id, $network_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
if ( ! empty( $network_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
$this->siteid = $network_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
} |
0 | 940 |
|
941 |
$old_blog_id = $this->blogid; |
|
942 |
$this->blogid = $blog_id; |
|
943 |
||
944 |
$this->prefix = $this->get_blog_prefix(); |
|
945 |
||
9 | 946 |
foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) { |
0 | 947 |
$this->$table = $prefixed_table; |
9 | 948 |
} |
949 |
||
950 |
foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) { |
|
0 | 951 |
$this->$table = $prefixed_table; |
9 | 952 |
} |
0 | 953 |
|
954 |
return $old_blog_id; |
|
955 |
} |
|
956 |
||
957 |
/** |
|
958 |
* Gets blog prefix. |
|
959 |
* |
|
960 |
* @since 3.0.0 |
|
16 | 961 |
* |
0 | 962 |
* @param int $blog_id Optional. |
963 |
* @return string Blog prefix. |
|
964 |
*/ |
|
5 | 965 |
public function get_blog_prefix( $blog_id = null ) { |
0 | 966 |
if ( is_multisite() ) { |
9 | 967 |
if ( null === $blog_id ) { |
0 | 968 |
$blog_id = $this->blogid; |
9 | 969 |
} |
16 | 970 |
|
0 | 971 |
$blog_id = (int) $blog_id; |
16 | 972 |
|
973 |
if ( defined( 'MULTISITE' ) && ( 0 === $blog_id || 1 === $blog_id ) ) { |
|
0 | 974 |
return $this->base_prefix; |
9 | 975 |
} else { |
0 | 976 |
return $this->base_prefix . $blog_id . '_'; |
9 | 977 |
} |
0 | 978 |
} else { |
979 |
return $this->base_prefix; |
|
980 |
} |
|
981 |
} |
|
982 |
||
983 |
/** |
|
984 |
* Returns an array of WordPress tables. |
|
985 |
* |
|
16 | 986 |
* Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to override the WordPress users |
987 |
* and usermeta tables that would otherwise be determined by the prefix. |
|
0 | 988 |
* |
16 | 989 |
* The $scope argument can take one of the following: |
0 | 990 |
* |
991 |
* 'all' - returns 'all' and 'global' tables. No old tables are returned. |
|
992 |
* 'blog' - returns the blog-level tables for the queried blog. |
|
16 | 993 |
* 'global' - returns the global tables for the installation, returning multisite tables only on multisite. |
0 | 994 |
* 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
995 |
* 'old' - returns tables which are deprecated. |
|
996 |
* |
|
997 |
* @since 3.0.0 |
|
16 | 998 |
* |
0 | 999 |
* @uses wpdb::$tables |
1000 |
* @uses wpdb::$old_tables |
|
1001 |
* @uses wpdb::$global_tables |
|
1002 |
* @uses wpdb::$ms_global_tables |
|
1003 |
* |
|
16 | 1004 |
* @param string $scope Optional. Possible values include 'all', 'global', 'ms_global', 'blog', |
1005 |
* or 'old' tables. Default 'all'. |
|
1006 |
* @param bool $prefix Optional. Whether to include table prefixes. If blog prefix is requested, |
|
1007 |
* then the custom users and usermeta tables will be mapped. Default true. |
|
1008 |
* @param int $blog_id Optional. The blog_id to prefix. Used only when prefix is requested. |
|
1009 |
* Defaults to wpdb::$blogid. |
|
0 | 1010 |
* @return array Table names. When a prefix is requested, the key is the unprefixed table name. |
1011 |
*/ |
|
5 | 1012 |
public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
0 | 1013 |
switch ( $scope ) { |
9 | 1014 |
case 'all': |
0 | 1015 |
$tables = array_merge( $this->global_tables, $this->tables ); |
9 | 1016 |
if ( is_multisite() ) { |
0 | 1017 |
$tables = array_merge( $tables, $this->ms_global_tables ); |
9 | 1018 |
} |
0 | 1019 |
break; |
9 | 1020 |
case 'blog': |
0 | 1021 |
$tables = $this->tables; |
1022 |
break; |
|
9 | 1023 |
case 'global': |
0 | 1024 |
$tables = $this->global_tables; |
9 | 1025 |
if ( is_multisite() ) { |
0 | 1026 |
$tables = array_merge( $tables, $this->ms_global_tables ); |
9 | 1027 |
} |
0 | 1028 |
break; |
9 | 1029 |
case 'ms_global': |
0 | 1030 |
$tables = $this->ms_global_tables; |
1031 |
break; |
|
9 | 1032 |
case 'old': |
0 | 1033 |
$tables = $this->old_tables; |
1034 |
break; |
|
9 | 1035 |
default: |
0 | 1036 |
return array(); |
1037 |
} |
|
1038 |
||
1039 |
if ( $prefix ) { |
|
9 | 1040 |
if ( ! $blog_id ) { |
0 | 1041 |
$blog_id = $this->blogid; |
9 | 1042 |
} |
1043 |
$blog_prefix = $this->get_blog_prefix( $blog_id ); |
|
1044 |
$base_prefix = $this->base_prefix; |
|
0 | 1045 |
$global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); |
1046 |
foreach ( $tables as $k => $table ) { |
|
16 | 1047 |
if ( in_array( $table, $global_tables, true ) ) { |
0 | 1048 |
$tables[ $table ] = $base_prefix . $table; |
9 | 1049 |
} else { |
0 | 1050 |
$tables[ $table ] = $blog_prefix . $table; |
9 | 1051 |
} |
0 | 1052 |
unset( $tables[ $k ] ); |
1053 |
} |
|
1054 |
||
9 | 1055 |
if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) { |
0 | 1056 |
$tables['users'] = CUSTOM_USER_TABLE; |
9 | 1057 |
} |
1058 |
||
1059 |
if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) { |
|
0 | 1060 |
$tables['usermeta'] = CUSTOM_USER_META_TABLE; |
9 | 1061 |
} |
0 | 1062 |
} |
1063 |
||
1064 |
return $tables; |
|
1065 |
} |
|
1066 |
||
1067 |
/** |
|
1068 |
* Selects a database using the current database connection. |
|
1069 |
* |
|
16 | 1070 |
* The database name will be changed based on the current database connection. |
1071 |
* On failure, the execution will bail and display a DB error. |
|
0 | 1072 |
* |
1073 |
* @since 0.71 |
|
1074 |
* |
|
16 | 1075 |
* @param string $db MySQL database name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
* @param resource|null $dbh Optional link identifier. |
0 | 1077 |
*/ |
5 | 1078 |
public function select( $db, $dbh = null ) { |
9 | 1079 |
if ( is_null( $dbh ) ) { |
0 | 1080 |
$dbh = $this->dbh; |
9 | 1081 |
} |
0 | 1082 |
|
5 | 1083 |
if ( $this->use_mysqli ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
$success = mysqli_select_db( $dbh, $db ); |
5 | 1085 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1086 |
$success = mysql_select_db( $db, $dbh ); |
5 | 1087 |
} |
1088 |
if ( ! $success ) { |
|
0 | 1089 |
$this->ready = false; |
5 | 1090 |
if ( ! did_action( 'template_redirect' ) ) { |
1091 |
wp_load_translations_early(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1093 |
$message = '<h1>' . __( 'Can’t select database' ) . "</h1>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1095 |
$message .= '<p>' . sprintf( |
16 | 1096 |
/* translators: %s: Database name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
__( 'We were able to connect to the database server (which means your username and password is okay) but not able to select the %s database.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1101 |
$message .= "<ul>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
$message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
$message .= '<li>' . sprintf( |
16 | 1105 |
/* translators: 1: Database user, 2: Database name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
__( 'Does the user %1$s have permission to use the %2$s database?' ), |
9 | 1107 |
'<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES ) . '</code>', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1108 |
'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1109 |
) . "</li>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1111 |
$message .= '<li>' . sprintf( |
16 | 1112 |
/* translators: %s: Database name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
__( 'On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
htmlspecialchars( $db, ENT_QUOTES ) |
9 | 1115 |
) . "</li>\n"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
$message .= "</ul>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1119 |
$message .= '<p>' . sprintf( |
16 | 1120 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
__( 'If you don’t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="%s">WordPress Support Forums</a>.' ), |
9 | 1122 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
$this->bail( $message, 'db_select_fail' ); |
5 | 1126 |
} |
0 | 1127 |
} |
1128 |
} |
|
1129 |
||
1130 |
/** |
|
1131 |
* Do not use, deprecated. |
|
1132 |
* |
|
1133 |
* Use esc_sql() or wpdb::prepare() instead. |
|
1134 |
* |
|
1135 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
* @deprecated 3.6.0 Use wpdb::prepare() |
16 | 1137 |
* @see wpdb::prepare() |
0 | 1138 |
* @see esc_sql() |
1139 |
* |
|
1140 |
* @param string $string |
|
1141 |
* @return string |
|
1142 |
*/ |
|
1143 |
function _weak_escape( $string ) { |
|
9 | 1144 |
if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' ); |
9 | 1146 |
} |
0 | 1147 |
return addslashes( $string ); |
1148 |
} |
|
1149 |
||
1150 |
/** |
|
16 | 1151 |
* Real escape, using mysqli_real_escape_string() or mysql_real_escape_string(). |
1152 |
* |
|
1153 |
* @since 2.8.0 |
|
0 | 1154 |
* |
5 | 1155 |
* @see mysqli_real_escape_string() |
0 | 1156 |
* @see mysql_real_escape_string() |
1157 |
* |
|
16 | 1158 |
* @param string $string String to escape. |
1159 |
* @return string Escaped string. |
|
0 | 1160 |
*/ |
1161 |
function _real_escape( $string ) { |
|
5 | 1162 |
if ( $this->dbh ) { |
1163 |
if ( $this->use_mysqli ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
$escaped = mysqli_real_escape_string( $this->dbh, $string ); |
5 | 1165 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
$escaped = mysql_real_escape_string( $string, $this->dbh ); |
5 | 1167 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
$class = get_class( $this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
if ( function_exists( '__' ) ) { |
16 | 1171 |
/* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
_doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
_doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
$escaped = addslashes( $string ); |
5 | 1177 |
} |
0 | 1178 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
return $this->add_placeholder_escape( $escaped ); |
0 | 1180 |
} |
1181 |
||
1182 |
/** |
|
16 | 1183 |
* Escapes data. Works on arrays. |
1184 |
* |
|
1185 |
* @since 2.8.0 |
|
0 | 1186 |
* |
1187 |
* @uses wpdb::_real_escape() |
|
1188 |
* |
|
16 | 1189 |
* @param string|array $data Data to escape. |
1190 |
* @return string|array Escaped data, in the same type as supplied. |
|
0 | 1191 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
public function _escape( $data ) { |
0 | 1193 |
if ( is_array( $data ) ) { |
1194 |
foreach ( $data as $k => $v ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
if ( is_array( $v ) ) { |
9 | 1196 |
$data[ $k ] = $this->_escape( $v ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
} else { |
9 | 1198 |
$data[ $k ] = $this->_real_escape( $v ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
} |
0 | 1200 |
} |
1201 |
} else { |
|
1202 |
$data = $this->_real_escape( $data ); |
|
1203 |
} |
|
1204 |
||
1205 |
return $data; |
|
1206 |
} |
|
1207 |
||
1208 |
/** |
|
1209 |
* Do not use, deprecated. |
|
1210 |
* |
|
1211 |
* Use esc_sql() or wpdb::prepare() instead. |
|
1212 |
* |
|
1213 |
* @since 0.71 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
* @deprecated 3.6.0 Use wpdb::prepare() |
0 | 1215 |
* @see wpdb::prepare() |
1216 |
* @see esc_sql() |
|
1217 |
* |
|
16 | 1218 |
* @param string|array $data Data to escape. |
1219 |
* @return string|array Escaped data, in the same type as supplied. |
|
0 | 1220 |
*/ |
5 | 1221 |
public function escape( $data ) { |
9 | 1222 |
if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' ); |
9 | 1224 |
} |
0 | 1225 |
if ( is_array( $data ) ) { |
1226 |
foreach ( $data as $k => $v ) { |
|
9 | 1227 |
if ( is_array( $v ) ) { |
1228 |
$data[ $k ] = $this->escape( $v, 'recursive' ); |
|
1229 |
} else { |
|
1230 |
$data[ $k ] = $this->_weak_escape( $v, 'internal' ); |
|
1231 |
} |
|
0 | 1232 |
} |
1233 |
} else { |
|
1234 |
$data = $this->_weak_escape( $data, 'internal' ); |
|
1235 |
} |
|
1236 |
||
1237 |
return $data; |
|
1238 |
} |
|
1239 |
||
1240 |
/** |
|
16 | 1241 |
* Escapes content by reference for insertion into the database, for security. |
0 | 1242 |
* |
1243 |
* @uses wpdb::_real_escape() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
* |
0 | 1245 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* |
16 | 1247 |
* @param string $string String to escape. |
0 | 1248 |
*/ |
5 | 1249 |
public function escape_by_ref( &$string ) { |
9 | 1250 |
if ( ! is_float( $string ) ) { |
0 | 1251 |
$string = $this->_real_escape( $string ); |
9 | 1252 |
} |
0 | 1253 |
} |
1254 |
||
1255 |
/** |
|
16 | 1256 |
* Prepares a SQL query for safe execution. |
0 | 1257 |
* |
16 | 1258 |
* Uses sprintf()-like syntax. The following placeholders can be used in the query string: |
0 | 1259 |
* %d (integer) |
1260 |
* %f (float) |
|
1261 |
* %s (string) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
* |
16 | 1263 |
* All placeholders MUST be left unquoted in the query string. A corresponding argument |
1264 |
* MUST be passed for each placeholder. |
|
0 | 1265 |
* |
16 | 1266 |
* Note: There is one exception to the above: for compatibility with old behavior, |
1267 |
* numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes |
|
1268 |
* added by this function, so should be passed with appropriate quotes around them. |
|
0 | 1269 |
* |
16 | 1270 |
* Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards |
1271 |
* (for example, to use in LIKE syntax) must be passed via a substitution argument containing |
|
1272 |
* the complete LIKE string, these cannot be inserted directly in the query string. |
|
1273 |
* Also see wpdb::esc_like(). |
|
0 | 1274 |
* |
16 | 1275 |
* Arguments may be passed as individual arguments to the method, or as a single array |
1276 |
* containing all arguments. A combination of the two is not supported. |
|
0 | 1277 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
* Examples: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
* $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
* |
0 | 1282 |
* @since 2.3.0 |
16 | 1283 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
1284 |
* by updating the function signature. The second parameter was changed |
|
1285 |
* from `$args` to `...$args`. |
|
1286 |
* |
|
1287 |
* @link https://www.php.net/sprintf Description of syntax. |
|
0 | 1288 |
* |
16 | 1289 |
* @param string $query Query statement with sprintf()-like placeholders. |
1290 |
* @param array|mixed $args The array of variables to substitute into the query's placeholders |
|
1291 |
* if being called with an array of arguments, or the first variable |
|
1292 |
* to substitute into the query's placeholders if being called with |
|
1293 |
* individual arguments. |
|
1294 |
* @param mixed ...$args Further variables to substitute into the query's placeholders |
|
1295 |
* if being called with individual arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
* @return string|void Sanitized query string, if there is a query to prepare. |
0 | 1297 |
*/ |
16 | 1298 |
public function prepare( $query, ...$args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
if ( is_null( $query ) ) { |
0 | 1300 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
} |
0 | 1302 |
|
5 | 1303 |
// This is not meant to be foolproof -- but it will catch obviously incorrect usage. |
1304 |
if ( strpos( $query, '%' ) === false ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
wp_load_translations_early(); |
16 | 1306 |
_doing_it_wrong( |
1307 |
'wpdb::prepare', |
|
1308 |
sprintf( |
|
1309 |
/* translators: %s: wpdb::prepare() */ |
|
1310 |
__( 'The query argument of %s must have a placeholder.' ), |
|
1311 |
'wpdb::prepare()' |
|
1312 |
), |
|
1313 |
'3.9.0' |
|
1314 |
); |
|
5 | 1315 |
} |
1316 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
// If args were passed as an array (as in vsprintf), move them up. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
$passed_as_array = false; |
16 | 1319 |
if ( is_array( $args[0] ) && count( $args ) === 1 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
$passed_as_array = true; |
9 | 1321 |
$args = $args[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
foreach ( $args as $arg ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
if ( ! is_scalar( $arg ) && ! is_null( $arg ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
wp_load_translations_early(); |
16 | 1327 |
_doing_it_wrong( |
1328 |
'wpdb::prepare', |
|
1329 |
sprintf( |
|
1330 |
/* translators: %s: Value type. */ |
|
1331 |
__( 'Unsupported value type (%s).' ), |
|
1332 |
gettype( $arg ) |
|
1333 |
), |
|
1334 |
'4.8.2' |
|
1335 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
* Specify the formatting allowed in a placeholder. The following are allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
* - Sign specifier. eg, $+d |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1343 |
* - Numbered placeholders. eg, %1$s |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
* - Padding specifier, including custom padding characters. eg, %05s, %'#5s |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
* - Alignment specifier. eg, %05-s |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
* - Precision specifier. eg, %.2f |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
$allowed_format = '(?:[1-9][0-9]*[$])?[-+0-9]*(?: |0|\'.)?[-+0-9]*(?:\.[0-9]+)?'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
* If a %s placeholder already has quotes around it, removing the existing quotes and re-inserting them |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
* ensures the quotes are consistent. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
* |
9 | 1354 |
* For backward compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
* used in the middle of longer strings, or as table name placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
$query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
$query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
$query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
|
16 | 1361 |
$query = preg_replace( "/(?<!%)(%($allowed_format)?f)/", '%\\2F', $query ); // Force floats to be locale-unaware. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
// Count the number of valid placeholders in the query. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
$placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
if ( count( $args ) !== $placeholders ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
if ( 1 === $placeholders && $passed_as_array ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
wp_load_translations_early(); |
16 | 1372 |
_doing_it_wrong( |
1373 |
'wpdb::prepare', |
|
1374 |
__( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ), |
|
1375 |
'4.9.0' |
|
1376 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
* If we don't have the right number of placeholders, but they were passed as individual arguments, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
* or we were expecting multiple arguments in an array, throw a warning. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
wp_load_translations_early(); |
9 | 1385 |
_doing_it_wrong( |
1386 |
'wpdb::prepare', |
|
1387 |
sprintf( |
|
16 | 1388 |
/* translators: 1: Number of placeholders, 2: Number of arguments passed. */ |
9 | 1389 |
__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
$placeholders, |
9 | 1391 |
count( $args ) |
1392 |
), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
'4.8.3' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
|
0 | 1398 |
array_walk( $args, array( $this, 'escape_by_ref' ) ); |
16 | 1399 |
$query = vsprintf( $query, $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
return $this->add_placeholder_escape( $query ); |
0 | 1402 |
} |
1403 |
||
1404 |
/** |
|
5 | 1405 |
* First half of escaping for LIKE special characters % and _ before preparing for MySQL. |
1406 |
* |
|
16 | 1407 |
* Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security. |
5 | 1408 |
* |
1409 |
* Example Prepared Statement: |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* $wild = '%'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* $find = 'only 43% of planets'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* $like = $wild . $wpdb->esc_like( $find ) . $wild; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
* $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like ); |
5 | 1415 |
* |
1416 |
* Example Escape Chain: |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
* $sql = esc_sql( $wpdb->esc_like( $input ) ); |
5 | 1419 |
* |
1420 |
* @since 4.0.0 |
|
1421 |
* |
|
16 | 1422 |
* @param string $text The raw text to be escaped. The input typed by the user |
1423 |
* should have no extra or deleted slashes. |
|
1424 |
* @return string Text in the form of a LIKE phrase. The output is not SQL safe. |
|
1425 |
* Call wpdb::prepare() or wpdb::_real_escape() next. |
|
5 | 1426 |
*/ |
1427 |
public function esc_like( $text ) { |
|
1428 |
return addcslashes( $text, '_%\\' ); |
|
1429 |
} |
|
1430 |
||
1431 |
/** |
|
16 | 1432 |
* Prints SQL/DB error. |
0 | 1433 |
* |
1434 |
* @since 0.71 |
|
16 | 1435 |
* |
1436 |
* @global array $EZSQL_ERROR Stores error information of query and error string. |
|
0 | 1437 |
* |
16 | 1438 |
* @param string $str The error to display. |
1439 |
* @return void|false Void if the showing of errors is enabled, false if disabled. |
|
0 | 1440 |
*/ |
5 | 1441 |
public function print_error( $str = '' ) { |
0 | 1442 |
global $EZSQL_ERROR; |
1443 |
||
9 | 1444 |
if ( ! $str ) { |
5 | 1445 |
if ( $this->use_mysqli ) { |
1446 |
$str = mysqli_error( $this->dbh ); |
|
1447 |
} else { |
|
1448 |
$str = mysql_error( $this->dbh ); |
|
1449 |
} |
|
1450 |
} |
|
9 | 1451 |
$EZSQL_ERROR[] = array( |
1452 |
'query' => $this->last_query, |
|
1453 |
'error_str' => $str, |
|
1454 |
); |
|
1455 |
||
1456 |
if ( $this->suppress_errors ) { |
|
0 | 1457 |
return false; |
9 | 1458 |
} |
0 | 1459 |
|
1460 |
wp_load_translations_early(); |
|
1461 |
||
16 | 1462 |
$caller = $this->get_caller(); |
1463 |
if ( $caller ) { |
|
1464 |
/* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function. */ |
|
0 | 1465 |
$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1466 |
} else { |
16 | 1467 |
/* translators: 1: Database error message, 2: SQL query. */ |
0 | 1468 |
$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
} |
0 | 1470 |
|
1471 |
error_log( $error_str ); |
|
1472 |
||
1473 |
// Are we showing errors? |
|
9 | 1474 |
if ( ! $this->show_errors ) { |
0 | 1475 |
return false; |
9 | 1476 |
} |
0 | 1477 |
|
16 | 1478 |
// If there is an error then take note of it. |
0 | 1479 |
if ( is_multisite() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
$msg = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1481 |
"%s [%s]\n%s\n", |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
__( 'WordPress database error:' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
$str, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1484 |
$this->last_query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1485 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1486 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
if ( defined( 'ERRORLOGFILE' ) ) { |
0 | 1488 |
error_log( $msg, 3, ERRORLOGFILE ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
if ( defined( 'DIEONDBERROR' ) ) { |
0 | 1491 |
wp_die( $msg ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
} |
0 | 1493 |
} else { |
1494 |
$str = htmlspecialchars( $str, ENT_QUOTES ); |
|
1495 |
$query = htmlspecialchars( $this->last_query, ENT_QUOTES ); |
|
1496 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
printf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1498 |
'<div id="error"><p class="wpdberror"><strong>%s</strong> [%s]<br /><code>%s</code></p></div>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
__( 'WordPress database error:' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
$str, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
$query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1502 |
); |
0 | 1503 |
} |
1504 |
} |
|
1505 |
||
1506 |
/** |
|
1507 |
* Enables showing of database errors. |
|
1508 |
* |
|
1509 |
* This function should be used only to enable showing of errors. |
|
16 | 1510 |
* wpdb::hide_errors() should be used instead for hiding errors. |
0 | 1511 |
* |
1512 |
* @since 0.71 |
|
16 | 1513 |
* |
0 | 1514 |
* @see wpdb::hide_errors() |
1515 |
* |
|
16 | 1516 |
* @param bool $show Optional. Whether to show errors. Default true. |
1517 |
* @return bool Whether showing of errors was previously active. |
|
0 | 1518 |
*/ |
5 | 1519 |
public function show_errors( $show = true ) { |
9 | 1520 |
$errors = $this->show_errors; |
0 | 1521 |
$this->show_errors = $show; |
1522 |
return $errors; |
|
1523 |
} |
|
1524 |
||
1525 |
/** |
|
1526 |
* Disables showing of database errors. |
|
1527 |
* |
|
1528 |
* By default database errors are not shown. |
|
1529 |
* |
|
1530 |
* @since 0.71 |
|
16 | 1531 |
* |
0 | 1532 |
* @see wpdb::show_errors() |
1533 |
* |
|
16 | 1534 |
* @return bool Whether showing of errors was previously active. |
0 | 1535 |
*/ |
5 | 1536 |
public function hide_errors() { |
9 | 1537 |
$show = $this->show_errors; |
0 | 1538 |
$this->show_errors = false; |
1539 |
return $show; |
|
1540 |
} |
|
1541 |
||
1542 |
/** |
|
16 | 1543 |
* Enables or disables suppressing of database errors. |
0 | 1544 |
* |
16 | 1545 |
* By default database errors are suppressed. |
0 | 1546 |
* |
1547 |
* @since 2.5.0 |
|
16 | 1548 |
* |
0 | 1549 |
* @see wpdb::hide_errors() |
16 | 1550 |
* |
1551 |
* @param bool $suppress Optional. Whether to suppress errors. Default true. |
|
1552 |
* @return bool Whether suppressing of errors was previously active. |
|
0 | 1553 |
*/ |
5 | 1554 |
public function suppress_errors( $suppress = true ) { |
9 | 1555 |
$errors = $this->suppress_errors; |
0 | 1556 |
$this->suppress_errors = (bool) $suppress; |
1557 |
return $errors; |
|
1558 |
} |
|
1559 |
||
1560 |
/** |
|
16 | 1561 |
* Kills cached query results. |
0 | 1562 |
* |
1563 |
* @since 0.71 |
|
1564 |
*/ |
|
5 | 1565 |
public function flush() { |
9 | 1566 |
$this->last_result = array(); |
1567 |
$this->col_info = null; |
|
1568 |
$this->last_query = null; |
|
16 | 1569 |
$this->rows_affected = 0; |
1570 |
$this->num_rows = 0; |
|
9 | 1571 |
$this->last_error = ''; |
0 | 1572 |
|
5 | 1573 |
if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { |
1574 |
mysqli_free_result( $this->result ); |
|
1575 |
$this->result = null; |
|
1576 |
||
16 | 1577 |
// Sanity check before using the handle. |
9 | 1578 |
if ( empty( $this->dbh ) || ! ( $this->dbh instanceof mysqli ) ) { |
5 | 1579 |
return; |
1580 |
} |
|
1581 |
||
16 | 1582 |
// Clear out any results from a multi-query. |
5 | 1583 |
while ( mysqli_more_results( $this->dbh ) ) { |
1584 |
mysqli_next_result( $this->dbh ); |
|
1585 |
} |
|
1586 |
} elseif ( is_resource( $this->result ) ) { |
|
0 | 1587 |
mysql_free_result( $this->result ); |
5 | 1588 |
} |
0 | 1589 |
} |
1590 |
||
1591 |
/** |
|
16 | 1592 |
* Connects to and selects database. |
5 | 1593 |
* |
16 | 1594 |
* If $allow_bail is false, the lack of database connection will need to be handled manually. |
0 | 1595 |
* |
1596 |
* @since 3.0.0 |
|
5 | 1597 |
* @since 3.9.0 $allow_bail parameter added. |
1598 |
* |
|
1599 |
* @param bool $allow_bail Optional. Allows the function to bail. Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1600 |
* @return bool True with a successful connection, false on failure. |
0 | 1601 |
*/ |
5 | 1602 |
public function db_connect( $allow_bail = true ) { |
0 | 1603 |
$this->is_mysql = true; |
1604 |
||
5 | 1605 |
/* |
1606 |
* Deprecated in 3.9+ when using MySQLi. No equivalent |
|
1607 |
* $new_link parameter exists for mysqli_* functions. |
|
1608 |
*/ |
|
9 | 1609 |
$new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
0 | 1610 |
$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
1611 |
||
5 | 1612 |
if ( $this->use_mysqli ) { |
1613 |
$this->dbh = mysqli_init(); |
|
1614 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1615 |
$host = $this->dbhost; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
$port = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1617 |
$socket = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1618 |
$is_ipv6 = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1619 |
|
16 | 1620 |
$host_data = $this->parse_db_host( $this->dbhost ); |
1621 |
if ( $host_data ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1622 |
list( $host, $port, $socket, $is_ipv6 ) = $host_data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1623 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1625 |
/* |
16 | 1626 |
* If using the `mysqlnd` library, the IPv6 address needs to be enclosed |
1627 |
* in square brackets, whereas it doesn't while using the `libmysqlclient` library. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1628 |
* @see https://bugs.php.net/bug.php?id=67563 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1629 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1630 |
if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1631 |
$host = "[$host]"; |
5 | 1632 |
} |
1633 |
||
1634 |
if ( WP_DEBUG ) { |
|
1635 |
mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
|
1636 |
} else { |
|
16 | 1637 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
5 | 1638 |
@mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
1639 |
} |
|
1640 |
||
1641 |
if ( $this->dbh->connect_errno ) { |
|
1642 |
$this->dbh = null; |
|
1643 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1644 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1645 |
* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: |
16 | 1646 |
* - We haven't previously connected, and |
1647 |
* - WP_USE_EXT_MYSQL isn't set to false, and |
|
1648 |
* - ext/mysql is loaded. |
|
1649 |
*/ |
|
5 | 1650 |
$attempt_fallback = true; |
1651 |
||
1652 |
if ( $this->has_connected ) { |
|
1653 |
$attempt_fallback = false; |
|
1654 |
} elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) { |
|
1655 |
$attempt_fallback = false; |
|
1656 |
} elseif ( ! function_exists( 'mysql_connect' ) ) { |
|
1657 |
$attempt_fallback = false; |
|
1658 |
} |
|
1659 |
||
1660 |
if ( $attempt_fallback ) { |
|
1661 |
$this->use_mysqli = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1662 |
return $this->db_connect( $allow_bail ); |
5 | 1663 |
} |
1664 |
} |
|
0 | 1665 |
} else { |
5 | 1666 |
if ( WP_DEBUG ) { |
1667 |
$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
|
1668 |
} else { |
|
16 | 1669 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
5 | 1670 |
$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
1671 |
} |
|
0 | 1672 |
} |
1673 |
||
5 | 1674 |
if ( ! $this->dbh && $allow_bail ) { |
0 | 1675 |
wp_load_translations_early(); |
5 | 1676 |
|
1677 |
// Load custom DB error template, if present. |
|
1678 |
if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
|
16 | 1679 |
require_once WP_CONTENT_DIR . '/db-error.php'; |
5 | 1680 |
die(); |
1681 |
} |
|
1682 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1683 |
$message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1684 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
$message .= '<p>' . sprintf( |
16 | 1686 |
/* translators: 1: wp-config.php, 2: Database host. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
__( 'This either means that the username and password information in your %1$s file is incorrect or we can’t contact the database server at %2$s. This could mean your host’s database server is down.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
'<code>wp-config.php</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1690 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
$message .= "<ul>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
$message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n"; |
16 | 1694 |
$message .= '<li>' . __( 'Are you sure you have typed the correct hostname?' ) . "</li>\n"; |
1695 |
$message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
$message .= "</ul>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
$message .= '<p>' . sprintf( |
16 | 1699 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
__( 'If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ), |
9 | 1701 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1702 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1703 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1704 |
$this->bail( $message, 'db_connect_fail' ); |
0 | 1705 |
|
5 | 1706 |
return false; |
1707 |
} elseif ( $this->dbh ) { |
|
1708 |
if ( ! $this->has_connected ) { |
|
1709 |
$this->init_charset(); |
|
1710 |
} |
|
1711 |
||
1712 |
$this->has_connected = true; |
|
1713 |
||
1714 |
$this->set_charset( $this->dbh ); |
|
1715 |
||
1716 |
$this->ready = true; |
|
1717 |
$this->set_sql_mode(); |
|
1718 |
$this->select( $this->dbname, $this->dbh ); |
|
1719 |
||
1720 |
return true; |
|
1721 |
} |
|
1722 |
||
1723 |
return false; |
|
1724 |
} |
|
1725 |
||
1726 |
/** |
|
16 | 1727 |
* Parses the DB_HOST setting to interpret it for mysqli_real_connect(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1728 |
* |
16 | 1729 |
* mysqli_real_connect() doesn't support the host param including a port or socket |
1730 |
* like mysql_connect() does. This duplicates how mysql_connect() detects a port |
|
1731 |
* and/or socket file. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1732 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1734 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1735 |
* @param string $host The DB_HOST setting to parse. |
16 | 1736 |
* @return array|false Array containing the host, the port, the socket and |
1737 |
* whether it is an IPv6 address, in that order. |
|
1738 |
* False if $host couldn't be parsed. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1739 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1740 |
public function parse_db_host( $host ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1741 |
$port = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1742 |
$socket = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1743 |
$is_ipv6 = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1744 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1745 |
// First peel off the socket parameter from the right, if it exists. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
$socket_pos = strpos( $host, ':/' ); |
16 | 1747 |
if ( false !== $socket_pos ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
$socket = substr( $host, $socket_pos + 1 ); |
9 | 1749 |
$host = substr( $host, 0, $socket_pos ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1750 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
// We need to check for an IPv6 address first. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1753 |
// An IPv6 address will always contain at least two colons. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1754 |
if ( substr_count( $host, ':' ) > 1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1755 |
$pattern = '#^(?:\[)?(?P<host>[0-9a-fA-F:]+)(?:\]:(?P<port>[\d]+))?#'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1756 |
$is_ipv6 = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1758 |
// We seem to be dealing with an IPv4 address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
$pattern = '#^(?P<host>[^:/]*)(?::(?P<port>[\d]+))?#'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1760 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1761 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1762 |
$matches = array(); |
9 | 1763 |
$result = preg_match( $pattern, $host, $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1764 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
if ( 1 !== $result ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1766 |
// Couldn't parse the address, bail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1767 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1768 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1770 |
$host = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1771 |
foreach ( array( 'host', 'port' ) as $component ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1772 |
if ( ! empty( $matches[ $component ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
$$component = $matches[ $component ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1774 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1777 |
return array( $host, $port, $socket, $is_ipv6 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1778 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1779 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1780 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1781 |
* Checks that the connection to the database is still up. If not, try to reconnect. |
5 | 1782 |
* |
16 | 1783 |
* If this function is unable to reconnect, it will forcibly die, or if called |
1784 |
* after the {@see 'template_redirect'} hook has been fired, return false instead. |
|
5 | 1785 |
* |
16 | 1786 |
* If $allow_bail is false, the lack of database connection will need to be handled manually. |
5 | 1787 |
* |
1788 |
* @since 3.9.0 |
|
1789 |
* |
|
1790 |
* @param bool $allow_bail Optional. Allows the function to bail. Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1791 |
* @return bool|void True if the connection is up. |
5 | 1792 |
*/ |
1793 |
public function check_connection( $allow_bail = true ) { |
|
1794 |
if ( $this->use_mysqli ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1795 |
if ( ! empty( $this->dbh ) && mysqli_ping( $this->dbh ) ) { |
5 | 1796 |
return true; |
1797 |
} |
|
1798 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1799 |
if ( ! empty( $this->dbh ) && mysql_ping( $this->dbh ) ) { |
5 | 1800 |
return true; |
1801 |
} |
|
0 | 1802 |
} |
1803 |
||
5 | 1804 |
$error_reporting = false; |
1805 |
||
16 | 1806 |
// Disable warnings, as we don't want to see a multitude of "unable to connect" messages. |
5 | 1807 |
if ( WP_DEBUG ) { |
1808 |
$error_reporting = error_reporting(); |
|
1809 |
error_reporting( $error_reporting & ~E_WARNING ); |
|
1810 |
} |
|
1811 |
||
1812 |
for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { |
|
16 | 1813 |
// On the last try, re-enable warnings. We want to see a single instance |
1814 |
// of the "unable to connect" message on the bail() screen, if it appears. |
|
5 | 1815 |
if ( $this->reconnect_retries === $tries && WP_DEBUG ) { |
1816 |
error_reporting( $error_reporting ); |
|
1817 |
} |
|
1818 |
||
1819 |
if ( $this->db_connect( false ) ) { |
|
1820 |
if ( $error_reporting ) { |
|
1821 |
error_reporting( $error_reporting ); |
|
1822 |
} |
|
1823 |
||
1824 |
return true; |
|
1825 |
} |
|
1826 |
||
1827 |
sleep( 1 ); |
|
1828 |
} |
|
1829 |
||
1830 |
// If template_redirect has already happened, it's too late for wp_die()/dead_db(). |
|
1831 |
// Let's just return and hope for the best. |
|
1832 |
if ( did_action( 'template_redirect' ) ) { |
|
1833 |
return false; |
|
1834 |
} |
|
1835 |
||
1836 |
if ( ! $allow_bail ) { |
|
1837 |
return false; |
|
1838 |
} |
|
1839 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1840 |
wp_load_translations_early(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1841 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1842 |
$message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1843 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
$message .= '<p>' . sprintf( |
16 | 1845 |
/* translators: %s: Database host. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
__( 'This means that we lost contact with the database server at %s. This could mean your host’s database server is down.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1847 |
'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1848 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
$message .= "<ul>\n"; |
16 | 1851 |
$message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
1852 |
$message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</li>\n"; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1853 |
$message .= "</ul>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1854 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1855 |
$message .= '<p>' . sprintf( |
16 | 1856 |
/* translators: %s: Support forums URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1857 |
__( 'If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ), |
9 | 1858 |
__( 'https://wordpress.org/support/forums/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1859 |
) . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1860 |
|
5 | 1861 |
// We weren't able to reconnect, so we better bail. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1862 |
$this->bail( $message, 'db_connect_fail' ); |
5 | 1863 |
|
16 | 1864 |
// Call dead_db() if bail didn't die, because this database is no more. |
1865 |
// It has ceased to be (at least temporarily). |
|
5 | 1866 |
dead_db(); |
0 | 1867 |
} |
1868 |
||
1869 |
/** |
|
16 | 1870 |
* Performs a MySQL database query, using current database connection. |
0 | 1871 |
* |
16 | 1872 |
* More information can be found on the Codex page. |
0 | 1873 |
* |
1874 |
* @since 0.71 |
|
1875 |
* |
|
16 | 1876 |
* @link https://codex.wordpress.org/Function_Reference/wpdb_Class |
1877 |
* |
|
1878 |
* @param string $query Database query. |
|
9 | 1879 |
* @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows |
1880 |
* affected/selected for all other queries. Boolean false on error. |
|
0 | 1881 |
*/ |
5 | 1882 |
public function query( $query ) { |
1883 |
if ( ! $this->ready ) { |
|
1884 |
$this->check_current_query = true; |
|
0 | 1885 |
return false; |
5 | 1886 |
} |
1887 |
||
0 | 1888 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1889 |
* Filters the database query. |
0 | 1890 |
* |
5 | 1891 |
* Some queries are made before the plugins have been loaded, |
1892 |
* and thus cannot be filtered with this method. |
|
0 | 1893 |
* |
1894 |
* @since 2.1.0 |
|
5 | 1895 |
* |
0 | 1896 |
* @param string $query Database query. |
1897 |
*/ |
|
1898 |
$query = apply_filters( 'query', $query ); |
|
1899 |
||
1900 |
$this->flush(); |
|
1901 |
||
16 | 1902 |
// Log how the function was called. |
0 | 1903 |
$this->func_call = "\$db->query(\"$query\")"; |
1904 |
||
5 | 1905 |
// If we're writing to the database, make sure the query will write safely. |
1906 |
if ( $this->check_current_query && ! $this->check_ascii( $query ) ) { |
|
1907 |
$stripped_query = $this->strip_invalid_text_from_query( $query ); |
|
1908 |
// strip_invalid_text_from_query() can perform queries, so we need |
|
1909 |
// to flush again, just to make sure everything is clear. |
|
1910 |
$this->flush(); |
|
1911 |
if ( $stripped_query !== $query ) { |
|
1912 |
$this->insert_id = 0; |
|
1913 |
return false; |
|
1914 |
} |
|
1915 |
} |
|
1916 |
||
1917 |
$this->check_current_query = true; |
|
1918 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1919 |
// Keep track of the last query for debug. |
0 | 1920 |
$this->last_query = $query; |
1921 |
||
5 | 1922 |
$this->_do_query( $query ); |
1923 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1924 |
// MySQL server has gone away, try to reconnect. |
5 | 1925 |
$mysql_errno = 0; |
1926 |
if ( ! empty( $this->dbh ) ) { |
|
1927 |
if ( $this->use_mysqli ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1928 |
if ( $this->dbh instanceof mysqli ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1929 |
$mysql_errno = mysqli_errno( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1930 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1931 |
// $dbh is defined, but isn't a real connection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
// Something has gone horribly wrong, let's try a reconnect. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
$mysql_errno = 2006; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
} |
5 | 1935 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
if ( is_resource( $this->dbh ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
$mysql_errno = mysql_errno( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1938 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
$mysql_errno = 2006; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
} |
5 | 1941 |
} |
1942 |
} |
|
1943 |
||
16 | 1944 |
if ( empty( $this->dbh ) || 2006 === $mysql_errno ) { |
5 | 1945 |
if ( $this->check_connection() ) { |
1946 |
$this->_do_query( $query ); |
|
1947 |
} else { |
|
1948 |
$this->insert_id = 0; |
|
1949 |
return false; |
|
1950 |
} |
|
1951 |
} |
|
0 | 1952 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1953 |
// If there is an error then take note of it. |
5 | 1954 |
if ( $this->use_mysqli ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1955 |
if ( $this->dbh instanceof mysqli ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1956 |
$this->last_error = mysqli_error( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1957 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1958 |
$this->last_error = __( 'Unable to retrieve the error message from MySQL' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1959 |
} |
5 | 1960 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1961 |
if ( is_resource( $this->dbh ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
$this->last_error = mysql_error( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1963 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1964 |
$this->last_error = __( 'Unable to retrieve the error message from MySQL' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1965 |
} |
5 | 1966 |
} |
1967 |
||
1968 |
if ( $this->last_error ) { |
|
0 | 1969 |
// Clear insert_id on a subsequent failed insert. |
9 | 1970 |
if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
0 | 1971 |
$this->insert_id = 0; |
9 | 1972 |
} |
0 | 1973 |
|
1974 |
$this->print_error(); |
|
1975 |
return false; |
|
1976 |
} |
|
1977 |
||
1978 |
if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { |
|
1979 |
$return_val = $this->result; |
|
1980 |
} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { |
|
5 | 1981 |
if ( $this->use_mysqli ) { |
1982 |
$this->rows_affected = mysqli_affected_rows( $this->dbh ); |
|
1983 |
} else { |
|
1984 |
$this->rows_affected = mysql_affected_rows( $this->dbh ); |
|
1985 |
} |
|
16 | 1986 |
// Take note of the insert_id. |
0 | 1987 |
if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
5 | 1988 |
if ( $this->use_mysqli ) { |
1989 |
$this->insert_id = mysqli_insert_id( $this->dbh ); |
|
1990 |
} else { |
|
1991 |
$this->insert_id = mysql_insert_id( $this->dbh ); |
|
1992 |
} |
|
0 | 1993 |
} |
16 | 1994 |
// Return number of rows affected. |
0 | 1995 |
$return_val = $this->rows_affected; |
1996 |
} else { |
|
1997 |
$num_rows = 0; |
|
5 | 1998 |
if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1999 |
while ( $row = mysqli_fetch_object( $this->result ) ) { |
9 | 2000 |
$this->last_result[ $num_rows ] = $row; |
5 | 2001 |
$num_rows++; |
2002 |
} |
|
2003 |
} elseif ( is_resource( $this->result ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2004 |
while ( $row = mysql_fetch_object( $this->result ) ) { |
9 | 2005 |
$this->last_result[ $num_rows ] = $row; |
5 | 2006 |
$num_rows++; |
2007 |
} |
|
0 | 2008 |
} |
2009 |
||
16 | 2010 |
// Log and return the number of rows selected. |
0 | 2011 |
$this->num_rows = $num_rows; |
2012 |
$return_val = $num_rows; |
|
2013 |
} |
|
2014 |
||
2015 |
return $return_val; |
|
2016 |
} |
|
2017 |
||
2018 |
/** |
|
5 | 2019 |
* Internal function to perform the mysql_query() call. |
2020 |
* |
|
2021 |
* @since 3.9.0 |
|
2022 |
* |
|
2023 |
* @see wpdb::query() |
|
2024 |
* |
|
2025 |
* @param string $query The query to run. |
|
2026 |
*/ |
|
2027 |
private function _do_query( $query ) { |
|
2028 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
|
2029 |
$this->timer_start(); |
|
2030 |
} |
|
2031 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
if ( ! empty( $this->dbh ) && $this->use_mysqli ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2033 |
$this->result = mysqli_query( $this->dbh, $query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
} elseif ( ! empty( $this->dbh ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2035 |
$this->result = mysql_query( $query, $this->dbh ); |
5 | 2036 |
} |
2037 |
$this->num_queries++; |
|
2038 |
||
2039 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
|
16 | 2040 |
$this->log_query( |
9 | 2041 |
$query, |
2042 |
$this->timer_stop(), |
|
2043 |
$this->get_caller(), |
|
2044 |
$this->time_start, |
|
16 | 2045 |
array() |
9 | 2046 |
); |
5 | 2047 |
} |
2048 |
} |
|
2049 |
||
2050 |
/** |
|
16 | 2051 |
* Logs query data. |
2052 |
* |
|
2053 |
* @since 5.3.0 |
|
2054 |
* |
|
2055 |
* @param string $query The query's SQL. |
|
2056 |
* @param float $query_time Total time spent on the query, in seconds. |
|
2057 |
* @param string $query_callstack Comma-separated list of the calling functions. |
|
2058 |
* @param float $query_start Unix timestamp of the time at the start of the query. |
|
2059 |
* @param array $query_data Custom query data. |
|
2060 |
*/ |
|
2061 |
public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) { |
|
2062 |
/** |
|
2063 |
* Filters the custom query data being logged. |
|
2064 |
* |
|
2065 |
* Caution should be used when modifying any of this data, it is recommended that any additional |
|
2066 |
* information you need to store about a query be added as a new associative entry to the fourth |
|
2067 |
* element $query_data. |
|
2068 |
* |
|
2069 |
* @since 5.3.0 |
|
2070 |
* |
|
2071 |
* @param array $query_data Custom query data. |
|
2072 |
* @param string $query The query's SQL. |
|
2073 |
* @param float $query_time Total time spent on the query, in seconds. |
|
2074 |
* @param string $query_callstack Comma-separated list of the calling functions. |
|
2075 |
* @param float $query_start Unix timestamp of the time at the start of the query. |
|
2076 |
*/ |
|
2077 |
$query_data = apply_filters( 'log_query_custom_data', $query_data, $query, $query_time, $query_callstack, $query_start ); |
|
2078 |
||
2079 |
$this->queries[] = array( |
|
2080 |
$query, |
|
2081 |
$query_time, |
|
2082 |
$query_callstack, |
|
2083 |
$query_start, |
|
2084 |
$query_data, |
|
2085 |
); |
|
2086 |
} |
|
2087 |
||
2088 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2089 |
* Generates and returns a placeholder escape string for use in queries returned by ::prepare(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2090 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2091 |
* @since 4.8.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2092 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2093 |
* @return string String to escape placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2094 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2095 |
public function placeholder_escape() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2096 |
static $placeholder; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2097 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2098 |
if ( ! $placeholder ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2099 |
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2100 |
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2101 |
// Old WP installs may not have AUTH_SALT defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2102 |
$salt = defined( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT : (string) rand(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2103 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2104 |
$placeholder = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2105 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2106 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2107 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2108 |
* Add the filter to remove the placeholder escaper. Uses priority 0, so that anything |
9 | 2109 |
* else attached to this filter will receive the query with the placeholder string removed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2110 |
*/ |
16 | 2111 |
if ( false === has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2112 |
add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2113 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2114 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2115 |
return $placeholder; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2116 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2117 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2118 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2119 |
* Adds a placeholder escape string, to escape anything that resembles a printf() placeholder. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2120 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2121 |
* @since 4.8.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2122 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2123 |
* @param string $query The query to escape. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2124 |
* @return string The query with the placeholder escape string inserted where necessary. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2125 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2126 |
public function add_placeholder_escape( $query ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2127 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2128 |
* To prevent returning anything that even vaguely resembles a placeholder, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2129 |
* we clobber every % we can find. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2130 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2131 |
return str_replace( '%', $this->placeholder_escape(), $query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2132 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2133 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2134 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2135 |
* Removes the placeholder escape strings from a query. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2137 |
* @since 4.8.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2139 |
* @param string $query The query from which the placeholder will be removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2140 |
* @return string The query with the placeholder removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2141 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2142 |
public function remove_placeholder_escape( $query ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2143 |
return str_replace( $this->placeholder_escape(), '%', $query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2144 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2145 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2146 |
/** |
16 | 2147 |
* Inserts a row into the table. |
0 | 2148 |
* |
16 | 2149 |
* Examples: |
5 | 2150 |
* wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
2151 |
* wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
|
0 | 2152 |
* |
2153 |
* @since 2.5.0 |
|
16 | 2154 |
* |
0 | 2155 |
* @see wpdb::prepare() |
2156 |
* @see wpdb::$field_types |
|
2157 |
* @see wp_set_wpdb_vars() |
|
2158 |
* |
|
16 | 2159 |
* @param string $table Table name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2160 |
* @param array $data Data to insert (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2161 |
* Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
16 | 2162 |
* Sending a null value will cause the column to be set to NULL - the corresponding |
2163 |
* format is ignored in this case. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2164 |
* @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2165 |
* If string, that format will be used for all of the values in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2166 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
16 | 2167 |
* If omitted, all values in $data will be treated as strings unless otherwise |
2168 |
* specified in wpdb::$field_types. |
|
0 | 2169 |
* @return int|false The number of rows inserted, or false on error. |
2170 |
*/ |
|
5 | 2171 |
public function insert( $table, $data, $format = null ) { |
0 | 2172 |
return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); |
2173 |
} |
|
2174 |
||
2175 |
/** |
|
16 | 2176 |
* Replaces a row in the table. |
0 | 2177 |
* |
16 | 2178 |
* Examples: |
5 | 2179 |
* wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
2180 |
* wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
|
0 | 2181 |
* |
2182 |
* @since 3.0.0 |
|
16 | 2183 |
* |
0 | 2184 |
* @see wpdb::prepare() |
2185 |
* @see wpdb::$field_types |
|
2186 |
* @see wp_set_wpdb_vars() |
|
2187 |
* |
|
16 | 2188 |
* @param string $table Table name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2189 |
* @param array $data Data to insert (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2190 |
* Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
16 | 2191 |
* Sending a null value will cause the column to be set to NULL - the corresponding |
2192 |
* format is ignored in this case. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2193 |
* @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
* If string, that format will be used for all of the values in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2195 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
16 | 2196 |
* If omitted, all values in $data will be treated as strings unless otherwise |
2197 |
* specified in wpdb::$field_types. |
|
0 | 2198 |
* @return int|false The number of rows affected, or false on error. |
2199 |
*/ |
|
5 | 2200 |
public function replace( $table, $data, $format = null ) { |
0 | 2201 |
return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); |
2202 |
} |
|
2203 |
||
2204 |
/** |
|
2205 |
* Helper function for insert and replace. |
|
2206 |
* |
|
2207 |
* Runs an insert or replace query based on $type argument. |
|
2208 |
* |
|
2209 |
* @since 3.0.0 |
|
16 | 2210 |
* |
0 | 2211 |
* @see wpdb::prepare() |
2212 |
* @see wpdb::$field_types |
|
2213 |
* @see wp_set_wpdb_vars() |
|
2214 |
* |
|
16 | 2215 |
* @param string $table Table name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2216 |
* @param array $data Data to insert (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2217 |
* Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
16 | 2218 |
* Sending a null value will cause the column to be set to NULL - the corresponding |
2219 |
* format is ignored in this case. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2220 |
* @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2221 |
* If string, that format will be used for all of the values in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2222 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
16 | 2223 |
* If omitted, all values in $data will be treated as strings unless otherwise |
2224 |
* specified in wpdb::$field_types. |
|
2225 |
* @param string $type Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'. |
|
2226 |
* Default 'INSERT'. |
|
0 | 2227 |
* @return int|false The number of rows affected, or false on error. |
2228 |
*/ |
|
2229 |
function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
|
5 | 2230 |
$this->insert_id = 0; |
2231 |
||
16 | 2232 |
if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) { |
5 | 2233 |
return false; |
2234 |
} |
|
2235 |
||
2236 |
$data = $this->process_fields( $table, $data, $format ); |
|
2237 |
if ( false === $data ) { |
|
0 | 2238 |
return false; |
5 | 2239 |
} |
2240 |
||
16 | 2241 |
$formats = array(); |
2242 |
$values = array(); |
|
5 | 2243 |
foreach ( $data as $value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2244 |
if ( is_null( $value['value'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2245 |
$formats[] = 'NULL'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2246 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2247 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2248 |
|
5 | 2249 |
$formats[] = $value['format']; |
2250 |
$values[] = $value['value']; |
|
0 | 2251 |
} |
5 | 2252 |
|
2253 |
$fields = '`' . implode( '`, `', array_keys( $data ) ) . '`'; |
|
2254 |
$formats = implode( ', ', $formats ); |
|
2255 |
||
2256 |
$sql = "$type INTO `$table` ($fields) VALUES ($formats)"; |
|
2257 |
||
2258 |
$this->check_current_query = false; |
|
2259 |
return $this->query( $this->prepare( $sql, $values ) ); |
|
0 | 2260 |
} |
2261 |
||
2262 |
/** |
|
16 | 2263 |
* Updates a row in the table. |
0 | 2264 |
* |
16 | 2265 |
* Examples: |
5 | 2266 |
* wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
2267 |
* wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
|
0 | 2268 |
* |
2269 |
* @since 2.5.0 |
|
16 | 2270 |
* |
0 | 2271 |
* @see wpdb::prepare() |
2272 |
* @see wpdb::$field_types |
|
2273 |
* @see wp_set_wpdb_vars() |
|
2274 |
* |
|
16 | 2275 |
* @param string $table Table name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2276 |
* @param array $data Data to update (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2277 |
* Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2278 |
* Sending a null value will cause the column to be set to NULL - the corresponding |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2279 |
* format is ignored in this case. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2280 |
* @param array $where A named array of WHERE clauses (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2281 |
* Multiple clauses will be joined with ANDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2282 |
* Both $where columns and $where values should be "raw". |
16 | 2283 |
* Sending a null value will create an IS NULL comparison - the corresponding |
2284 |
* format will be ignored in this case. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2285 |
* @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2286 |
* If string, that format will be used for all of the values in $data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2287 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
16 | 2288 |
* If omitted, all values in $data will be treated as strings unless otherwise |
2289 |
* specified in wpdb::$field_types. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
* @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2291 |
* If string, that format will be used for all of the items in $where. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2292 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
* If omitted, all values in $where will be treated as strings. |
0 | 2294 |
* @return int|false The number of rows updated, or false on error. |
2295 |
*/ |
|
5 | 2296 |
public function update( $table, $data, $where, $format = null, $where_format = null ) { |
2297 |
if ( ! is_array( $data ) || ! is_array( $where ) ) { |
|
0 | 2298 |
return false; |
5 | 2299 |
} |
2300 |
||
2301 |
$data = $this->process_fields( $table, $data, $format ); |
|
2302 |
if ( false === $data ) { |
|
2303 |
return false; |
|
2304 |
} |
|
2305 |
$where = $this->process_fields( $table, $where, $where_format ); |
|
2306 |
if ( false === $where ) { |
|
2307 |
return false; |
|
0 | 2308 |
} |
2309 |
||
16 | 2310 |
$fields = array(); |
2311 |
$conditions = array(); |
|
2312 |
$values = array(); |
|
5 | 2313 |
foreach ( $data as $field => $value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2314 |
if ( is_null( $value['value'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2315 |
$fields[] = "`$field` = NULL"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2317 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2318 |
|
5 | 2319 |
$fields[] = "`$field` = " . $value['format']; |
2320 |
$values[] = $value['value']; |
|
2321 |
} |
|
2322 |
foreach ( $where as $field => $value ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2323 |
if ( is_null( $value['value'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
$conditions[] = "`$field` IS NULL"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
|
5 | 2328 |
$conditions[] = "`$field` = " . $value['format']; |
9 | 2329 |
$values[] = $value['value']; |
0 | 2330 |
} |
2331 |
||
9 | 2332 |
$fields = implode( ', ', $fields ); |
5 | 2333 |
$conditions = implode( ' AND ', $conditions ); |
2334 |
||
2335 |
$sql = "UPDATE `$table` SET $fields WHERE $conditions"; |
|
2336 |
||
2337 |
$this->check_current_query = false; |
|
2338 |
return $this->query( $this->prepare( $sql, $values ) ); |
|
0 | 2339 |
} |
2340 |
||
2341 |
/** |
|
16 | 2342 |
* Deletes a row in the table. |
0 | 2343 |
* |
16 | 2344 |
* Examples: |
5 | 2345 |
* wpdb::delete( 'table', array( 'ID' => 1 ) ) |
2346 |
* wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
|
0 | 2347 |
* |
2348 |
* @since 3.4.0 |
|
16 | 2349 |
* |
0 | 2350 |
* @see wpdb::prepare() |
2351 |
* @see wpdb::$field_types |
|
2352 |
* @see wp_set_wpdb_vars() |
|
2353 |
* |
|
16 | 2354 |
* @param string $table Table name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2355 |
* @param array $where A named array of WHERE clauses (in column => value pairs). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2356 |
* Multiple clauses will be joined with ANDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2357 |
* Both $where columns and $where values should be "raw". |
16 | 2358 |
* Sending a null value will create an IS NULL comparison - the corresponding |
2359 |
* format will be ignored in this case. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2360 |
* @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2361 |
* If string, that format will be used for all of the items in $where. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2362 |
* A format is one of '%d', '%f', '%s' (integer, float, string). |
16 | 2363 |
* If omitted, all values in $data will be treated as strings unless otherwise |
2364 |
* specified in wpdb::$field_types. |
|
0 | 2365 |
* @return int|false The number of rows updated, or false on error. |
2366 |
*/ |
|
5 | 2367 |
public function delete( $table, $where, $where_format = null ) { |
2368 |
if ( ! is_array( $where ) ) { |
|
2369 |
return false; |
|
2370 |
} |
|
2371 |
||
2372 |
$where = $this->process_fields( $table, $where, $where_format ); |
|
2373 |
if ( false === $where ) { |
|
2374 |
return false; |
|
2375 |
} |
|
2376 |
||
16 | 2377 |
$conditions = array(); |
2378 |
$values = array(); |
|
5 | 2379 |
foreach ( $where as $field => $value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2380 |
if ( is_null( $value['value'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2381 |
$conditions[] = "`$field` IS NULL"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2382 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2383 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2384 |
|
5 | 2385 |
$conditions[] = "`$field` = " . $value['format']; |
9 | 2386 |
$values[] = $value['value']; |
5 | 2387 |
} |
2388 |
||
2389 |
$conditions = implode( ' AND ', $conditions ); |
|
2390 |
||
2391 |
$sql = "DELETE FROM `$table` WHERE $conditions"; |
|
2392 |
||
2393 |
$this->check_current_query = false; |
|
2394 |
return $this->query( $this->prepare( $sql, $values ) ); |
|
2395 |
} |
|
2396 |
||
2397 |
/** |
|
2398 |
* Processes arrays of field/value pairs and field formats. |
|
2399 |
* |
|
16 | 2400 |
* This is a helper method for wpdb's CRUD methods, which take field/value pairs |
2401 |
* for inserts, updates, and where clauses. This method first pairs each value |
|
2402 |
* with a format. Then it determines the charset of that field, using that |
|
2403 |
* to determine if any invalid text would be stripped. If text is stripped, |
|
2404 |
* then field processing is rejected and the query fails. |
|
5 | 2405 |
* |
2406 |
* @since 4.2.0 |
|
2407 |
* |
|
2408 |
* @param string $table Table name. |
|
2409 |
* @param array $data Field/value pair. |
|
2410 |
* @param mixed $format Format for each field. |
|
16 | 2411 |
* @return array|false An array of fields that contain paired value and formats. |
2412 |
* False for invalid values. |
|
5 | 2413 |
*/ |
2414 |
protected function process_fields( $table, $data, $format ) { |
|
2415 |
$data = $this->process_field_formats( $data, $format ); |
|
2416 |
if ( false === $data ) { |
|
0 | 2417 |
return false; |
5 | 2418 |
} |
2419 |
||
2420 |
$data = $this->process_field_charsets( $data, $table ); |
|
2421 |
if ( false === $data ) { |
|
2422 |
return false; |
|
2423 |
} |
|
2424 |
||
2425 |
$data = $this->process_field_lengths( $data, $table ); |
|
2426 |
if ( false === $data ) { |
|
2427 |
return false; |
|
2428 |
} |
|
2429 |
||
2430 |
$converted_data = $this->strip_invalid_text( $data ); |
|
2431 |
||
2432 |
if ( $data !== $converted_data ) { |
|
2433 |
return false; |
|
2434 |
} |
|
2435 |
||
2436 |
return $data; |
|
2437 |
} |
|
2438 |
||
2439 |
/** |
|
2440 |
* Prepares arrays of value/format pairs as passed to wpdb CRUD methods. |
|
2441 |
* |
|
2442 |
* @since 4.2.0 |
|
2443 |
* |
|
2444 |
* @param array $data Array of fields to values. |
|
2445 |
* @param mixed $format Formats to be mapped to the values in $data. |
|
2446 |
* @return array Array, keyed by field names with values being an array |
|
2447 |
* of 'value' and 'format' keys. |
|
2448 |
*/ |
|
2449 |
protected function process_field_formats( $data, $format ) { |
|
16 | 2450 |
$formats = (array) $format; |
2451 |
$original_formats = $formats; |
|
5 | 2452 |
|
2453 |
foreach ( $data as $field => $value ) { |
|
2454 |
$value = array( |
|
2455 |
'value' => $value, |
|
2456 |
'format' => '%s', |
|
2457 |
); |
|
2458 |
||
2459 |
if ( ! empty( $format ) ) { |
|
2460 |
$value['format'] = array_shift( $formats ); |
|
2461 |
if ( ! $value['format'] ) { |
|
2462 |
$value['format'] = reset( $original_formats ); |
|
2463 |
} |
|
0 | 2464 |
} elseif ( isset( $this->field_types[ $field ] ) ) { |
5 | 2465 |
$value['format'] = $this->field_types[ $field ]; |
2466 |
} |
|
2467 |
||
2468 |
$data[ $field ] = $value; |
|
2469 |
} |
|
2470 |
||
2471 |
return $data; |
|
2472 |
} |
|
2473 |
||
2474 |
/** |
|
16 | 2475 |
* Adds field charsets to field/value/format arrays generated by wpdb::process_field_formats(). |
5 | 2476 |
* |
2477 |
* @since 4.2.0 |
|
2478 |
* |
|
2479 |
* @param array $data As it comes from the wpdb::process_field_formats() method. |
|
2480 |
* @param string $table Table name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2481 |
* @return array|false The same array as $data with additional 'charset' keys. |
16 | 2482 |
* False on failure. |
5 | 2483 |
*/ |
2484 |
protected function process_field_charsets( $data, $table ) { |
|
2485 |
foreach ( $data as $field => $value ) { |
|
2486 |
if ( '%d' === $value['format'] || '%f' === $value['format'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2487 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2488 |
* We can skip this field if we know it isn't a string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2489 |
* This checks %d/%f versus ! %s because its sprintf() could take more. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2490 |
*/ |
5 | 2491 |
$value['charset'] = false; |
0 | 2492 |
} else { |
5 | 2493 |
$value['charset'] = $this->get_col_charset( $table, $field ); |
2494 |
if ( is_wp_error( $value['charset'] ) ) { |
|
2495 |
return false; |
|
2496 |
} |
|
0 | 2497 |
} |
2498 |
||
5 | 2499 |
$data[ $field ] = $value; |
0 | 2500 |
} |
2501 |
||
5 | 2502 |
return $data; |
0 | 2503 |
} |
2504 |
||
5 | 2505 |
/** |
16 | 2506 |
* For string fields, records the maximum string length that field can safely save. |
5 | 2507 |
* |
2508 |
* @since 4.2.1 |
|
2509 |
* |
|
2510 |
* @param array $data As it comes from the wpdb::process_field_charsets() method. |
|
2511 |
* @param string $table Table name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2512 |
* @return array|false The same array as $data with additional 'length' keys, or false if |
5 | 2513 |
* any of the values were too long for their corresponding field. |
2514 |
*/ |
|
2515 |
protected function process_field_lengths( $data, $table ) { |
|
2516 |
foreach ( $data as $field => $value ) { |
|
2517 |
if ( '%d' === $value['format'] || '%f' === $value['format'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2518 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2519 |
* We can skip this field if we know it isn't a string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2520 |
* This checks %d/%f versus ! %s because its sprintf() could take more. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2521 |
*/ |
5 | 2522 |
$value['length'] = false; |
2523 |
} else { |
|
2524 |
$value['length'] = $this->get_col_length( $table, $field ); |
|
2525 |
if ( is_wp_error( $value['length'] ) ) { |
|
2526 |
return false; |
|
2527 |
} |
|
2528 |
} |
|
2529 |
||
2530 |
$data[ $field ] = $value; |
|
2531 |
} |
|
2532 |
||
2533 |
return $data; |
|
2534 |
} |
|
0 | 2535 |
|
2536 |
/** |
|
16 | 2537 |
* Retrieves one variable from the database. |
0 | 2538 |
* |
2539 |
* Executes a SQL query and returns the value from the SQL result. |
|
16 | 2540 |
* If the SQL result contains more than one column and/or more than one row, |
2541 |
* the value in the column and row specified is returned. If $query is null, |
|
2542 |
* the value in the specified column and row from the previous SQL result is returned. |
|
0 | 2543 |
* |
2544 |
* @since 0.71 |
|
2545 |
* |
|
2546 |
* @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2547 |
* @param int $x Optional. Column of value to return. Indexed from 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2548 |
* @param int $y Optional. Row of value to return. Indexed from 0. |
16 | 2549 |
* @return string|null Database query result (as string), or null on failure. |
0 | 2550 |
*/ |
5 | 2551 |
public function get_var( $query = null, $x = 0, $y = 0 ) { |
0 | 2552 |
$this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
5 | 2553 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2554 |
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
5 | 2555 |
$this->check_current_query = false; |
2556 |
} |
|
2557 |
||
2558 |
if ( $query ) { |
|
0 | 2559 |
$this->query( $query ); |
5 | 2560 |
} |
0 | 2561 |
|
16 | 2562 |
// Extract var out of cached results based on x,y vals. |
9 | 2563 |
if ( ! empty( $this->last_result[ $y ] ) ) { |
2564 |
$values = array_values( get_object_vars( $this->last_result[ $y ] ) ); |
|
0 | 2565 |
} |
2566 |
||
16 | 2567 |
// If there is a value return it, else return null. |
2568 |
return ( isset( $values[ $x ] ) && '' !== $values[ $x ] ) ? $values[ $x ] : null; |
|
0 | 2569 |
} |
2570 |
||
2571 |
/** |
|
16 | 2572 |
* Retrieves one row from the database. |
0 | 2573 |
* |
2574 |
* Executes a SQL query and returns the row from the SQL result. |
|
2575 |
* |
|
2576 |
* @since 0.71 |
|
2577 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2578 |
* @param string|null $query SQL query. |
16 | 2579 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
2580 |
* correspond to an stdClass object, an associative array, or a numeric array, |
|
2581 |
* respectively. Default OBJECT. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2582 |
* @param int $y Optional. Row to return. Indexed from 0. |
16 | 2583 |
* @return array|object|null|void Database query result in format specified by $output or null on failure. |
0 | 2584 |
*/ |
5 | 2585 |
public function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
0 | 2586 |
$this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
5 | 2587 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2588 |
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
5 | 2589 |
$this->check_current_query = false; |
2590 |
} |
|
2591 |
||
2592 |
if ( $query ) { |
|
0 | 2593 |
$this->query( $query ); |
5 | 2594 |
} else { |
0 | 2595 |
return null; |
5 | 2596 |
} |
0 | 2597 |
|
9 | 2598 |
if ( ! isset( $this->last_result[ $y ] ) ) { |
0 | 2599 |
return null; |
9 | 2600 |
} |
0 | 2601 |
|
16 | 2602 |
if ( OBJECT === $output ) { |
9 | 2603 |
return $this->last_result[ $y ] ? $this->last_result[ $y ] : null; |
16 | 2604 |
} elseif ( ARRAY_A === $output ) { |
9 | 2605 |
return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null; |
16 | 2606 |
} elseif ( ARRAY_N === $output ) { |
9 | 2607 |
return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null; |
16 | 2608 |
} elseif ( OBJECT === strtoupper( $output ) ) { |
2609 |
// Back compat for OBJECT being previously case-insensitive. |
|
9 | 2610 |
return $this->last_result[ $y ] ? $this->last_result[ $y ] : null; |
0 | 2611 |
} else { |
9 | 2612 |
$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' ); |
0 | 2613 |
} |
2614 |
} |
|
2615 |
||
2616 |
/** |
|
16 | 2617 |
* Retrieves one column from the database. |
0 | 2618 |
* |
2619 |
* Executes a SQL query and returns the column from the SQL result. |
|
16 | 2620 |
* If the SQL result contains more than one column, the column specified is returned. |
2621 |
* If $query is null, the specified column from the previous SQL result is returned. |
|
0 | 2622 |
* |
2623 |
* @since 0.71 |
|
2624 |
* |
|
2625 |
* @param string|null $query Optional. SQL query. Defaults to previous query. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2626 |
* @param int $x Optional. Column to return. Indexed from 0. |
0 | 2627 |
* @return array Database query result. Array indexed from 0 by SQL result row number. |
2628 |
*/ |
|
9 | 2629 |
public function get_col( $query = null, $x = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2630 |
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
5 | 2631 |
$this->check_current_query = false; |
2632 |
} |
|
2633 |
||
2634 |
if ( $query ) { |
|
0 | 2635 |
$this->query( $query ); |
5 | 2636 |
} |
0 | 2637 |
|
2638 |
$new_array = array(); |
|
16 | 2639 |
// Extract the column values. |
9 | 2640 |
if ( $this->last_result ) { |
2641 |
for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { |
|
2642 |
$new_array[ $i ] = $this->get_var( null, $x, $i ); |
|
2643 |
} |
|
0 | 2644 |
} |
2645 |
return $new_array; |
|
2646 |
} |
|
2647 |
||
2648 |
/** |
|
16 | 2649 |
* Retrieves an entire SQL result set from the database (i.e., many rows). |
0 | 2650 |
* |
2651 |
* Executes a SQL query and returns the entire SQL result. |
|
2652 |
* |
|
2653 |
* @since 0.71 |
|
2654 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2655 |
* @param string $query SQL query. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2656 |
* @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
16 | 2657 |
* With one of the first three, return an array of rows indexed |
2658 |
* from 0 by SQL result row number. Each row is an associative array |
|
2659 |
* (column => value, ...), a numerically indexed array (0 => value, ...), |
|
2660 |
* or an object ( ->column = value ), respectively. With OBJECT_K, |
|
2661 |
* return an associative array of row objects keyed by the value |
|
2662 |
* of each row's first column's value. Duplicate keys are discarded. |
|
2663 |
* @return array|object|null Database query results. |
|
0 | 2664 |
*/ |
5 | 2665 |
public function get_results( $query = null, $output = OBJECT ) { |
0 | 2666 |
$this->func_call = "\$db->get_results(\"$query\", $output)"; |
2667 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2668 |
if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
5 | 2669 |
$this->check_current_query = false; |
2670 |
} |
|
2671 |
||
2672 |
if ( $query ) { |
|
0 | 2673 |
$this->query( $query ); |
5 | 2674 |
} else { |
0 | 2675 |
return null; |
5 | 2676 |
} |
0 | 2677 |
|
2678 |
$new_array = array(); |
|
16 | 2679 |
if ( OBJECT === $output ) { |
2680 |
// Return an integer-keyed array of row objects. |
|
0 | 2681 |
return $this->last_result; |
16 | 2682 |
} elseif ( OBJECT_K === $output ) { |
2683 |
// Return an array of row objects with keys from column 1. |
|
2684 |
// (Duplicates are discarded.) |
|
9 | 2685 |
if ( $this->last_result ) { |
2686 |
foreach ( $this->last_result as $row ) { |
|
2687 |
$var_by_ref = get_object_vars( $row ); |
|
2688 |
$key = array_shift( $var_by_ref ); |
|
2689 |
if ( ! isset( $new_array[ $key ] ) ) { |
|
2690 |
$new_array[ $key ] = $row; |
|
2691 |
} |
|
2692 |
} |
|
0 | 2693 |
} |
2694 |
return $new_array; |
|
16 | 2695 |
} elseif ( ARRAY_A === $output || ARRAY_N === $output ) { |
0 | 2696 |
// Return an integer-keyed array of... |
2697 |
if ( $this->last_result ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2698 |
foreach ( (array) $this->last_result as $row ) { |
16 | 2699 |
if ( ARRAY_N === $output ) { |
2700 |
// ...integer-keyed row arrays. |
|
0 | 2701 |
$new_array[] = array_values( get_object_vars( $row ) ); |
2702 |
} else { |
|
16 | 2703 |
// ...column name-keyed row arrays. |
0 | 2704 |
$new_array[] = get_object_vars( $row ); |
2705 |
} |
|
2706 |
} |
|
2707 |
} |
|
2708 |
return $new_array; |
|
5 | 2709 |
} elseif ( strtoupper( $output ) === OBJECT ) { |
16 | 2710 |
// Back compat for OBJECT being previously case-insensitive. |
5 | 2711 |
return $this->last_result; |
0 | 2712 |
} |
2713 |
return null; |
|
2714 |
} |
|
2715 |
||
2716 |
/** |
|
5 | 2717 |
* Retrieves the character set for the given table. |
2718 |
* |
|
2719 |
* @since 4.2.0 |
|
2720 |
* |
|
2721 |
* @param string $table Table name. |
|
2722 |
* @return string|WP_Error Table character set, WP_Error object if it couldn't be found. |
|
2723 |
*/ |
|
2724 |
protected function get_table_charset( $table ) { |
|
2725 |
$tablekey = strtolower( $table ); |
|
2726 |
||
2727 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2728 |
* Filters the table charset value before the DB is checked. |
5 | 2729 |
* |
2730 |
* Passing a non-null value to the filter will effectively short-circuit |
|
2731 |
* checking the DB for the charset, returning that value instead. |
|
2732 |
* |
|
2733 |
* @since 4.2.0 |
|
2734 |
* |
|
16 | 2735 |
* @param string|null $charset The character set to use. Default null. |
2736 |
* @param string $table The name of the table being checked. |
|
5 | 2737 |
*/ |
2738 |
$charset = apply_filters( 'pre_get_table_charset', null, $table ); |
|
2739 |
if ( null !== $charset ) { |
|
2740 |
return $charset; |
|
2741 |
} |
|
2742 |
||
2743 |
if ( isset( $this->table_charset[ $tablekey ] ) ) { |
|
2744 |
return $this->table_charset[ $tablekey ]; |
|
2745 |
} |
|
2746 |
||
16 | 2747 |
$charsets = array(); |
2748 |
$columns = array(); |
|
5 | 2749 |
|
2750 |
$table_parts = explode( '.', $table ); |
|
9 | 2751 |
$table = '`' . implode( '`.`', $table_parts ) . '`'; |
2752 |
$results = $this->get_results( "SHOW FULL COLUMNS FROM $table" ); |
|
5 | 2753 |
if ( ! $results ) { |
2754 |
return new WP_Error( 'wpdb_get_table_charset_failure' ); |
|
2755 |
} |
|
2756 |
||
2757 |
foreach ( $results as $column ) { |
|
2758 |
$columns[ strtolower( $column->Field ) ] = $column; |
|
2759 |
} |
|
2760 |
||
2761 |
$this->col_meta[ $tablekey ] = $columns; |
|
2762 |
||
2763 |
foreach ( $columns as $column ) { |
|
2764 |
if ( ! empty( $column->Collation ) ) { |
|
2765 |
list( $charset ) = explode( '_', $column->Collation ); |
|
2766 |
||
2767 |
// If the current connection can't support utf8mb4 characters, let's only send 3-byte utf8 characters. |
|
2768 |
if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) { |
|
2769 |
$charset = 'utf8'; |
|
2770 |
} |
|
2771 |
||
2772 |
$charsets[ strtolower( $charset ) ] = true; |
|
2773 |
} |
|
2774 |
||
2775 |
list( $type ) = explode( '(', $column->Type ); |
|
2776 |
||
2777 |
// A binary/blob means the whole query gets treated like this. |
|
16 | 2778 |
if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ), true ) ) { |
5 | 2779 |
$this->table_charset[ $tablekey ] = 'binary'; |
2780 |
return 'binary'; |
|
2781 |
} |
|
2782 |
} |
|
2783 |
||
2784 |
// utf8mb3 is an alias for utf8. |
|
2785 |
if ( isset( $charsets['utf8mb3'] ) ) { |
|
2786 |
$charsets['utf8'] = true; |
|
2787 |
unset( $charsets['utf8mb3'] ); |
|
2788 |
} |
|
2789 |
||
2790 |
// Check if we have more than one charset in play. |
|
2791 |
$count = count( $charsets ); |
|
2792 |
if ( 1 === $count ) { |
|
2793 |
$charset = key( $charsets ); |
|
2794 |
} elseif ( 0 === $count ) { |
|
2795 |
// No charsets, assume this table can store whatever. |
|
2796 |
$charset = false; |
|
2797 |
} else { |
|
2798 |
// More than one charset. Remove latin1 if present and recalculate. |
|
2799 |
unset( $charsets['latin1'] ); |
|
2800 |
$count = count( $charsets ); |
|
2801 |
if ( 1 === $count ) { |
|
2802 |
// Only one charset (besides latin1). |
|
2803 |
$charset = key( $charsets ); |
|
2804 |
} elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) { |
|
2805 |
// Two charsets, but they're utf8 and utf8mb4, use utf8. |
|
2806 |
$charset = 'utf8'; |
|
2807 |
} else { |
|
2808 |
// Two mixed character sets. ascii. |
|
2809 |
$charset = 'ascii'; |
|
2810 |
} |
|
2811 |
} |
|
2812 |
||
2813 |
$this->table_charset[ $tablekey ] = $charset; |
|
2814 |
return $charset; |
|
2815 |
} |
|
2816 |
||
2817 |
/** |
|
2818 |
* Retrieves the character set for the given column. |
|
2819 |
* |
|
2820 |
* @since 4.2.0 |
|
2821 |
* |
|
2822 |
* @param string $table Table name. |
|
2823 |
* @param string $column Column name. |
|
16 | 2824 |
* @return string|false|WP_Error Column character set as a string. False if the column has |
2825 |
* no character set. WP_Error object if there was an error. |
|
5 | 2826 |
*/ |
2827 |
public function get_col_charset( $table, $column ) { |
|
9 | 2828 |
$tablekey = strtolower( $table ); |
5 | 2829 |
$columnkey = strtolower( $column ); |
2830 |
||
2831 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2832 |
* Filters the column charset value before the DB is checked. |
5 | 2833 |
* |
2834 |
* Passing a non-null value to the filter will short-circuit |
|
2835 |
* checking the DB for the charset, returning that value instead. |
|
2836 |
* |
|
2837 |
* @since 4.2.0 |
|
2838 |
* |
|
16 | 2839 |
* @param string|null $charset The character set to use. Default null. |
2840 |
* @param string $table The name of the table being checked. |
|
2841 |
* @param string $column The name of the column being checked. |
|
5 | 2842 |
*/ |
2843 |
$charset = apply_filters( 'pre_get_col_charset', null, $table, $column ); |
|
2844 |
if ( null !== $charset ) { |
|
2845 |
return $charset; |
|
2846 |
} |
|
2847 |
||
2848 |
// Skip this entirely if this isn't a MySQL database. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2849 |
if ( empty( $this->is_mysql ) ) { |
5 | 2850 |
return false; |
2851 |
} |
|
2852 |
||
2853 |
if ( empty( $this->table_charset[ $tablekey ] ) ) { |
|
2854 |
// This primes column information for us. |
|
2855 |
$table_charset = $this->get_table_charset( $table ); |
|
2856 |
if ( is_wp_error( $table_charset ) ) { |
|
2857 |
return $table_charset; |
|
2858 |
} |
|
2859 |
} |
|
2860 |
||
2861 |
// If still no column information, return the table charset. |
|
2862 |
if ( empty( $this->col_meta[ $tablekey ] ) ) { |
|
2863 |
return $this->table_charset[ $tablekey ]; |
|
2864 |
} |
|
2865 |
||
2866 |
// If this column doesn't exist, return the table charset. |
|
2867 |
if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) { |
|
2868 |
return $this->table_charset[ $tablekey ]; |
|
2869 |
} |
|
2870 |
||
2871 |
// Return false when it's not a string column. |
|
2872 |
if ( empty( $this->col_meta[ $tablekey ][ $columnkey ]->Collation ) ) { |
|
2873 |
return false; |
|
2874 |
} |
|
2875 |
||
2876 |
list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation ); |
|
2877 |
return $charset; |
|
2878 |
} |
|
2879 |
||
2880 |
/** |
|
16 | 2881 |
* Retrieves the maximum string length allowed in a given column. |
2882 |
* |
|
5 | 2883 |
* The length may either be specified as a byte length or a character length. |
2884 |
* |
|
2885 |
* @since 4.2.1 |
|
2886 |
* |
|
2887 |
* @param string $table Table name. |
|
2888 |
* @param string $column Column name. |
|
16 | 2889 |
* @return array|false|WP_Error array( 'length' => (int), 'type' => 'byte' | 'char' ). |
2890 |
* False if the column has no length (for example, numeric column). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2891 |
* WP_Error object if there was an error. |
5 | 2892 |
*/ |
2893 |
public function get_col_length( $table, $column ) { |
|
9 | 2894 |
$tablekey = strtolower( $table ); |
5 | 2895 |
$columnkey = strtolower( $column ); |
2896 |
||
2897 |
// Skip this entirely if this isn't a MySQL database. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2898 |
if ( empty( $this->is_mysql ) ) { |
5 | 2899 |
return false; |
2900 |
} |
|
2901 |
||
2902 |
if ( empty( $this->col_meta[ $tablekey ] ) ) { |
|
2903 |
// This primes column information for us. |
|
2904 |
$table_charset = $this->get_table_charset( $table ); |
|
2905 |
if ( is_wp_error( $table_charset ) ) { |
|
2906 |
return $table_charset; |
|
2907 |
} |
|
2908 |
} |
|
2909 |
||
2910 |
if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) { |
|
2911 |
return false; |
|
2912 |
} |
|
2913 |
||
2914 |
$typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type ); |
|
2915 |
||
2916 |
$type = strtolower( $typeinfo[0] ); |
|
2917 |
if ( ! empty( $typeinfo[1] ) ) { |
|
2918 |
$length = trim( $typeinfo[1], ')' ); |
|
2919 |
} else { |
|
2920 |
$length = false; |
|
2921 |
} |
|
2922 |
||
9 | 2923 |
switch ( $type ) { |
5 | 2924 |
case 'char': |
2925 |
case 'varchar': |
|
2926 |
return array( |
|
2927 |
'type' => 'char', |
|
2928 |
'length' => (int) $length, |
|
2929 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2930 |
|
5 | 2931 |
case 'binary': |
2932 |
case 'varbinary': |
|
2933 |
return array( |
|
2934 |
'type' => 'byte', |
|
2935 |
'length' => (int) $length, |
|
2936 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2937 |
|
5 | 2938 |
case 'tinyblob': |
2939 |
case 'tinytext': |
|
2940 |
return array( |
|
2941 |
'type' => 'byte', |
|
2942 |
'length' => 255, // 2^8 - 1 |
|
2943 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2944 |
|
5 | 2945 |
case 'blob': |
2946 |
case 'text': |
|
2947 |
return array( |
|
2948 |
'type' => 'byte', |
|
2949 |
'length' => 65535, // 2^16 - 1 |
|
2950 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2951 |
|
5 | 2952 |
case 'mediumblob': |
2953 |
case 'mediumtext': |
|
2954 |
return array( |
|
2955 |
'type' => 'byte', |
|
2956 |
'length' => 16777215, // 2^24 - 1 |
|
2957 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2958 |
|
5 | 2959 |
case 'longblob': |
2960 |
case 'longtext': |
|
2961 |
return array( |
|
2962 |
'type' => 'byte', |
|
2963 |
'length' => 4294967295, // 2^32 - 1 |
|
2964 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2965 |
|
5 | 2966 |
default: |
2967 |
return false; |
|
2968 |
} |
|
2969 |
} |
|
2970 |
||
2971 |
/** |
|
16 | 2972 |
* Checks if a string is ASCII. |
5 | 2973 |
* |
2974 |
* The negative regex is faster for non-ASCII strings, as it allows |
|
2975 |
* the search to finish as soon as it encounters a non-ASCII character. |
|
2976 |
* |
|
2977 |
* @since 4.2.0 |
|
2978 |
* |
|
2979 |
* @param string $string String to check. |
|
2980 |
* @return bool True if ASCII, false if not. |
|
2981 |
*/ |
|
2982 |
protected function check_ascii( $string ) { |
|
2983 |
if ( function_exists( 'mb_check_encoding' ) ) { |
|
2984 |
if ( mb_check_encoding( $string, 'ASCII' ) ) { |
|
2985 |
return true; |
|
2986 |
} |
|
2987 |
} elseif ( ! preg_match( '/[^\x00-\x7F]/', $string ) ) { |
|
2988 |
return true; |
|
2989 |
} |
|
2990 |
||
2991 |
return false; |
|
2992 |
} |
|
2993 |
||
2994 |
/** |
|
16 | 2995 |
* Checks if the query is accessing a collation considered safe on the current version of MySQL. |
5 | 2996 |
* |
2997 |
* @since 4.2.0 |
|
2998 |
* |
|
2999 |
* @param string $query The query to check. |
|
3000 |
* @return bool True if the collation is safe, false if it isn't. |
|
3001 |
*/ |
|
3002 |
protected function check_safe_collation( $query ) { |
|
3003 |
if ( $this->checking_collation ) { |
|
3004 |
return true; |
|
3005 |
} |
|
3006 |
||
3007 |
// We don't need to check the collation for queries that don't read data. |
|
3008 |
$query = ltrim( $query, "\r\n\t (" ); |
|
3009 |
if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $query ) ) { |
|
3010 |
return true; |
|
3011 |
} |
|
3012 |
||
3013 |
// All-ASCII queries don't need extra checking. |
|
3014 |
if ( $this->check_ascii( $query ) ) { |
|
3015 |
return true; |
|
3016 |
} |
|
3017 |
||
3018 |
$table = $this->get_table_from_query( $query ); |
|
3019 |
if ( ! $table ) { |
|
3020 |
return false; |
|
3021 |
} |
|
3022 |
||
3023 |
$this->checking_collation = true; |
|
9 | 3024 |
$collation = $this->get_table_charset( $table ); |
5 | 3025 |
$this->checking_collation = false; |
3026 |
||
3027 |
// Tables with no collation, or latin1 only, don't need extra checking. |
|
3028 |
if ( false === $collation || 'latin1' === $collation ) { |
|
3029 |
return true; |
|
3030 |
} |
|
3031 |
||
3032 |
$table = strtolower( $table ); |
|
3033 |
if ( empty( $this->col_meta[ $table ] ) ) { |
|
3034 |
return false; |
|
3035 |
} |
|
3036 |
||
3037 |
// If any of the columns don't have one of these collations, it needs more sanity checking. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3038 |
foreach ( $this->col_meta[ $table ] as $col ) { |
5 | 3039 |
if ( empty( $col->Collation ) ) { |
3040 |
continue; |
|
3041 |
} |
|
3042 |
||
3043 |
if ( ! in_array( $col->Collation, array( 'utf8_general_ci', 'utf8_bin', 'utf8mb4_general_ci', 'utf8mb4_bin' ), true ) ) { |
|
3044 |
return false; |
|
3045 |
} |
|
3046 |
} |
|
3047 |
||
3048 |
return true; |
|
3049 |
} |
|
3050 |
||
3051 |
/** |
|
3052 |
* Strips any invalid characters based on value/charset pairs. |
|
3053 |
* |
|
3054 |
* @since 4.2.0 |
|
3055 |
* |
|
16 | 3056 |
* @param array $data Array of value arrays. Each value array has the keys 'value' and 'charset'. |
3057 |
* An optional 'ascii' key can be set to false to avoid redundant ASCII checks. |
|
3058 |
* @return array|WP_Error The $data parameter, with invalid characters removed from each value. |
|
3059 |
* This works as a passthrough: any additional keys such as 'field' are |
|
3060 |
* retained in each value array. If we cannot remove invalid characters, |
|
3061 |
* a WP_Error object is returned. |
|
5 | 3062 |
*/ |
3063 |
protected function strip_invalid_text( $data ) { |
|
3064 |
$db_check_string = false; |
|
3065 |
||
3066 |
foreach ( $data as &$value ) { |
|
3067 |
$charset = $value['charset']; |
|
3068 |
||
3069 |
if ( is_array( $value['length'] ) ) { |
|
9 | 3070 |
$length = $value['length']['length']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3071 |
$truncate_by_byte_length = 'byte' === $value['length']['type']; |
5 | 3072 |
} else { |
3073 |
$length = false; |
|
16 | 3074 |
// Since we have no length, we'll never truncate. Initialize the variable to false. |
3075 |
// True would take us through an unnecessary (for this case) codepath below. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3076 |
$truncate_by_byte_length = false; |
5 | 3077 |
} |
3078 |
||
3079 |
// There's no charset to work with. |
|
3080 |
if ( false === $charset ) { |
|
3081 |
continue; |
|
3082 |
} |
|
3083 |
||
3084 |
// Column isn't a string. |
|
3085 |
if ( ! is_string( $value['value'] ) ) { |
|
3086 |
continue; |
|
3087 |
} |
|
3088 |
||
3089 |
$needs_validation = true; |
|
3090 |
if ( |
|
16 | 3091 |
// latin1 can store any byte sequence. |
5 | 3092 |
'latin1' === $charset |
3093 |
|| |
|
3094 |
// ASCII is always OK. |
|
3095 |
( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) ) |
|
3096 |
) { |
|
3097 |
$truncate_by_byte_length = true; |
|
9 | 3098 |
$needs_validation = false; |
5 | 3099 |
} |
3100 |
||
3101 |
if ( $truncate_by_byte_length ) { |
|
3102 |
mbstring_binary_safe_encoding(); |
|
3103 |
if ( false !== $length && strlen( $value['value'] ) > $length ) { |
|
3104 |
$value['value'] = substr( $value['value'], 0, $length ); |
|
3105 |
} |
|
3106 |
reset_mbstring_encoding(); |
|
3107 |
||
3108 |
if ( ! $needs_validation ) { |
|
3109 |
continue; |
|
3110 |
} |
|
3111 |
} |
|
3112 |
||
3113 |
// utf8 can be handled by regex, which is a bunch faster than a DB lookup. |
|
3114 |
if ( ( 'utf8' === $charset || 'utf8mb3' === $charset || 'utf8mb4' === $charset ) && function_exists( 'mb_strlen' ) ) { |
|
3115 |
$regex = '/ |
|
3116 |
( |
|
3117 |
(?: [\x00-\x7F] # single-byte sequences 0xxxxxxx |
|
3118 |
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
|
3119 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
3120 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
3121 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
3122 |
| [\xEE-\xEF][\x80-\xBF]{2}'; |
|
3123 |
||
3124 |
if ( 'utf8mb4' === $charset ) { |
|
3125 |
$regex .= ' |
|
3126 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
3127 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
3128 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
3129 |
'; |
|
3130 |
} |
|
3131 |
||
9 | 3132 |
$regex .= '){1,40} # ...one or more times |
5 | 3133 |
) |
3134 |
| . # anything else |
|
3135 |
/x'; |
|
3136 |
$value['value'] = preg_replace( $regex, '$1', $value['value'] ); |
|
3137 |
||
3138 |
if ( false !== $length && mb_strlen( $value['value'], 'UTF-8' ) > $length ) { |
|
3139 |
$value['value'] = mb_substr( $value['value'], 0, $length, 'UTF-8' ); |
|
3140 |
} |
|
3141 |
continue; |
|
3142 |
} |
|
3143 |
||
3144 |
// We couldn't use any local conversions, send it to the DB. |
|
16 | 3145 |
$value['db'] = true; |
3146 |
$db_check_string = true; |
|
5 | 3147 |
} |
3148 |
unset( $value ); // Remove by reference. |
|
3149 |
||
3150 |
if ( $db_check_string ) { |
|
3151 |
$queries = array(); |
|
3152 |
foreach ( $data as $col => $value ) { |
|
3153 |
if ( ! empty( $value['db'] ) ) { |
|
3154 |
// We're going to need to truncate by characters or bytes, depending on the length value we have. |
|
16 | 3155 |
if ( isset( $value['length']['type'] ) && 'byte' === $value['length']['type'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3156 |
// Using binary causes LEFT() to truncate by bytes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3157 |
$charset = 'binary'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3158 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3159 |
$charset = $value['charset']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3160 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3162 |
if ( $this->charset ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3163 |
$connection_charset = $this->charset; |
5 | 3164 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3165 |
if ( $this->use_mysqli ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3166 |
$connection_charset = mysqli_character_set_name( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3167 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3168 |
$connection_charset = mysql_client_encoding(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3169 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3170 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3171 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3172 |
if ( is_array( $value['length'] ) ) { |
9 | 3173 |
$length = sprintf( '%.0f', $value['length']['length'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3174 |
$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset )", $value['value'] ); |
9 | 3175 |
} elseif ( 'binary' !== $charset ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3176 |
// If we don't have a length, there's no need to convert binary - it will always return the same result. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3177 |
$queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] ); |
5 | 3178 |
} |
3179 |
||
3180 |
unset( $data[ $col ]['db'] ); |
|
3181 |
} |
|
3182 |
} |
|
3183 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3184 |
$sql = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3185 |
foreach ( $queries as $column => $query ) { |
5 | 3186 |
if ( ! $query ) { |
3187 |
continue; |
|
3188 |
} |
|
3189 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3190 |
$sql[] = $query . " AS x_$column"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3191 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3192 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3193 |
$this->check_current_query = false; |
9 | 3194 |
$row = $this->get_row( 'SELECT ' . implode( ', ', $sql ), ARRAY_A ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3195 |
if ( ! $row ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3196 |
return new WP_Error( 'wpdb_strip_invalid_text_failure' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3197 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3199 |
foreach ( array_keys( $data ) as $column ) { |
9 | 3200 |
if ( isset( $row[ "x_$column" ] ) ) { |
3201 |
$data[ $column ]['value'] = $row[ "x_$column" ]; |
|
5 | 3202 |
} |
3203 |
} |
|
3204 |
} |
|
3205 |
||
3206 |
return $data; |
|
3207 |
} |
|
3208 |
||
3209 |
/** |
|
3210 |
* Strips any invalid characters from the query. |
|
3211 |
* |
|
3212 |
* @since 4.2.0 |
|
3213 |
* |
|
3214 |
* @param string $query Query to convert. |
|
3215 |
* @return string|WP_Error The converted query, or a WP_Error object if the conversion fails. |
|
3216 |
*/ |
|
3217 |
protected function strip_invalid_text_from_query( $query ) { |
|
3218 |
// We don't need to check the collation for queries that don't read data. |
|
3219 |
$trimmed_query = ltrim( $query, "\r\n\t (" ); |
|
3220 |
if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $trimmed_query ) ) { |
|
3221 |
return $query; |
|
3222 |
} |
|
3223 |
||
3224 |
$table = $this->get_table_from_query( $query ); |
|
3225 |
if ( $table ) { |
|
3226 |
$charset = $this->get_table_charset( $table ); |
|
3227 |
if ( is_wp_error( $charset ) ) { |
|
3228 |
return $charset; |
|
3229 |
} |
|
3230 |
||
16 | 3231 |
// We can't reliably strip text from tables containing binary/blob columns. |
5 | 3232 |
if ( 'binary' === $charset ) { |
3233 |
return $query; |
|
3234 |
} |
|
3235 |
} else { |
|
3236 |
$charset = $this->charset; |
|
3237 |
} |
|
3238 |
||
3239 |
$data = array( |
|
3240 |
'value' => $query, |
|
3241 |
'charset' => $charset, |
|
3242 |
'ascii' => false, |
|
3243 |
'length' => false, |
|
3244 |
); |
|
3245 |
||
3246 |
$data = $this->strip_invalid_text( array( $data ) ); |
|
3247 |
if ( is_wp_error( $data ) ) { |
|
3248 |
return $data; |
|
3249 |
} |
|
3250 |
||
3251 |
return $data[0]['value']; |
|
3252 |
} |
|
3253 |
||
3254 |
/** |
|
3255 |
* Strips any invalid characters from the string for a given table and column. |
|
3256 |
* |
|
3257 |
* @since 4.2.0 |
|
3258 |
* |
|
3259 |
* @param string $table Table name. |
|
3260 |
* @param string $column Column name. |
|
3261 |
* @param string $value The text to check. |
|
3262 |
* @return string|WP_Error The converted string, or a WP_Error object if the conversion fails. |
|
3263 |
*/ |
|
3264 |
public function strip_invalid_text_for_column( $table, $column, $value ) { |
|
3265 |
if ( ! is_string( $value ) ) { |
|
3266 |
return $value; |
|
3267 |
} |
|
3268 |
||
3269 |
$charset = $this->get_col_charset( $table, $column ); |
|
3270 |
if ( ! $charset ) { |
|
3271 |
// Not a string column. |
|
3272 |
return $value; |
|
3273 |
} elseif ( is_wp_error( $charset ) ) { |
|
3274 |
// Bail on real errors. |
|
3275 |
return $charset; |
|
3276 |
} |
|
3277 |
||
3278 |
$data = array( |
|
3279 |
$column => array( |
|
3280 |
'value' => $value, |
|
3281 |
'charset' => $charset, |
|
3282 |
'length' => $this->get_col_length( $table, $column ), |
|
9 | 3283 |
), |
5 | 3284 |
); |
3285 |
||
3286 |
$data = $this->strip_invalid_text( $data ); |
|
3287 |
if ( is_wp_error( $data ) ) { |
|
3288 |
return $data; |
|
3289 |
} |
|
3290 |
||
3291 |
return $data[ $column ]['value']; |
|
3292 |
} |
|
3293 |
||
3294 |
/** |
|
16 | 3295 |
* Finds the first table name referenced in a query. |
5 | 3296 |
* |
3297 |
* @since 4.2.0 |
|
3298 |
* |
|
3299 |
* @param string $query The query to search. |
|
16 | 3300 |
* @return string|false The table name found, or false if a table couldn't be found. |
5 | 3301 |
*/ |
3302 |
protected function get_table_from_query( $query ) { |
|
3303 |
// Remove characters that can legally trail the table name. |
|
3304 |
$query = rtrim( $query, ';/-#' ); |
|
3305 |
||
3306 |
// Allow (select...) union [...] style queries. Use the first query's table name. |
|
3307 |
$query = ltrim( $query, "\r\n\t (" ); |
|
3308 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3309 |
// Strip everything between parentheses except nested selects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3310 |
$query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', $query ); |
5 | 3311 |
|
3312 |
// Quickly match most common queries. |
|
9 | 3313 |
if ( preg_match( |
3314 |
'/^\s*(?:' |
|
5 | 3315 |
. 'SELECT.*?\s+FROM' |
3316 |
. '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?' |
|
3317 |
. '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?' |
|
3318 |
. '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?' |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3319 |
. '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?' |
9 | 3320 |
. ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is', |
3321 |
$query, |
|
3322 |
$maybe |
|
3323 |
) ) { |
|
5 | 3324 |
return str_replace( '`', '', $maybe[1] ); |
3325 |
} |
|
3326 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3327 |
// SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3328 |
if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3329 |
return $maybe[2]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3330 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3331 |
|
16 | 3332 |
/* |
3333 |
* SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%' |
|
3334 |
* This quoted LIKE operand seldom holds a full table name. |
|
3335 |
* It is usually a pattern for matching a prefix so we just |
|
3336 |
* strip the trailing % and unescape the _ to get 'wp_123_' |
|
3337 |
* which drop-ins can use for routing these SQL statements. |
|
3338 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3339 |
if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3340 |
return str_replace( '\\_', '_', $maybe[2] ); |
5 | 3341 |
} |
3342 |
||
3343 |
// Big pattern for the rest of the table-related queries. |
|
9 | 3344 |
if ( preg_match( |
3345 |
'/^\s*(?:' |
|
5 | 3346 |
. '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM' |
3347 |
. '|DESCRIBE|DESC|EXPLAIN|HANDLER' |
|
3348 |
. '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?' |
|
3349 |
. '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE' |
|
3350 |
. '|TRUNCATE(?:\s+TABLE)?' |
|
3351 |
. '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?' |
|
3352 |
. '|ALTER(?:\s+IGNORE)?\s+TABLE' |
|
3353 |
. '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?' |
|
3354 |
. '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON' |
|
3355 |
. '|DROP\s+INDEX.*\s+ON' |
|
3356 |
. '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE' |
|
3357 |
. '|(?:GRANT|REVOKE).*ON\s+TABLE' |
|
3358 |
. '|SHOW\s+(?:.*FROM|.*TABLE)' |
|
9 | 3359 |
. ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', |
3360 |
$query, |
|
3361 |
$maybe |
|
3362 |
) ) { |
|
5 | 3363 |
return str_replace( '`', '', $maybe[1] ); |
3364 |
} |
|
3365 |
||
3366 |
return false; |
|
3367 |
} |
|
3368 |
||
3369 |
/** |
|
16 | 3370 |
* Loads the column metadata from the last query. |
0 | 3371 |
* |
3372 |
* @since 3.5.0 |
|
3373 |
*/ |
|
3374 |
protected function load_col_info() { |
|
9 | 3375 |
if ( $this->col_info ) { |
0 | 3376 |
return; |
9 | 3377 |
} |
0 | 3378 |
|
5 | 3379 |
if ( $this->use_mysqli ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3380 |
$num_fields = mysqli_num_fields( $this->result ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3381 |
for ( $i = 0; $i < $num_fields; $i++ ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3382 |
$this->col_info[ $i ] = mysqli_fetch_field( $this->result ); |
5 | 3383 |
} |
3384 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3385 |
$num_fields = mysql_num_fields( $this->result ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3386 |
for ( $i = 0; $i < $num_fields; $i++ ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3387 |
$this->col_info[ $i ] = mysql_fetch_field( $this->result, $i ); |
5 | 3388 |
} |
0 | 3389 |
} |
3390 |
} |
|
3391 |
||
3392 |
/** |
|
16 | 3393 |
* Retrieves column metadata from the last query. |
0 | 3394 |
* |
3395 |
* @since 0.71 |
|
3396 |
* |
|
16 | 3397 |
* @param string $info_type Optional. Possible values include 'name', 'table', 'def', 'max_length', |
3398 |
* 'not_null', 'primary_key', 'multiple_key', 'unique_key', 'numeric', |
|
3399 |
* 'blob', 'type', 'unsigned', 'zerofill'. Default 'name'. |
|
3400 |
* @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. |
|
3401 |
* 3: if the col is numeric. 4: col's type. Default -1. |
|
3402 |
* @return mixed Column results. |
|
0 | 3403 |
*/ |
5 | 3404 |
public function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
0 | 3405 |
$this->load_col_info(); |
3406 |
||
3407 |
if ( $this->col_info ) { |
|
16 | 3408 |
if ( -1 === $col_offset ) { |
9 | 3409 |
$i = 0; |
0 | 3410 |
$new_array = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3411 |
foreach ( (array) $this->col_info as $col ) { |
9 | 3412 |
$new_array[ $i ] = $col->{$info_type}; |
0 | 3413 |
$i++; |
3414 |
} |
|
3415 |
return $new_array; |
|
3416 |
} else { |
|
9 | 3417 |
return $this->col_info[ $col_offset ]->{$info_type}; |
0 | 3418 |
} |
3419 |
} |
|
3420 |
} |
|
3421 |
||
3422 |
/** |
|
3423 |
* Starts the timer, for debugging purposes. |
|
3424 |
* |
|
3425 |
* @since 1.5.0 |
|
3426 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3427 |
* @return true |
0 | 3428 |
*/ |
5 | 3429 |
public function timer_start() { |
0 | 3430 |
$this->time_start = microtime( true ); |
3431 |
return true; |
|
3432 |
} |
|
3433 |
||
3434 |
/** |
|
3435 |
* Stops the debugging timer. |
|
3436 |
* |
|
3437 |
* @since 1.5.0 |
|
3438 |
* |
|
16 | 3439 |
* @return float Total time spent on the query, in seconds. |
0 | 3440 |
*/ |
5 | 3441 |
public function timer_stop() { |
0 | 3442 |
return ( microtime( true ) - $this->time_start ); |
3443 |
} |
|
3444 |
||
3445 |
/** |
|
3446 |
* Wraps errors in a nice header and footer and dies. |
|
3447 |
* |
|
3448 |
* Will not die if wpdb::$show_errors is false. |
|
3449 |
* |
|
3450 |
* @since 1.5.0 |
|
3451 |
* |
|
16 | 3452 |
* @param string $message The error message. |
3453 |
* @param string $error_code Optional. A computer-readable string to identify the error. |
|
3454 |
* Default '500'. |
|
3455 |
* @return void|false Void if the showing of errors is enabled, false if disabled. |
|
0 | 3456 |
*/ |
5 | 3457 |
public function bail( $message, $error_code = '500' ) { |
9 | 3458 |
if ( $this->show_errors ) { |
3459 |
$error = ''; |
|
3460 |
||
3461 |
if ( $this->use_mysqli ) { |
|
3462 |
if ( $this->dbh instanceof mysqli ) { |
|
3463 |
$error = mysqli_error( $this->dbh ); |
|
3464 |
} elseif ( mysqli_connect_errno() ) { |
|
3465 |
$error = mysqli_connect_error(); |
|
3466 |
} |
|
3467 |
} else { |
|
3468 |
if ( is_resource( $this->dbh ) ) { |
|
3469 |
$error = mysql_error( $this->dbh ); |
|
3470 |
} else { |
|
3471 |
$error = mysql_error(); |
|
3472 |
} |
|
3473 |
} |
|
3474 |
||
3475 |
if ( $error ) { |
|
3476 |
$message = '<p><code>' . $error . "</code></p>\n" . $message; |
|
3477 |
} |
|
3478 |
||
3479 |
wp_die( $message ); |
|
3480 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3481 |
if ( class_exists( 'WP_Error', false ) ) { |
9 | 3482 |
$this->error = new WP_Error( $error_code, $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3483 |
} else { |
0 | 3484 |
$this->error = $message; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3485 |
} |
9 | 3486 |
|
0 | 3487 |
return false; |
3488 |
} |
|
3489 |
} |
|
3490 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3491 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3492 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3493 |
* Closes the current database connection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3494 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3495 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3496 |
* |
16 | 3497 |
* @return bool True if the connection was successfully closed, |
3498 |
* false if it wasn't, or if the connection doesn't exist. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3499 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3500 |
public function close() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3501 |
if ( ! $this->dbh ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3502 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3504 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3505 |
if ( $this->use_mysqli ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3506 |
$closed = mysqli_close( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3507 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3508 |
$closed = mysql_close( $this->dbh ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3509 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3510 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3511 |
if ( $closed ) { |
9 | 3512 |
$this->dbh = null; |
3513 |
$this->ready = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3514 |
$this->has_connected = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3515 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3516 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3517 |
return $closed; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3518 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3519 |
|
0 | 3520 |
/** |
16 | 3521 |
* Determines whether MySQL database is at least the required minimum version. |
0 | 3522 |
* |
3523 |
* @since 2.5.0 |
|
3524 |
* |
|
16 | 3525 |
* @global string $wp_version The WordPress version string. |
3526 |
* @global string $required_mysql_version The required MySQL version string. |
|
3527 |
* @return void|WP_Error |
|
0 | 3528 |
*/ |
5 | 3529 |
public function check_database_version() { |
0 | 3530 |
global $wp_version, $required_mysql_version; |
16 | 3531 |
// Make sure the server has the required MySQL version. |
9 | 3532 |
if ( version_compare( $this->db_version(), $required_mysql_version, '<' ) ) { |
16 | 3533 |
/* translators: 1: WordPress version number, 2: Minimum required MySQL version number. */ |
3534 |
return new WP_Error( 'database_version', sprintf( __( '<strong>Error</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3535 |
} |
0 | 3536 |
} |
3537 |
||
3538 |
/** |
|
16 | 3539 |
* Determines whether the database supports collation. |
0 | 3540 |
* |
3541 |
* Called when WordPress is generating the table scheme. |
|
3542 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3543 |
* Use `wpdb::has_cap( 'collation' )`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3544 |
* |
0 | 3545 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3546 |
* @deprecated 3.5.0 Use wpdb::has_cap() |
0 | 3547 |
* |
16 | 3548 |
* @return bool True if collation is supported, false if not. |
0 | 3549 |
*/ |
5 | 3550 |
public function supports_collation() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3551 |
_deprecated_function( __FUNCTION__, '3.5.0', 'wpdb::has_cap( \'collation\' )' ); |
0 | 3552 |
return $this->has_cap( 'collation' ); |
3553 |
} |
|
3554 |
||
3555 |
/** |
|
16 | 3556 |
* Retrieves the database character collate. |
0 | 3557 |
* |
3558 |
* @since 3.5.0 |
|
3559 |
* |
|
3560 |
* @return string The database character collate. |
|
3561 |
*/ |
|
3562 |
public function get_charset_collate() { |
|
3563 |
$charset_collate = ''; |
|
3564 |
||
9 | 3565 |
if ( ! empty( $this->charset ) ) { |
0 | 3566 |
$charset_collate = "DEFAULT CHARACTER SET $this->charset"; |
9 | 3567 |
} |
3568 |
if ( ! empty( $this->collate ) ) { |
|
0 | 3569 |
$charset_collate .= " COLLATE $this->collate"; |
9 | 3570 |
} |
0 | 3571 |
|
3572 |
return $charset_collate; |
|
3573 |
} |
|
3574 |
||
3575 |
/** |
|
16 | 3576 |
* Determines if a database supports a particular feature. |
0 | 3577 |
* |
3578 |
* @since 2.7.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3579 |
* @since 4.1.0 Added support for the 'utf8mb4' feature. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3580 |
* @since 4.6.0 Added support for the 'utf8mb4_520' feature. |
5 | 3581 |
* |
0 | 3582 |
* @see wpdb::db_version() |
3583 |
* |
|
16 | 3584 |
* @param string $db_cap The feature to check for. Accepts 'collation', 'group_concat', |
3585 |
* 'subqueries', 'set_charset', 'utf8mb4', or 'utf8mb4_520'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3586 |
* @return int|false Whether the database feature is supported, false otherwise. |
0 | 3587 |
*/ |
5 | 3588 |
public function has_cap( $db_cap ) { |
0 | 3589 |
$version = $this->db_version(); |
3590 |
||
3591 |
switch ( strtolower( $db_cap ) ) { |
|
9 | 3592 |
case 'collation': // @since 2.5.0 |
3593 |
case 'group_concat': // @since 2.7.0 |
|
3594 |
case 'subqueries': // @since 2.7.0 |
|
0 | 3595 |
return version_compare( $version, '4.1', '>=' ); |
9 | 3596 |
case 'set_charset': |
0 | 3597 |
return version_compare( $version, '5.0.7', '>=' ); |
9 | 3598 |
case 'utf8mb4': // @since 4.1.0 |
5 | 3599 |
if ( version_compare( $version, '5.5.3', '<' ) ) { |
3600 |
return false; |
|
3601 |
} |
|
3602 |
if ( $this->use_mysqli ) { |
|
3603 |
$client_version = mysqli_get_client_info(); |
|
3604 |
} else { |
|
3605 |
$client_version = mysql_get_client_info(); |
|
3606 |
} |
|
3607 |
||
3608 |
/* |
|
3609 |
* libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server. |
|
3610 |
* mysqlnd has supported utf8mb4 since 5.0.9. |
|
3611 |
*/ |
|
3612 |
if ( false !== strpos( $client_version, 'mysqlnd' ) ) { |
|
3613 |
$client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $client_version ); |
|
3614 |
return version_compare( $client_version, '5.0.9', '>=' ); |
|
3615 |
} else { |
|
3616 |
return version_compare( $client_version, '5.5.3', '>=' ); |
|
3617 |
} |
|
9 | 3618 |
case 'utf8mb4_520': // @since 4.6.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3619 |
return version_compare( $version, '5.6', '>=' ); |
5 | 3620 |
} |
0 | 3621 |
|
3622 |
return false; |
|
3623 |
} |
|
3624 |
||
3625 |
/** |
|
16 | 3626 |
* Retrieves the name of the function that called wpdb. |
0 | 3627 |
* |
16 | 3628 |
* Searches up the list of functions until it reaches the one that would |
3629 |
* most logically had called this method. |
|
0 | 3630 |
* |
3631 |
* @since 2.5.0 |
|
3632 |
* |
|
16 | 3633 |
* @return string Comma-separated list of the calling functions. |
0 | 3634 |
*/ |
5 | 3635 |
public function get_caller() { |
0 | 3636 |
return wp_debug_backtrace_summary( __CLASS__ ); |
3637 |
} |
|
3638 |
||
3639 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3640 |
* Retrieves the MySQL server version. |
0 | 3641 |
* |
3642 |
* @since 2.7.0 |
|
3643 |
* |
|
16 | 3644 |
* @return string|null Version number on success, null on failure. |
0 | 3645 |
*/ |
5 | 3646 |
public function db_version() { |
16 | 3647 |
return preg_replace( '/[^0-9.].*/', '', $this->db_server_info() ); |
3648 |
} |
|
3649 |
||
3650 |
/** |
|
3651 |
* Retrieves full MySQL server information. |
|
3652 |
* |
|
3653 |
* @since 5.5.0 |
|
3654 |
* |
|
3655 |
* @return string|false Server info on success, false on failure. |
|
3656 |
*/ |
|
3657 |
public function db_server_info() { |
|
5 | 3658 |
if ( $this->use_mysqli ) { |
3659 |
$server_info = mysqli_get_server_info( $this->dbh ); |
|
3660 |
} else { |
|
3661 |
$server_info = mysql_get_server_info( $this->dbh ); |
|
3662 |
} |
|
16 | 3663 |
|
3664 |
return $server_info; |
|
0 | 3665 |
} |
3666 |
} |