wp/wp-includes/wp-db.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    59 	 * Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true.
    59 	 * Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true.
    60 	 *
    60 	 *
    61 	 * @since 0.71
    61 	 * @since 0.71
    62 	 * @var bool
    62 	 * @var bool
    63 	 */
    63 	 */
    64 	var $show_errors = false;
    64 	public $show_errors = false;
    65 
    65 
    66 	/**
    66 	/**
    67 	 * Whether to suppress errors during the DB bootstrapping. Default false.
    67 	 * Whether to suppress errors during the DB bootstrapping. Default false.
    68 	 *
    68 	 *
    69 	 * @since 2.5.0
    69 	 * @since 2.5.0
    70 	 * @var bool
    70 	 * @var bool
    71 	 */
    71 	 */
    72 	var $suppress_errors = false;
    72 	public $suppress_errors = false;
    73 
    73 
    74 	/**
    74 	/**
    75 	 * The error encountered during the last query.
    75 	 * The error encountered during the last query.
    76 	 *
    76 	 *
    77 	 * @since 2.5.0
    77 	 * @since 2.5.0
    99 	 * Count of rows affected by the last query.
    99 	 * Count of rows affected by the last query.
   100 	 *
   100 	 *
   101 	 * @since 0.71
   101 	 * @since 0.71
   102 	 * @var int
   102 	 * @var int
   103 	 */
   103 	 */
   104 	var $rows_affected = 0;
   104 	public $rows_affected = 0;
   105 
   105 
   106 	/**
   106 	/**
   107 	 * The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT).
   107 	 * The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT).
   108 	 *
   108 	 *
   109 	 * @since 0.71
   109 	 * @since 0.71
   115 	 * The last query made.
   115 	 * The last query made.
   116 	 *
   116 	 *
   117 	 * @since 0.71
   117 	 * @since 0.71
   118 	 * @var string
   118 	 * @var string
   119 	 */
   119 	 */
   120 	var $last_query;
   120 	public $last_query;
   121 
   121 
   122 	/**
   122 	/**
   123 	 * Results of the last query.
   123 	 * Results of the last query.
   124 	 *
   124 	 *
   125 	 * @since 0.71
   125 	 * @since 0.71
   126 	 * @var array|null
   126 	 * @var array|null
   127 	 */
   127 	 */
   128 	var $last_result;
   128 	public $last_result;
   129 
   129 
   130 	/**
   130 	/**
   131 	 * MySQL result, which is either a resource or boolean.
   131 	 * MySQL result, which is either a resource or boolean.
   132 	 *
   132 	 *
   133 	 * @since 0.71
   133 	 * @since 0.71
   196 	 *         @type float  $3 Unix timestamp of the time at the start of the query.
   196 	 *         @type float  $3 Unix timestamp of the time at the start of the query.
   197 	 *         @type array  $4 Custom query data.
   197 	 *         @type array  $4 Custom query data.
   198 	 *     }
   198 	 *     }
   199 	 * }
   199 	 * }
   200 	 */
   200 	 */
   201 	var $queries;
   201 	public $queries;
   202 
   202 
   203 	/**
   203 	/**
   204 	 * The number of times to retry reconnecting before dying. Default 5.
   204 	 * The number of times to retry reconnecting before dying. Default 5.
   205 	 *
   205 	 *
   206 	 * @since 3.9.0
   206 	 * @since 3.9.0
   232 	 * Whether the database queries are ready to start executing.
   232 	 * Whether the database queries are ready to start executing.
   233 	 *
   233 	 *
   234 	 * @since 2.3.2
   234 	 * @since 2.3.2
   235 	 * @var bool
   235 	 * @var bool
   236 	 */
   236 	 */
   237 	var $ready = false;
   237 	public $ready = false;
   238 
   238 
   239 	/**
   239 	/**
   240 	 * Blog ID.
   240 	 * Blog ID.
   241 	 *
   241 	 *
   242 	 * @since 3.0.0
   242 	 * @since 3.0.0
   257 	 *
   257 	 *
   258 	 * @since 2.5.0
   258 	 * @since 2.5.0
   259 	 * @see wpdb::tables()
   259 	 * @see wpdb::tables()
   260 	 * @var array
   260 	 * @var array
   261 	 */
   261 	 */
   262 	var $tables = array(
   262 	public $tables = array(
   263 		'posts',
   263 		'posts',
   264 		'comments',
   264 		'comments',
   265 		'links',
   265 		'links',
   266 		'options',
   266 		'options',
   267 		'postmeta',
   267 		'postmeta',
   279 	 *
   279 	 *
   280 	 * @since 2.9.0
   280 	 * @since 2.9.0
   281 	 * @see wpdb::tables()
   281 	 * @see wpdb::tables()
   282 	 * @var array
   282 	 * @var array
   283 	 */
   283 	 */
   284 	var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
   284 	public $old_tables = array( 'categories', 'post2cat', 'link2cat' );
   285 
   285 
   286 	/**
   286 	/**
   287 	 * List of WordPress global tables.
   287 	 * List of WordPress global tables.
   288 	 *
   288 	 *
   289 	 * @since 3.0.0
   289 	 * @since 3.0.0
   290 	 * @see wpdb::tables()
   290 	 * @see wpdb::tables()
   291 	 * @var array
   291 	 * @var array
   292 	 */
   292 	 */
   293 	var $global_tables = array( 'users', 'usermeta' );
   293 	public $global_tables = array( 'users', 'usermeta' );
   294 
   294 
   295 	/**
   295 	/**
   296 	 * List of Multisite global tables.
   296 	 * List of Multisite global tables.
   297 	 *
   297 	 *
   298 	 * @since 3.0.0
   298 	 * @since 3.0.0
   299 	 * @see wpdb::tables()
   299 	 * @see wpdb::tables()
   300 	 * @var array
   300 	 * @var array
   301 	 */
   301 	 */
   302 	var $ms_global_tables = array(
   302 	public $ms_global_tables = array(
   303 		'blogs',
   303 		'blogs',
   304 		'blogmeta',
   304 		'blogmeta',
   305 		'signups',
   305 		'signups',
   306 		'site',
   306 		'site',
   307 		'sitemeta',
   307 		'sitemeta',
  1157 	 *
  1157 	 *
  1158 	 * @param string $string String to escape.
  1158 	 * @param string $string String to escape.
  1159 	 * @return string Escaped string.
  1159 	 * @return string Escaped string.
  1160 	 */
  1160 	 */
  1161 	function _real_escape( $string ) {
  1161 	function _real_escape( $string ) {
       
  1162 		if ( ! is_scalar( $string ) && ! is_null( $string ) ) {
       
  1163 			return '';
       
  1164 		}
       
  1165 
  1162 		if ( $this->dbh ) {
  1166 		if ( $this->dbh ) {
  1163 			if ( $this->use_mysqli ) {
  1167 			if ( $this->use_mysqli ) {
  1164 				$escaped = mysqli_real_escape_string( $this->dbh, $string );
  1168 				$escaped = mysqli_real_escape_string( $this->dbh, $string );
  1165 			} else {
  1169 			} else {
  1166 				$escaped = mysql_real_escape_string( $string, $this->dbh );
  1170 				$escaped = mysql_real_escape_string( $string, $this->dbh );
  1363 		$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents.
  1367 		$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents.
  1364 
  1368 
  1365 		// Count the number of valid placeholders in the query.
  1369 		// Count the number of valid placeholders in the query.
  1366 		$placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches );
  1370 		$placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches );
  1367 
  1371 
  1368 		if ( count( $args ) !== $placeholders ) {
  1372 		$args_count = count( $args );
       
  1373 
       
  1374 		if ( $args_count !== $placeholders ) {
  1369 			if ( 1 === $placeholders && $passed_as_array ) {
  1375 			if ( 1 === $placeholders && $passed_as_array ) {
  1370 				// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
  1376 				// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
  1371 				wp_load_translations_early();
  1377 				wp_load_translations_early();
  1372 				_doing_it_wrong(
  1378 				_doing_it_wrong(
  1373 					'wpdb::prepare',
  1379 					'wpdb::prepare',
  1386 					'wpdb::prepare',
  1392 					'wpdb::prepare',
  1387 					sprintf(
  1393 					sprintf(
  1388 						/* translators: 1: Number of placeholders, 2: Number of arguments passed. */
  1394 						/* translators: 1: Number of placeholders, 2: Number of arguments passed. */
  1389 						__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
  1395 						__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
  1390 						$placeholders,
  1396 						$placeholders,
  1391 						count( $args )
  1397 						$args_count
  1392 					),
  1398 					),
  1393 					'4.8.3'
  1399 					'4.8.3'
  1394 				);
  1400 				);
       
  1401 
       
  1402 				/*
       
  1403 				 * If we don't have enough arguments to match the placeholders,
       
  1404 				 * return an empty string to avoid a fatal error on PHP 8.
       
  1405 				 */
       
  1406 				if ( $args_count < $placeholders ) {
       
  1407 					$max_numbered_placeholder = ! empty( $matches[3] ) ? max( array_map( 'intval', $matches[3] ) ) : 0;
       
  1408 
       
  1409 					if ( ! $max_numbered_placeholder || $args_count < $max_numbered_placeholder ) {
       
  1410 						return '';
       
  1411 					}
       
  1412 				}
  1395 			}
  1413 			}
  1396 		}
  1414 		}
  1397 
  1415 
  1398 		array_walk( $args, array( $this, 'escape_by_ref' ) );
  1416 		array_walk( $args, array( $this, 'escape_by_ref' ) );
  1399 		$query = vsprintf( $query, $args );
  1417 		$query = vsprintf( $query, $args );
  1867 	}
  1885 	}
  1868 
  1886 
  1869 	/**
  1887 	/**
  1870 	 * Performs a MySQL database query, using current database connection.
  1888 	 * Performs a MySQL database query, using current database connection.
  1871 	 *
  1889 	 *
  1872 	 * More information can be found on the Codex page.
  1890 	 * More information can be found on the documentation page.
  1873 	 *
  1891 	 *
  1874 	 * @since 0.71
  1892 	 * @since 0.71
  1875 	 *
  1893 	 *
  1876 	 * @link https://codex.wordpress.org/Function_Reference/wpdb_Class
  1894 	 * @link https://developer.wordpress.org/reference/classes/wpdb/
  1877 	 *
  1895 	 *
  1878 	 * @param string $query Database query.
  1896 	 * @param string $query Database query.
  1879 	 * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows
  1897 	 * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows
  1880 	 *                  affected/selected for all other queries. Boolean false on error.
  1898 	 *                  affected/selected for all other queries. Boolean false on error.
  1881 	 */
  1899 	 */
  1895 		 *
  1913 		 *
  1896 		 * @param string $query Database query.
  1914 		 * @param string $query Database query.
  1897 		 */
  1915 		 */
  1898 		$query = apply_filters( 'query', $query );
  1916 		$query = apply_filters( 'query', $query );
  1899 
  1917 
       
  1918 		if ( ! $query ) {
       
  1919 			$this->insert_id = 0;
       
  1920 			return false;
       
  1921 		}
       
  1922 
  1900 		$this->flush();
  1923 		$this->flush();
  1901 
  1924 
  1902 		// Log how the function was called.
  1925 		// Log how the function was called.
  1903 		$this->func_call = "\$db->query(\"$query\")";
  1926 		$this->func_call = "\$db->query(\"$query\")";
  1904 
  1927 
  2058 	 * @param float  $query_start     Unix timestamp of the time at the start of the query.
  2081 	 * @param float  $query_start     Unix timestamp of the time at the start of the query.
  2059 	 * @param array  $query_data      Custom query data.
  2082 	 * @param array  $query_data      Custom query data.
  2060 	 */
  2083 	 */
  2061 	public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) {
  2084 	public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) {
  2062 		/**
  2085 		/**
  2063 		 * Filters the custom query data being logged.
  2086 		 * Filters the custom data to log alongside a query.
  2064 		 *
  2087 		 *
  2065 		 * Caution should be used when modifying any of this data, it is recommended that any additional
  2088 		 * Caution should be used when modifying any of this data, it is recommended that any additional
  2066 		 * information you need to store about a query be added as a new associative entry to the fourth
  2089 		 * information you need to store about a query be added as a new associative array element.
  2067 		 * element $query_data.
       
  2068 		 *
  2090 		 *
  2069 		 * @since 5.3.0
  2091 		 * @since 5.3.0
  2070 		 *
  2092 		 *
  2071 		 * @param array  $query_data      Custom query data.
  2093 		 * @param array  $query_data      Custom query data.
  2072 		 * @param string $query           The query's SQL.
  2094 		 * @param string $query           The query's SQL.
  3621 
  3643 
  3622 		return false;
  3644 		return false;
  3623 	}
  3645 	}
  3624 
  3646 
  3625 	/**
  3647 	/**
  3626 	 * Retrieves the name of the function that called wpdb.
  3648 	 * Retrieves a comma-separated list of the names of the functions that called wpdb.
  3627 	 *
       
  3628 	 * Searches up the list of functions until it reaches the one that would
       
  3629 	 * most logically had called this method.
       
  3630 	 *
  3649 	 *
  3631 	 * @since 2.5.0
  3650 	 * @since 2.5.0
  3632 	 *
  3651 	 *
  3633 	 * @return string Comma-separated list of the calling functions.
  3652 	 * @return string Comma-separated list of the calling functions.
  3634 	 */
  3653 	 */