2003-05-28 04:41:04 -04:00
< ? php
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress database access abstraction class
2008-06-22 16:23:23 -04:00
*
* Original code from { @ link http :// php . justinvincent . com Justin Vincent ( justin @ visunet . ie )}
*
* @ package WordPress
* @ subpackage Database
* @ since 0.71
*/
/**
* @ since 0.71
*/
2010-02-22 17:53:54 -05:00
define ( 'EZSQL_VERSION' , 'WP1.25' );
2008-06-22 16:23:23 -04:00
/**
* @ since 0.71
*/
2014-03-03 12:36:13 -05:00
define ( 'OBJECT' , 'OBJECT' );
2019-01-11 01:08:50 -05:00
// phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase
2014-03-03 12:36:13 -05:00
define ( 'object' , 'OBJECT' ); // Back compat.
2008-06-22 16:23:23 -04:00
/**
2010-02-26 01:14:26 -05:00
* @ since 2.5 . 0
2008-06-22 16:23:23 -04:00
*/
2010-04-10 08:39:25 -04:00
define ( 'OBJECT_K' , 'OBJECT_K' );
2008-06-22 16:23:23 -04:00
/**
* @ since 0.71
*/
2010-04-10 08:39:25 -04:00
define ( 'ARRAY_A' , 'ARRAY_A' );
2008-06-22 16:23:23 -04:00
/**
* @ since 0.71
*/
2010-04-10 08:39:25 -04:00
define ( 'ARRAY_N' , 'ARRAY_N' );
2004-08-20 13:52:49 -04:00
2008-06-22 16:23:23 -04:00
/**
2020-05-03 07:45:06 -04:00
* WordPress database access abstraction class .
*
* This class is used to interact with a database without needing to use raw SQL statements .
* By default , WordPress uses this class to instantiate the global $wpdb object , providing
* access to the WordPress database .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* It is possible to replace this class with your own by setting the $wpdb global variable
* in wp - content / db . php file to your class . The wpdb class will still be included , so you can
* extend it or simply use your own .
2008-06-22 16:23:23 -04:00
*
2020-05-02 13:25:07 -04:00
* @ link https :// developer . wordpress . org / reference / classes / wpdb /
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*/
2004-04-25 22:28:06 -04:00
class wpdb {
2008-06-22 16:23:23 -04:00
/**
2014-05-02 08:19:15 -04:00
* Whether to show SQL / DB errors .
*
2020-05-02 13:25:07 -04:00
* Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var bool
*/
2020-10-17 12:26:09 -04:00
public $show_errors = false ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* Whether to suppress errors during the DB bootstrapping . Default false .
2008-06-22 16:23:23 -04:00
*
2010-12-20 07:49:18 -05:00
* @ since 2.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var bool
*/
2020-10-17 12:26:09 -04:00
public $suppress_errors = false ;
2008-06-22 16:23:23 -04:00
/**
2020-05-03 07:45:06 -04:00
* The error encountered during the last query .
2008-06-22 16:23:23 -04:00
*
2010-12-20 07:49:18 -05:00
* @ since 2.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $last_error = '' ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* The number of queries made .
2008-06-22 16:23:23 -04:00
*
* @ since 1.2 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var int
*/
2015-05-28 12:34:25 -04:00
public $num_queries = 0 ;
2008-06-22 16:23:23 -04:00
2010-02-22 17:53:54 -05:00
/**
2020-05-02 13:25:07 -04:00
* Count of rows returned by the last query .
2010-02-22 17:53:54 -05:00
*
2012-08-15 11:32:35 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2010-02-22 17:53:54 -05:00
* @ var int
*/
2015-05-28 12:34:25 -04:00
public $num_rows = 0 ;
2010-02-22 17:53:54 -05:00
/**
2020-05-02 13:25:07 -04:00
* Count of rows affected by the last query .
2010-02-22 17:53:54 -05:00
*
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2010-02-22 17:53:54 -05:00
* @ var int
*/
2020-10-17 12:26:09 -04:00
public $rows_affected = 0 ;
2010-02-22 17:53:54 -05:00
2010-05-19 12:07:42 -04:00
/**
2020-05-02 13:25:07 -04:00
* The ID generated for an AUTO_INCREMENT column by the last query ( usually INSERT ) .
2010-05-19 12:07:42 -04:00
*
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2010-05-19 12:07:42 -04:00
* @ var int
*/
2015-05-28 12:34:25 -04:00
public $insert_id = 0 ;
2010-05-19 12:07:42 -04:00
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* The last query made .
2008-06-22 16:23:23 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2019-05-25 22:30:51 -04:00
* @ var string
2008-06-22 16:23:23 -04:00
*/
2020-10-17 12:26:09 -04:00
public $last_query ;
2008-06-22 16:23:23 -04:00
2010-02-22 17:53:54 -05:00
/**
2020-05-02 13:25:07 -04:00
* Results of the last query .
2010-02-22 17:53:54 -05:00
*
2012-08-15 11:32:35 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2021-12-14 11:24:02 -05:00
* @ var stdClass [] | null
2010-02-22 17:53:54 -05:00
*/
2020-10-17 12:26:09 -04:00
public $last_result ;
2010-02-22 17:53:54 -05:00
2012-08-14 16:21:10 -04:00
/**
2021-12-29 18:14:01 -05:00
* Database query result .
2021-12-14 11:24:02 -05:00
*
* Possible values :
*
2021-12-29 18:14:01 -05:00
* - For successful SELECT , SHOW , DESCRIBE , or EXPLAIN queries :
* - `mysqli_result` instance when the `mysqli` driver is in use
* - `resource` when the older `mysql` driver is in use
2021-12-14 11:24:02 -05:00
* - `true` for other query types that were successful
2021-12-29 18:14:01 -05:00
* - `null` if a query is yet to be made or if the result has since been flushed
2021-12-14 11:24:02 -05:00
* - `false` if the query returned an error
2012-08-14 16:21:10 -04:00
*
2012-08-14 16:34:12 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2021-12-14 11:24:02 -05:00
* @ var mysqli_result | resource | bool | null
2012-08-14 16:21:10 -04:00
*/
protected $result ;
2014-11-14 16:28:22 -05:00
/**
2020-05-02 13:25:07 -04:00
* Cached column info , for sanity checking data before inserting .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2021-09-22 17:23:00 -04:00
*
2014-11-14 16:28:22 -05:00
* @ var array
*/
protected $col_meta = array ();
/**
2021-12-29 18:14:01 -05:00
* Calculated character sets keyed by table name .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2021-09-22 17:23:00 -04:00
*
2021-12-29 18:14:01 -05:00
* @ var string []
2014-11-14 16:28:22 -05:00
*/
protected $table_charset = array ();
/**
2021-12-29 18:14:01 -05:00
* Whether text fields in the current query need to be sanity checked .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2021-09-22 17:23:00 -04:00
*
2014-11-14 16:28:22 -05:00
* @ var bool
*/
protected $check_current_query = true ;
2015-04-20 00:46:25 -04:00
/**
* Flag to ensure we don ' t run into recursion problems when checking the collation .
*
* @ since 4.2 . 0
2021-09-22 17:23:00 -04:00
*
2015-04-20 00:46:25 -04:00
* @ see wpdb :: check_safe_collation ()
2015-06-26 21:03:25 -04:00
* @ var bool
2015-04-20 00:46:25 -04:00
*/
private $checking_collation = false ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* Saved info on the table column .
2008-06-22 16:23:23 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var array
*/
2012-08-08 02:24:59 -04:00
protected $col_info ;
2008-06-22 16:23:23 -04:00
/**
2018-03-05 17:54:35 -05:00
* Log of queries that were executed , for debugging purposes .
2008-06-22 16:23:23 -04:00
*
* @ since 1.5 . 0
2018-03-05 17:54:35 -05:00
* @ since 2.5 . 0 The third element in each query log was added to record the calling functions .
2018-12-19 19:59:47 -05:00
* @ since 5.1 . 0 The fourth element in each query log was added to record the start time .
2019-07-15 00:07:57 -04:00
* @ since 5.3 . 0 The fifth element in each query log was added to record custom data .
2018-03-05 17:54:35 -05:00
*
* @ var array [] {
2021-12-29 18:14:01 -05:00
* Array of arrays containing information about queries that were executed .
2018-03-05 17:54:35 -05:00
*
* @ type array ... $ 0 {
* Data for each query .
*
* @ type string $ 0 The query ' s SQL .
* @ type float $ 1 Total time spent on the query , in seconds .
2020-05-03 07:45:06 -04:00
* @ type string $ 2 Comma - separated list of the calling functions .
2018-03-05 17:54:35 -05:00
* @ type float $ 3 Unix timestamp of the time at the start of the query .
2019-07-15 00:07:57 -04:00
* @ type array $ 4 Custom query data .
2018-03-05 17:54:35 -05:00
* }
* }
2008-06-22 16:23:23 -04:00
*/
2020-10-17 12:26:09 -04:00
public $queries ;
2008-06-22 16:23:23 -04:00
2014-02-02 17:07:12 -05:00
/**
2020-05-02 13:25:07 -04:00
* The number of times to retry reconnecting before dying . Default 5.
2014-02-02 17:07:12 -05:00
*
* @ since 3.9 . 0
2021-09-22 17:23:00 -04:00
*
2014-02-02 17:07:12 -05:00
* @ see wpdb :: check_connection ()
* @ var int
*/
protected $reconnect_retries = 5 ;
2008-06-22 16:23:23 -04:00
/**
2021-12-29 18:14:01 -05:00
* WordPress table prefix .
2008-06-22 16:23:23 -04:00
*
2020-05-02 13:25:07 -04:00
* You can set this to have multiple WordPress installations in a single database .
* The second reason is for possible security precautions .
2008-06-22 16:23:23 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 2.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2015-05-28 12:34:25 -04:00
public $prefix = '' ;
2008-06-22 16:23:23 -04:00
2014-02-24 19:30:13 -05:00
/**
* WordPress base table prefix .
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2014-02-24 19:30:13 -05:00
* @ var string
*/
2017-11-26 18:57:55 -05:00
public $base_prefix ;
2014-02-24 19:30:13 -05:00
2008-06-22 16:23:23 -04:00
/**
* Whether the database queries are ready to start executing .
*
2012-08-15 11:32:35 -04:00
* @ since 2.3 . 2
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var bool
*/
2020-10-17 12:26:09 -04:00
public $ready = false ;
2010-02-22 17:53:54 -05:00
/**
2015-01-29 06:35:22 -05:00
* Blog ID .
2010-02-22 17:53:54 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-22 17:53:54 -05:00
* @ var int
*/
2014-05-19 11:36:13 -04:00
public $blogid = 0 ;
2010-02-22 17:53:54 -05:00
/**
2015-01-29 06:35:22 -05:00
* Site ID .
2010-02-22 17:53:54 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-22 17:53:54 -05:00
* @ var int
*/
2014-05-19 11:36:13 -04:00
public $siteid = 0 ;
2003-05-28 04:41:04 -04:00
2008-06-22 16:23:23 -04:00
/**
2021-12-29 18:14:01 -05:00
* List of WordPress per - site tables .
2008-06-22 16:23:23 -04:00
*
2010-02-26 01:14:26 -05:00
* @ since 2.5 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ see wpdb :: tables ()
2021-12-14 11:24:02 -05:00
* @ var string []
2008-06-22 16:23:23 -04:00
*/
2020-10-17 12:26:09 -04:00
public $tables = array (
2017-11-30 18:11:00 -05:00
'posts' ,
'comments' ,
'links' ,
'options' ,
'postmeta' ,
'terms' ,
'term_taxonomy' ,
'term_relationships' ,
'termmeta' ,
'commentmeta' ,
);
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* List of deprecated WordPress tables .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* 'categories' , 'post2cat' , and 'link2cat' were deprecated in 2.3 . 0 , db version 5539.
2010-02-23 17:04:29 -05:00
*
* @ since 2.9 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ see wpdb :: tables ()
2021-12-14 11:24:02 -05:00
* @ var string []
2008-06-22 16:23:23 -04:00
*/
2020-10-17 12:26:09 -04:00
public $old_tables = array ( 'categories' , 'post2cat' , 'link2cat' );
2010-02-23 17:04:29 -05:00
/**
2020-05-02 13:25:07 -04:00
* List of WordPress global tables .
2010-02-23 17:04:29 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ see wpdb :: tables ()
2021-12-14 11:24:02 -05:00
* @ var string []
2010-02-23 17:04:29 -05:00
*/
2020-10-17 12:26:09 -04:00
public $global_tables = array ( 'users' , 'usermeta' );
2010-02-23 17:04:29 -05:00
/**
2020-05-02 13:25:07 -04:00
* List of Multisite global tables .
2010-02-23 17:04:29 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ see wpdb :: tables ()
2021-12-14 11:24:02 -05:00
* @ var string []
2010-02-23 17:04:29 -05:00
*/
2020-10-17 12:26:09 -04:00
public $ms_global_tables = array (
2017-11-30 18:11:00 -05:00
'blogs' ,
2018-03-15 22:15:31 -04:00
'blogmeta' ,
2017-11-30 18:11:00 -05:00
'signups' ,
'site' ,
'sitemeta' ,
'sitecategories' ,
'registration_log' ,
);
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Comments table .
2008-06-22 16:23:23 -04:00
*
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $comments ;
2008-06-22 16:23:23 -04:00
2010-02-23 17:04:29 -05:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Comment Metadata table .
2010-02-23 17:04:29 -05:00
*
* @ since 2.9 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $commentmeta ;
2010-02-23 17:04:29 -05:00
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Links table .
2008-06-22 16:23:23 -04:00
*
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $links ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Options table .
2008-06-22 16:23:23 -04:00
*
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $options ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Post Metadata table .
2008-06-22 16:23:23 -04:00
*
2010-02-26 01:14:26 -05:00
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $postmeta ;
2008-06-22 16:23:23 -04:00
2009-09-17 16:17:33 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Posts table .
2009-09-17 16:17:33 -04:00
*
2010-02-23 17:04:29 -05:00
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2009-09-17 16:17:33 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $posts ;
2009-09-17 16:17:33 -04:00
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Terms table .
2008-06-22 16:23:23 -04:00
*
* @ since 2.3 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $terms ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Term Relationships table .
2008-06-22 16:23:23 -04:00
*
* @ since 2.3 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $term_relationships ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Term Taxonomy table .
2008-06-22 16:23:23 -04:00
*
* @ since 2.3 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $term_taxonomy ;
2008-06-22 16:23:23 -04:00
2015-09-24 23:59:27 -04:00
/**
* WordPress Term Meta table .
*
* @ since 4.4 . 0
2021-09-22 17:23:00 -04:00
*
2015-09-24 23:59:27 -04:00
* @ var string
*/
public $termmeta ;
2015-12-06 15:26:24 -05:00
//
// Global and Multisite tables
//
2010-02-23 17:04:29 -05:00
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress User Metadata table .
2008-06-22 16:23:23 -04:00
*
* @ since 2.3 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $usermeta ;
2008-06-22 16:23:23 -04:00
2009-09-06 14:46:27 -04:00
/**
2020-05-02 13:25:07 -04:00
* WordPress Users table .
2009-09-06 14:46:27 -04:00
*
2010-02-23 17:04:29 -05:00
* @ since 1.5 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-23 17:04:29 -05:00
* @ var string
2009-09-06 14:46:27 -04:00
*/
2014-05-19 11:36:13 -04:00
public $users ;
2009-09-06 14:46:27 -04:00
2010-02-19 14:57:03 -05:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Blogs table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $blogs ;
2010-02-19 14:57:03 -05:00
2018-03-15 22:15:31 -04:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Blog Metadata table .
2018-03-15 22:15:31 -04:00
*
2019-01-08 03:18:50 -05:00
* @ since 5.1 . 0
2021-09-22 17:23:00 -04:00
*
2018-03-15 22:15:31 -04:00
* @ var string
*/
public $blogmeta ;
2010-02-19 14:57:03 -05:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Registration Log table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $registration_log ;
2009-09-14 10:03:32 -04:00
2009-04-10 17:56:30 -04:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Signups table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $signups ;
2010-02-19 14:57:03 -05:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Sites table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $site ;
2010-02-19 14:57:03 -05:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Sitewide Terms table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $sitecategories ;
2010-02-19 14:57:03 -05:00
/**
2020-05-02 13:25:07 -04:00
* Multisite Site Metadata table .
2010-02-19 14:57:03 -05:00
*
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-02-19 14:57:03 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $sitemeta ;
2010-02-19 14:57:03 -05:00
/**
2020-05-03 07:45:06 -04:00
* Format specifiers for DB columns .
2010-02-23 04:06:50 -05:00
*
2020-05-03 07:45:06 -04:00
* Columns not listed here default to % s . Initialized during WP load .
* Keys are column names , values are format types : 'ID' => '%d' .
2009-04-10 17:56:30 -04:00
*
* @ since 2.8 . 0
2021-09-22 17:23:00 -04:00
*
2012-03-24 11:24:31 -04:00
* @ see wpdb :: prepare ()
* @ see wpdb :: insert ()
* @ see wpdb :: update ()
* @ see wpdb :: delete ()
2010-02-19 14:57:03 -05:00
* @ see wp_set_wpdb_vars ()
2010-01-19 12:28:48 -05:00
* @ var array
2009-04-10 17:56:30 -04:00
*/
2014-05-19 11:36:13 -04:00
public $field_types = array ();
2009-04-10 17:56:30 -04:00
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* Database table columns charset .
2008-06-22 16:23:23 -04:00
*
* @ since 2.2 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $charset ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* Database table columns collate .
2008-06-22 16:23:23 -04:00
*
* @ since 2.2 . 0
2021-09-22 17:23:00 -04:00
*
2008-06-22 16:23:23 -04:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $collate ;
2007-02-01 19:04:35 -05:00
2009-08-21 13:00:53 -04:00
/**
2020-05-02 13:25:07 -04:00
* Database Username .
2009-08-21 13:00:53 -04:00
*
* @ since 2.9 . 0
2021-09-22 17:23:00 -04:00
*
2012-08-08 02:28:57 -04:00
* @ var string
*/
protected $dbuser ;
/**
2020-05-02 13:25:07 -04:00
* Database Password .
2012-08-08 02:28:57 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 3.1 . 0
2021-09-22 17:23:00 -04:00
*
2012-08-08 02:28:57 -04:00
* @ var string
*/
protected $dbpassword ;
/**
2020-05-02 13:25:07 -04:00
* Database Name .
2012-08-08 02:28:57 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 3.1 . 0
2021-09-22 17:23:00 -04:00
*
2012-08-08 02:28:57 -04:00
* @ var string
*/
protected $dbname ;
/**
2020-05-02 13:25:07 -04:00
* Database Host .
2012-08-08 02:28:57 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 3.1 . 0
2021-09-22 17:23:00 -04:00
*
2012-08-08 02:28:57 -04:00
* @ var string
*/
protected $dbhost ;
/**
2021-12-14 11:24:02 -05:00
* Database handle .
*
* Possible values :
*
2021-12-29 18:14:01 -05:00
* - `mysqli` instance when the `mysqli` driver is in use
* - `resource` when the older `mysql` driver is in use
2021-12-14 11:24:02 -05:00
* - `null` if the connection is yet to be made or has been closed
* - `false` if the connection has failed
2012-08-08 02:28:57 -04:00
*
2012-08-15 11:32:35 -04:00
* @ since 0.71
2021-09-22 17:23:00 -04:00
*
2021-12-14 11:24:02 -05:00
* @ var mysqli | resource | false | null
2009-08-21 13:00:53 -04:00
*/
2012-08-08 02:28:57 -04:00
protected $dbh ;
2009-08-21 13:00:53 -04:00
2010-03-11 16:30:29 -05:00
/**
2020-05-02 13:25:07 -04:00
* A textual description of the last query / get_row / get_var call .
2010-03-11 16:30:29 -05:00
*
2010-12-01 14:24:38 -05:00
* @ since 3.0 . 0
2021-09-22 17:23:00 -04:00
*
2010-03-11 16:30:29 -05:00
* @ var string
*/
2014-05-19 11:36:13 -04:00
public $func_call ;
2010-03-11 16:30:29 -05:00
2011-10-25 01:29:28 -04:00
/**
* Whether MySQL is used as the database engine .
*
2020-05-03 07:45:06 -04:00
* Set in wpdb :: db_connect () to true , by default . This is used when checking
2011-10-25 01:29:28 -04:00
* against the required MySQL version for WordPress . Normally , a replacement
* database drop - in ( db . php ) will skip these checks , but setting this to true
* will force the checks to occur .
*
* @ since 3.3 . 0
2021-09-22 17:23:00 -04:00
*
2011-10-25 01:29:28 -04:00
* @ var bool
*/
public $is_mysql = null ;
2014-02-02 16:39:13 -05:00
/**
* A list of incompatible SQL modes .
*
* @ since 3.9 . 0
2021-09-22 17:23:00 -04:00
*
2021-12-14 11:24:02 -05:00
* @ var string []
2014-02-02 16:39:13 -05:00
*/
2017-11-30 18:11:00 -05:00
protected $incompatible_modes = array (
'NO_ZERO_DATE' ,
'ONLY_FULL_GROUP_BY' ,
'STRICT_TRANS_TABLES' ,
'STRICT_ALL_TABLES' ,
'TRADITIONAL' ,
2020-02-03 20:14:07 -05:00
'ANSI' ,
2017-11-30 18:11:00 -05:00
);
2014-02-02 16:39:13 -05:00
2014-02-24 19:40:13 -05:00
/**
2020-05-02 13:25:07 -04:00
* Whether to use mysqli over mysql . Default false .
2014-02-24 19:40:13 -05:00
*
* @ since 3.9 . 0
2021-09-22 17:23:00 -04:00
*
2014-02-24 19:40:13 -05:00
* @ var bool
*/
private $use_mysqli = false ;
2014-04-03 17:58:16 -04:00
/**
2020-05-02 13:25:07 -04:00
* Whether we ' ve managed to successfully connect at some point .
2014-04-03 17:58:16 -04:00
*
* @ since 3.9 . 0
2021-09-22 17:23:00 -04:00
*
2014-04-03 17:58:16 -04:00
* @ var bool
*/
private $has_connected = false ;
2021-09-22 17:23:00 -04:00
/**
* Time when the last query was performed .
*
* Only set when `SAVEQUERIES` is defined and truthy .
*
* @ since 1.5 . 0
*
* @ var float
*/
public $time_start = null ;
/**
* The last SQL error that was encountered .
*
* @ since 2.5 . 0
*
* @ var WP_Error | string
*/
public $error = null ;
2008-06-22 16:23:23 -04:00
/**
2020-05-02 13:25:07 -04:00
* Connects to the database server and selects a database .
2008-06-22 16:23:23 -04:00
*
2021-12-29 18:14:01 -05:00
* Does the actual setting up
2020-05-03 07:45:06 -04:00
* of the class properties and connection to the database .
2008-06-22 16:23:23 -04:00
*
* @ since 2.0 . 8
*
2020-05-03 07:45:06 -04:00
* @ link https :// core . trac . wordpress . org / ticket / 3354
2015-05-28 16:10:29 -04:00
*
2021-12-29 18:14:01 -05:00
* @ param string $dbuser Database user .
* @ param string $dbpassword Database password .
* @ param string $dbname Database name .
* @ param string $dbhost Database host .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function __construct ( $dbuser , $dbpassword , $dbname , $dbhost ) {
2017-11-30 18:11:00 -05:00
if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
2007-12-22 12:45:30 -05:00
$this -> show_errors ();
2017-11-30 18:11:00 -05:00
}
2007-12-22 12:45:30 -05:00
2021-12-29 18:14:01 -05:00
// Use the `mysqli` extension if it exists unless `WP_USE_EXT_MYSQL` is defined as true.
2014-02-25 10:53:13 -05:00
if ( function_exists ( 'mysqli_connect' ) ) {
2017-12-12 00:43:48 -05:00
$this -> use_mysqli = true ;
2014-04-07 18:27:15 -04:00
if ( defined ( 'WP_USE_EXT_MYSQL' ) ) {
$this -> use_mysqli = ! WP_USE_EXT_MYSQL ;
2014-02-25 10:53:13 -05:00
}
}
2014-02-24 19:40:13 -05:00
2017-11-30 18:11:00 -05:00
$this -> dbuser = $dbuser ;
2010-08-26 14:34:18 -04:00
$this -> dbpassword = $dbpassword ;
2017-11-30 18:11:00 -05:00
$this -> dbname = $dbname ;
$this -> dbhost = $dbhost ;
2010-08-26 14:34:18 -04:00
2014-07-03 15:57:14 -04:00
// wp-config.php creation will manually connect when ready.
if ( defined ( 'WP_SETUP_CONFIG' ) ) {
return ;
}
2010-08-26 14:34:18 -04:00
$this -> db_connect ();
}
2012-08-08 02:24:59 -04:00
/**
2016-05-27 13:12:29 -04:00
* Makes private properties readable for backward compatibility .
2012-08-08 02:24:59 -04:00
*
* @ since 3.5 . 0
*
2020-05-02 13:25:07 -04:00
* @ param string $name The private member to get , and optionally process .
2020-05-03 07:45:06 -04:00
* @ return mixed The private member .
2012-08-08 02:24:59 -04:00
*/
2014-05-19 11:36:13 -04:00
public function __get ( $name ) {
2017-11-30 18:11:00 -05:00
if ( 'col_info' === $name ) {
2012-08-08 02:24:59 -04:00
$this -> load_col_info ();
2017-11-30 18:11:00 -05:00
}
2012-08-08 02:24:59 -04:00
2012-08-14 16:26:04 -04:00
return $this -> $name ;
}
/**
2016-05-27 13:12:29 -04:00
* Makes private properties settable for backward compatibility .
2012-08-14 16:26:04 -04:00
*
* @ since 3.5 . 0
*
2020-05-02 13:25:07 -04:00
* @ param string $name The private member to set .
* @ param mixed $value The value to set .
2012-08-14 16:26:04 -04:00
*/
2014-05-19 11:36:13 -04:00
public function __set ( $name , $value ) {
2014-11-14 16:28:22 -05:00
$protected_members = array (
'col_meta' ,
'table_charset' ,
'check_current_query' ,
);
2017-11-30 18:11:00 -05:00
if ( in_array ( $name , $protected_members , true ) ) {
2014-11-14 16:28:22 -05:00
return ;
}
2012-08-14 16:26:04 -04:00
$this -> $name = $value ;
}
/**
2016-05-27 13:12:29 -04:00
* Makes private properties check - able for backward compatibility .
2012-08-14 16:26:04 -04:00
*
* @ since 3.5 . 0
*
2020-05-03 07:45:06 -04:00
* @ param string $name The private member to check .
* @ return bool If the member is set or not .
2012-08-14 16:26:04 -04:00
*/
2014-05-19 11:36:13 -04:00
public function __isset ( $name ) {
2012-08-14 16:26:04 -04:00
return isset ( $this -> $name );
}
/**
2016-05-27 13:12:29 -04:00
* Makes private properties un - settable for backward compatibility .
2012-08-14 16:26:04 -04:00
*
* @ since 3.5 . 0
*
* @ param string $name The private member to unset
*/
2014-05-19 11:36:13 -04:00
public function __unset ( $name ) {
2012-08-14 16:26:04 -04:00
unset ( $this -> $name );
2012-08-08 02:24:59 -04:00
}
2010-08-26 14:34:18 -04:00
/**
2020-05-03 07:45:06 -04:00
* Sets $this -> charset and $this -> collate .
2010-12-20 07:49:18 -05:00
*
* @ since 3.1 . 0
2010-08-26 14:34:18 -04:00
*/
2014-05-19 11:36:13 -04:00
public function init_charset () {
2016-08-30 03:14:41 -04:00
$charset = '' ;
$collate = '' ;
2017-11-30 18:11:00 -05:00
if ( function_exists ( 'is_multisite' ) && is_multisite () ) {
2016-05-31 22:38:29 -04:00
$charset = 'utf8' ;
2015-02-05 23:51:22 -05:00
if ( defined ( 'DB_COLLATE' ) && DB_COLLATE ) {
2016-05-31 22:38:29 -04:00
$collate = DB_COLLATE ;
2015-02-05 23:51:22 -05:00
} else {
2016-05-31 22:38:29 -04:00
$collate = 'utf8_general_ci' ;
2015-02-05 23:51:22 -05:00
}
2010-02-18 20:03:58 -05:00
} elseif ( defined ( 'DB_COLLATE' ) ) {
2016-05-31 22:38:29 -04:00
$collate = DB_COLLATE ;
2010-01-18 17:21:36 -05:00
}
2010-01-08 14:26:00 -05:00
2015-02-05 23:51:22 -05:00
if ( defined ( 'DB_CHARSET' ) ) {
2016-05-31 22:38:29 -04:00
$charset = DB_CHARSET ;
2015-02-05 23:51:22 -05:00
}
2016-05-31 22:38:29 -04:00
$charset_collate = $this -> determine_charset ( $charset , $collate );
$this -> charset = $charset_collate [ 'charset' ];
$this -> collate = $charset_collate [ 'collate' ];
}
/**
2016-07-20 15:33:30 -04:00
* Determines the best charset and collation to use given a charset and collation .
2016-05-31 22:38:29 -04:00
*
* For example , when able , utf8mb4 should be used instead of utf8 .
*
* @ since 4.6 . 0
*
2016-07-20 15:33:30 -04:00
* @ param string $charset The character set to check .
* @ param string $collate The collation to check .
2019-11-05 16:23:02 -05:00
* @ return array {
* The most appropriate character set and collation to use .
*
* @ type string $charset Character set .
* @ type string $collate Collation .
* }
2016-05-31 22:38:29 -04:00
*/
public function determine_charset ( $charset , $collate ) {
2015-07-02 23:26:24 -04:00
if ( ( $this -> use_mysqli && ! ( $this -> dbh instanceof mysqli ) ) || empty ( $this -> dbh ) ) {
2016-05-31 22:38:29 -04:00
return compact ( 'charset' , 'collate' );
2015-02-05 23:51:22 -05:00
}
2016-05-31 22:38:29 -04:00
if ( 'utf8' === $charset && $this -> has_cap ( 'utf8mb4' ) ) {
$charset = 'utf8mb4' ;
2015-02-05 23:51:22 -05:00
}
2016-09-08 19:49:30 -04:00
if ( 'utf8mb4' === $charset && ! $this -> has_cap ( 'utf8mb4' ) ) {
$charset = 'utf8' ;
$collate = str_replace ( 'utf8mb4_' , 'utf8_' , $collate );
}
2016-05-31 22:38:29 -04:00
if ( 'utf8mb4' === $charset ) {
2016-05-23 01:54:27 -04:00
// _general_ is outdated, so we can upgrade it to _unicode_, instead.
2016-05-31 22:38:29 -04:00
if ( ! $collate || 'utf8_general_ci' === $collate ) {
$collate = 'utf8mb4_unicode_ci' ;
2016-05-23 01:54:27 -04:00
} else {
2016-05-31 22:38:29 -04:00
$collate = str_replace ( 'utf8_' , 'utf8mb4_' , $collate );
2016-05-23 01:54:27 -04:00
}
2015-02-05 23:51:22 -05:00
}
2016-05-23 02:32:29 -04:00
// _unicode_520_ is a better collation, we should use that when it's available.
2016-05-31 22:38:29 -04:00
if ( $this -> has_cap ( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) {
$collate = 'utf8mb4_unicode_520_ci' ;
2016-05-23 02:32:29 -04:00
}
2016-05-31 22:38:29 -04:00
return compact ( 'charset' , 'collate' );
2010-08-26 14:34:18 -04:00
}
2007-02-01 19:04:35 -05:00
2010-08-26 14:34:18 -04:00
/**
* Sets the connection ' s character set .
2010-10-19 03:48:22 -04:00
*
2010-12-20 07:49:18 -05:00
* @ since 3.1 . 0
*
2021-12-14 11:24:02 -05:00
* @ param mysqli | resource $dbh The connection returned by `mysqli_connect()` or `mysql_connect()` .
* @ param string $charset Optional . The character set . Default null .
* @ param string $collate Optional . The collation . Default null .
2010-08-26 14:34:18 -04:00
*/
2014-05-19 11:36:13 -04:00
public function set_charset ( $dbh , $charset = null , $collate = null ) {
2017-11-30 18:11:00 -05:00
if ( ! isset ( $charset ) ) {
2010-08-26 14:34:18 -04:00
$charset = $this -> charset ;
2017-11-30 18:11:00 -05:00
}
if ( ! isset ( $collate ) ) {
2010-08-26 14:34:18 -04:00
$collate = $this -> collate ;
2017-11-30 18:11:00 -05:00
}
2013-04-28 20:31:56 -04:00
if ( $this -> has_cap ( 'collation' ) && ! empty ( $charset ) ) {
2016-08-30 03:38:32 -04:00
$set_charset_succeeded = true ;
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
if ( function_exists ( 'mysqli_set_charset' ) && $this -> has_cap ( 'set_charset' ) ) {
2016-08-30 03:38:32 -04:00
$set_charset_succeeded = mysqli_set_charset ( $dbh , $charset );
}
if ( $set_charset_succeeded ) {
$query = $this -> prepare ( 'SET NAMES %s' , $charset );
2017-11-30 18:11:00 -05:00
if ( ! empty ( $collate ) ) {
2016-08-30 03:38:32 -04:00
$query .= $this -> prepare ( ' COLLATE %s' , $collate );
2017-11-30 18:11:00 -05:00
}
2016-08-30 03:38:32 -04:00
mysqli_query ( $dbh , $query );
2014-02-24 19:40:13 -05:00
}
2009-12-15 16:01:57 -05:00
} else {
2014-02-24 19:40:13 -05:00
if ( function_exists ( 'mysql_set_charset' ) && $this -> has_cap ( 'set_charset' ) ) {
2016-08-30 03:38:32 -04:00
$set_charset_succeeded = mysql_set_charset ( $charset , $dbh );
}
if ( $set_charset_succeeded ) {
$query = $this -> prepare ( 'SET NAMES %s' , $charset );
2017-11-30 18:11:00 -05:00
if ( ! empty ( $collate ) ) {
2016-08-30 03:38:32 -04:00
$query .= $this -> prepare ( ' COLLATE %s' , $collate );
2017-11-30 18:11:00 -05:00
}
2016-08-30 03:38:32 -04:00
mysql_query ( $query , $dbh );
2014-02-24 19:40:13 -05:00
}
2008-03-30 13:56:27 -04:00
}
}
2007-01-06 18:36:51 -05:00
}
2014-02-02 16:39:13 -05:00
/**
2020-05-03 07:45:06 -04:00
* Changes the current SQL mode , and ensures its WordPress compatibility .
2014-02-02 16:39:13 -05:00
*
2020-05-02 13:25:07 -04:00
* If no modes are passed , it will ensure the current MySQL server modes are compatible .
2014-02-02 16:39:13 -05:00
*
* @ since 3.9 . 0
*
2020-05-03 07:45:06 -04:00
* @ param array $modes Optional . A list of SQL modes to set . Default empty array .
2014-02-02 16:39:13 -05:00
*/
2014-05-19 11:36:13 -04:00
public function set_sql_mode ( $modes = array () ) {
2014-02-02 16:39:13 -05:00
if ( empty ( $modes ) ) {
2014-11-27 06:00:23 -05:00
if ( $this -> use_mysqli ) {
$res = mysqli_query ( $this -> dbh , 'SELECT @@SESSION.sql_mode' );
2014-02-24 19:40:13 -05:00
} else {
2014-11-27 06:00:23 -05:00
$res = mysql_query ( 'SELECT @@SESSION.sql_mode' , $this -> dbh );
}
if ( empty ( $res ) ) {
return ;
2014-02-24 19:40:13 -05:00
}
2014-11-27 06:00:23 -05:00
if ( $this -> use_mysqli ) {
$modes_array = mysqli_fetch_array ( $res );
if ( empty ( $modes_array [ 0 ] ) ) {
return ;
}
$modes_str = $modes_array [ 0 ];
} else {
$modes_str = mysql_result ( $res , 0 );
}
if ( empty ( $modes_str ) ) {
return ;
}
$modes = explode ( ',' , $modes_str );
2014-02-02 16:39:13 -05:00
}
$modes = array_change_key_case ( $modes , CASE_UPPER );
/**
2016-05-22 14:50:28 -04:00
* Filters the list of incompatible SQL modes to exclude .
2014-02-02 16:39:13 -05:00
*
* @ since 3.9 . 0
*
2014-02-09 15:51:13 -05:00
* @ param array $incompatible_modes An array of incompatible modes .
2014-02-02 16:39:13 -05:00
*/
$incompatible_modes = ( array ) apply_filters ( 'incompatible_sql_modes' , $this -> incompatible_modes );
2015-08-25 16:28:22 -04:00
foreach ( $modes as $i => $mode ) {
2020-04-04 23:02:11 -04:00
if ( in_array ( $mode , $incompatible_modes , true ) ) {
2014-11-27 06:00:23 -05:00
unset ( $modes [ $i ] );
2014-02-02 16:39:13 -05:00
}
}
$modes_str = implode ( ',' , $modes );
2014-11-27 06:00:23 -05:00
if ( $this -> use_mysqli ) {
mysqli_query ( $this -> dbh , " SET SESSION sql_mode=' $modes_str ' " );
} else {
mysql_query ( " SET SESSION sql_mode=' $modes_str ' " , $this -> dbh );
2014-02-24 19:40:13 -05:00
}
2014-02-02 16:39:13 -05:00
}
2008-06-22 16:23:23 -04:00
/**
* Sets the table prefix for the WordPress tables .
*
* @ since 2.5 . 0
*
2015-05-28 16:10:29 -04:00
* @ param string $prefix Alphanumeric name for the new prefix .
2020-05-03 07:45:06 -04:00
* @ param bool $set_table_names Optional . Whether the table names , e . g . wpdb :: $posts ,
* should be updated or not . Default true .
2020-05-02 13:25:07 -04:00
* @ return string | WP_Error Old prefix or WP_Error on error .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function set_prefix ( $prefix , $set_table_names = true ) {
2007-10-30 23:59:18 -04:00
2017-11-30 18:11:00 -05:00
if ( preg_match ( '|[^a-z0-9_]|i' , $prefix ) ) {
return new WP_Error ( 'invalid_db_prefix' , 'Invalid database prefix' );
}
2007-10-30 23:59:18 -04:00
2010-02-22 17:53:54 -05:00
$old_prefix = is_multisite () ? '' : $prefix ;
2010-01-18 17:21:36 -05:00
2017-11-30 18:11:00 -05:00
if ( isset ( $this -> base_prefix ) ) {
2010-01-08 14:26:00 -05:00
$old_prefix = $this -> base_prefix ;
2017-11-30 18:11:00 -05:00
}
2010-02-23 17:04:29 -05:00
2010-01-08 14:26:00 -05:00
$this -> base_prefix = $prefix ;
2010-02-23 17:04:29 -05:00
2010-02-24 16:30:13 -05:00
if ( $set_table_names ) {
2017-11-30 18:11:00 -05:00
foreach ( $this -> tables ( 'global' ) as $table => $prefixed_table ) {
2010-02-24 16:30:13 -05:00
$this -> $table = $prefixed_table ;
2017-11-30 18:11:00 -05:00
}
2010-01-08 14:26:00 -05:00
2017-11-30 18:11:00 -05:00
if ( is_multisite () && empty ( $this -> blogid ) ) {
2010-02-24 16:30:13 -05:00
return $old_prefix ;
2017-11-30 18:11:00 -05:00
}
2007-10-30 23:59:18 -04:00
2010-04-10 08:39:25 -04:00
$this -> prefix = $this -> get_blog_prefix ();
2010-02-19 14:57:03 -05:00
2017-11-30 18:11:00 -05:00
foreach ( $this -> tables ( 'blog' ) as $table => $prefixed_table ) {
2010-02-24 16:30:13 -05:00
$this -> $table = $prefixed_table ;
2017-11-30 18:11:00 -05:00
}
2007-10-30 23:59:18 -04:00
2017-11-30 18:11:00 -05:00
foreach ( $this -> tables ( 'old' ) as $table => $prefixed_table ) {
2010-02-24 16:30:13 -05:00
$this -> $table = $prefixed_table ;
2017-11-30 18:11:00 -05:00
}
2010-02-24 16:30:13 -05:00
}
2007-10-30 23:59:18 -04:00
return $old_prefix ;
}
2010-02-22 17:53:54 -05:00
/**
2020-06-28 10:09:04 -04:00
* Sets blog ID .
2010-02-22 17:53:54 -05:00
*
* @ since 3.0 . 0
2015-05-28 16:10:29 -04:00
*
2010-02-23 17:04:29 -05:00
* @ param int $blog_id
2017-08-12 08:48:47 -04:00
* @ param int $network_id Optional .
2020-06-28 10:09:04 -04:00
* @ return int Previous blog ID .
2010-02-23 04:06:50 -05:00
*/
2017-08-12 08:48:47 -04:00
public function set_blog_id ( $blog_id , $network_id = 0 ) {
if ( ! empty ( $network_id ) ) {
$this -> siteid = $network_id ;
}
2010-01-08 14:26:00 -05:00
2010-02-22 17:53:54 -05:00
$old_blog_id = $this -> blogid ;
2010-01-08 14:26:00 -05:00
$this -> blogid = $blog_id ;
2010-04-10 08:39:25 -04:00
$this -> prefix = $this -> get_blog_prefix ();
2010-01-08 14:26:00 -05:00
2017-11-30 18:11:00 -05:00
foreach ( $this -> tables ( 'blog' ) as $table => $prefixed_table ) {
2010-02-23 04:06:50 -05:00
$this -> $table = $prefixed_table ;
2017-11-30 18:11:00 -05:00
}
2010-02-19 14:57:03 -05:00
2017-11-30 18:11:00 -05:00
foreach ( $this -> tables ( 'old' ) as $table => $prefixed_table ) {
2010-02-23 04:06:50 -05:00
$this -> $table = $prefixed_table ;
2017-11-30 18:11:00 -05:00
}
2010-01-08 14:26:00 -05:00
return $old_blog_id ;
}
2010-02-22 17:53:54 -05:00
/**
* Gets blog prefix .
*
* @ since 3.0 . 0
2020-05-03 07:45:06 -04:00
*
2010-02-23 17:04:29 -05:00
* @ param int $blog_id Optional .
2010-02-22 17:53:54 -05:00
* @ return string Blog prefix .
*/
2014-05-19 11:36:13 -04:00
public function get_blog_prefix ( $blog_id = null ) {
2010-04-02 18:47:51 -04:00
if ( is_multisite () ) {
2017-11-30 18:11:00 -05:00
if ( null === $blog_id ) {
2010-04-02 18:47:51 -04:00
$blog_id = $this -> blogid ;
2017-11-30 18:11:00 -05:00
}
2020-07-18 07:39:04 -04:00
2011-06-27 16:47:04 -04:00
$blog_id = ( int ) $blog_id ;
2020-07-18 07:39:04 -04:00
if ( defined ( 'MULTISITE' ) && ( 0 === $blog_id || 1 === $blog_id ) ) {
2010-02-13 14:57:43 -05:00
return $this -> base_prefix ;
2017-11-30 18:11:00 -05:00
} else {
2010-02-13 14:57:43 -05:00
return $this -> base_prefix . $blog_id . '_' ;
2017-11-30 18:11:00 -05:00
}
2010-01-08 14:26:00 -05:00
} else {
return $this -> base_prefix ;
}
}
2010-02-19 14:57:03 -05:00
/**
* Returns an array of WordPress tables .
*
2021-12-29 18:14:01 -05:00
* Also allows for the `CUSTOM_USER_TABLE` and `CUSTOM_USER_META_TABLE` to override the WordPress users
2020-05-03 07:45:06 -04:00
* and usermeta tables that would otherwise be determined by the prefix .
2010-02-23 04:06:50 -05:00
*
2021-12-29 18:14:01 -05:00
* The `$scope` argument can take one of the following :
2010-02-24 13:58:52 -05:00
*
2021-12-29 18:14:01 -05:00
* - 'all' - returns 'all' and 'global' tables . No old tables are returned .
* - 'blog' - returns the blog - level tables for the queried blog .
* - 'global' - returns the global tables for the installation , returning multisite tables only on multisite .
* - 'ms_global' - returns the multisite global tables , regardless if current installation is multisite .
* - 'old' - returns tables which are deprecated .
2010-02-24 13:58:52 -05:00
*
2010-02-19 14:57:03 -05:00
* @ since 3.0 . 0
2020-05-03 07:45:06 -04:00
*
2010-02-23 17:04:29 -05:00
* @ uses wpdb :: $tables
* @ uses wpdb :: $old_tables
* @ uses wpdb :: $global_tables
* @ uses wpdb :: $ms_global_tables
2010-02-19 14:57:03 -05:00
*
2020-05-03 07:45:06 -04:00
* @ param string $scope Optional . Possible values include 'all' , 'global' , 'ms_global' , 'blog' ,
* or 'old' tables . Default 'all' .
* @ param bool $prefix Optional . Whether to include table prefixes . If blog prefix is requested ,
* then the custom users and usermeta tables will be mapped . Default true .
* @ param int $blog_id Optional . The blog_id to prefix . Used only when prefix is requested .
2021-12-29 18:14:01 -05:00
* Defaults to `wpdb::$blogid` .
2021-07-01 17:02:57 -04:00
* @ return string [] Table names . When a prefix is requested , the key is the unprefixed table name .
2010-02-23 04:06:50 -05:00
*/
2014-05-19 11:36:13 -04:00
public function tables ( $scope = 'all' , $prefix = true , $blog_id = 0 ) {
2010-02-19 14:57:03 -05:00
switch ( $scope ) {
2017-11-30 18:11:00 -05:00
case 'all' :
2010-02-24 13:58:52 -05:00
$tables = array_merge ( $this -> global_tables , $this -> tables );
2017-11-30 18:11:00 -05:00
if ( is_multisite () ) {
2010-02-24 13:58:52 -05:00
$tables = array_merge ( $tables , $this -> ms_global_tables );
2017-11-30 18:11:00 -05:00
}
2010-02-23 14:58:53 -05:00
break ;
2017-11-30 18:11:00 -05:00
case 'blog' :
2010-02-26 01:14:26 -05:00
$tables = $this -> tables ;
break ;
2017-11-30 18:11:00 -05:00
case 'global' :
2010-02-23 04:06:50 -05:00
$tables = $this -> global_tables ;
2017-11-30 18:11:00 -05:00
if ( is_multisite () ) {
2010-02-23 04:06:50 -05:00
$tables = array_merge ( $tables , $this -> ms_global_tables );
2017-11-30 18:11:00 -05:00
}
2010-02-19 14:57:03 -05:00
break ;
2017-11-30 18:11:00 -05:00
case 'ms_global' :
2010-02-24 13:58:52 -05:00
$tables = $this -> ms_global_tables ;
break ;
2017-11-30 18:11:00 -05:00
case 'old' :
2010-02-24 13:58:52 -05:00
$tables = $this -> old_tables ;
break ;
2017-11-30 18:11:00 -05:00
default :
2010-02-24 13:58:52 -05:00
return array ();
2010-02-19 14:57:03 -05:00
}
if ( $prefix ) {
2017-11-30 18:11:00 -05:00
if ( ! $blog_id ) {
2010-02-23 15:38:41 -05:00
$blog_id = $this -> blogid ;
2017-11-30 18:11:00 -05:00
}
$blog_prefix = $this -> get_blog_prefix ( $blog_id );
$base_prefix = $this -> base_prefix ;
2010-02-23 04:06:50 -05:00
$global_tables = array_merge ( $this -> global_tables , $this -> ms_global_tables );
2010-02-20 05:50:57 -05:00
foreach ( $tables as $k => $table ) {
2020-04-04 23:02:11 -04:00
if ( in_array ( $table , $global_tables , true ) ) {
2010-02-23 04:06:50 -05:00
$tables [ $table ] = $base_prefix . $table ;
2017-11-30 18:11:00 -05:00
} else {
2010-02-23 17:04:29 -05:00
$tables [ $table ] = $blog_prefix . $table ;
2017-11-30 18:11:00 -05:00
}
2010-02-23 04:06:50 -05:00
unset ( $tables [ $k ] );
2010-02-19 14:57:03 -05:00
}
2010-02-23 04:06:50 -05:00
2017-11-30 18:11:00 -05:00
if ( isset ( $tables [ 'users' ] ) && defined ( 'CUSTOM_USER_TABLE' ) ) {
2010-02-23 04:06:50 -05:00
$tables [ 'users' ] = CUSTOM_USER_TABLE ;
2017-11-30 18:11:00 -05:00
}
2010-02-23 04:06:50 -05:00
2017-11-30 18:11:00 -05:00
if ( isset ( $tables [ 'usermeta' ] ) && defined ( 'CUSTOM_USER_META_TABLE' ) ) {
2010-02-23 04:06:50 -05:00
$tables [ 'usermeta' ] = CUSTOM_USER_META_TABLE ;
2017-11-30 18:11:00 -05:00
}
2010-02-19 14:57:03 -05:00
}
return $tables ;
}
2006-11-29 20:44:33 -05:00
/**
2021-12-14 11:24:02 -05:00
* Selects a database using the current or provided database connection .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* The database name will be changed based on the current database connection .
* On failure , the execution will bail and display a DB error .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2021-12-29 18:14:01 -05:00
* @ param string $db Database name .
2021-12-14 11:24:02 -05:00
* @ param mysqli | resource $dbh Optional database connection .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function select ( $db , $dbh = null ) {
2017-11-30 18:11:00 -05:00
if ( is_null ( $dbh ) ) {
2010-06-16 15:28:01 -04:00
$dbh = $this -> dbh ;
2017-11-30 18:11:00 -05:00
}
2010-06-16 15:28:01 -04:00
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
2015-12-10 22:40:26 -05:00
$success = mysqli_select_db ( $dbh , $db );
2014-02-24 19:40:13 -05:00
} else {
2015-12-10 22:40:26 -05:00
$success = mysql_select_db ( $db , $dbh );
2014-02-24 19:40:13 -05:00
}
if ( ! $success ) {
2007-12-22 01:24:48 -05:00
$this -> ready = false ;
2014-02-25 23:54:14 -05:00
if ( ! did_action ( 'template_redirect' ) ) {
wp_load_translations_early ();
2015-10-08 12:52:24 -04:00
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
$message = '<h1>' . __ ( 'Cannot select database' ) . " </h1> \n " ;
2015-10-08 12:52:24 -04:00
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Database name. */
Administration: Remove self-reference ("we") in WordPress Admin.
This changes updates many strings to remove self-references to an undefined "we" collective across the WordPress Administration.
The goal of this change is to better match the guidelines and recommendations set forth in the make/core handbook, specifically:
> the word "we" should be avoided (…) unless its made very clear which group is speaking.
Props johnbillion, shital-patel, audrasjb, marybaum, SergeyBiryukov, peterwilsoncc, johnjamesjacoby, kebbet, costdev, chaion07, davidbaumwald.
Fixes #46057.
Built from https://develop.svn.wordpress.org/trunk@53131
git-svn-id: http://core.svn.wordpress.org/trunk@52720 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-11 07:42:04 -04:00
__ ( 'The database server could be connected to (which means your username and password is okay) but the %s database could not be selected.' ),
2015-10-08 12:52:24 -04:00
'<code>' . htmlspecialchars ( $db , ENT_QUOTES ) . '</code>'
) . " </p> \n " ;
$message .= " <ul> \n " ;
$message .= '<li>' . __ ( 'Are you sure it exists?' ) . " </li> \n " ;
$message .= '<li>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: 1: Database user, 2: Database name. */
2015-10-08 12:52:24 -04:00
__ ( 'Does the user %1$s have permission to use the %2$s database?' ),
2017-11-30 18:11:00 -05:00
'<code>' . htmlspecialchars ( $this -> dbuser , ENT_QUOTES ) . '</code>' ,
2015-10-08 12:52:24 -04:00
'<code>' . htmlspecialchars ( $db , ENT_QUOTES ) . '</code>'
) . " </li> \n " ;
$message .= '<li>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Database name. */
2015-10-08 12:52:24 -04:00
__ ( '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?' ),
htmlspecialchars ( $db , ENT_QUOTES )
2017-11-30 18:11:00 -05:00
) . " </li> \n " ;
2015-10-08 12:52:24 -04:00
$message .= " </ul> \n " ;
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Support forums URL. */
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
__ ( 'If you do not 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>.' ),
2019-04-08 18:59:56 -04:00
__ ( 'https://wordpress.org/support/forums/' )
2015-10-08 12:52:24 -04:00
) . " </p> \n " ;
$this -> bail ( $message , 'db_select_fail' );
2014-02-25 23:54:14 -05:00
}
2003-05-28 04:41:04 -04:00
}
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2010-02-22 17:53:54 -05:00
/**
2013-07-16 13:44:42 -04:00
* Do not use , deprecated .
*
* Use esc_sql () or wpdb :: prepare () instead .
2010-02-22 17:53:54 -05:00
*
2010-02-26 01:14:26 -05:00
* @ since 2.8 . 0
2015-08-20 18:42:25 -04:00
* @ deprecated 3.6 . 0 Use wpdb :: prepare ()
2020-05-03 07:45:06 -04:00
* @ see wpdb :: prepare ()
2013-07-16 13:44:42 -04:00
* @ see esc_sql ()
2010-02-22 17:53:54 -05:00
*
2010-02-26 01:14:26 -05:00
* @ param string $string
2010-02-22 17:53:54 -05:00
* @ return string
*/
2021-10-18 13:52:58 -04:00
public function _weak_escape ( $string ) {
2017-11-30 18:11:00 -05:00
if ( func_num_args () === 1 && function_exists ( '_deprecated_function' ) ) {
2016-07-06 08:40:29 -04:00
_deprecated_function ( __METHOD__ , '3.6.0' , 'wpdb::prepare() or esc_sql()' );
2017-11-30 18:11:00 -05:00
}
2010-02-22 17:53:54 -05:00
return addslashes ( $string );
2009-02-20 14:05:12 -05:00
}
2010-02-22 17:53:54 -05:00
/**
2020-05-03 07:45:06 -04:00
* Real escape , using mysqli_real_escape_string () or mysql_real_escape_string () .
*
* @ since 2.8 . 0
2010-02-22 17:53:54 -05:00
*
2014-02-24 19:40:13 -05:00
* @ see mysqli_real_escape_string ()
2010-02-24 13:58:52 -05:00
* @ see mysql_real_escape_string ()
2010-02-22 17:53:54 -05:00
*
2020-05-02 13:25:07 -04:00
* @ param string $string String to escape .
* @ return string Escaped string .
2010-02-22 17:53:54 -05:00
*/
2021-10-18 13:52:58 -04:00
public function _real_escape ( $string ) {
2021-09-10 12:36:57 -04:00
if ( ! is_scalar ( $string ) ) {
2020-09-15 21:48:10 -04:00
return '' ;
}
2014-02-24 19:40:13 -05:00
if ( $this -> dbh ) {
if ( $this -> use_mysqli ) {
2017-10-31 08:00:49 -04:00
$escaped = mysqli_real_escape_string ( $this -> dbh , $string );
2014-02-24 19:40:13 -05:00
} else {
2017-10-31 08:00:49 -04:00
$escaped = mysql_real_escape_string ( $string , $this -> dbh );
2014-02-24 19:40:13 -05:00
}
2014-10-05 17:06:15 -04:00
} else {
2017-10-31 08:00:49 -04:00
$class = get_class ( $this );
2021-11-16 23:05:02 -05:00
wp_load_translations_early ();
/* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */
_doing_it_wrong ( $class , sprintf ( __ ( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' );
2017-10-31 08:00:49 -04:00
$escaped = addslashes ( $string );
2014-10-05 17:06:15 -04:00
}
2017-10-31 08:00:49 -04:00
return $this -> add_placeholder_escape ( $escaped );
2009-02-20 14:05:12 -05:00
}
2010-02-22 17:53:54 -05:00
/**
2020-05-03 07:45:06 -04:00
* Escapes data . Works on arrays .
*
* @ since 2.8 . 0
2010-02-22 17:53:54 -05:00
*
2010-06-29 20:05:18 -04:00
* @ uses wpdb :: _real_escape ()
2010-02-22 17:53:54 -05:00
*
2020-05-02 13:25:07 -04:00
* @ param string | array $data Data to escape .
* @ return string | array Escaped data , in the same type as supplied .
2010-02-22 17:53:54 -05:00
*/
2016-08-22 17:10:28 -04:00
public function _escape ( $data ) {
2010-02-22 17:53:54 -05:00
if ( is_array ( $data ) ) {
2013-07-16 13:44:42 -04:00
foreach ( $data as $k => $v ) {
2016-08-22 17:10:28 -04:00
if ( is_array ( $v ) ) {
2017-11-30 18:11:00 -05:00
$data [ $k ] = $this -> _escape ( $v );
2016-08-22 17:10:28 -04:00
} else {
2017-11-30 18:11:00 -05:00
$data [ $k ] = $this -> _real_escape ( $v );
2016-08-22 17:10:28 -04:00
}
2009-02-20 14:05:12 -05:00
}
} else {
$data = $this -> _real_escape ( $data );
}
return $data ;
}
2006-11-29 20:44:33 -05:00
/**
2013-07-16 13:44:42 -04:00
* Do not use , deprecated .
2006-11-29 20:44:33 -05:00
*
2013-07-16 13:44:42 -04:00
* Use esc_sql () or wpdb :: prepare () instead .
2008-06-22 16:23:23 -04:00
*
2010-02-23 17:04:29 -05:00
* @ since 0.71
2015-08-20 18:42:25 -04:00
* @ deprecated 3.6 . 0 Use wpdb :: prepare ()
2013-07-16 13:44:42 -04:00
* @ see wpdb :: prepare ()
* @ see esc_sql ()
*
2020-05-03 07:45:06 -04:00
* @ param string | array $data Data to escape .
* @ return string | array Escaped data , in the same type as supplied .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function escape ( $data ) {
2017-11-30 18:11:00 -05:00
if ( func_num_args () === 1 && function_exists ( '_deprecated_function' ) ) {
2016-07-06 08:40:29 -04:00
_deprecated_function ( __METHOD__ , '3.6.0' , 'wpdb::prepare() or esc_sql()' );
2017-11-30 18:11:00 -05:00
}
2010-02-22 17:53:54 -05:00
if ( is_array ( $data ) ) {
2013-07-16 13:44:42 -04:00
foreach ( $data as $k => $v ) {
2017-11-30 18:11:00 -05:00
if ( is_array ( $v ) ) {
$data [ $k ] = $this -> escape ( $v , 'recursive' );
} else {
$data [ $k ] = $this -> _weak_escape ( $v , 'internal' );
}
2009-02-20 14:05:12 -05:00
}
} else {
2013-07-16 13:44:42 -04:00
$data = $this -> _weak_escape ( $data , 'internal' );
2009-02-20 14:05:12 -05:00
}
return $data ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2007-07-04 12:18:57 -04:00
/**
2020-05-02 13:25:07 -04:00
* Escapes content by reference for insertion into the database , for security .
2008-06-22 16:23:23 -04:00
*
2010-02-23 17:04:29 -05:00
* @ uses wpdb :: _real_escape ()
2015-05-28 16:10:29 -04:00
*
2008-06-22 16:23:23 -04:00
* @ since 2.3 . 0
2015-05-28 16:10:29 -04:00
*
2020-05-02 13:25:07 -04:00
* @ param string $string String to escape .
2007-07-04 12:18:57 -04:00
*/
2014-05-19 11:36:13 -04:00
public function escape_by_ref ( & $string ) {
2017-11-30 18:11:00 -05:00
if ( ! is_float ( $string ) ) {
2012-06-28 15:57:31 -04:00
$string = $this -> _real_escape ( $string );
2017-11-30 18:11:00 -05:00
}
2007-07-04 12:18:57 -04:00
}
/**
2020-05-02 13:25:07 -04:00
* Prepares a SQL query for safe execution .
2008-06-22 16:23:23 -04:00
*
2020-05-02 13:25:07 -04:00
* Uses sprintf () - like syntax . The following placeholders can be used in the query string :
2021-12-29 18:14:01 -05:00
*
* - % d ( integer )
* - % f ( float )
* - % s ( string )
2010-02-22 17:53:54 -05:00
*
2020-05-03 07:45:06 -04:00
* All placeholders MUST be left unquoted in the query string . A corresponding argument
* MUST be passed for each placeholder .
2010-02-22 17:53:54 -05:00
*
2020-05-03 07:45:06 -04:00
* Note : There is one exception to the above : for compatibility with old behavior ,
2021-12-29 18:14:01 -05:00
* numbered or formatted string placeholders ( eg , `%1$s` , `%5s` ) will not have quotes
2020-05-03 07:45:06 -04:00
* added by this function , so should be passed with appropriate quotes around them .
2017-10-31 08:00:49 -04:00
*
2021-12-29 18:14:01 -05:00
* Literal percentage signs ( `%` ) in the query string must be written as `%%` . Percentage wildcards
2020-05-03 07:45:06 -04:00
* ( for example , to use in LIKE syntax ) must be passed via a substitution argument containing
* the complete LIKE string , these cannot be inserted directly in the query string .
* Also see wpdb :: esc_like () .
2009-04-10 17:57:40 -04:00
*
2020-05-03 07:45:06 -04:00
* Arguments may be passed as individual arguments to the method , or as a single array
* containing all arguments . A combination of the two is not supported .
2009-04-10 17:57:40 -04:00
*
2017-09-28 07:45:45 -04:00
* Examples :
2021-12-29 18:14:01 -05:00
*
2017-09-28 07:45:45 -04:00
* $wpdb -> prepare ( " SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s " , array ( 'foo' , 1337 , '%bar' ) );
2016-08-20 09:17:30 -04:00
* $wpdb -> prepare ( " SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s " , 'foo' );
2009-04-10 17:57:40 -04:00
*
2008-06-22 16:23:23 -04:00
* @ since 2.3 . 0
2019-10-09 00:28:02 -04:00
* @ since 5.3 . 0 Formalized the existing and already documented `...$args` parameter
* by updating the function signature . The second parameter was changed
* from `$args` to `...$args` .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* @ link https :// www . php . net / sprintf Description of syntax .
*
2020-05-02 13:25:07 -04:00
* @ param string $query Query statement with sprintf () - like placeholders .
2020-05-03 07:45:06 -04:00
* @ param array | mixed $args The array of variables to substitute into the query ' s placeholders
* if being called with an array of arguments , or the first variable
* to substitute into the query ' s placeholders if being called with
2019-07-11 20:17:57 -04:00
* individual arguments .
2020-05-03 07:45:06 -04:00
* @ param mixed ... $args Further variables to substitute into the query ' s placeholders
* if being called with individual arguments .
2015-05-28 16:10:29 -04:00
* @ return string | void Sanitized query string , if there is a query to prepare .
2010-02-22 17:53:54 -05:00
*/
2019-07-11 20:17:57 -04:00
public function prepare ( $query , ... $args ) {
2017-10-31 08:00:49 -04:00
if ( is_null ( $query ) ) {
2007-07-04 12:18:57 -04:00
return ;
2017-10-31 08:00:49 -04:00
}
2010-02-23 17:04:29 -05:00
2014-02-02 16:48:13 -05:00
// This is not meant to be foolproof -- but it will catch obviously incorrect usage.
2014-02-02 16:47:11 -05:00
if ( strpos ( $query , '%' ) === false ) {
2017-10-01 22:45:47 -04:00
wp_load_translations_early ();
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
_doing_it_wrong (
'wpdb::prepare' ,
sprintf (
/* translators: %s: wpdb::prepare() */
__ ( 'The query argument of %s must have a placeholder.' ),
'wpdb::prepare()'
),
'3.9.0'
);
2014-02-02 16:47:11 -05:00
}
2017-10-31 08:00:49 -04:00
// If args were passed as an array (as in vsprintf), move them up.
$passed_as_array = false ;
2021-11-18 09:37:01 -05:00
if ( isset ( $args [ 0 ] ) && is_array ( $args [ 0 ] ) && 1 === count ( $args ) ) {
2017-10-31 08:00:49 -04:00
$passed_as_array = true ;
2017-11-30 18:11:00 -05:00
$args = $args [ 0 ];
2017-09-19 10:48:47 -04:00
}
foreach ( $args as $arg ) {
2017-09-19 12:06:45 -04:00
if ( ! is_scalar ( $arg ) && ! is_null ( $arg ) ) {
2017-10-01 22:45:47 -04:00
wp_load_translations_early ();
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
_doing_it_wrong (
'wpdb::prepare' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Value type. */
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
__ ( 'Unsupported value type (%s).' ),
gettype ( $arg )
),
'4.8.2'
);
2017-09-19 10:48:47 -04:00
}
}
2017-10-31 08:00:49 -04:00
/*
* Specify the formatting allowed in a placeholder . The following are allowed :
*
* - Sign specifier . eg , $ + d
* - Numbered placeholders . eg , % 1 $s
* - Padding specifier , including custom padding characters . eg , % 05 s , % ' #5s
* - Alignment specifier . eg , % 05 - s
* - Precision specifier . eg , %. 2 f
*/
$allowed_format = '(?:[1-9][0-9]*[$])?[-+0-9]*(?: |0|\'.)?[-+0-9]*(?:\.[0-9]+)?' ;
2017-10-01 22:11:47 -04:00
2017-10-31 08:00:49 -04:00
/*
* If a % s placeholder already has quotes around it , removing the existing quotes and re - inserting them
* ensures the quotes are consistent .
*
2019-04-17 09:26:51 -04:00
* For backward compatibility , this is only applied to % s , and not to placeholders like % 1 $s , which are frequently
2017-10-31 08:00:49 -04:00
* used in the middle of longer strings , or as table name placeholders .
*/
$query = str_replace ( " '%s' " , '%s' , $query ); // Strip any existing single quotes.
$query = str_replace ( '"%s"' , '%s' , $query ); // Strip any existing double quotes.
$query = preg_replace ( '/(?<!%)%s/' , " '%s' " , $query ); // Quote the strings, avoiding escaped strings like %%s.
2017-10-01 22:11:47 -04:00
2020-01-28 19:45:18 -05:00
$query = preg_replace ( " /(?<!%)(%( $allowed_format )?f)/ " , '%\\2F' , $query ); // Force floats to be locale-unaware.
2017-10-31 08:00:49 -04:00
$query = preg_replace ( " /%(?:%| $ |(?!( $allowed_format )?[sdF]))/ " , '%%\\1' , $query ); // Escape any unescaped percents.
// Count the number of valid placeholders in the query.
$placeholders = preg_match_all ( " /(^|[^%]|(%%)+)%( $allowed_format )?[sdF]/ " , $query , $matches );
2020-09-15 22:29:06 -04:00
$args_count = count ( $args );
if ( $args_count !== $placeholders ) {
2017-10-31 08:00:49 -04:00
if ( 1 === $placeholders && $passed_as_array ) {
// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
wp_load_translations_early ();
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
_doing_it_wrong (
'wpdb::prepare' ,
__ ( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ),
'4.9.0'
);
2017-10-31 08:00:49 -04:00
return ;
} else {
/*
* If we don ' t have the right number of placeholders , but they were passed as individual arguments ,
* or we were expecting multiple arguments in an array , throw a warning .
*/
wp_load_translations_early ();
2017-11-30 18:11:00 -05:00
_doing_it_wrong (
'wpdb::prepare' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: 1: Number of placeholders, 2: Number of arguments passed. */
2017-11-30 18:11:00 -05:00
__ ( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
2017-10-31 08:00:49 -04:00
$placeholders ,
2020-09-15 22:29:06 -04:00
$args_count
2017-11-30 18:11:00 -05:00
),
2017-10-31 08:00:49 -04:00
'4.8.3'
);
2020-09-15 22:29:06 -04:00
/*
* If we don ' t have enough arguments to match the placeholders ,
* return an empty string to avoid a fatal error on PHP 8.
*/
if ( $args_count < $placeholders ) {
$max_numbered_placeholder = ! empty ( $matches [ 3 ] ) ? max ( array_map ( 'intval' , $matches [ 3 ] ) ) : 0 ;
if ( ! $max_numbered_placeholder || $args_count < $max_numbered_placeholder ) {
return '' ;
}
}
2017-10-31 08:00:49 -04:00
}
2017-10-01 22:11:47 -04:00
}
2012-10-04 16:00:16 -04:00
array_walk ( $args , array ( $this , 'escape_by_ref' ) );
2019-07-09 01:45:58 -04:00
$query = vsprintf ( $query , $args );
2017-10-31 08:00:49 -04:00
return $this -> add_placeholder_escape ( $query );
2007-07-04 12:18:57 -04:00
}
2014-06-09 20:30:15 -04:00
/**
2021-12-29 18:14:01 -05:00
* First half of escaping for `LIKE` special characters `%` and `_` before preparing for SQL .
2014-06-09 20:30:15 -04:00
*
2020-05-03 07:45:06 -04:00
* Use this only before wpdb :: prepare () or esc_sql () . Reversing the order is very bad for security .
2014-06-09 20:30:15 -04:00
*
* Example Prepared Statement :
2016-03-27 14:27:26 -04:00
*
* $wild = '%' ;
* $find = 'only 43% of planets' ;
* $like = $wild . $wpdb -> esc_like ( $find ) . $wild ;
2017-09-28 00:32:46 -04:00
* $sql = $wpdb -> prepare ( " SELECT * FROM $wpdb->posts WHERE post_content LIKE %s " , $like );
2014-06-09 20:30:15 -04:00
*
* Example Escape Chain :
2016-03-27 14:27:26 -04:00
*
* $sql = esc_sql ( $wpdb -> esc_like ( $input ) );
2014-06-09 20:30:15 -04:00
*
* @ since 4.0 . 0
*
2020-05-03 07:45:06 -04:00
* @ param string $text The raw text to be escaped . The input typed by the user
* should have no extra or deleted slashes .
* @ return string Text in the form of a LIKE phrase . The output is not SQL safe .
* Call wpdb :: prepare () or wpdb :: _real_escape () next .
2014-06-09 20:30:15 -04:00
*/
2014-07-13 21:02:15 -04:00
public function esc_like ( $text ) {
2014-06-09 20:30:15 -04:00
return addcslashes ( $text , '_%\\' );
}
2008-06-22 16:23:23 -04:00
/**
2020-05-03 07:45:06 -04:00
* Prints SQL / DB error .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2020-05-03 07:45:06 -04:00
* @ global array $EZSQL_ERROR Stores error information of query and error string .
*
* @ param string $str The error to display .
2020-01-11 13:32:05 -05:00
* @ return void | false Void if the showing of errors is enabled , false if disabled .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function print_error ( $str = '' ) {
2004-04-25 22:28:06 -04:00
global $EZSQL_ERROR ;
2008-03-12 18:41:43 -04:00
2017-11-30 18:11:00 -05:00
if ( ! $str ) {
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
$str = mysqli_error ( $this -> dbh );
} else {
$str = mysql_error ( $this -> dbh );
}
}
2017-11-30 18:11:00 -05:00
$EZSQL_ERROR [] = array (
'query' => $this -> last_query ,
'error_str' => $str ,
);
2004-04-25 22:28:06 -04:00
2017-11-30 18:11:00 -05:00
if ( $this -> suppress_errors ) {
2008-03-12 18:41:43 -04:00
return false ;
2017-11-30 18:11:00 -05:00
}
2008-03-12 18:41:43 -04:00
2012-01-26 15:34:27 -05:00
wp_load_translations_early ();
2019-07-02 19:42:58 -04:00
$caller = $this -> get_caller ();
if ( $caller ) {
2019-09-02 20:41:05 -04:00
/* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function. */
2012-01-26 15:34:27 -05:00
$error_str = sprintf ( __ ( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str , $this -> last_query , $caller );
2016-11-20 20:22:32 -05:00
} else {
2019-09-02 20:41:05 -04:00
/* translators: 1: Database error message, 2: SQL query. */
2012-01-26 15:34:27 -05:00
$error_str = sprintf ( __ ( 'WordPress database error %1$s for query %2$s' ), $str , $this -> last_query );
2016-11-20 20:22:32 -05:00
}
2008-03-20 15:08:40 -04:00
2012-09-10 21:35:43 -04:00
error_log ( $error_str );
2007-12-16 16:38:24 -05:00
2010-02-26 01:14:26 -05:00
// Are we showing errors?
2017-11-30 18:11:00 -05:00
if ( ! $this -> show_errors ) {
2006-02-12 02:53:23 -05:00
return false ;
2017-11-30 18:11:00 -05:00
}
2007-12-16 16:38:24 -05:00
2020-01-28 19:45:18 -05:00
// If there is an error then take note of it.
2010-01-18 17:21:36 -05:00
if ( is_multisite () ) {
2015-06-30 16:00:25 -04:00
$msg = sprintf (
" %s [%s] \n %s \n " ,
__ ( 'WordPress database error:' ),
$str ,
$this -> last_query
);
if ( defined ( 'ERRORLOGFILE' ) ) {
2010-02-18 20:03:58 -05:00
error_log ( $msg , 3 , ERRORLOGFILE );
2015-06-30 16:00:25 -04:00
}
if ( defined ( 'DIEONDBERROR' ) ) {
2010-02-23 17:04:29 -05:00
wp_die ( $msg );
2015-06-30 16:00:25 -04:00
}
2010-01-18 17:21:36 -05:00
} else {
2010-02-22 17:53:54 -05:00
$str = htmlspecialchars ( $str , ENT_QUOTES );
$query = htmlspecialchars ( $this -> last_query , ENT_QUOTES );
2010-01-18 17:21:36 -05:00
2015-06-30 16:00:25 -04:00
printf (
'<div id="error"><p class="wpdberror"><strong>%s</strong> [%s]<br /><code>%s</code></p></div>' ,
__ ( 'WordPress database error:' ),
$str ,
$query
);
2010-01-18 17:21:36 -05:00
}
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2008-06-22 16:23:23 -04:00
/**
* Enables showing of database errors .
*
2020-05-03 07:45:06 -04:00
* This function should be used only to enable showing of errors .
* wpdb :: hide_errors () should be used instead for hiding errors .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
2020-05-03 07:45:06 -04:00
*
2010-02-22 17:53:54 -05:00
* @ see wpdb :: hide_errors ()
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* @ param bool $show Optional . Whether to show errors . Default true .
2020-05-02 13:25:07 -04:00
* @ return bool Whether showing of errors was previously active .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function show_errors ( $show = true ) {
2017-11-30 18:11:00 -05:00
$errors = $this -> show_errors ;
2007-12-16 16:38:24 -05:00
$this -> show_errors = $show ;
return $errors ;
2004-04-25 22:28:06 -04:00
}
2006-02-12 02:53:23 -05:00
2008-06-22 16:23:23 -04:00
/**
* Disables showing of database errors .
*
2010-02-22 17:53:54 -05:00
* By default database errors are not shown .
*
2008-06-22 16:23:23 -04:00
* @ since 0.71
2020-05-03 07:45:06 -04:00
*
2010-02-22 17:53:54 -05:00
* @ see wpdb :: show_errors ()
2008-06-22 16:23:23 -04:00
*
2020-05-02 13:25:07 -04:00
* @ return bool Whether showing of errors was previously active .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function hide_errors () {
2017-11-30 18:11:00 -05:00
$show = $this -> show_errors ;
2004-04-25 22:28:06 -04:00
$this -> show_errors = false ;
2007-12-16 16:38:24 -05:00
return $show ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2008-06-22 16:23:23 -04:00
/**
2020-05-03 07:45:06 -04:00
* Enables or disables suppressing of database errors .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* By default database errors are suppressed .
2010-02-22 17:53:54 -05:00
*
2010-12-20 07:49:18 -05:00
* @ since 2.5 . 0
2020-05-03 07:45:06 -04:00
*
2010-02-22 17:53:54 -05:00
* @ see wpdb :: hide_errors ()
2020-05-02 13:25:07 -04:00
*
2020-05-03 07:45:06 -04:00
* @ param bool $suppress Optional . Whether to suppress errors . Default true .
* @ return bool Whether suppressing of errors was previously active .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function suppress_errors ( $suppress = true ) {
2017-11-30 18:11:00 -05:00
$errors = $this -> suppress_errors ;
2010-02-22 17:53:54 -05:00
$this -> suppress_errors = ( bool ) $suppress ;
2008-03-12 18:41:43 -04:00
return $errors ;
}
2008-06-22 16:23:23 -04:00
/**
2020-05-03 07:45:06 -04:00
* Kills cached query results .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*/
2014-05-19 11:36:13 -04:00
public function flush () {
2017-11-30 18:11:00 -05:00
$this -> last_result = array ();
$this -> col_info = null ;
$this -> last_query = null ;
2019-07-02 19:42:58 -04:00
$this -> rows_affected = 0 ;
$this -> num_rows = 0 ;
2017-11-30 18:11:00 -05:00
$this -> last_error = '' ;
2012-08-14 16:21:10 -04:00
2014-11-10 00:40:23 -05:00
if ( $this -> use_mysqli && $this -> result instanceof mysqli_result ) {
mysqli_free_result ( $this -> result );
$this -> result = null ;
2020-01-28 19:45:18 -05:00
// Sanity check before using the handle.
2017-11-30 18:11:00 -05:00
if ( empty ( $this -> dbh ) || ! ( $this -> dbh instanceof mysqli ) ) {
2014-11-10 21:17:28 -05:00
return ;
}
2020-01-28 19:45:18 -05:00
// Clear out any results from a multi-query.
2014-11-10 00:40:23 -05:00
while ( mysqli_more_results ( $this -> dbh ) ) {
mysqli_next_result ( $this -> dbh );
2014-02-24 19:40:13 -05:00
}
2015-01-08 02:05:25 -05:00
} elseif ( is_resource ( $this -> result ) ) {
2014-11-10 00:40:23 -05:00
mysql_free_result ( $this -> result );
2014-02-24 19:40:13 -05:00
}
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2010-08-26 14:34:18 -04:00
/**
2020-05-03 07:45:06 -04:00
* Connects to and selects database .
2010-12-20 07:49:18 -05:00
*
2021-12-29 18:14:01 -05:00
* If `$allow_bail` is false , the lack of database connection will need to be handled manually .
2014-02-02 17:07:12 -05:00
*
2014-04-07 18:02:15 -04:00
* @ since 3.0 . 0
* @ since 3.9 . 0 $allow_bail parameter added .
2014-02-02 17:07:12 -05:00
*
2014-04-07 18:02:15 -04:00
* @ param bool $allow_bail Optional . Allows the function to bail . Default true .
2015-05-28 16:10:29 -04:00
* @ return bool True with a successful connection , false on failure .
2010-08-26 14:34:18 -04:00
*/
2014-05-19 11:36:13 -04:00
public function db_connect ( $allow_bail = true ) {
2011-10-25 01:29:28 -04:00
$this -> is_mysql = true ;
2014-06-04 01:00:15 -04:00
/*
* Deprecated in 3.9 + when using MySQLi . No equivalent
* $new_link parameter exists for mysqli_ * functions .
*/
2017-11-30 18:11:00 -05:00
$new_link = defined ( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true ;
2012-08-24 13:44:21 -04:00
$client_flags = defined ( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0 ;
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
Code Modernization: Set the MySQLi error reporting off for PHP 8.1.
Prior to PHP 8.1, the default error handling mode was `MYSQLI_REPORT_OFF`. An error in the extension, database, query, or the database connection returned `false` and emitted a PHP warning:
{{{
$mysqli = new mysqli("localhost", "non-existing-user", "", "");
Warning: mysqli::__construct(): (HY000/2002): No connection could be made because the target machine actively refused it in ... on line ...
}}}
From PHP 8.1 and later, the default error mode is set to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT`. An error in the extension, database, query, or the database connection throws an exception:
{{{
$mysqli = new mysqli("localhost", "non-existing-user", "", "");
Fatal error: Uncaught mysqli_sql_exception: Connection refused in ...:...
}}}
WordPress has its own error reporting and gracefully handles the database errors by inspecting the error codes. Setting the MySQLi error reporting to off avoids fatal errors due to uncaught exceptions and maintains the current behavior.
References:
* [https://php.watch/versions/8.1/mysqli-error-mode PHP 8.1: MySQLi: Default error mode set to exceptions]
* [https://wiki.php.net/rfc/mysqli_default_errmode PHP RFC: Change Default mysqli Error Mode]
Props ayeshrajans, jrf.
Fixes #52825.
Built from https://develop.svn.wordpress.org/trunk@51582
git-svn-id: http://core.svn.wordpress.org/trunk@51193 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-08-08 10:10:01 -04:00
/*
* Set the MySQLi error reporting off because WordPress handles its own .
* This is due to the default value change from `MYSQLI_REPORT_OFF`
* to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1 .
*/
mysqli_report ( MYSQLI_REPORT_OFF );
2014-02-24 19:40:13 -05:00
$this -> dbh = mysqli_init ();
2017-09-28 01:37:44 -04:00
$host = $this -> dbhost ;
$port = null ;
$socket = null ;
$is_ipv6 = false ;
2017-10-10 20:10:47 -04:00
2019-07-02 19:42:58 -04:00
$host_data = $this -> parse_db_host ( $this -> dbhost );
if ( $host_data ) {
2017-09-28 01:37:44 -04:00
list ( $host , $port , $socket , $is_ipv6 ) = $host_data ;
}
/*
2020-05-03 07:45:06 -04:00
* If using the `mysqlnd` library , the IPv6 address needs to be enclosed
* in square brackets , whereas it doesn ' t while using the `libmysqlclient` library .
2017-09-28 01:37:44 -04:00
* @ see https :// bugs . php . net / bug . php ? id = 67563
*/
if ( $is_ipv6 && extension_loaded ( 'mysqlnd' ) ) {
$host = " [ $host ] " ;
2013-12-01 19:09:10 -05:00
}
2014-02-24 19:40:13 -05:00
2016-04-22 01:19:27 -04:00
if ( WP_DEBUG ) {
mysqli_real_connect ( $this -> dbh , $host , $this -> dbuser , $this -> dbpassword , null , $port , $socket , $client_flags );
} else {
2019-07-09 01:45:58 -04:00
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
2016-04-22 01:19:27 -04:00
@ mysqli_real_connect ( $this -> dbh , $host , $this -> dbuser , $this -> dbpassword , null , $port , $socket , $client_flags );
}
2014-02-25 23:15:18 -05:00
if ( $this -> dbh -> connect_errno ) {
$this -> dbh = null ;
2014-04-03 17:58:16 -04:00
2017-09-28 01:37:44 -04:00
/*
* It ' s possible ext / mysqli is misconfigured . Fall back to ext / mysql if :
2019-07-05 01:21:56 -04:00
* - We haven ' t previously connected , and
* - WP_USE_EXT_MYSQL isn ' t set to false , and
* - ext / mysql is loaded .
*/
2014-04-03 17:58:16 -04:00
$attempt_fallback = true ;
if ( $this -> has_connected ) {
$attempt_fallback = false ;
2015-01-08 02:05:25 -05:00
} elseif ( defined ( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) {
2014-04-03 17:58:16 -04:00
$attempt_fallback = false ;
2015-01-08 02:05:25 -05:00
} elseif ( ! function_exists ( 'mysql_connect' ) ) {
2014-04-03 17:58:16 -04:00
$attempt_fallback = false ;
}
if ( $attempt_fallback ) {
$this -> use_mysqli = false ;
2015-09-24 00:30:25 -04:00
return $this -> db_connect ( $allow_bail );
2014-04-03 17:58:16 -04:00
}
2014-02-25 23:15:18 -05:00
}
2010-10-14 14:47:39 -04:00
} else {
2016-04-22 01:19:27 -04:00
if ( WP_DEBUG ) {
$this -> dbh = mysql_connect ( $this -> dbhost , $this -> dbuser , $this -> dbpassword , $new_link , $client_flags );
} else {
2019-07-09 01:45:58 -04:00
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
2016-04-22 01:19:27 -04:00
$this -> dbh = @ mysql_connect ( $this -> dbhost , $this -> dbuser , $this -> dbpassword , $new_link , $client_flags );
}
2010-10-14 14:47:39 -04:00
}
2010-08-26 14:34:18 -04:00
2014-02-02 17:07:12 -05:00
if ( ! $this -> dbh && $allow_bail ) {
2012-01-26 15:34:27 -05:00
wp_load_translations_early ();
2014-01-29 00:28:12 -05:00
// Load custom DB error template, if present.
if ( file_exists ( WP_CONTENT_DIR . '/db-error.php' ) ) {
2020-02-06 01:33:11 -05:00
require_once WP_CONTENT_DIR . '/db-error.php' ;
2014-01-29 00:28:12 -05:00
die ();
}
2015-10-08 12:52:24 -04:00
$message = '<h1>' . __ ( 'Error establishing a database connection' ) . " </h1> \n " ;
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: 1: wp-config.php, 2: Database host. */
Administration: Remove self-reference ("we") in WordPress Admin.
This changes updates many strings to remove self-references to an undefined "we" collective across the WordPress Administration.
The goal of this change is to better match the guidelines and recommendations set forth in the make/core handbook, specifically:
> the word "we" should be avoided (…) unless its made very clear which group is speaking.
Props johnbillion, shital-patel, audrasjb, marybaum, SergeyBiryukov, peterwilsoncc, johnjamesjacoby, kebbet, costdev, chaion07, davidbaumwald.
Fixes #46057.
Built from https://develop.svn.wordpress.org/trunk@53131
git-svn-id: http://core.svn.wordpress.org/trunk@52720 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-11 07:42:04 -04:00
__ ( 'This either means that the username and password information in your %1$s file is incorrect or that contact with the database server at %2$s could not be established. This could mean your host’s database server is down.' ),
2015-10-08 12:52:24 -04:00
'<code>wp-config.php</code>' ,
'<code>' . htmlspecialchars ( $this -> dbhost , ENT_QUOTES ) . '</code>'
) . " </p> \n " ;
$message .= " <ul> \n " ;
$message .= '<li>' . __ ( 'Are you sure you have the correct username and password?' ) . " </li> \n " ;
2019-07-04 06:58:56 -04:00
$message .= '<li>' . __ ( 'Are you sure you have typed the correct hostname?' ) . " </li> \n " ;
$message .= '<li>' . __ ( 'Are you sure the database server is running?' ) . " </li> \n " ;
2015-10-08 12:52:24 -04:00
$message .= " </ul> \n " ;
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Support forums URL. */
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
__ ( 'If you are 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>.' ),
2019-04-08 18:59:56 -04:00
__ ( 'https://wordpress.org/support/forums/' )
2015-10-08 12:52:24 -04:00
) . " </p> \n " ;
$this -> bail ( $message , 'db_connect_fail' );
2010-10-21 15:55:28 -04:00
2014-02-02 17:07:12 -05:00
return false ;
2015-01-08 02:05:25 -05:00
} elseif ( $this -> dbh ) {
2015-02-05 23:51:22 -05:00
if ( ! $this -> has_connected ) {
$this -> init_charset ();
}
2014-04-03 17:58:16 -04:00
$this -> has_connected = true ;
2015-02-05 23:51:22 -05:00
2014-02-02 17:07:12 -05:00
$this -> set_charset ( $this -> dbh );
2015-02-05 23:51:22 -05:00
2014-02-02 17:07:12 -05:00
$this -> ready = true ;
2014-11-19 20:46:24 -05:00
$this -> set_sql_mode ();
2014-02-02 17:07:12 -05:00
$this -> select ( $this -> dbname , $this -> dbh );
return true ;
2010-01-08 14:26:00 -05:00
}
2010-08-26 14:34:18 -04:00
2014-02-02 17:07:12 -05:00
return false ;
}
2017-09-28 01:37:44 -04:00
/**
2020-05-03 07:45:06 -04:00
* Parses the DB_HOST setting to interpret it for mysqli_real_connect () .
2017-09-28 01:37:44 -04:00
*
2020-05-03 07:45:06 -04:00
* mysqli_real_connect () doesn ' t support the host param including a port or socket
* like mysql_connect () does . This duplicates how mysql_connect () detects a port
* and / or socket file .
2017-09-28 01:37:44 -04:00
*
* @ since 4.9 . 0
*
* @ param string $host The DB_HOST setting to parse .
2022-04-28 05:46:21 -04:00
* @ return array | false {
* Array containing the host , the port , the socket and
* whether it is an IPv6 address , in that order .
* False if the host couldn ' t be parsed .
*
* @ type string $ 0 Host name .
* @ type string | null $ 1 Port .
* @ type string | null $ 2 Socket .
* @ type bool $ 3 Whether it is an IPv6 address .
* }
2017-09-28 01:37:44 -04:00
*/
public function parse_db_host ( $host ) {
$port = null ;
$socket = null ;
$is_ipv6 = false ;
2017-11-24 00:52:46 -05:00
// First peel off the socket parameter from the right, if it exists.
$socket_pos = strpos ( $host , ':/' );
2020-02-09 11:55:09 -05:00
if ( false !== $socket_pos ) {
2017-11-24 00:52:46 -05:00
$socket = substr ( $host , $socket_pos + 1 );
2017-11-30 18:11:00 -05:00
$host = substr ( $host , 0 , $socket_pos );
2017-11-24 00:52:46 -05:00
}
2017-09-28 01:37:44 -04:00
// We need to check for an IPv6 address first.
// An IPv6 address will always contain at least two colons.
if ( substr_count ( $host , ':' ) > 1 ) {
2018-01-22 22:39:30 -05:00
$pattern = '#^(?:\[)?(?P<host>[0-9a-fA-F:]+)(?:\]:(?P<port>[\d]+))?#' ;
2017-09-28 01:37:44 -04:00
$is_ipv6 = true ;
} else {
// We seem to be dealing with an IPv4 address.
2018-01-22 22:39:30 -05:00
$pattern = '#^(?P<host>[^:/]*)(?::(?P<port>[\d]+))?#' ;
2017-09-28 01:37:44 -04:00
}
$matches = array ();
2017-11-30 18:11:00 -05:00
$result = preg_match ( $pattern , $host , $matches );
2017-09-28 01:37:44 -04:00
if ( 1 !== $result ) {
// Couldn't parse the address, bail.
return false ;
}
2017-10-10 20:10:47 -04:00
$host = '' ;
2017-11-24 00:52:46 -05:00
foreach ( array ( 'host' , 'port' ) as $component ) {
2017-10-10 20:10:47 -04:00
if ( ! empty ( $matches [ $component ] ) ) {
$$component = $matches [ $component ];
2017-09-28 01:37:44 -04:00
}
}
return array ( $host , $port , $socket , $is_ipv6 );
}
2014-02-02 17:07:12 -05:00
/**
2016-05-23 15:02:28 -04:00
* Checks that the connection to the database is still up . If not , try to reconnect .
2014-02-02 17:07:12 -05:00
*
2020-05-03 07:45:06 -04:00
* If this function is unable to reconnect , it will forcibly die , or if called
* after the { @ see 'template_redirect' } hook has been fired , return false instead .
2014-02-02 17:07:12 -05:00
*
2021-12-29 18:14:01 -05:00
* If `$allow_bail` is false , the lack of database connection will need to be handled manually .
2014-02-02 17:07:12 -05:00
*
2014-04-07 18:02:15 -04:00
* @ since 3.9 . 0
2014-04-03 00:06:14 -04:00
*
2014-04-07 18:02:15 -04:00
* @ param bool $allow_bail Optional . Allows the function to bail . Default true .
2015-05-28 16:10:29 -04:00
* @ return bool | void True if the connection is up .
2014-02-02 17:07:12 -05:00
*/
2014-05-19 11:36:13 -04:00
public function check_connection ( $allow_bail = true ) {
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
2015-12-10 22:40:26 -05:00
if ( ! empty ( $this -> dbh ) && mysqli_ping ( $this -> dbh ) ) {
2014-02-24 19:40:13 -05:00
return true ;
}
} else {
2015-12-10 22:40:26 -05:00
if ( ! empty ( $this -> dbh ) && mysql_ping ( $this -> dbh ) ) {
2014-02-24 19:40:13 -05:00
return true ;
}
2014-02-02 17:07:12 -05:00
}
$error_reporting = false ;
2020-01-28 19:45:18 -05:00
// Disable warnings, as we don't want to see a multitude of "unable to connect" messages.
2014-02-02 17:07:12 -05:00
if ( WP_DEBUG ) {
$error_reporting = error_reporting ();
error_reporting ( $error_reporting & ~ E_WARNING );
}
for ( $tries = 1 ; $tries <= $this -> reconnect_retries ; $tries ++ ) {
2020-05-03 07:45:06 -04:00
// On the last try, re-enable warnings. We want to see a single instance
// of the "unable to connect" message on the bail() screen, if it appears.
2014-02-02 17:07:12 -05:00
if ( $this -> reconnect_retries === $tries && WP_DEBUG ) {
error_reporting ( $error_reporting );
}
if ( $this -> db_connect ( false ) ) {
if ( $error_reporting ) {
error_reporting ( $error_reporting );
}
return true ;
}
2010-08-26 14:34:18 -04:00
2014-02-02 17:07:12 -05:00
sleep ( 1 );
}
2014-02-02 16:39:13 -05:00
2014-02-25 23:54:14 -05:00
// If template_redirect has already happened, it's too late for wp_die()/dead_db().
// Let's just return and hope for the best.
if ( did_action ( 'template_redirect' ) ) {
return false ;
}
2014-04-03 00:06:14 -04:00
if ( ! $allow_bail ) {
return false ;
}
2015-10-08 13:11:24 -04:00
wp_load_translations_early ();
2015-10-08 12:52:24 -04:00
$message = '<h1>' . __ ( 'Error reconnecting to the database' ) . " </h1> \n " ;
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Database host. */
Administration: Remove self-reference ("we") in WordPress Admin.
This changes updates many strings to remove self-references to an undefined "we" collective across the WordPress Administration.
The goal of this change is to better match the guidelines and recommendations set forth in the make/core handbook, specifically:
> the word "we" should be avoided (…) unless its made very clear which group is speaking.
Props johnbillion, shital-patel, audrasjb, marybaum, SergeyBiryukov, peterwilsoncc, johnjamesjacoby, kebbet, costdev, chaion07, davidbaumwald.
Fixes #46057.
Built from https://develop.svn.wordpress.org/trunk@53131
git-svn-id: http://core.svn.wordpress.org/trunk@52720 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-11 07:42:04 -04:00
__ ( 'This means that the contact with the database server at %s was lost. This could mean your host’s database server is down.' ),
2015-10-08 12:52:24 -04:00
'<code>' . htmlspecialchars ( $this -> dbhost , ENT_QUOTES ) . '</code>'
) . " </p> \n " ;
$message .= " <ul> \n " ;
2019-07-04 06:58:56 -04:00
$message .= '<li>' . __ ( 'Are you sure the database server is running?' ) . " </li> \n " ;
$message .= '<li>' . __ ( 'Are you sure the database server is not under particularly heavy load?' ) . " </li> \n " ;
2015-10-08 12:52:24 -04:00
$message .= " </ul> \n " ;
$message .= '<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Support forums URL. */
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
__ ( 'If you are 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>.' ),
2019-04-08 18:59:56 -04:00
__ ( 'https://wordpress.org/support/forums/' )
2015-10-08 12:52:24 -04:00
) . " </p> \n " ;
2014-02-02 17:07:12 -05:00
// We weren't able to reconnect, so we better bail.
2015-10-08 12:52:24 -04:00
$this -> bail ( $message , 'db_connect_fail' );
2010-08-26 14:34:18 -04:00
2020-05-03 07:45:06 -04:00
// Call dead_db() if bail didn't die, because this database is no more.
// It has ceased to be (at least temporarily).
2014-02-02 17:07:12 -05:00
dead_db ();
2010-01-08 14:26:00 -05:00
}
2008-06-22 16:23:23 -04:00
/**
2021-12-29 18:14:01 -05:00
* Performs a database query , using current database connection .
2008-06-22 16:23:23 -04:00
*
2020-12-29 15:14:11 -05:00
* More information can be found on the documentation page .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2020-12-29 15:14:11 -05:00
* @ link https :// developer . wordpress . org / reference / classes / wpdb /
2020-05-03 07:45:06 -04:00
*
* @ param string $query Database query .
2018-04-28 10:44:23 -04:00
* @ return int | bool Boolean true for CREATE , ALTER , TRUNCATE and DROP queries . Number of rows
* affected / selected for all other queries . Boolean false on error .
2008-06-22 16:23:23 -04:00
*/
2014-05-19 11:36:13 -04:00
public function query ( $query ) {
2014-11-14 16:28:22 -05:00
if ( ! $this -> ready ) {
$this -> check_current_query = true ;
2007-12-21 14:30:08 -05:00
return false ;
2014-11-14 16:28:22 -05:00
}
2014-02-02 17:07:12 -05:00
2013-09-06 13:07:09 -04:00
/**
2016-05-22 14:50:28 -04:00
* Filters the database query .
2013-09-06 13:07:09 -04:00
*
2014-02-09 15:51:13 -05:00
* Some queries are made before the plugins have been loaded ,
* and thus cannot be filtered with this method .
2013-09-06 13:07:09 -04:00
*
* @ since 2.1 . 0
2014-02-09 15:51:13 -05:00
*
2013-09-06 13:07:09 -04:00
* @ param string $query Database query .
*/
2012-01-26 15:34:27 -05:00
$query = apply_filters ( 'query' , $query );
2006-12-06 19:02:03 -05:00
2020-09-30 08:09:08 -04:00
if ( ! $query ) {
$this -> insert_id = 0 ;
return false ;
}
2004-04-25 22:28:06 -04:00
$this -> flush ();
2003-05-28 04:41:04 -04:00
2020-01-28 19:45:18 -05:00
// Log how the function was called.
2004-04-25 22:28:06 -04:00
$this -> func_call = " \$ db->query( \" $query\ " ) " ;
2014-11-14 16:28:22 -05:00
// If we're writing to the database, make sure the query will write safely.
if ( $this -> check_current_query && ! $this -> check_ascii ( $query ) ) {
$stripped_query = $this -> strip_invalid_text_from_query ( $query );
2015-01-08 03:32:22 -05:00
// strip_invalid_text_from_query() can perform queries, so we need
// to flush again, just to make sure everything is clear.
$this -> flush ();
2014-11-14 16:28:22 -05:00
if ( $stripped_query !== $query ) {
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
$this -> insert_id = 0 ;
$this -> last_query = $query ;
2021-11-19 14:00:00 -05:00
wp_load_translations_early ();
$this -> last_error = __ ( 'WordPress database error: Could not perform query because it contains invalid data.' );
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
2014-11-14 16:28:22 -05:00
return false ;
}
}
$this -> check_current_query = true ;
2016-05-24 01:24:26 -04:00
// Keep track of the last query for debug.
2004-04-25 22:28:06 -04:00
$this -> last_query = $query ;
2014-02-02 17:07:12 -05:00
$this -> _do_query ( $query );
2007-02-27 10:24:54 -05:00
2021-12-29 18:14:01 -05:00
// Database server has gone away, try to reconnect.
2014-02-24 19:40:13 -05:00
$mysql_errno = 0 ;
if ( ! empty ( $this -> dbh ) ) {
if ( $this -> use_mysqli ) {
2016-05-24 01:24:26 -04:00
if ( $this -> dbh instanceof mysqli ) {
$mysql_errno = mysqli_errno ( $this -> dbh );
} else {
// $dbh is defined, but isn't a real connection.
// Something has gone horribly wrong, let's try a reconnect.
$mysql_errno = 2006 ;
}
2014-02-24 19:40:13 -05:00
} else {
2016-05-24 01:24:26 -04:00
if ( is_resource ( $this -> dbh ) ) {
$mysql_errno = mysql_errno ( $this -> dbh );
} else {
$mysql_errno = 2006 ;
}
2014-02-24 19:40:13 -05:00
}
}
2020-07-18 07:39:04 -04:00
if ( empty ( $this -> dbh ) || 2006 === $mysql_errno ) {
2014-02-02 17:07:12 -05:00
if ( $this -> check_connection () ) {
$this -> _do_query ( $query );
2014-02-25 23:54:14 -05:00
} else {
$this -> insert_id = 0 ;
return false ;
2014-02-02 17:07:12 -05:00
}
}
2004-04-25 22:28:06 -04:00
2016-05-24 01:24:26 -04:00
// If there is an error then take note of it.
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
2016-05-24 01:24:26 -04:00
if ( $this -> dbh instanceof mysqli ) {
$this -> last_error = mysqli_error ( $this -> dbh );
} else {
$this -> last_error = __ ( 'Unable to retrieve the error message from MySQL' );
}
2014-02-24 19:40:13 -05:00
} else {
2016-05-24 01:24:26 -04:00
if ( is_resource ( $this -> dbh ) ) {
$this -> last_error = mysql_error ( $this -> dbh );
} else {
$this -> last_error = __ ( 'Unable to retrieve the error message from MySQL' );
}
2014-02-24 19:40:13 -05:00
}
if ( $this -> last_error ) {
2013-07-29 14:14:05 -04:00
// Clear insert_id on a subsequent failed insert.
2017-11-30 18:11:00 -05:00
if ( $this -> insert_id && preg_match ( '/^\s*(insert|replace)\s/i' , $query ) ) {
2013-07-29 14:14:05 -04:00
$this -> insert_id = 0 ;
2017-11-30 18:11:00 -05:00
}
2013-07-29 14:14:05 -04:00
2004-04-25 22:54:06 -04:00
$this -> print_error ();
return false ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2012-06-29 11:08:17 -04:00
if ( preg_match ( '/^\s*(create|alter|truncate|drop)\s/i' , $query ) ) {
2011-05-27 11:46:56 -04:00
$return_val = $this -> result ;
2012-06-29 11:08:17 -04:00
} elseif ( preg_match ( '/^\s*(insert|delete|update|replace)\s/i' , $query ) ) {
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
$this -> rows_affected = mysqli_affected_rows ( $this -> dbh );
} else {
$this -> rows_affected = mysql_affected_rows ( $this -> dbh );
}
2020-01-28 19:45:18 -05:00
// Take note of the insert_id.
2012-06-29 11:08:17 -04:00
if ( preg_match ( '/^\s*(insert|replace)\s/i' , $query ) ) {
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
$this -> insert_id = mysqli_insert_id ( $this -> dbh );
} else {
$this -> insert_id = mysql_insert_id ( $this -> dbh );
}
2004-04-25 22:54:06 -04:00
}
2020-01-28 19:45:18 -05:00
// Return number of rows affected.
2004-04-25 22:54:06 -04:00
$return_val = $this -> rows_affected ;
2004-04-25 22:28:06 -04:00
} else {
2010-11-12 17:44:16 -05:00
$num_rows = 0 ;
2014-11-30 19:03:22 -05:00
if ( $this -> use_mysqli && $this -> result instanceof mysqli_result ) {
2015-12-10 22:40:26 -05:00
while ( $row = mysqli_fetch_object ( $this -> result ) ) {
2017-11-30 18:11:00 -05:00
$this -> last_result [ $num_rows ] = $row ;
2014-02-24 19:40:13 -05:00
$num_rows ++ ;
}
2015-01-08 02:05:25 -05:00
} elseif ( is_resource ( $this -> result ) ) {
2015-12-10 22:40:26 -05:00
while ( $row = mysql_fetch_object ( $this -> result ) ) {
2017-11-30 18:11:00 -05:00
$this -> last_result [ $num_rows ] = $row ;
2014-02-24 19:40:13 -05:00
$num_rows ++ ;
}
2010-11-12 17:44:16 -05:00
}
2020-05-03 07:45:06 -04:00
// Log and return the number of rows selected.
2010-11-12 17:44:16 -05:00
$this -> num_rows = $num_rows ;
$return_val = $num_rows ;
2004-04-25 22:54:06 -04:00
}
2003-05-28 04:41:04 -04:00
2004-04-25 22:54:06 -04:00
return $return_val ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2014-02-02 17:07:12 -05:00
/**
2014-04-07 18:02:15 -04:00
* Internal function to perform the mysql_query () call .
2014-02-02 17:07:12 -05:00
*
* @ since 3.9 . 0
*
* @ see wpdb :: query ()
*
2014-04-07 18:02:15 -04:00
* @ param string $query The query to run .
2014-02-02 17:07:12 -05:00
*/
private function _do_query ( $query ) {
if ( defined ( 'SAVEQUERIES' ) && SAVEQUERIES ) {
$this -> timer_start ();
}
2015-12-10 22:40:26 -05:00
if ( ! empty ( $this -> dbh ) && $this -> use_mysqli ) {
$this -> result = mysqli_query ( $this -> dbh , $query );
} elseif ( ! empty ( $this -> dbh ) ) {
$this -> result = mysql_query ( $query , $this -> dbh );
2014-02-24 19:40:13 -05:00
}
2014-02-02 17:07:12 -05:00
$this -> num_queries ++ ;
if ( defined ( 'SAVEQUERIES' ) && SAVEQUERIES ) {
2019-07-15 00:07:57 -04:00
$this -> log_query (
2018-03-05 17:54:35 -05:00
$query ,
$this -> timer_stop (),
$this -> get_caller (),
$this -> time_start ,
2019-07-15 00:07:57 -04:00
array ()
2018-03-05 17:54:35 -05:00
);
2014-02-02 17:07:12 -05:00
}
}
2019-07-15 00:07:57 -04:00
/**
* Logs query data .
*
* @ since 5.3 . 0
*
* @ param string $query The query ' s SQL .
* @ param float $query_time Total time spent on the query , in seconds .
2020-05-03 07:45:06 -04:00
* @ param string $query_callstack Comma - separated list of the calling functions .
2019-07-15 00:07:57 -04:00
* @ param float $query_start Unix timestamp of the time at the start of the query .
* @ param array $query_data Custom query data .
*/
public function log_query ( $query , $query_time , $query_callstack , $query_start , $query_data ) {
/**
2021-01-07 09:17:11 -05:00
* Filters the custom data to log alongside a query .
2019-07-15 00:07:57 -04:00
*
* Caution should be used when modifying any of this data , it is recommended that any additional
2021-01-07 09:17:11 -05:00
* information you need to store about a query be added as a new associative array element .
2019-07-15 00:07:57 -04:00
*
* @ since 5.3 . 0
*
* @ param array $query_data Custom query data .
* @ param string $query The query ' s SQL .
* @ param float $query_time Total time spent on the query , in seconds .
2020-05-03 07:45:06 -04:00
* @ param string $query_callstack Comma - separated list of the calling functions .
2019-07-15 00:07:57 -04:00
* @ param float $query_start Unix timestamp of the time at the start of the query .
*/
$query_data = apply_filters ( 'log_query_custom_data' , $query_data , $query , $query_time , $query_callstack , $query_start );
$this -> queries [] = array (
$query ,
$query_time ,
$query_callstack ,
$query_start ,
$query_data ,
);
}
2017-10-31 08:00:49 -04:00
/**
* Generates and returns a placeholder escape string for use in queries returned by :: prepare () .
*
* @ since 4.8 . 3
*
* @ return string String to escape placeholders .
*/
public function placeholder_escape () {
static $placeholder ;
if ( ! $placeholder ) {
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
$algo = function_exists ( 'hash' ) ? 'sha256' : 'sha1' ;
// Old WP installs may not have AUTH_SALT defined.
2017-11-06 20:09:47 -05:00
$salt = defined ( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT : ( string ) rand ();
2017-10-31 08:00:49 -04:00
$placeholder = '{' . hash_hmac ( $algo , uniqid ( $salt , true ), $salt ) . '}' ;
}
/*
* Add the filter to remove the placeholder escaper . Uses priority 0 , so that anything
2019-03-27 13:09:52 -04:00
* else attached to this filter will receive the query with the placeholder string removed .
2017-10-31 08:00:49 -04:00
*/
2019-07-31 22:25:56 -04:00
if ( false === has_filter ( 'query' , array ( $this , 'remove_placeholder_escape' ) ) ) {
2017-10-31 08:00:49 -04:00
add_filter ( 'query' , array ( $this , 'remove_placeholder_escape' ), 0 );
}
return $placeholder ;
}
/**
* Adds a placeholder escape string , to escape anything that resembles a printf () placeholder .
*
* @ since 4.8 . 3
*
* @ param string $query The query to escape .
* @ return string The query with the placeholder escape string inserted where necessary .
*/
public function add_placeholder_escape ( $query ) {
/*
* To prevent returning anything that even vaguely resembles a placeholder ,
* we clobber every % we can find .
*/
return str_replace ( '%' , $this -> placeholder_escape (), $query );
}
/**
* Removes the placeholder escape strings from a query .
*
* @ since 4.8 . 3
*
* @ param string $query The query from which the placeholder will be removed .
* @ return string The query with the placeholder removed .
*/
public function remove_placeholder_escape ( $query ) {
return str_replace ( $this -> placeholder_escape (), '%' , $query );
}
2007-10-10 18:01:40 -04:00
/**
2020-05-03 07:45:06 -04:00
* Inserts a row into the table .
2009-04-10 17:57:40 -04:00
*
2020-05-02 13:25:07 -04:00
* Examples :
2021-12-29 18:14:01 -05:00
*
2014-11-24 01:31:21 -05:00
* wpdb :: insert ( 'table' , array ( 'column' => 'foo' , 'field' => 'bar' ) )
* wpdb :: insert ( 'table' , array ( 'column' => 'foo' , 'field' => 1337 ), array ( '%s' , '%d' ) )
2008-06-22 16:23:23 -04:00
*
* @ since 2.5 . 0
2020-05-03 07:45:06 -04:00
*
2009-04-10 17:57:40 -04:00
* @ see wpdb :: prepare ()
2010-02-23 17:04:29 -05:00
* @ see wpdb :: $field_types
* @ see wp_set_wpdb_vars ()
2008-06-22 16:23:23 -04:00
*
2020-05-02 13:25:07 -04:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ param array $data Data to insert ( in column => value pairs ) .
* Both $data columns and $data values should be " raw " ( neither should be SQL escaped ) .
2020-05-02 13:25:07 -04:00
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array | string $format Optional . An array of formats to be mapped to each of the value in $data .
* If string , that format will be used for all of the values in $data .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
2020-05-02 13:25:07 -04:00
* If omitted , all values in $data will be treated as strings unless otherwise
* specified in wpdb :: $field_types .
2009-04-10 17:57:40 -04:00
* @ return int | false The number of rows inserted , or false on error .
2007-10-10 18:01:40 -04:00
*/
2014-05-19 11:36:13 -04:00
public function insert ( $table , $data , $format = null ) {
2010-02-27 10:45:09 -05:00
return $this -> _insert_replace_helper ( $table , $data , $format , 'INSERT' );
}
/**
2020-05-03 07:45:06 -04:00
* Replaces a row in the table .
2010-02-27 10:45:09 -05:00
*
2020-05-02 13:25:07 -04:00
* Examples :
2021-12-29 18:14:01 -05:00
*
2014-11-24 01:31:21 -05:00
* wpdb :: replace ( 'table' , array ( 'column' => 'foo' , 'field' => 'bar' ) )
* wpdb :: replace ( 'table' , array ( 'column' => 'foo' , 'field' => 1337 ), array ( '%s' , '%d' ) )
2010-02-27 10:45:09 -05:00
*
* @ since 3.0 . 0
2020-05-03 07:45:06 -04:00
*
2010-02-27 10:45:09 -05:00
* @ see wpdb :: prepare ()
* @ see wpdb :: $field_types
* @ see wp_set_wpdb_vars ()
*
2020-05-02 13:25:07 -04:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ param array $data Data to insert ( in column => value pairs ) .
* Both $data columns and $data values should be " raw " ( neither should be SQL escaped ) .
2020-05-02 13:25:07 -04:00
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array | string $format Optional . An array of formats to be mapped to each of the value in $data .
* If string , that format will be used for all of the values in $data .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
2020-05-02 13:25:07 -04:00
* If omitted , all values in $data will be treated as strings unless otherwise
* specified in wpdb :: $field_types .
2010-02-27 10:45:09 -05:00
* @ return int | false The number of rows affected , or false on error .
*/
2014-05-19 11:36:13 -04:00
public function replace ( $table , $data , $format = null ) {
2010-02-27 10:45:09 -05:00
return $this -> _insert_replace_helper ( $table , $data , $format , 'REPLACE' );
}
/**
* Helper function for insert and replace .
*
* Runs an insert or replace query based on $type argument .
*
* @ since 3.0 . 0
2020-05-03 07:45:06 -04:00
*
2010-02-27 10:45:09 -05:00
* @ see wpdb :: prepare ()
* @ see wpdb :: $field_types
* @ see wp_set_wpdb_vars ()
*
2020-05-03 07:45:06 -04:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ param array $data Data to insert ( in column => value pairs ) .
* Both $data columns and $data values should be " raw " ( neither should be SQL escaped ) .
2020-05-02 13:25:07 -04:00
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array | string $format Optional . An array of formats to be mapped to each of the value in $data .
* If string , that format will be used for all of the values in $data .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
2020-05-02 13:25:07 -04:00
* If omitted , all values in $data will be treated as strings unless otherwise
* specified in wpdb :: $field_types .
2020-07-23 17:11:05 -04:00
* @ param string $type Optional . Type of operation . Possible values include 'INSERT' or 'REPLACE' .
2020-05-03 07:45:06 -04:00
* Default 'INSERT' .
2010-02-27 10:45:09 -05:00
* @ return int | false The number of rows affected , or false on error .
*/
2021-10-18 13:52:58 -04:00
public function _insert_replace_helper ( $table , $data , $format = null , $type = 'INSERT' ) {
2015-05-05 23:00:25 -04:00
$this -> insert_id = 0 ;
2020-04-04 23:02:11 -04:00
if ( ! in_array ( strtoupper ( $type ), array ( 'REPLACE' , 'INSERT' ), true ) ) {
2010-02-27 10:45:09 -05:00
return false ;
2009-03-05 18:47:02 -05:00
}
2014-11-14 16:28:22 -05:00
$data = $this -> process_fields ( $table , $data , $format );
if ( false === $data ) {
return false ;
}
2019-07-02 19:42:58 -04:00
$formats = array ();
$values = array ();
2014-11-14 16:28:22 -05:00
foreach ( $data as $value ) {
2015-10-01 01:37:26 -04:00
if ( is_null ( $value [ 'value' ] ) ) {
$formats [] = 'NULL' ;
continue ;
}
2014-11-14 16:28:22 -05:00
$formats [] = $value [ 'format' ];
$values [] = $value [ 'value' ];
}
$fields = '`' . implode ( '`, `' , array_keys ( $data ) ) . '`' ;
$formats = implode ( ', ' , $formats );
$sql = " $type INTO ` $table ` ( $fields ) VALUES ( $formats ) " ;
$this -> check_current_query = false ;
return $this -> query ( $this -> prepare ( $sql , $values ) );
2007-10-10 18:01:40 -04:00
}
/**
2020-05-03 07:45:06 -04:00
* Updates a row in the table .
2009-04-10 17:57:40 -04:00
*
2020-05-02 13:25:07 -04:00
* Examples :
2021-12-29 18:14:01 -05:00
*
2014-11-24 01:31:21 -05:00
* wpdb :: update ( 'table' , array ( 'column' => 'foo' , 'field' => 'bar' ), array ( 'ID' => 1 ) )
* wpdb :: update ( 'table' , array ( 'column' => 'foo' , 'field' => 1337 ), array ( 'ID' => 1 ), array ( '%s' , '%d' ), array ( '%d' ) )
2008-06-22 16:23:23 -04:00
*
* @ since 2.5 . 0
2020-05-03 07:45:06 -04:00
*
2009-04-10 17:57:40 -04:00
* @ see wpdb :: prepare ()
2010-02-23 17:04:29 -05:00
* @ see wpdb :: $field_types
* @ see wp_set_wpdb_vars ()
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ param array $data Data to update ( in column => value pairs ) .
* Both $data columns and $data values should be " raw " ( neither should be SQL escaped ) .
2015-10-01 01:37:26 -04:00
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array $where A named array of WHERE clauses ( in column => value pairs ) .
* Multiple clauses will be joined with ANDs .
* Both $where columns and $where values should be " raw " .
2020-05-02 13:25:07 -04:00
* Sending a null value will create an IS NULL comparison - the corresponding
* format will be ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array | string $format Optional . An array of formats to be mapped to each of the values in $data .
* If string , that format will be used for all of the values in $data .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
2020-05-02 13:25:07 -04:00
* If omitted , all values in $data will be treated as strings unless otherwise
* specified in wpdb :: $field_types .
2015-05-28 16:10:29 -04:00
* @ param array | string $where_format Optional . An array of formats to be mapped to each of the values in $where .
* If string , that format will be used for all of the items in $where .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
* If omitted , all values in $where will be treated as strings .
2009-04-10 17:57:40 -04:00
* @ return int | false The number of rows updated , or false on error .
2007-10-10 18:01:40 -04:00
*/
2014-05-19 11:36:13 -04:00
public function update ( $table , $data , $where , $format = null , $where_format = null ) {
2014-11-14 16:28:22 -05:00
if ( ! is_array ( $data ) || ! is_array ( $where ) ) {
2009-03-05 18:47:02 -05:00
return false ;
2014-11-14 16:28:22 -05:00
}
2009-03-05 18:47:02 -05:00
2014-11-14 16:28:22 -05:00
$data = $this -> process_fields ( $table , $data , $format );
if ( false === $data ) {
return false ;
}
$where = $this -> process_fields ( $table , $where , $where_format );
if ( false === $where ) {
return false ;
2009-03-05 18:47:02 -05:00
}
2007-10-12 21:44:25 -04:00
2019-07-02 19:42:58 -04:00
$fields = array ();
$conditions = array ();
$values = array ();
2014-11-14 16:28:22 -05:00
foreach ( $data as $field => $value ) {
2015-10-01 01:37:26 -04:00
if ( is_null ( $value [ 'value' ] ) ) {
$fields [] = " ` $field ` = NULL " ;
continue ;
}
2014-11-14 16:28:22 -05:00
$fields [] = " ` $field ` = " . $value [ 'format' ];
$values [] = $value [ 'value' ];
}
foreach ( $where as $field => $value ) {
2015-10-01 01:37:26 -04:00
if ( is_null ( $value [ 'value' ] ) ) {
$conditions [] = " ` $field ` IS NULL " ;
continue ;
}
2014-11-14 16:28:22 -05:00
$conditions [] = " ` $field ` = " . $value [ 'format' ];
2017-11-30 18:11:00 -05:00
$values [] = $value [ 'value' ];
2009-03-05 18:47:02 -05:00
}
2008-08-09 01:36:14 -04:00
2017-11-30 18:11:00 -05:00
$fields = implode ( ', ' , $fields );
2014-11-14 16:28:22 -05:00
$conditions = implode ( ' AND ' , $conditions );
$sql = " UPDATE ` $table ` SET $fields WHERE $conditions " ;
2017-10-31 08:00:49 -04:00
2014-11-14 16:28:22 -05:00
$this -> check_current_query = false ;
return $this -> query ( $this -> prepare ( $sql , $values ) );
2007-10-10 18:01:40 -04:00
}
2012-03-24 11:24:31 -04:00
/**
2020-05-03 07:45:06 -04:00
* Deletes a row in the table .
2012-03-24 11:24:31 -04:00
*
2020-05-03 07:45:06 -04:00
* Examples :
2021-12-29 18:14:01 -05:00
*
2014-11-24 01:31:21 -05:00
* wpdb :: delete ( 'table' , array ( 'ID' => 1 ) )
* wpdb :: delete ( 'table' , array ( 'ID' => 1 ), array ( '%d' ) )
2012-03-24 11:24:31 -04:00
*
2012-03-27 08:00:39 -04:00
* @ since 3.4 . 0
2020-05-03 07:45:06 -04:00
*
2012-03-24 11:24:31 -04:00
* @ see wpdb :: prepare ()
* @ see wpdb :: $field_types
* @ see wp_set_wpdb_vars ()
*
2020-05-03 07:45:06 -04:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ param array $where A named array of WHERE clauses ( in column => value pairs ) .
* Multiple clauses will be joined with ANDs .
* Both $where columns and $where values should be " raw " .
2020-05-02 13:25:07 -04:00
* Sending a null value will create an IS NULL comparison - the corresponding
* format will be ignored in this case .
2015-05-28 16:10:29 -04:00
* @ param array | string $where_format Optional . An array of formats to be mapped to each of the values in $where .
* If string , that format will be used for all of the items in $where .
* A format is one of '%d' , '%f' , '%s' ( integer , float , string ) .
2020-05-02 13:25:07 -04:00
* If omitted , all values in $data will be treated as strings unless otherwise
* specified in wpdb :: $field_types .
2022-06-04 08:15:12 -04:00
* @ return int | false The number of rows deleted , or false on error .
2012-03-24 11:24:31 -04:00
*/
2014-05-19 11:36:13 -04:00
public function delete ( $table , $where , $where_format = null ) {
2014-11-14 16:28:22 -05:00
if ( ! is_array ( $where ) ) {
2012-03-24 11:24:31 -04:00
return false ;
2014-11-14 16:28:22 -05:00
}
2012-03-24 11:24:31 -04:00
2014-11-14 16:28:22 -05:00
$where = $this -> process_fields ( $table , $where , $where_format );
if ( false === $where ) {
return false ;
}
2012-03-24 11:24:31 -04:00
2019-07-02 19:42:58 -04:00
$conditions = array ();
$values = array ();
2014-11-14 16:28:22 -05:00
foreach ( $where as $field => $value ) {
2015-10-01 01:37:26 -04:00
if ( is_null ( $value [ 'value' ] ) ) {
$conditions [] = " ` $field ` IS NULL " ;
continue ;
}
2014-11-14 16:28:22 -05:00
$conditions [] = " ` $field ` = " . $value [ 'format' ];
2017-11-30 18:11:00 -05:00
$values [] = $value [ 'value' ];
2014-11-14 16:28:22 -05:00
}
2012-03-24 11:24:31 -04:00
2014-11-14 16:28:22 -05:00
$conditions = implode ( ' AND ' , $conditions );
$sql = " DELETE FROM ` $table ` WHERE $conditions " ;
$this -> check_current_query = false ;
return $this -> query ( $this -> prepare ( $sql , $values ) );
}
/**
* Processes arrays of field / value pairs and field formats .
*
2020-05-03 07:45:06 -04:00
* This is a helper method for wpdb ' s CRUD methods , which take field / value pairs
* for inserts , updates , and where clauses . This method first pairs each value
* with a format . Then it determines the charset of that field , using that
* to determine if any invalid text would be stripped . If text is stripped ,
* then field processing is rejected and the query fails .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $table Table name .
* @ param array $data Field / value pair .
* @ param mixed $format Format for each field .
2020-05-03 07:45:06 -04:00
* @ return array | false An array of fields that contain paired value and formats .
* False for invalid values .
2014-11-14 16:28:22 -05:00
*/
protected function process_fields ( $table , $data , $format ) {
$data = $this -> process_field_formats ( $data , $format );
2015-04-27 10:03:27 -04:00
if ( false === $data ) {
return false ;
}
2014-11-14 16:28:22 -05:00
$data = $this -> process_field_charsets ( $data , $table );
if ( false === $data ) {
return false ;
}
2015-04-27 10:03:27 -04:00
$data = $this -> process_field_lengths ( $data , $table );
if ( false === $data ) {
return false ;
}
2014-11-14 16:28:22 -05:00
$converted_data = $this -> strip_invalid_text ( $data );
if ( $data !== $converted_data ) {
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
$problem_fields = array ();
foreach ( $data as $field => $value ) {
if ( $value !== $converted_data [ $field ] ) {
$problem_fields [] = $field ;
}
}
2021-11-19 14:00:00 -05:00
wp_load_translations_early ();
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
if ( 1 === count ( $problem_fields ) ) {
2021-11-19 14:00:00 -05:00
$this -> last_error = sprintf (
/* translators: %s: Database field where the error occurred. */
__ ( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ),
reset ( $problem_fields )
);
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
} else {
2021-11-19 14:00:00 -05:00
$this -> last_error = sprintf (
/* translators: %s: Database fields where the error occurred. */
__ ( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ),
implode ( ', ' , $problem_fields )
);
WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long.
Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property.
Sets `last_error` with an error message if:
* `wpdb::query()` fails for invalid data
* `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data
Sets `last_query` if `wpdb::query()` fails for invalid data.
If `__()` is not available, uses non-translated error message to ensure the error is captured.
There is no change to wpdb aborting when an error occurs.
Adds tests.
Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.
Built from https://develop.svn.wordpress.org/trunk@52176
git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-15 21:59:00 -05:00
}
2014-11-14 16:28:22 -05:00
return false ;
}
return $data ;
}
/**
* Prepares arrays of value / format pairs as passed to wpdb CRUD methods .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param array $data Array of fields to values .
* @ param mixed $format Formats to be mapped to the values in $data .
2020-05-03 07:45:06 -04:00
* @ return array Array , keyed by field names with values being an array
* of 'value' and 'format' keys .
2014-11-14 16:28:22 -05:00
*/
protected function process_field_formats ( $data , $format ) {
2019-07-02 19:42:58 -04:00
$formats = ( array ) $format ;
$original_formats = $formats ;
2014-11-14 16:28:22 -05:00
foreach ( $data as $field => $value ) {
$value = array (
'value' => $value ,
'format' => '%s' ,
);
if ( ! empty ( $format ) ) {
$value [ 'format' ] = array_shift ( $formats );
if ( ! $value [ 'format' ] ) {
$value [ 'format' ] = reset ( $original_formats );
}
2012-03-24 11:24:31 -04:00
} elseif ( isset ( $this -> field_types [ $field ] ) ) {
2014-11-14 16:28:22 -05:00
$value [ 'format' ] = $this -> field_types [ $field ];
2012-03-24 11:24:31 -04:00
}
2014-11-14 16:28:22 -05:00
$data [ $field ] = $value ;
2012-03-24 11:24:31 -04:00
}
2014-11-14 16:28:22 -05:00
return $data ;
2012-03-24 11:24:31 -04:00
}
2014-11-14 16:28:22 -05:00
/**
2020-05-03 07:45:06 -04:00
* Adds field charsets to field / value / format arrays generated by wpdb :: process_field_formats () .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
2015-04-05 13:15:26 -04:00
* @ param array $data As it comes from the wpdb :: process_field_formats () method .
2014-11-14 16:28:22 -05:00
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ return array | false The same array as $data with additional 'charset' keys .
2020-05-03 07:45:06 -04:00
* False on failure .
2014-11-14 16:28:22 -05:00
*/
protected function process_field_charsets ( $data , $table ) {
foreach ( $data as $field => $value ) {
if ( '%d' === $value [ 'format' ] || '%f' === $value [ 'format' ] ) {
2015-09-16 08:46:28 -04:00
/*
* We can skip this field if we know it isn ' t a string .
* This checks % d /% f versus ! % s because its sprintf () could take more .
*/
2014-11-14 16:28:22 -05:00
$value [ 'charset' ] = false ;
} else {
$value [ 'charset' ] = $this -> get_col_charset ( $table , $field );
if ( is_wp_error ( $value [ 'charset' ] ) ) {
return false ;
}
}
$data [ $field ] = $value ;
}
return $data ;
}
2012-03-24 11:24:31 -04:00
2015-04-27 10:03:27 -04:00
/**
2020-05-03 07:45:06 -04:00
* For string fields , records the maximum string length that field can safely save .
2015-04-27 10:03:27 -04:00
*
* @ since 4.2 . 1
*
* @ param array $data As it comes from the wpdb :: process_field_charsets () method .
* @ param string $table Table name .
2015-05-28 16:10:29 -04:00
* @ return array | false The same array as $data with additional 'length' keys , or false if
2015-04-27 10:03:27 -04:00
* any of the values were too long for their corresponding field .
*/
protected function process_field_lengths ( $data , $table ) {
foreach ( $data as $field => $value ) {
if ( '%d' === $value [ 'format' ] || '%f' === $value [ 'format' ] ) {
2015-09-16 08:46:28 -04:00
/*
* We can skip this field if we know it isn ' t a string .
* This checks % d /% f versus ! % s because its sprintf () could take more .
*/
2015-04-27 10:03:27 -04:00
$value [ 'length' ] = false ;
} else {
$value [ 'length' ] = $this -> get_col_length ( $table , $field );
if ( is_wp_error ( $value [ 'length' ] ) ) {
return false ;
}
}
$data [ $field ] = $value ;
}
return $data ;
}
2006-11-29 20:44:33 -05:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves one variable from the database .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* Executes a SQL query and returns the value from the SQL result .
* If the SQL result contains more than one column and / or more than one row ,
* the value in the column and row specified is returned . If $query is null ,
* the value in the specified column and row from the previous SQL result is returned .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2010-02-23 17:04:29 -05:00
* @ param string | null $query Optional . SQL query . Defaults to null , use the result from the previous query .
2015-05-28 16:10:29 -04:00
* @ param int $x Optional . Column of value to return . Indexed from 0.
* @ param int $y Optional . Row of value to return . Indexed from 0.
2020-05-03 07:45:06 -04:00
* @ return string | null Database query result ( as string ), or null on failure .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function get_var ( $query = null , $x = 0 , $y = 0 ) {
2010-02-22 17:53:54 -05:00
$this -> func_call = " \$ db->get_var( \" $query\ " , $x , $y ) " ;
2014-11-14 16:28:22 -05:00
if ( $query ) {
2021-07-10 07:15:59 -04:00
if ( $this -> check_current_query && $this -> check_safe_collation ( $query ) ) {
$this -> check_current_query = false ;
}
2010-02-22 17:53:54 -05:00
$this -> query ( $query );
2014-11-14 16:28:22 -05:00
}
2003-05-28 04:41:04 -04:00
2020-05-03 07:45:06 -04:00
// Extract var out of cached results based on x,y vals.
2017-11-30 18:11:00 -05:00
if ( ! empty ( $this -> last_result [ $y ] ) ) {
$values = array_values ( get_object_vars ( $this -> last_result [ $y ] ) );
2003-05-28 04:41:04 -04:00
}
2020-05-03 07:45:06 -04:00
// If there is a value return it, else return null.
2020-02-09 11:55:09 -05:00
return ( isset ( $values [ $x ] ) && '' !== $values [ $x ] ) ? $values [ $x ] : null ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2006-11-29 20:44:33 -05:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves one row from the database .
2008-06-22 16:23:23 -04:00
*
2009-04-10 17:57:40 -04:00
* Executes a SQL query and returns the row from the SQL result .
*
2008-06-22 16:23:23 -04:00
* @ since 0.71
*
2015-05-28 16:10:29 -04:00
* @ param string | null $query SQL query .
2020-07-23 17:55:04 -04:00
* @ param string $output Optional . The required return type . One of OBJECT , ARRAY_A , or ARRAY_N , which
* correspond to an stdClass object , an associative array , or a numeric array ,
* respectively . Default OBJECT .
2015-05-28 16:10:29 -04:00
* @ param int $y Optional . Row to return . Indexed from 0.
2020-05-03 07:45:06 -04:00
* @ return array | object | null | void Database query result in format specified by $output or null on failure .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function get_row ( $query = null , $output = OBJECT , $y = 0 ) {
2004-04-25 22:28:06 -04:00
$this -> func_call = " \$ db->get_row( \" $query\ " , $output , $y ) " ;
2015-04-20 00:46:25 -04:00
2014-11-14 16:28:22 -05:00
if ( $query ) {
2021-07-10 07:15:59 -04:00
if ( $this -> check_current_query && $this -> check_safe_collation ( $query ) ) {
$this -> check_current_query = false ;
}
2010-02-22 17:53:54 -05:00
$this -> query ( $query );
2014-11-14 16:28:22 -05:00
} else {
2007-08-09 21:33:56 -04:00
return null ;
2014-11-14 16:28:22 -05:00
}
2007-06-13 22:25:30 -04:00
2017-11-30 18:11:00 -05:00
if ( ! isset ( $this -> last_result [ $y ] ) ) {
2006-09-22 10:40:40 -04:00
return null ;
2017-11-30 18:11:00 -05:00
}
2003-05-28 04:41:04 -04:00
2020-07-18 07:39:04 -04:00
if ( OBJECT === $output ) {
2017-11-30 18:11:00 -05:00
return $this -> last_result [ $y ] ? $this -> last_result [ $y ] : null ;
2020-07-18 07:39:04 -04:00
} elseif ( ARRAY_A === $output ) {
2017-11-30 18:11:00 -05:00
return $this -> last_result [ $y ] ? get_object_vars ( $this -> last_result [ $y ] ) : null ;
2020-07-18 07:39:04 -04:00
} elseif ( ARRAY_N === $output ) {
2017-11-30 18:11:00 -05:00
return $this -> last_result [ $y ] ? array_values ( get_object_vars ( $this -> last_result [ $y ] ) ) : null ;
2020-02-09 11:55:09 -05:00
} elseif ( OBJECT === strtoupper ( $output ) ) {
2020-01-28 19:45:18 -05:00
// Back compat for OBJECT being previously case-insensitive.
2017-11-30 18:11:00 -05:00
return $this -> last_result [ $y ] ? $this -> last_result [ $y ] : null ;
2004-04-25 22:54:06 -04:00
} else {
2017-11-30 18:11:00 -05:00
$this -> print_error ( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
2003-05-28 04:41:04 -04:00
}
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2006-11-29 20:44:33 -05:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves one column from the database .
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* Executes a SQL query and returns the column from the SQL result .
* If the SQL result contains more than one column , the column specified is returned .
* If $query is null , the specified column from the previous SQL result is returned .
2009-04-10 17:57:40 -04:00
*
2008-06-22 16:23:23 -04:00
* @ since 0.71
*
2010-02-23 17:04:29 -05:00
* @ param string | null $query Optional . SQL query . Defaults to previous query .
2015-05-28 16:10:29 -04:00
* @ param int $x Optional . Column to return . Indexed from 0.
2010-02-22 17:53:54 -05:00
* @ return array Database query result . Array indexed from 0 by SQL result row number .
2006-11-29 20:44:33 -05:00
*/
2017-11-30 18:11:00 -05:00
public function get_col ( $query = null , $x = 0 ) {
2014-11-14 16:28:22 -05:00
if ( $query ) {
2021-07-10 07:15:59 -04:00
if ( $this -> check_current_query && $this -> check_safe_collation ( $query ) ) {
$this -> check_current_query = false ;
}
2010-02-22 17:53:54 -05:00
$this -> query ( $query );
2014-11-14 16:28:22 -05:00
}
2007-08-10 15:30:24 -04:00
2007-08-27 15:44:53 -04:00
$new_array = array ();
2020-01-28 19:45:18 -05:00
// Extract the column values.
2018-12-17 13:39:52 -05:00
if ( $this -> last_result ) {
for ( $i = 0 , $j = count ( $this -> last_result ); $i < $j ; $i ++ ) {
$new_array [ $i ] = $this -> get_var ( null , $x , $i );
}
2004-04-25 22:28:06 -04:00
}
return $new_array ;
}
2003-05-28 04:41:04 -04:00
2006-11-29 20:44:33 -05:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves an entire SQL result set from the database ( i . e . , many rows ) .
2009-04-10 17:57:40 -04:00
*
* Executes a SQL query and returns the entire SQL result .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2015-05-28 16:10:29 -04:00
* @ param string $query SQL query .
* @ param string $output Optional . Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants .
2020-05-03 07:45:06 -04:00
* With one of the first three , return an array of rows indexed
* from 0 by SQL result row number . Each row is an associative array
* ( column => value , ... ), a numerically indexed array ( 0 => value , ... ),
* or an object ( -> column = value ), respectively . With OBJECT_K ,
* return an associative array of row objects keyed by the value
* of each row 's first column' s value . Duplicate keys are discarded .
2020-05-02 13:25:07 -04:00
* @ return array | object | null Database query results .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function get_results ( $query = null , $output = OBJECT ) {
2004-04-25 22:28:06 -04:00
$this -> func_call = " \$ db->get_results( \" $query\ " , $output ) " ;
2003-05-28 04:41:04 -04:00
2014-11-14 16:28:22 -05:00
if ( $query ) {
2021-07-10 07:15:59 -04:00
if ( $this -> check_current_query && $this -> check_safe_collation ( $query ) ) {
$this -> check_current_query = false ;
}
2010-02-22 17:53:54 -05:00
$this -> query ( $query );
2014-11-14 16:28:22 -05:00
} else {
2007-08-09 21:33:56 -04:00
return null ;
2014-11-14 16:28:22 -05:00
}
2003-05-28 04:41:04 -04:00
2010-02-15 05:00:39 -05:00
$new_array = array ();
2020-07-18 07:39:04 -04:00
if ( OBJECT === $output ) {
2020-01-28 19:45:18 -05:00
// Return an integer-keyed array of row objects.
2010-11-12 17:44:16 -05:00
return $this -> last_result ;
2020-07-18 07:39:04 -04:00
} elseif ( OBJECT_K === $output ) {
2020-01-28 19:45:18 -05:00
// Return an array of row objects with keys from column 1.
// (Duplicates are discarded.)
2018-12-17 13:39:52 -05:00
if ( $this -> last_result ) {
foreach ( $this -> last_result as $row ) {
$var_by_ref = get_object_vars ( $row );
$key = array_shift ( $var_by_ref );
if ( ! isset ( $new_array [ $key ] ) ) {
$new_array [ $key ] = $row ;
}
2017-11-30 18:11:00 -05:00
}
2008-02-25 17:00:27 -05:00
}
return $new_array ;
2020-07-18 07:39:04 -04:00
} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
2010-11-12 17:44:16 -05:00
// Return an integer-keyed array of...
if ( $this -> last_result ) {
2015-08-25 16:28:22 -04:00
foreach ( ( array ) $this -> last_result as $row ) {
2020-07-18 07:39:04 -04:00
if ( ARRAY_N === $output ) {
2020-01-28 19:45:18 -05:00
// ...integer-keyed row arrays.
2010-11-12 17:44:16 -05:00
$new_array [] = array_values ( get_object_vars ( $row ) );
} else {
2020-01-28 19:45:18 -05:00
// ...column name-keyed row arrays.
2010-11-12 17:44:16 -05:00
$new_array [] = get_object_vars ( $row );
}
}
2003-05-28 04:41:04 -04:00
}
2010-03-11 16:30:29 -05:00
return $new_array ;
2014-03-03 12:36:13 -05:00
} elseif ( strtoupper ( $output ) === OBJECT ) {
2020-01-28 19:45:18 -05:00
// Back compat for OBJECT being previously case-insensitive.
2014-03-03 12:36:13 -05:00
return $this -> last_result ;
2003-05-28 04:41:04 -04:00
}
2010-03-11 16:30:29 -05:00
return null ;
2004-04-25 22:28:06 -04:00
}
2003-05-28 04:41:04 -04:00
2014-11-14 16:28:22 -05:00
/**
* Retrieves the character set for the given table .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $table Table name .
2015-04-05 13:17:27 -04:00
* @ return string | WP_Error Table character set , WP_Error object if it couldn ' t be found .
2014-11-14 16:28:22 -05:00
*/
protected function get_table_charset ( $table ) {
2014-12-01 22:00:23 -05:00
$tablekey = strtolower ( $table );
2014-11-14 16:28:22 -05:00
/**
2016-05-22 14:50:28 -04:00
* Filters the table charset value before the DB is checked .
2014-11-14 16:28:22 -05:00
*
2021-12-29 12:28:00 -05:00
* Returning a non - null value from the filter will effectively short - circuit
2014-11-14 16:28:22 -05:00
* checking the DB for the charset , returning that value instead .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
2021-12-29 18:14:01 -05:00
* @ param string | WP_Error | null $charset The character set to use , WP_Error object
* if it couldn ' t be found . Default null .
* @ param string $table The name of the table being checked .
2014-11-14 16:28:22 -05:00
*/
$charset = apply_filters ( 'pre_get_table_charset' , null , $table );
if ( null !== $charset ) {
return $charset ;
}
2014-12-01 22:00:23 -05:00
if ( isset ( $this -> table_charset [ $tablekey ] ) ) {
return $this -> table_charset [ $tablekey ];
2014-11-14 16:28:22 -05:00
}
2019-07-02 19:42:58 -04:00
$charsets = array ();
$columns = array ();
2015-05-06 01:01:26 -04:00
$table_parts = explode ( '.' , $table );
2017-11-30 18:11:00 -05:00
$table = '`' . implode ( '`.`' , $table_parts ) . '`' ;
$results = $this -> get_results ( " SHOW FULL COLUMNS FROM $table " );
2014-11-14 16:28:22 -05:00
if ( ! $results ) {
2021-11-30 16:03:00 -05:00
return new WP_Error ( 'wpdb_get_table_charset_failure' , __ ( 'Could not retrieve table charset.' ) );
2014-11-14 16:28:22 -05:00
}
foreach ( $results as $column ) {
$columns [ strtolower ( $column -> Field ) ] = $column ;
}
2014-12-01 22:00:23 -05:00
$this -> col_meta [ $tablekey ] = $columns ;
2014-11-14 16:28:22 -05:00
foreach ( $columns as $column ) {
2014-11-17 22:38:23 -05:00
if ( ! empty ( $column -> Collation ) ) {
2014-11-14 16:28:22 -05:00
list ( $charset ) = explode ( '_' , $column -> Collation );
2015-03-31 22:22:31 -04:00
// If the current connection can't support utf8mb4 characters, let's only send 3-byte utf8 characters.
if ( 'utf8mb4' === $charset && ! $this -> has_cap ( 'utf8mb4' ) ) {
$charset = 'utf8' ;
}
2014-11-14 16:28:22 -05:00
$charsets [ strtolower ( $charset ) ] = true ;
}
list ( $type ) = explode ( '(' , $column -> Type );
// A binary/blob means the whole query gets treated like this.
2020-04-04 23:02:11 -04:00
if ( in_array ( strtoupper ( $type ), array ( 'BINARY' , 'VARBINARY' , 'TINYBLOB' , 'MEDIUMBLOB' , 'BLOB' , 'LONGBLOB' ), true ) ) {
2014-12-01 22:00:23 -05:00
$this -> table_charset [ $tablekey ] = 'binary' ;
2014-11-14 16:28:22 -05:00
return 'binary' ;
}
}
// utf8mb3 is an alias for utf8.
if ( isset ( $charsets [ 'utf8mb3' ] ) ) {
$charsets [ 'utf8' ] = true ;
unset ( $charsets [ 'utf8mb3' ] );
}
// Check if we have more than one charset in play.
$count = count ( $charsets );
if ( 1 === $count ) {
$charset = key ( $charsets );
} elseif ( 0 === $count ) {
// No charsets, assume this table can store whatever.
2014-11-17 22:38:23 -05:00
$charset = false ;
2014-11-14 16:28:22 -05:00
} else {
// More than one charset. Remove latin1 if present and recalculate.
unset ( $charsets [ 'latin1' ] );
$count = count ( $charsets );
if ( 1 === $count ) {
// Only one charset (besides latin1).
$charset = key ( $charsets );
} elseif ( 2 === $count && isset ( $charsets [ 'utf8' ], $charsets [ 'utf8mb4' ] ) ) {
// Two charsets, but they're utf8 and utf8mb4, use utf8.
$charset = 'utf8' ;
} else {
// Two mixed character sets. ascii.
$charset = 'ascii' ;
}
}
2014-12-01 22:00:23 -05:00
$this -> table_charset [ $tablekey ] = $charset ;
2014-11-14 16:28:22 -05:00
return $charset ;
}
/**
* Retrieves the character set for the given column .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $table Table name .
* @ param string $column Column name .
2020-05-03 07:45:06 -04:00
* @ return string | false | WP_Error Column character set as a string . False if the column has
* no character set . WP_Error object if there was an error .
2014-11-14 16:28:22 -05:00
*/
2015-02-05 23:51:22 -05:00
public function get_col_charset ( $table , $column ) {
2017-11-30 18:11:00 -05:00
$tablekey = strtolower ( $table );
2014-12-01 22:00:23 -05:00
$columnkey = strtolower ( $column );
2014-11-14 16:28:22 -05:00
/**
2016-05-22 14:50:28 -04:00
* Filters the column charset value before the DB is checked .
2014-11-14 16:28:22 -05:00
*
* Passing a non - null value to the filter will short - circuit
* checking the DB for the charset , returning that value instead .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
2019-09-21 13:41:57 -04:00
* @ param string | null $charset The character set to use . Default null .
* @ param string $table The name of the table being checked .
* @ param string $column The name of the column being checked .
2014-11-14 16:28:22 -05:00
*/
$charset = apply_filters ( 'pre_get_col_charset' , null , $table , $column );
if ( null !== $charset ) {
return $charset ;
}
2014-11-17 22:38:23 -05:00
// Skip this entirely if this isn't a MySQL database.
2015-09-27 21:17:24 -04:00
if ( empty ( $this -> is_mysql ) ) {
2014-11-17 22:38:23 -05:00
return false ;
}
2014-12-01 22:00:23 -05:00
if ( empty ( $this -> table_charset [ $tablekey ] ) ) {
2014-11-14 16:28:22 -05:00
// This primes column information for us.
$table_charset = $this -> get_table_charset ( $table );
if ( is_wp_error ( $table_charset ) ) {
return $table_charset ;
}
}
// If still no column information, return the table charset.
2014-12-01 22:00:23 -05:00
if ( empty ( $this -> col_meta [ $tablekey ] ) ) {
return $this -> table_charset [ $tablekey ];
2014-11-14 16:28:22 -05:00
}
// If this column doesn't exist, return the table charset.
2014-12-01 22:00:23 -05:00
if ( empty ( $this -> col_meta [ $tablekey ][ $columnkey ] ) ) {
return $this -> table_charset [ $tablekey ];
2014-11-14 16:28:22 -05:00
}
// Return false when it's not a string column.
2014-12-01 22:00:23 -05:00
if ( empty ( $this -> col_meta [ $tablekey ][ $columnkey ] -> Collation ) ) {
2014-11-14 16:28:22 -05:00
return false ;
}
2014-12-01 22:00:23 -05:00
list ( $charset ) = explode ( '_' , $this -> col_meta [ $tablekey ][ $columnkey ] -> Collation );
2014-11-14 16:28:22 -05:00
return $charset ;
}
2015-04-27 10:03:27 -04:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves the maximum string length allowed in a given column .
2020-05-02 13:25:07 -04:00
*
2015-05-05 23:00:25 -04:00
* The length may either be specified as a byte length or a character length .
2015-04-27 10:03:27 -04:00
*
* @ since 4.2 . 1
*
* @ param string $table Table name .
* @ param string $column Column name .
2022-04-28 05:46:21 -04:00
* @ return array | false | WP_Error {
* Array of column length information , false if the column has no length ( for
* example , numeric column ), WP_Error object if there was an error .
*
* @ type int $length The column length .
* @ type string $type One of 'byte' or 'char' .
2015-04-27 10:03:27 -04:00
*/
public function get_col_length ( $table , $column ) {
2017-11-30 18:11:00 -05:00
$tablekey = strtolower ( $table );
2015-04-27 10:03:27 -04:00
$columnkey = strtolower ( $column );
// Skip this entirely if this isn't a MySQL database.
2015-09-27 21:17:24 -04:00
if ( empty ( $this -> is_mysql ) ) {
2015-04-27 10:03:27 -04:00
return false ;
}
if ( empty ( $this -> col_meta [ $tablekey ] ) ) {
// This primes column information for us.
$table_charset = $this -> get_table_charset ( $table );
if ( is_wp_error ( $table_charset ) ) {
return $table_charset ;
}
}
if ( empty ( $this -> col_meta [ $tablekey ][ $columnkey ] ) ) {
return false ;
}
$typeinfo = explode ( '(' , $this -> col_meta [ $tablekey ][ $columnkey ] -> Type );
$type = strtolower ( $typeinfo [ 0 ] );
if ( ! empty ( $typeinfo [ 1 ] ) ) {
$length = trim ( $typeinfo [ 1 ], ')' );
} else {
$length = false ;
}
2017-11-30 18:11:00 -05:00
switch ( $type ) {
2015-04-27 10:03:27 -04:00
case 'char' :
case 'varchar' :
2015-05-05 23:00:25 -04:00
return array (
'type' => 'char' ,
'length' => ( int ) $length ,
);
2015-08-21 14:41:25 -04:00
2015-05-05 23:00:25 -04:00
case 'binary' :
case 'varbinary' :
return array (
'type' => 'byte' ,
'length' => ( int ) $length ,
);
2015-08-21 14:41:25 -04:00
2015-04-27 10:03:27 -04:00
case 'tinyblob' :
case 'tinytext' :
2015-05-05 23:00:25 -04:00
return array (
'type' => 'byte' ,
'length' => 255 , // 2^8 - 1
);
2015-08-21 14:41:25 -04:00
2015-04-27 10:03:27 -04:00
case 'blob' :
case 'text' :
2015-05-05 23:00:25 -04:00
return array (
'type' => 'byte' ,
'length' => 65535 , // 2^16 - 1
);
2015-08-21 14:41:25 -04:00
2015-04-27 10:03:27 -04:00
case 'mediumblob' :
case 'mediumtext' :
2015-05-05 23:00:25 -04:00
return array (
'type' => 'byte' ,
'length' => 16777215 , // 2^24 - 1
);
2015-08-21 14:41:25 -04:00
2015-04-27 10:03:27 -04:00
case 'longblob' :
case 'longtext' :
2015-05-05 23:00:25 -04:00
return array (
'type' => 'byte' ,
'length' => 4294967295 , // 2^32 - 1
);
2015-08-21 14:41:25 -04:00
2015-04-27 10:03:27 -04:00
default :
return false ;
}
}
2014-11-14 16:28:22 -05:00
/**
2020-05-03 07:45:06 -04:00
* Checks if a string is ASCII .
2014-11-14 16:28:22 -05:00
*
2020-05-03 07:45:06 -04:00
* The negative regex is faster for non - ASCII strings , as it allows
* the search to finish as soon as it encounters a non - ASCII character .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $string String to check .
* @ return bool True if ASCII , false if not .
*/
protected function check_ascii ( $string ) {
if ( function_exists ( 'mb_check_encoding' ) ) {
if ( mb_check_encoding ( $string , 'ASCII' ) ) {
return true ;
}
} elseif ( ! preg_match ( '/[^\x00-\x7F]/' , $string ) ) {
return true ;
}
return false ;
}
2015-04-20 00:46:25 -04:00
/**
2020-05-03 07:45:06 -04:00
* Checks if the query is accessing a collation considered safe on the current version of MySQL .
2015-04-20 00:46:25 -04:00
*
* @ since 4.2 . 0
*
* @ param string $query The query to check .
* @ return bool True if the collation is safe , false if it isn ' t .
*/
protected function check_safe_collation ( $query ) {
if ( $this -> checking_collation ) {
return true ;
}
2015-04-21 01:11:27 -04:00
// We don't need to check the collation for queries that don't read data.
$query = ltrim ( $query , " \r \n \t ( " );
2015-05-06 02:05:26 -04:00
if ( preg_match ( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i' , $query ) ) {
2015-04-21 01:11:27 -04:00
return true ;
}
2015-04-21 01:24:26 -04:00
// All-ASCII queries don't need extra checking.
if ( $this -> check_ascii ( $query ) ) {
return true ;
}
2015-04-20 00:46:25 -04:00
$table = $this -> get_table_from_query ( $query );
if ( ! $table ) {
return false ;
}
$this -> checking_collation = true ;
2017-11-30 18:11:00 -05:00
$collation = $this -> get_table_charset ( $table );
2015-04-20 00:46:25 -04:00
$this -> checking_collation = false ;
2015-04-21 01:11:27 -04:00
// Tables with no collation, or latin1 only, don't need extra checking.
if ( false === $collation || 'latin1' === $collation ) {
return true ;
}
2015-04-20 00:46:25 -04:00
$table = strtolower ( $table );
if ( empty ( $this -> col_meta [ $table ] ) ) {
return false ;
}
2015-04-21 01:11:27 -04:00
// If any of the columns don't have one of these collations, it needs more sanity checking.
2015-08-25 16:28:22 -04:00
foreach ( $this -> col_meta [ $table ] as $col ) {
2015-04-20 00:46:25 -04:00
if ( empty ( $col -> Collation ) ) {
continue ;
}
if ( ! in_array ( $col -> Collation , array ( 'utf8_general_ci' , 'utf8_bin' , 'utf8mb4_general_ci' , 'utf8mb4_bin' ), true ) ) {
return false ;
}
}
return true ;
}
2014-11-14 16:28:22 -05:00
/**
* Strips any invalid characters based on value / charset pairs .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
2020-05-03 07:45:06 -04:00
* @ param array $data Array of value arrays . Each value array has the keys 'value' and 'charset' .
* An optional 'ascii' key can be set to false to avoid redundant ASCII checks .
* @ return array | WP_Error The $data parameter , with invalid characters removed from each value .
* This works as a passthrough : any additional keys such as 'field' are
* retained in each value array . If we cannot remove invalid characters ,
* a WP_Error object is returned .
2014-11-14 16:28:22 -05:00
*/
protected function strip_invalid_text ( $data ) {
$db_check_string = false ;
foreach ( $data as & $value ) {
$charset = $value [ 'charset' ];
2015-05-05 23:00:25 -04:00
if ( is_array ( $value [ 'length' ] ) ) {
2017-11-30 18:11:00 -05:00
$length = $value [ 'length' ][ 'length' ];
2015-07-17 03:07:26 -04:00
$truncate_by_byte_length = 'byte' === $value [ 'length' ][ 'type' ];
2015-05-05 23:00:25 -04:00
} else {
$length = false ;
2020-05-03 07:45:06 -04:00
// Since we have no length, we'll never truncate. Initialize the variable to false.
// True would take us through an unnecessary (for this case) codepath below.
2015-07-17 03:07:26 -04:00
$truncate_by_byte_length = false ;
2015-05-05 23:00:25 -04:00
}
// There's no charset to work with.
if ( false === $charset ) {
2014-11-14 16:28:22 -05:00
continue ;
}
2015-05-05 23:00:25 -04:00
// Column isn't a string.
2014-11-17 22:38:23 -05:00
if ( ! is_string ( $value [ 'value' ] ) ) {
2014-11-14 16:28:22 -05:00
continue ;
}
2015-05-05 23:00:25 -04:00
$needs_validation = true ;
if (
2020-05-02 13:25:07 -04:00
// latin1 can store any byte sequence.
2015-05-05 23:00:25 -04:00
'latin1' === $charset
||
// ASCII is always OK.
( ! isset ( $value [ 'ascii' ] ) && $this -> check_ascii ( $value [ 'value' ] ) )
) {
$truncate_by_byte_length = true ;
2017-11-30 18:11:00 -05:00
$needs_validation = false ;
2014-11-14 16:28:22 -05:00
}
2015-05-05 23:00:25 -04:00
if ( $truncate_by_byte_length ) {
mbstring_binary_safe_encoding ();
if ( false !== $length && strlen ( $value [ 'value' ] ) > $length ) {
$value [ 'value' ] = substr ( $value [ 'value' ], 0 , $length );
}
reset_mbstring_encoding ();
if ( ! $needs_validation ) {
2014-11-14 16:28:22 -05:00
continue ;
}
}
2015-02-05 23:51:22 -05:00
// utf8 can be handled by regex, which is a bunch faster than a DB lookup.
2015-05-05 23:00:25 -04:00
if ( ( 'utf8' === $charset || 'utf8mb3' === $charset || 'utf8mb4' === $charset ) && function_exists ( 'mb_strlen' ) ) {
2014-11-14 16:28:22 -05:00
$regex = ' /
(
( ? : [ \x00 - \x7F ] # single-byte sequences 0xxxxxxx
| [ \xC2 - \xDF ][ \x80 - \xBF ] # double-byte sequences 110xxxxx 10xxxxxx
| \xE0 [ \xA0 - \xBF ][ \x80 - \xBF ] # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [ \xE1 - \xEC ][ \x80 - \xBF ]{ 2 }
| \xED [ \x80 - \x9F ][ \x80 - \xBF ]
2015-02-05 23:51:22 -05:00
| [ \xEE - \xEF ][ \x80 - \xBF ]{ 2 } ' ;
2015-05-05 23:00:25 -04:00
if ( 'utf8mb4' === $charset ) {
2015-02-05 23:51:22 -05:00
$regex .= '
| \xF0 [ \x90 - \xBF ][ \x80 - \xBF ]{ 2 } # four-byte sequences 11110xxx 10xxxxxx * 3
| [ \xF1 - \xF3 ][ \x80 - \xBF ]{ 3 }
| \xF4 [ \x80 - \x8F ][ \x80 - \xBF ]{ 2 }
' ;
}
2017-11-30 18:11:00 -05:00
$regex .= ' ){ 1 , 40 } # ...one or more times
2014-11-14 16:28:22 -05:00
)
| . # anything else
/ x ' ;
$value [ 'value' ] = preg_replace ( $regex , '$1' , $value [ 'value' ] );
2015-05-05 23:00:25 -04:00
if ( false !== $length && mb_strlen ( $value [ 'value' ], 'UTF-8' ) > $length ) {
$value [ 'value' ] = mb_substr ( $value [ 'value' ], 0 , $length , 'UTF-8' );
}
2014-11-14 16:28:22 -05:00
continue ;
}
// We couldn't use any local conversions, send it to the DB.
2019-07-02 19:42:58 -04:00
$value [ 'db' ] = true ;
$db_check_string = true ;
2014-11-14 16:28:22 -05:00
}
unset ( $value ); // Remove by reference.
if ( $db_check_string ) {
$queries = array ();
foreach ( $data as $col => $value ) {
if ( ! empty ( $value [ 'db' ] ) ) {
2015-05-05 23:00:25 -04:00
// We're going to need to truncate by characters or bytes, depending on the length value we have.
2019-07-15 02:25:57 -04:00
if ( isset ( $value [ 'length' ][ 'type' ] ) && 'byte' === $value [ 'length' ][ 'type' ] ) {
2015-07-17 02:34:26 -04:00
// Using binary causes LEFT() to truncate by bytes.
$charset = 'binary' ;
2015-05-05 23:00:25 -04:00
} else {
2015-07-17 02:34:26 -04:00
$charset = $value [ 'charset' ];
2015-05-05 23:00:25 -04:00
}
2015-11-17 01:13:26 -05:00
if ( $this -> charset ) {
$connection_charset = $this -> charset ;
} else {
if ( $this -> use_mysqli ) {
$connection_charset = mysqli_character_set_name ( $this -> dbh );
} else {
$connection_charset = mysql_client_encoding ();
}
}
2015-07-17 03:07:26 -04:00
if ( is_array ( $value [ 'length' ] ) ) {
2017-11-30 18:11:00 -05:00
$length = sprintf ( '%.0f' , $value [ 'length' ][ 'length' ] );
2017-09-19 13:56:45 -04:00
$queries [ $col ] = $this -> prepare ( " CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset ) " , $value [ 'value' ] );
2017-11-30 18:11:00 -05:00
} elseif ( 'binary' !== $charset ) {
2015-07-17 03:07:26 -04:00
// If we don't have a length, there's no need to convert binary - it will always return the same result.
2015-11-17 01:13:26 -05:00
$queries [ $col ] = $this -> prepare ( " CONVERT( CONVERT( %s USING $charset ) USING $connection_charset ) " , $value [ 'value' ] );
2015-07-17 03:07:26 -04:00
}
2015-07-17 02:34:26 -04:00
2015-04-22 11:02:28 -04:00
unset ( $data [ $col ][ 'db' ] );
2014-11-14 16:28:22 -05:00
}
}
2015-07-17 02:34:26 -04:00
$sql = array ();
foreach ( $queries as $column => $query ) {
2014-11-14 16:28:22 -05:00
if ( ! $query ) {
continue ;
}
2015-07-17 02:34:26 -04:00
$sql [] = $query . " AS x_ $column " ;
}
2014-11-14 16:28:22 -05:00
2015-07-17 02:34:26 -04:00
$this -> check_current_query = false ;
2017-11-30 18:11:00 -05:00
$row = $this -> get_row ( 'SELECT ' . implode ( ', ' , $sql ), ARRAY_A );
2015-07-17 02:34:26 -04:00
if ( ! $row ) {
2021-11-30 16:03:00 -05:00
return new WP_Error ( 'wpdb_strip_invalid_text_failure' , __ ( 'Could not strip invalid text.' ) );
2014-11-14 16:28:22 -05:00
}
2015-07-17 02:34:26 -04:00
foreach ( array_keys ( $data ) as $column ) {
2017-11-30 18:11:00 -05:00
if ( isset ( $row [ " x_ $column " ] ) ) {
$data [ $column ][ 'value' ] = $row [ " x_ $column " ];
2015-07-28 02:32:24 -04:00
}
2014-11-14 16:28:22 -05:00
}
}
return $data ;
}
/**
* Strips any invalid characters from the query .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
2020-05-03 07:45:06 -04:00
* @ param string $query Query to convert .
2015-04-05 13:17:27 -04:00
* @ return string | WP_Error The converted query , or a WP_Error object if the conversion fails .
2014-11-14 16:28:22 -05:00
*/
protected function strip_invalid_text_from_query ( $query ) {
2015-05-06 02:05:26 -04:00
// We don't need to check the collation for queries that don't read data.
$trimmed_query = ltrim ( $query , " \r \n \t ( " );
if ( preg_match ( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i' , $trimmed_query ) ) {
return $query ;
}
2014-11-14 16:28:22 -05:00
$table = $this -> get_table_from_query ( $query );
if ( $table ) {
$charset = $this -> get_table_charset ( $table );
if ( is_wp_error ( $charset ) ) {
return $charset ;
}
2020-01-28 19:45:18 -05:00
// We can't reliably strip text from tables containing binary/blob columns.
2014-11-14 16:28:22 -05:00
if ( 'binary' === $charset ) {
return $query ;
}
} else {
$charset = $this -> charset ;
}
$data = array (
'value' => $query ,
'charset' => $charset ,
'ascii' => false ,
2015-05-05 23:00:25 -04:00
'length' => false ,
2014-11-14 16:28:22 -05:00
);
$data = $this -> strip_invalid_text ( array ( $data ) );
if ( is_wp_error ( $data ) ) {
return $data ;
}
return $data [ 0 ][ 'value' ];
}
/**
* Strips any invalid characters from the string for a given table and column .
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $table Table name .
* @ param string $column Column name .
* @ param string $value The text to check .
2015-04-05 13:17:27 -04:00
* @ return string | WP_Error The converted string , or a WP_Error object if the conversion fails .
2014-11-14 16:28:22 -05:00
*/
public function strip_invalid_text_for_column ( $table , $column , $value ) {
2015-05-05 23:00:25 -04:00
if ( ! is_string ( $value ) ) {
2014-11-14 16:28:22 -05:00
return $value ;
}
$charset = $this -> get_col_charset ( $table , $column );
if ( ! $charset ) {
// Not a string column.
return $value ;
} elseif ( is_wp_error ( $charset ) ) {
// Bail on real errors.
return $charset ;
}
$data = array (
$column => array (
'value' => $value ,
'charset' => $charset ,
2015-05-05 23:00:25 -04:00
'length' => $this -> get_col_length ( $table , $column ),
2017-11-30 18:11:00 -05:00
),
2014-11-14 16:28:22 -05:00
);
$data = $this -> strip_invalid_text ( $data );
if ( is_wp_error ( $data ) ) {
return $data ;
}
return $data [ $column ][ 'value' ];
}
/**
2020-05-03 07:45:06 -04:00
* Finds the first table name referenced in a query .
2014-11-14 16:28:22 -05:00
*
2015-01-04 22:51:22 -05:00
* @ since 4.2 . 0
2014-11-14 16:28:22 -05:00
*
* @ param string $query The query to search .
2020-06-19 18:55:12 -04:00
* @ return string | false The table name found , or false if a table couldn ' t be found .
2014-11-14 16:28:22 -05:00
*/
protected function get_table_from_query ( $query ) {
// Remove characters that can legally trail the table name.
$query = rtrim ( $query , ';/-#' );
// Allow (select...) union [...] style queries. Use the first query's table name.
$query = ltrim ( $query , " \r \n \t ( " );
2015-07-14 06:19:24 -04:00
// Strip everything between parentheses except nested selects.
$query = preg_replace ( '/\((?!\s*select)[^(]*?\)/is' , '()' , $query );
2014-11-14 16:28:22 -05:00
// Quickly match most common queries.
2017-11-30 18:11:00 -05:00
if ( preg_match (
'/^\s*(?:'
2014-11-14 16:28:22 -05:00
. 'SELECT.*?\s+FROM'
. '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
. '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
. '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
2016-09-01 20:19:28 -04:00
. '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?'
2018-08-16 21:51:36 -04:00
. ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is' ,
$query ,
$maybe
2017-11-30 18:11:00 -05:00
) ) {
2015-05-06 01:01:26 -04:00
return str_replace ( '`' , '' , $maybe [ 1 ] );
2014-11-14 16:28:22 -05:00
}
2016-11-16 23:21:31 -05:00
// SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts'
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 ) ) {
return $maybe [ 2 ];
}
2020-01-28 19:45:18 -05:00
/*
* SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%'
2020-05-03 07:45:06 -04:00
* This quoted LIKE operand seldom holds a full table name .
* It is usually a pattern for matching a prefix so we just
* strip the trailing % and unescape the _ to get 'wp_123_'
* which drop - ins can use for routing these SQL statements .
2020-01-28 19:45:18 -05:00
*/
2016-11-16 23:21:31 -05:00
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 ) ) {
return str_replace ( '\\_' , '_' , $maybe [ 2 ] );
2014-11-14 16:28:22 -05:00
}
// Big pattern for the rest of the table-related queries.
2017-11-30 18:11:00 -05:00
if ( preg_match (
'/^\s*(?:'
2014-11-14 16:28:22 -05:00
. '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
. '|DESCRIBE|DESC|EXPLAIN|HANDLER'
. '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
. '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE'
. '|TRUNCATE(?:\s+TABLE)?'
. '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
. '|ALTER(?:\s+IGNORE)?\s+TABLE'
. '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
. '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
. '|DROP\s+INDEX.*\s+ON'
. '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
. '|(?:GRANT|REVOKE).*ON\s+TABLE'
. '|SHOW\s+(?:.*FROM|.*TABLE)'
2018-08-16 21:51:36 -04:00
. ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is' ,
$query ,
$maybe
2017-11-30 18:11:00 -05:00
) ) {
2015-05-06 01:01:26 -04:00
return str_replace ( '`' , '' , $maybe [ 1 ] );
2014-11-14 16:28:22 -05:00
}
return false ;
}
2012-08-08 02:24:59 -04:00
/**
2020-05-03 07:45:06 -04:00
* Loads the column metadata from the last query .
2012-08-08 02:24:59 -04:00
*
* @ since 3.5 . 0
*/
protected function load_col_info () {
2017-11-30 18:11:00 -05:00
if ( $this -> col_info ) {
2012-08-08 02:24:59 -04:00
return ;
2017-11-30 18:11:00 -05:00
}
2012-08-08 02:24:59 -04:00
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
2015-12-10 22:40:26 -05:00
$num_fields = mysqli_num_fields ( $this -> result );
2015-05-19 16:30:27 -04:00
for ( $i = 0 ; $i < $num_fields ; $i ++ ) {
2015-12-10 22:40:26 -05:00
$this -> col_info [ $i ] = mysqli_fetch_field ( $this -> result );
2014-02-24 19:40:13 -05:00
}
} else {
2015-12-10 22:40:26 -05:00
$num_fields = mysql_num_fields ( $this -> result );
2015-05-19 16:30:27 -04:00
for ( $i = 0 ; $i < $num_fields ; $i ++ ) {
2015-12-10 22:40:26 -05:00
$this -> col_info [ $i ] = mysql_fetch_field ( $this -> result , $i );
2014-02-24 19:40:13 -05:00
}
2012-08-08 02:24:59 -04:00
}
}
2006-11-29 20:44:33 -05:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves column metadata from the last query .
2008-06-22 16:23:23 -04:00
*
* @ since 0.71
*
2020-07-23 17:11:05 -04:00
* @ param string $info_type Optional . Possible values include 'name' , 'table' , 'def' , 'max_length' ,
* 'not_null' , 'primary_key' , 'multiple_key' , 'unique_key' , 'numeric' ,
* 'blob' , 'type' , 'unsigned' , 'zerofill' . Default 'name' .
* @ 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 . Default - 1.
2020-05-03 07:45:06 -04:00
* @ return mixed Column results .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function get_col_info ( $info_type = 'name' , $col_offset = - 1 ) {
2012-08-08 02:24:59 -04:00
$this -> load_col_info ();
2004-04-25 22:28:06 -04:00
if ( $this -> col_info ) {
2020-07-18 07:39:04 -04:00
if ( - 1 === $col_offset ) {
2017-11-30 18:11:00 -05:00
$i = 0 ;
2010-02-15 05:00:39 -05:00
$new_array = array ();
2015-08-25 16:28:22 -04:00
foreach ( ( array ) $this -> col_info as $col ) {
2017-11-30 18:11:00 -05:00
$new_array [ $i ] = $col -> { $info_type };
2004-04-25 22:28:06 -04:00
$i ++ ;
2003-05-28 04:41:04 -04:00
}
2004-04-25 22:28:06 -04:00
return $new_array ;
} else {
2017-11-30 18:11:00 -05:00
return $this -> col_info [ $col_offset ] -> { $info_type };
2003-05-28 04:41:04 -04:00
}
}
}
2004-07-22 21:35:56 -04:00
2006-11-29 20:44:33 -05:00
/**
2008-06-22 16:23:23 -04:00
* Starts the timer , for debugging purposes .
*
* @ since 1.5 . 0
*
2015-05-28 16:10:29 -04:00
* @ return true
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function timer_start () {
2011-12-20 16:36:53 -05:00
$this -> time_start = microtime ( true );
2004-08-20 13:52:49 -04:00
return true ;
2004-07-22 21:35:56 -04:00
}
2006-02-12 02:53:23 -05:00
2006-11-29 20:44:33 -05:00
/**
2008-06-22 16:23:23 -04:00
* Stops the debugging timer .
*
* @ since 1.5 . 0
*
2020-01-11 13:32:05 -05:00
* @ return float Total time spent on the query , in seconds .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function timer_stop () {
2011-12-20 16:36:53 -05:00
return ( microtime ( true ) - $this -> time_start );
2004-07-22 21:35:56 -04:00
}
2006-11-29 20:44:33 -05:00
/**
2009-04-10 17:57:40 -04:00
* Wraps errors in a nice header and footer and dies .
*
2012-10-17 09:11:45 -04:00
* Will not die if wpdb :: $show_errors is false .
2008-06-22 16:23:23 -04:00
*
* @ since 1.5 . 0
*
2020-05-03 07:45:06 -04:00
* @ param string $message The error message .
* @ param string $error_code Optional . A computer - readable string to identify the error .
* Default '500' .
* @ return void | false Void if the showing of errors is enabled , false if disabled .
2006-11-29 20:44:33 -05:00
*/
2014-05-19 11:36:13 -04:00
public function bail ( $message , $error_code = '500' ) {
2018-02-15 09:29:30 -05:00
if ( $this -> show_errors ) {
$error = '' ;
if ( $this -> use_mysqli ) {
if ( $this -> dbh instanceof mysqli ) {
$error = mysqli_error ( $this -> dbh );
} elseif ( mysqli_connect_errno () ) {
$error = mysqli_connect_error ();
}
} else {
if ( is_resource ( $this -> dbh ) ) {
$error = mysql_error ( $this -> dbh );
} else {
$error = mysql_error ();
}
}
if ( $error ) {
$message = '<p><code>' . $error . " </code></p> \n " . $message ;
}
wp_die ( $message );
} else {
2015-09-19 23:52:25 -04:00
if ( class_exists ( 'WP_Error' , false ) ) {
2017-11-30 18:11:00 -05:00
$this -> error = new WP_Error ( $error_code , $message );
2015-09-19 23:52:25 -04:00
} else {
2007-12-19 21:10:09 -05:00
$this -> error = $message ;
2015-09-19 23:52:25 -04:00
}
2018-02-15 09:29:30 -05:00
2006-07-10 01:29:10 -04:00
return false ;
2007-12-19 21:10:09 -05:00
}
2004-07-22 21:35:56 -04:00
}
2007-12-19 21:10:09 -05:00
2016-02-01 19:13:26 -05:00
/**
* Closes the current database connection .
*
* @ since 4.5 . 0
*
2020-05-03 07:45:06 -04:00
* @ return bool True if the connection was successfully closed ,
* false if it wasn 't, or if the connection doesn' t exist .
2016-02-01 19:13:26 -05:00
*/
public function close () {
if ( ! $this -> dbh ) {
return false ;
}
if ( $this -> use_mysqli ) {
$closed = mysqli_close ( $this -> dbh );
} else {
$closed = mysql_close ( $this -> dbh );
}
if ( $closed ) {
2017-11-30 18:11:00 -05:00
$this -> dbh = null ;
$this -> ready = false ;
2016-03-14 23:28:26 -04:00
$this -> has_connected = false ;
2016-02-01 19:13:26 -05:00
}
return $closed ;
}
2007-10-06 04:40:54 -04:00
/**
2020-05-03 07:45:06 -04:00
* Determines whether MySQL database is at least the required minimum version .
2008-06-22 16:23:23 -04:00
*
* @ since 2.5 . 0
*
2020-05-03 07:45:06 -04:00
* @ global string $wp_version The WordPress version string .
* @ global string $required_mysql_version The required MySQL version string .
* @ return void | WP_Error
2007-10-06 04:40:54 -04:00
*/
2014-05-19 11:36:13 -04:00
public function check_database_version () {
2009-12-22 06:49:22 -05:00
global $wp_version , $required_mysql_version ;
2020-01-28 19:45:18 -05:00
// Make sure the server has the required MySQL version.
2017-11-30 18:11:00 -05:00
if ( version_compare ( $this -> db_version (), $required_mysql_version , '<' ) ) {
2019-09-02 20:41:05 -04:00
/* translators: 1: WordPress version number, 2: Minimum required MySQL version number. */
2022-06-02 11:05:13 -04:00
return new WP_Error ( 'database_version' , sprintf ( __ ( '<strong>Error:</strong> WordPress %1$s requires MySQL %2$s or higher' ), $wp_version , $required_mysql_version ) );
2016-11-20 20:22:32 -05:00
}
2007-10-06 04:40:54 -04:00
}
/**
2020-05-03 07:45:06 -04:00
* Determines whether the database supports collation .
2008-06-22 16:23:23 -04:00
*
* Called when WordPress is generating the table scheme .
*
2015-08-20 18:42:25 -04:00
* Use `wpdb::has_cap( 'collation' )` .
*
2008-06-22 16:23:23 -04:00
* @ since 2.5 . 0
2015-08-20 18:42:25 -04:00
* @ deprecated 3.5 . 0 Use wpdb :: has_cap ()
2008-06-22 16:23:23 -04:00
*
2020-05-03 07:45:06 -04:00
* @ return bool True if collation is supported , false if not .
2007-10-06 04:40:54 -04:00
*/
2014-05-19 11:36:13 -04:00
public function supports_collation () {
2016-07-06 08:40:29 -04:00
_deprecated_function ( __FUNCTION__ , '3.5.0' , 'wpdb::has_cap( \'collation\' )' );
2008-08-26 19:57:48 -04:00
return $this -> has_cap ( 'collation' );
2007-10-06 04:40:54 -04:00
}
2007-11-20 19:14:58 -05:00
2012-08-08 02:13:48 -04:00
/**
2020-05-03 07:45:06 -04:00
* Retrieves the database character collate .
2012-08-08 02:13:48 -04:00
*
* @ since 3.5 . 0
*
* @ return string The database character collate .
*/
public function get_charset_collate () {
$charset_collate = '' ;
2017-11-30 18:11:00 -05:00
if ( ! empty ( $this -> charset ) ) {
2012-08-08 02:13:48 -04:00
$charset_collate = " DEFAULT CHARACTER SET $this->charset " ;
2017-11-30 18:11:00 -05:00
}
if ( ! empty ( $this -> collate ) ) {
2012-08-08 02:13:48 -04:00
$charset_collate .= " COLLATE $this->collate " ;
2017-11-30 18:11:00 -05:00
}
2012-08-08 02:13:48 -04:00
return $charset_collate ;
}
2008-08-26 15:18:58 -04:00
/**
2020-05-03 07:45:06 -04:00
* Determines if a database supports a particular feature .
2010-02-22 17:53:54 -05:00
*
2010-12-20 07:49:18 -05:00
* @ since 2.7 . 0
2016-07-20 15:33:30 -04:00
* @ since 4.1 . 0 Added support for the 'utf8mb4' feature .
* @ since 4.6 . 0 Added support for the 'utf8mb4_520' feature .
2014-12-07 00:23:22 -05:00
*
2013-04-28 20:31:56 -04:00
* @ see wpdb :: db_version ()
2010-02-22 17:53:54 -05:00
*
2020-05-03 07:45:06 -04:00
* @ param string $db_cap The feature to check for . Accepts 'collation' , 'group_concat' ,
* 'subqueries' , 'set_charset' , 'utf8mb4' , or 'utf8mb4_520' .
2015-05-28 16:10:29 -04:00
* @ return int | false Whether the database feature is supported , false otherwise .
2008-08-26 15:18:58 -04:00
*/
2014-05-19 11:36:13 -04:00
public function has_cap ( $db_cap ) {
2008-08-26 19:57:48 -04:00
$version = $this -> db_version ();
2010-02-22 17:53:54 -05:00
switch ( strtolower ( $db_cap ) ) {
2017-11-30 18:11:00 -05:00
case 'collation' : // @since 2.5.0
case 'group_concat' : // @since 2.7.0
case 'subqueries' : // @since 2.7.0
2010-02-22 17:53:54 -05:00
return version_compare ( $version , '4.1' , '>=' );
2017-11-30 18:11:00 -05:00
case 'set_charset' :
2013-04-28 20:31:56 -04:00
return version_compare ( $version , '5.0.7' , '>=' );
2017-11-30 18:11:00 -05:00
case 'utf8mb4' : // @since 4.1.0
2015-02-09 17:28:28 -05:00
if ( version_compare ( $version , '5.5.3' , '<' ) ) {
return false ;
}
if ( $this -> use_mysqli ) {
2015-03-15 08:20:26 -04:00
$client_version = mysqli_get_client_info ();
2015-02-09 17:28:28 -05:00
} else {
2015-03-15 08:20:26 -04:00
$client_version = mysql_get_client_info ();
2015-02-09 17:28:28 -05:00
}
2015-03-15 08:20:26 -04:00
2015-03-31 05:18:27 -04:00
/*
* libmysql has supported utf8mb4 since 5.5 . 3 , same as the MySQL server .
* mysqlnd has supported utf8mb4 since 5.0 . 9.
*/
if ( false !== strpos ( $client_version , 'mysqlnd' ) ) {
$client_version = preg_replace ( '/^\D+([\d.]+).*/' , '$1' , $client_version );
return version_compare ( $client_version , '5.0.9' , '>=' );
} else {
return version_compare ( $client_version , '5.5.3' , '>=' );
}
2017-11-30 18:11:00 -05:00
case 'utf8mb4_520' : // @since 4.6.0
2016-05-23 02:32:29 -04:00
return version_compare ( $version , '5.6' , '>=' );
2014-09-29 06:30:16 -04:00
}
2008-08-26 19:57:48 -04:00
return false ;
2008-08-26 15:18:58 -04:00
}
2007-11-20 19:14:58 -05:00
/**
2021-01-07 09:17:11 -05:00
* Retrieves a comma - separated list of the names of the functions that called wpdb .
2008-06-22 16:23:23 -04:00
*
* @ since 2.5 . 0
*
2020-05-03 07:45:06 -04:00
* @ return string Comma - separated list of the calling functions .
2007-11-20 19:14:58 -05:00
*/
2014-05-19 11:36:13 -04:00
public function get_caller () {
2012-01-28 06:56:50 -05:00
return wp_debug_backtrace_summary ( __CLASS__ );
2007-11-20 19:14:58 -05:00
}
2008-08-26 18:30:56 -04:00
/**
2021-12-29 18:14:01 -05:00
* Retrieves the database server version .
2010-02-23 17:04:29 -05:00
*
2010-12-20 07:49:18 -05:00
* @ since 2.7 . 0
*
2020-03-12 11:55:07 -04:00
* @ return string | null Version number on success , null on failure .
2008-08-26 18:30:56 -04:00
*/
2014-05-19 11:36:13 -04:00
public function db_version () {
2020-03-12 11:55:07 -04:00
return preg_replace ( '/[^0-9.].*/' , '' , $this -> db_server_info () );
}
/**
2021-12-29 18:14:01 -05:00
* Retrieves full database server information .
2020-03-12 11:55:07 -04:00
*
* @ since 5.5 . 0
*
* @ return string | false Server info on success , false on failure .
*/
public function db_server_info () {
2014-02-24 19:40:13 -05:00
if ( $this -> use_mysqli ) {
$server_info = mysqli_get_server_info ( $this -> dbh );
} else {
$server_info = mysql_get_server_info ( $this -> dbh );
}
2020-03-12 11:55:07 -04:00
return $server_info ;
2008-08-26 18:30:56 -04:00
}
2004-04-25 22:28:06 -04:00
}