[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> wp-db.php (source)

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


Generated : Sat Oct 24 08:20:01 2020 Cross-referenced by PHPXref