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