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