+ /**
+ * Change the current SQL mode, and ensure its WordPress compatibility.
+ *
+ * If no modes are passed, it will ensure the current MySQL server
+ * modes are compatible.
+ *
+ * @since 3.9.0
+ *
+ * @param array $modes Optional. A list of SQL modes to set.
+ */
+ public function set_sql_mode( $modes = array() ) {
+ if ( empty( $modes ) ) {
+ if ( $this->use_mysqli ) {
+ $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
+ } else {
+ $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
+ }
+
+ if ( empty( $res ) ) {
+ return;
+ }
+
+ if ( $this->use_mysqli ) {
+ $modes_array = mysqli_fetch_array( $res );
+ if ( empty( $modes_array[0] ) ) {
+ return;
+ }
+ $modes_str = $modes_array[0];
+ } else {
+ $modes_str = mysql_result( $res, 0 );
+ }
+
+ if ( empty( $modes_str ) ) {
+ return;
+ }
+
+ $modes = explode( ',', $modes_str );
+ }
+
+ $modes = array_change_key_case( $modes, CASE_UPPER );
+
+ /**
+ * Filter the list of incompatible SQL modes to exclude.
+ *
+ * @since 3.9.0
+ *
+ * @see wpdb::$incompatible_modes
+ *
+ * @param array $incompatible_modes An array of incompatible modes.
+ */
+ $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
+
+ foreach( $modes as $i => $mode ) {
+ if ( in_array( $mode, $incompatible_modes ) ) {
+ unset( $modes[ $i ] );
+ }
+ }
+
+ $modes_str = implode( ',', $modes );
+
+ if ( $this->use_mysqli ) {
+ mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
+ } else {
+ mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );
+ }
+ }
+
+ /**
+ * Sets the table prefix for the WordPress tables.
+ *
+ * @since 2.5.0
+ *
+ * @param string $prefix Alphanumeric name for the new prefix.
+ * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
+ * @return string|WP_Error Old prefix or WP_Error on error
+ */
+ public function set_prefix( $prefix, $set_table_names = true ) {
+
+ if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
+ return new WP_Error('invalid_db_prefix', 'Invalid database prefix' );
+
+ $old_prefix = is_multisite() ? '' : $prefix;
+
+ if ( isset( $this->base_prefix ) )
+ $old_prefix = $this->base_prefix;
+
+ $this->base_prefix = $prefix;
+
+ if ( $set_table_names ) {
+ foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ if ( is_multisite() && empty( $this->blogid ) )
+ return $old_prefix;
+
+ $this->prefix = $this->get_blog_prefix();
+
+ foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+ }
+ return $old_prefix;
+ }
+
+ /**
+ * Sets blog id.
+ *
+ * @since 3.0.0
+ * @access public
+ * @param int $blog_id
+ * @param int $site_id Optional.
+ * @return string previous blog id
+ */
+ public function set_blog_id( $blog_id, $site_id = 0 ) {
+ if ( ! empty( $site_id ) )
+ $this->siteid = $site_id;
+
+ $old_blog_id = $this->blogid;
+ $this->blogid = $blog_id;
+
+ $this->prefix = $this->get_blog_prefix();
+
+ foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+ $this->$table = $prefixed_table;
+
+ return $old_blog_id;
+ }
+
+ /**
+ * Gets blog prefix.
+ *
+ * @uses is_multisite()
+ * @since 3.0.0
+ * @param int $blog_id Optional.
+ * @return string Blog prefix.
+ */
+ public function get_blog_prefix( $blog_id = null ) {
+ if ( is_multisite() ) {
+ if ( null === $blog_id )
+ $blog_id = $this->blogid;
+ $blog_id = (int) $blog_id;
+ if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
+ return $this->base_prefix;
+ else
+ return $this->base_prefix . $blog_id . '_';
+ } else {
+ return $this->base_prefix;
+ }
+ }
+
+ /**
+ * Returns an array of WordPress tables.
+ *
+ * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
+ * override the WordPress users and usermeta tables that would otherwise
+ * be determined by the prefix.
+ *
+ * The scope argument can take one of the following:
+ *
+ * 'all' - returns 'all' and 'global' tables. No old tables are returned.
+ * 'blog' - returns the blog-level tables for the queried blog.
+ * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
+ * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
+ * 'old' - returns tables which are deprecated.
+ *
+ * @since 3.0.0
+ * @uses wpdb::$tables
+ * @uses wpdb::$old_tables
+ * @uses wpdb::$global_tables
+ * @uses wpdb::$ms_global_tables
+ * @uses is_multisite()
+ *
+ * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
+ * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
+ * prefix is requested, then the custom users and usermeta tables will be mapped.
+ * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
+ * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
+ */
+ public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
+ switch ( $scope ) {
+ case 'all' :
+ $tables = array_merge( $this->global_tables, $this->tables );
+ if ( is_multisite() )
+ $tables = array_merge( $tables, $this->ms_global_tables );
+ break;
+ case 'blog' :
+ $tables = $this->tables;
+ break;
+ case 'global' :
+ $tables = $this->global_tables;
+ if ( is_multisite() )
+ $tables = array_merge( $tables, $this->ms_global_tables );
+ break;
+ case 'ms_global' :
+ $tables = $this->ms_global_tables;
+ break;
+ case 'old' :
+ $tables = $this->old_tables;
+ break;
+ default :
+ return array();
+ }
+
+ if ( $prefix ) {
+ if ( ! $blog_id )
+ $blog_id = $this->blogid;
+ $blog_prefix = $this->get_blog_prefix( $blog_id );
+ $base_prefix = $this->base_prefix;
+ $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
+ foreach ( $tables as $k => $table ) {
+ if ( in_array( $table, $global_tables ) )
+ $tables[ $table ] = $base_prefix . $table;
+ else
+ $tables[ $table ] = $blog_prefix . $table;
+ unset( $tables[ $k ] );
+ }
+
+ if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
+ $tables['users'] = CUSTOM_USER_TABLE;
+
+ if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
+ $tables['usermeta'] = CUSTOM_USER_META_TABLE;
+ }
+
+ return $tables;
+ }
+
+ /**
+ * Selects a database using the current database connection.
+ *
+ * The database name will be changed based on the current database
+ * connection. On failure, the execution will bail and display an DB error.
+ *
+ * @since 0.71
+ *
+ * @param string $db MySQL database name
+ * @param resource $dbh Optional link identifier.
+ * @return null Always null.
+ */
+ public function select( $db, $dbh = null ) {
+ if ( is_null($dbh) )
+ $dbh = $this->dbh;
+
+ if ( $this->use_mysqli ) {
+ $success = @mysqli_select_db( $dbh, $db );
+ } else {
+ $success = @mysql_select_db( $db, $dbh );
+ }
+ if ( ! $success ) {
+ $this->ready = false;
+ if ( ! did_action( 'template_redirect' ) ) {
+ wp_load_translations_early();
+ $this->bail( sprintf( __( '<h1>Can’t select database</h1>
+<p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>