Make WordPress Core

Changeset 13314


Ignore:
Timestamp:
02/22/2010 10:53:54 PM (15 years ago)
Author:
nacin
Message:

Docs and code convention cleanups. see #11644 props hakre

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r13245 r13314  
    1313 * @since 0.71
    1414 */
    15 define('EZSQL_VERSION', 'WP1.25');
     15define( 'EZSQL_VERSION', 'WP1.25' );
    1616
    1717/**
    1818 * @since 0.71
    1919 */
    20 define('OBJECT', 'OBJECT', true);
     20define( 'OBJECT', 'OBJECT', true );
    2121
    2222/**
    2323 * @since {@internal Version Unknown}}
    2424 */
    25 define('OBJECT_K', 'OBJECT_K', false);
     25define( 'OBJECT_K', 'OBJECT_K', false );
    2626
    2727/**
    2828 * @since 0.71
    2929 */
    30 define('ARRAY_A', 'ARRAY_A', false);
     30define( 'ARRAY_A', 'ARRAY_A', false );
    3131
    3232/**
    3333 * @since 0.71
    3434 */
    35 define('ARRAY_N', 'ARRAY_N', false);
     35define( 'ARRAY_N', 'ARRAY_N', false );
    3636
    3737/**
     
    6666     *
    6767     * @access private
     68     * @since 2.5
     69     * @var bool
     70     */
     71    var $suppress_errors = false;
     72
     73    /**
     74     * The last error during query.
     75     *
     76     * @see get_last_error()
     77     * @since 2.5
     78     * @access private
     79     * @var string
     80     */
     81    var $last_error = '';
     82
     83    /**
     84     * Amount of queries made
     85     *
     86     * @since 1.2.0
     87     * @access private
     88     * @var int
     89     */
     90    var $num_queries = 0;
     91
     92    /**
     93     * Count of rows returned by previous query
     94     *
     95     * @since 1.2
     96     * @var int
     97     */
     98    var $num_rows = 0;
     99
     100    /**
     101     * Count of affected rows by previous query
     102     *
     103     * @since 0.71
     104     * @var int
     105     */
     106    var $rows_affected = 0;
     107
     108    /**
     109     * Saved result of the last query made
     110     *
     111     * @since 1.2.0
     112     * @access private
     113     * @var array
     114     */
     115    var $last_query;
     116
     117    /**
     118     * Results of the last query made
     119     *
    68120     * @since {@internal Version Unknown}}
    69      * @var bool
    70      */
    71     var $suppress_errors = false;
    72 
    73     /**
    74      * The last error during query.
    75      *
    76      * @since {@internal Version Unknown}}
    77      * @var string
    78      */
    79     var $last_error = '';
    80 
    81     /**
    82      * Amount of queries made
    83      *
    84      * @since 1.2.0
    85      * @access private
    86      * @var int
    87      */
    88     var $num_queries = 0;
    89 
    90     /**
    91      * Saved result of the last query made
    92      *
    93      * @since 1.2.0
    94      * @access private
    95      * @var array
    96      */
    97     var $last_query;
     121     * @var mixed
     122     */
     123    var $last_result;
    98124
    99125    /**
     
    136162     */
    137163    var $ready = false;
     164
     165    /**
     166     * {@internal Missing Description}}
     167     *
     168     * @since 3.0.0
     169     * @var int
     170     */
    138171    var $blogid = 0;
     172
     173    /**
     174     * {@internal Missing Description}}
     175     *
     176     * @since 3.0.0
     177     * @var int
     178     */
    139179    var $siteid = 0;
    140180
     
    252292     * List of deprecated WordPress tables
    253293     *
     294     * @deprecated
    254295     * @since 2.9.0
    255296     * @access private
     
    344385
    345386    /**
    346      * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.
    347      *
    348      * Keys are colmn names, values are format types: 'ID' => '%d'
     387     * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
     388     * 
     389     * Keys are column names, values are format types: 'ID' => '%d'
    349390     *
    350391     * @since 2.8.0
     
    407448     * @param string $dbhost MySQL database host
    408449     */
    409     function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
     450    function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
    410451        if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
    411452            $this->db_connect();
    412         return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
     453        return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    413454    }
    414455
     
    427468     * @param string $dbhost MySQL database host
    428469     */
    429     function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
    430         register_shutdown_function(array(&$this, "__destruct"));
     470    function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
     471        register_shutdown_function( array( &$this, '__destruct' ) );
    431472
    432473        if ( WP_DEBUG )
     
    443484        }
    444485
    445         if ( defined('DB_CHARSET') )
     486        if ( defined( 'DB_CHARSET' ) )
    446487            $this->charset = DB_CHARSET;
    447488
    448489        $this->dbuser = $dbuser;
    449490
    450         $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
    451         if (!$this->dbh) {
    452             $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
     491        $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
     492        if ( !$this->dbh ) {
     493            $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    453494<h1>Error establishing a database connection</h1>
    454495<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
     
    459500</ul>
    460501<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
    461 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost), 'db_connect_fail');
     502"/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
    462503            return;
    463504        }
     
    465506        $this->ready = true;
    466507
    467         if ( $this->has_cap( 'collation' ) && !empty($this->charset) ) {
    468             if ( function_exists('mysql_set_charset') ) {
    469                 mysql_set_charset($this->charset, $this->dbh);
     508        if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) {
     509            if ( function_exists( 'mysql_set_charset' ) ) {
     510                mysql_set_charset( $this->charset, $this->dbh );
    470511                $this->real_escape = true;
    471512            } else {
    472                 $collation_query = "SET NAMES '{$this->charset}'";
    473                 if ( !empty($this->collate) )
    474                     $collation_query .= " COLLATE '{$this->collate}'";
    475                 $this->query($collation_query);
     513                $query = $this->prepare( 'SET NAMES %s', $this->charset );
     514                if ( ! empty( $this->collate ) )
     515                    $query .= $this->prepare( ' COLLATE %s', $this->collate );
     516                $this->query( $query );
    476517            }
    477518        }
    478519
    479         $this->select($dbname, $this->dbh);
     520        $this->select( $dbname, $this->dbh );
    480521    }
    481522
     
    484525     *
    485526     * @since 2.0.8
    486      *
    487      * @return bool Always true
     527     * @return bool true
    488528     */
    489529    function __destruct() {
     
    504544    function set_prefix( $prefix ) {
    505545
    506         if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     546        if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
    507547            return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
    508548
    509         if ( is_multisite() )
    510             $old_prefix = '';
    511         else
    512             $old_prefix = $prefix;
     549        $old_prefix = is_multisite() ? '' : $prefix;
    513550
    514551        if ( isset( $this->base_prefix ) )
     
    518555            $this->$table = $prefix . $table;
    519556
    520         if ( defined('VHOST') && empty( $this->blogid ) )
     557        if ( defined( 'VHOST' ) && empty( $this->blogid ) )
    521558            return $old_prefix;
    522559
     
    529566            $this->$table = $this->prefix . $table;
    530567
    531         if ( defined('CUSTOM_USER_TABLE') )
     568        if ( defined( 'CUSTOM_USER_TABLE' ) )
    532569            $this->users = CUSTOM_USER_TABLE;
    533570
    534         if ( defined('CUSTOM_USER_META_TABLE') )
     571        if ( defined( 'CUSTOM_USER_META_TABLE' ) )
    535572            $this->usermeta = CUSTOM_USER_META_TABLE;
    536573
     
    538575    }
    539576
    540     function set_blog_id($blog_id, $site_id = '') {
    541         if ( !empty($site_id) )
     577    /**
     578     * Sets blog id.
     579     *
     580     * @since 3.0.0
     581     * @access public
     582     * @param string $blog_id
     583     * @param string $site_id. Optional.
     584     * @return string previous blog id
     585     */
     586    function set_blog_id( $blog_id, $site_id = '' ) {
     587        if ( ! empty( $site_id ) )
    542588            $this->siteid = $site_id;
    543589
    544         $old_blog_id = $this->blogid;
     590        $old_blog_id  = $this->blogid;
    545591        $this->blogid = $blog_id;
    546592
     
    556602    }
    557603
    558     function get_blog_prefix( $blog_id = '' ) {
     604    /**
     605     * Gets blog prefix.
     606     *
     607     * @uses is_multisite()
     608     * @since 3.0.0
     609     * @param int $blog_id. Optional.
     610     * @return string Blog prefix.
     611     */
     612    function get_blog_prefix( $blog_id = 0 ) {
    559613        if ( is_multisite() && $blog_id ) {
    560614            if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) )
     
    621675     * @return null Always null.
    622676     */
    623     function select($db, &$dbh) {
    624         if (!@mysql_select_db($db, $dbh)) {
     677    function select( $db, &$dbh ) {
     678        if ( !@mysql_select_db( $db, $dbh ) ) {
    625679            $this->ready = false;
    626             $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
     680            $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
    627681<h1>Can&#8217;t select database</h1>
    628682<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>
     
    632686<li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
    633687</ul>
    634 <p>If you don\'t know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser), 'db_select_fail');
     688<p>If you don\'t know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser ), 'db_select_fail' );
    635689            return;
    636690        }
    637691    }
    638692
    639     function _weak_escape($string) {
    640         return addslashes($string);
    641     }
    642 
    643     function _real_escape($string) {
     693    /**
     694     * Weak escape
     695     *
     696     * @see addslashes()
     697     * @since  unknown
     698     * @access private
     699     *
     700     * @param  string $string
     701     * @return string
     702     */
     703    function _weak_escape( $string ) {
     704        return addslashes( $string );
     705    }
     706
     707    /**
     708     * Real escape
     709     *
     710     * @see mysql_real_escape_string()
     711     * @see addslashes()
     712     * @since 2.8
     713     * @access private
     714     *
     715     * @param  string $string to escape
     716     * @return string escaped
     717     */
     718    function _real_escape( $string ) {
    644719        if ( $this->dbh && $this->real_escape )
    645720            return mysql_real_escape_string( $string, $this->dbh );
     
    648723    }
    649724
    650     function _escape($data) {
    651         if ( is_array($data) ) {
     725    /**
     726     * Escape data.
     727     *
     728     * @see esc_sql()
     729     * @since  2.8
     730     * @access private
     731     *
     732     * @param  string|array $data
     733     * @return string|array escaped
     734     */
     735    function _escape( $data ) {
     736        if ( is_array( $data ) ) {
    652737            foreach ( (array) $data as $k => $v ) {
    653738                if ( is_array($v) )
     
    668753     * @since 0.71
    669754     *
    670      * @param string|array $data
    671      * @return string query safe string
    672      */
    673     function escape($data) {
    674         if ( is_array($data) ) {
     755     * @param  string|array $data to escape
     756     * @return string|array escaped as query safe string
     757     */
     758    function escape( $data ) {
     759        if ( is_array( $data ) ) {
    675760            foreach ( (array) $data as $k => $v ) {
    676                 if ( is_array($v) )
     761                if ( is_array( $v ) )
    677762                    $data[$k] = $this->escape( $v );
    678763                else
     
    691776     * @since 2.3.0
    692777     *
    693      * @param string $s
    694      */
    695     function escape_by_ref(&$string) {
     778     * @param  string $string to escape
     779     * @return void
     780     */
     781    function escape_by_ref( &$string ) {
    696782        $string = $this->_real_escape( $string );
    697783    }
     
    699785    /**
    700786     * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
     787     *
     788     * The following directives can be used in the query format string:
     789     *   %d (decimal number) 
     790     *   %s (string)
     791     *   %% (literal percentage sign - no argument needed)
     792     *
     793     * Both %d and %s are to be left unquoted in the query string and   
     794     * they need an argument passed for them. Literals (%) as parts of
     795     * the query must be properly written as %%.
    701796     *
    702797     * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
     
    716811     *
    717812     * @param string $query Query statement with sprintf()-like placeholders
    718      * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
    719      * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
    720      * @return null|string Sanitized query string
    721      */
    722     function prepare($query = null) { // ( $query, *$args )
     813     * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
     814     *  {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
     815     *  being called like {@link http://php.net/sprintf sprintf()}.
     816     * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
     817     *  {@link http://php.net/sprintf sprintf()}.
     818     * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
     819     *  if there was something to prepare
     820     */
     821    function prepare( $query = null ) { // ( $query, *$args )
    723822        if ( is_null( $query ) )
    724823            return;
    725824        $args = func_get_args();
    726         array_shift($args);
     825        array_shift( $args );
    727826        // If args were passed as an array (as in vsprintf), move them up
    728         if ( isset($args[0]) && is_array($args[0]) )
     827        if ( isset( $args[0] ) && is_array($args[0]) )
    729828            $args = $args[0];
    730         $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
    731         $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
    732         $query = str_replace('%s', "'%s'", $query); // quote the strings
    733         array_walk($args, array(&$this, 'escape_by_ref'));
    734         return @vsprintf($query, $args);
     829        $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
     830        $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
     831        $query = str_replace( '%s', "'%s'", $query ); // quote the strings
     832        array_walk( $args, array( &$this, 'escape_by_ref' ) );
     833        return @vsprintf( $query, $args );
    735834    }
    736835
     
    744843     * @return bool False if the showing of errors is disabled.
    745844     */
    746     function print_error($str = '') {
     845    function print_error( $str = '' ) {
    747846        global $EZSQL_ERROR;
    748847
    749         if (!$str) $str = mysql_error($this->dbh);
    750         $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
     848        if ( !$str )
     849            $str = mysql_error( $this->dbh );
     850        $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    751851
    752852        if ( $this->suppress_errors )
     
    781881                die( $msg );
    782882        } else {
    783             $str = htmlspecialchars($str, ENT_QUOTES);
    784             $query = htmlspecialchars($this->last_query, ENT_QUOTES);
     883            $str   = htmlspecialchars( $str, ENT_QUOTES );
     884            $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
    785885
    786886            print "<div id='error'>
     
    800900     *
    801901     * @since 0.71
     902     * @see wpdb::hide_errors()
    802903     *
    803904     * @param bool $show Whether to show or hide errors
     
    813914     * Disables showing of database errors.
    814915     *
    815      * @since 0.71
     916     * By default database errors are not shown.
     917     *
     918     * @since 0.71
     919     * @see wpdb::show_errors()
    816920     *
    817921     * @return bool Whether showing of errors was active or not
     
    826930     * Whether to suppress database errors.
    827931     *
    828      * @param unknown_type $suppress
    829      * @return unknown
     932     * By default database errors are suppressed, with a simple
     933     * call to this function they can be enabled.
     934     *
     935     * @since 2.5
     936     * @see wpdb::hide_errors()
     937     * @param bool $suppress Optional. New value. Defaults to true.
     938     * @return bool Old value
    830939     */
    831940    function suppress_errors( $suppress = true ) {
    832941        $errors = $this->suppress_errors;
    833         $this->suppress_errors = $suppress;
     942        $this->suppress_errors = (bool) $suppress;
    834943        return $errors;
    835944    }
     
    839948     *
    840949     * @since 0.71
     950     * @return void
    841951     */
    842952    function flush() {
    843953        $this->last_result = array();
    844         $this->col_info = null;
    845         $this->last_query = null;
     954        $this->col_info    = null;
     955        $this->last_query  = null;
    846956    }
    847957
    848958    function db_connect( $query = "SELECT" ) {
    849959        global $db_list, $global_db_list;
    850         if ( is_array( $db_list ) == false )
     960        if ( ! is_array( $db_list ) )
    851961            return true;
    852962
     
    868978        $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
    869979        if (!$this->$dbhname ) {
    870             $this->bail("
     980            $this->bail(/*WP_I18N_DB_CONNECT_DB*/"
    871981<h1>Error establishing a database connection</h1>
    872982<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>{$details['db_host']}</code>. This could mean your host's database server is down.</p>
     
    877987</ul>
    878988<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
    879 ");
     989"/*/WP_I18N_DB_CONNECT_DB*/);
    880990        }
    881991        $this->select( $details[ 'db_name' ], $this->$dbhname );
     
    889999     * @since 0.71
    8901000     *
    891      * @param string $query
     1001     * @param string $query Database query
    8921002     * @return int|false Number of rows affected/selected or false on error
    8931003     */
    894     function query($query) {
     1004    function query( $query ) {
    8951005        if ( ! $this->ready )
    8961006            return false;
     
    8981008        // filter the query, if filters are available
    8991009        // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
    900         if ( function_exists('apply_filters') )
    901             $query = apply_filters('query', $query);
    902 
    903         // initialise return
     1010        if ( function_exists( 'apply_filters' ) )
     1011            $query = apply_filters( 'query', $query );
     1012
     1013        // initialize return
    9041014        $return_val = 0;
    9051015        $this->flush();
     
    9111021        $this->last_query = $query;
    9121022
    913         // Perform the query via std mysql_query function..
    914         if ( defined('SAVEQUERIES') && SAVEQUERIES )
     1023        if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    9151024            $this->timer_start();
    9161025
     
    9401049        }
    9411050
    942         $this->result = @mysql_query($query, $dbh);
     1051        $this->result = @mysql_query( $query, $dbh );
    9431052        ++$this->num_queries;
    9441053
     
    9471056
    9481057        // If there is an error then take note of it..
    949         if ( $this->last_error = mysql_error($dbh) ) {
     1058        if ( $this->last_error = mysql_error( $dbh ) ) {
    9501059            $this->print_error();
    9511060            return false;
    9521061        }
    9531062
    954         if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
    955             $this->rows_affected = mysql_affected_rows($dbh);
     1063        if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
     1064            $this->rows_affected = mysql_affected_rows( $dbh );
    9561065            // Take note of the insert_id
    957             if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
     1066            if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    9581067                $this->insert_id = mysql_insert_id($dbh);
    9591068            }
     
    9621071        } else {
    9631072            $i = 0;
    964             while ($i < @mysql_num_fields($this->result)) {
    965                 $this->col_info[$i] = @mysql_fetch_field($this->result);
     1073            while ( $i < @mysql_num_fields( $this->result ) ) {
     1074                $this->col_info[$i] = @mysql_fetch_field( $this->result );
    9661075                $i++;
    9671076            }
    9681077            $num_rows = 0;
    969             while ( $row = @mysql_fetch_object($this->result) ) {
     1078            while ( $row = @mysql_fetch_object( $this->result ) ) {
    9701079                $this->last_result[$num_rows] = $row;
    9711080                $num_rows++;
    9721081            }
    9731082
    974             @mysql_free_result($this->result);
     1083            @mysql_free_result( $this->result );
    9751084
    9761085            // Log number of rows the query returned
     1086            // and return number of rows selected
    9771087            $this->num_rows = $num_rows;
    978 
    979             // Return number of rows selected
    980             $return_val = $this->num_rows;
     1088            $return_val     = $num_rows;
    9811089        }
    9821090
     
    9991107     * @return int|false The number of rows inserted, or false on error.
    10001108     */
    1001     function insert($table, $data, $format = null) {
     1109    function insert( $table, $data, $format = null ) {
    10021110        $formats = $format = (array) $format;
    1003         $fields = array_keys($data);
     1111        $fields = array_keys( $data );
    10041112        $formatted_fields = array();
    10051113        foreach ( $fields as $field ) {
    1006             if ( !empty($format) )
    1007                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
    1008             elseif ( isset($this->field_types[$field]) )
     1114            if ( !empty( $format ) )
     1115                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
     1116            elseif ( isset( $this->field_types[$field] ) )
    10091117                $form = $this->field_types[$field];
    10101118            else
     
    10131121        }
    10141122        $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
    1015         return $this->query( $this->prepare( $sql, $data) );
     1123        return $this->query( $this->prepare( $sql, $data ) );
    10161124    }
    10171125
     
    10341142     * @return int|false The number of rows updated, or false on error.
    10351143     */
    1036     function update($table, $data, $where, $format = null, $where_format = null) {
    1037         if ( !is_array( $where ) )
     1144    function update( $table, $data, $where, $format = null, $where_format = null ) {
     1145        if ( ! is_array( $where ) )
    10381146            return false;
    10391147
    10401148        $formats = $format = (array) $format;
    10411149        $bits = $wheres = array();
    1042         foreach ( (array) array_keys($data) as $field ) {
    1043             if ( !empty($format) )
    1044                 $form = ( $form = array_shift($formats) ) ? $form : $format[0];
     1150        foreach ( (array) array_keys( $data ) as $field ) {
     1151            if ( !empty( $format ) )
     1152                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    10451153            elseif ( isset($this->field_types[$field]) )
    10461154                $form = $this->field_types[$field];
     
    10511159
    10521160        $where_formats = $where_format = (array) $where_format;
    1053         foreach ( (array) array_keys($where) as $field ) {
    1054             if ( !empty($where_format) )
    1055                 $form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0];
    1056             elseif ( isset($this->field_types[$field]) )
     1161        foreach ( (array) array_keys( $where ) as $field ) {
     1162            if ( !empty( $where_format ) )
     1163                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1164            elseif ( isset( $this->field_types[$field] ) )
    10571165                $form = $this->field_types[$field];
    10581166            else
     
    10621170
    10631171        $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
    1064         return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) );
     1172        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    10651173    }
    10661174
     
    10741182     * @since 0.71
    10751183     *
    1076      * @param string|null $query SQL query.  If null, use the result from the previous query.
     1184     * @param string|null $query Optional. SQL query. If null (default), uses the result from the previous query.
    10771185     * @param int $x (optional) Column of value to return.  Indexed from 0.
    10781186     * @param int $y (optional) Row of value to return.  Indexed from 0.
    1079      * @return string Database query result
    1080      */
    1081     function get_var($query=null, $x = 0, $y = 0) {
    1082         $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
     1187     * @return string|null Database query result, or null on failure
     1188     */
     1189    function get_var( $query = null, $x = 0, $y = 0 ) {
     1190        $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
    10831191        if ( $query )
    1084             $this->query($query);
     1192            $this->query( $query );
    10851193
    10861194        // Extract var out of cached results based x,y vals
    10871195        if ( !empty( $this->last_result[$y] ) ) {
    1088             $values = array_values(get_object_vars($this->last_result[$y]));
     1196            $values = array_values( get_object_vars( $this->last_result[$y] ) );
    10891197        }
    10901198
    10911199        // If there is a value return it else return null
    1092         return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
     1200        return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
    10931201    }
    10941202
     
    11031211     * @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants.  Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
    11041212     * @param int $y (optional) Row to return.  Indexed from 0.
    1105      * @return mixed Database query result in format specifed by $output
    1106      */
    1107     function get_row($query = null, $output = OBJECT, $y = 0) {
     1213     * @return mixed Database query result in format specifed by $output or null on failure
     1214     */
     1215    function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    11081216        $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    11091217        if ( $query )
    1110             $this->query($query);
     1218            $this->query( $query );
    11111219        else
    11121220            return null;
    11131221
    1114         if ( !isset($this->last_result[$y]) )
     1222        if ( !isset( $this->last_result[$y] ) )
    11151223            return null;
    11161224
     
    11181226            return $this->last_result[$y] ? $this->last_result[$y] : null;
    11191227        } elseif ( $output == ARRAY_A ) {
    1120             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
     1228            return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
    11211229        } elseif ( $output == ARRAY_N ) {
    1122             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
     1230            return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
    11231231        } else {
    11241232            $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
     
    11351243     * @since 0.71
    11361244     *
    1137      * @param string|null $query SQL query.  If null, use the result from the previous query.
    1138      * @param int $x Column to return. Indexed from 0.
    1139      * @return array Database query result.  Array indexed from 0 by SQL result row number.
    1140      */
    1141     function get_col($query = null , $x = 0) {
     1245     * @param string|null $query Optional. SQL query. If null (default), use the result from the previous query.
     1246     * @param int $x Optional. Column to return. Indexed from 0.
     1247     * @return array Database query result. Array indexed from 0 by SQL result row number.
     1248     */
     1249    function get_col( $query = null , $x = 0 ) {
    11421250        if ( $query )
    1143             $this->query($query);
     1251            $this->query( $query );
    11441252
    11451253        $new_array = array();
    11461254        // Extract the column values
    1147         for ( $i=0; $i < count($this->last_result); $i++ ) {
    1148             $new_array[$i] = $this->get_var(null, $x, $i);
     1255        for ( $i=0; $i < count( $this->last_result ); $i++ ) {
     1256            $new_array[$i] = $this->get_var( null, $x, $i );
    11491257        }
    11501258        return $new_array;
     
    11621270     * @return mixed Database query results
    11631271     */
    1164     function get_results($query = null, $output = OBJECT) {
     1272    function get_results( $query = null, $output = OBJECT ) {
    11651273        $this->func_call = "\$db->get_results(\"$query\", $output)";
    11661274
    11671275        if ( $query )
    1168             $this->query($query);
     1276            $this->query( $query );
    11691277        else
    11701278            return null;
     
    12071315     * @since 0.71
    12081316     *
    1209      * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
    1210      * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
     1317     * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
     1318     * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
    12111319     * @return mixed Column Results
    12121320     */
    1213     function get_col_info($info_type = 'name', $col_offset = -1) {
     1321    function get_col_info( $info_type = 'name', $col_offset = -1 ) {
    12141322        if ( $this->col_info ) {
    12151323            if ( $col_offset == -1 ) {
    1216                 $i = 0;
     1324                $i         = 0;
    12171325                $new_array = array();
    12181326                foreach( (array) $this->col_info as $col ) {
     
    12351343     */
    12361344    function timer_start() {
    1237         $mtime = microtime();
    1238         $mtime = explode(' ', $mtime);
     1345        $mtime            = explode( ' ', microtime() );
    12391346        $this->time_start = $mtime[1] + $mtime[0];
    12401347        return true;
     
    12491356     */
    12501357    function timer_stop() {
    1251         $mtime = microtime();
    1252         $mtime = explode(' ', $mtime);
    1253         $time_end = $mtime[1] + $mtime[0];
     1358        $mtime      = explode( ' ', microtime() );
     1359        $time_end   = $mtime[1] + $mtime[0];
    12541360        $time_total = $time_end - $this->time_start;
    12551361        return $time_total;
     
    12671373     * @return false|void
    12681374     */
    1269     function bail($message, $error_code = '500') {
     1375    function bail( $message, $error_code = '500' ) {
    12701376        if ( !$this->show_errors ) {
    1271             if ( class_exists('WP_Error') )
     1377            if ( class_exists( 'WP_Error' ) )
    12721378                $this->error = new WP_Error($error_code, $message);
    12731379            else
     
    12831389     * @since 2.5.0
    12841390     * @uses $wp_version
     1391     * @uses $required_mysql_version
    12851392     *
    12861393     * @return WP_Error
     
    12901397        // Make sure the server has the required MySQL version
    12911398        if ( version_compare($this->db_version(), $required_mysql_version, '<') )
    1292             return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher'), $wp_version, $required_mysql_version));
     1399            return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
    12931400    }
    12941401
     
    13071414
    13081415    /**
    1309      * Generic function to determine if a database supports a particular feature
     1416     * Determine if a database supports a particular feature
     1417     *
     1418     * @since 2.7
     1419     * @see   wpdb::db_version()
     1420     *
    13101421     * @param string $db_cap the feature
    1311      * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
     1422     * @param false|string|resource $dbh_or_table. Not implemented.
     1423     *  Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
    13121424     * @return bool
    13131425     */
     
    13151427        $version = $this->db_version();
    13161428
    1317         switch ( strtolower( $db_cap ) ) :
    1318         case 'collation' :    // @since 2.5.0
    1319         case 'group_concat' : // @since 2.7
    1320         case 'subqueries' :   // @since 2.7
    1321             return version_compare($version, '4.1', '>=');
    1322             break;
    1323         endswitch;
     1429        switch ( strtolower( $db_cap ) ) {
     1430            case 'collation' :    // @since 2.5.0
     1431            case 'group_concat' : // @since 2.7
     1432            case 'subqueries' :   // @since 2.7
     1433                return version_compare( $version, '4.1', '>=' );
     1434        };
    13241435
    13251436        return false;
     
    13561467    /**
    13571468     * The database version number
    1358      * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
     1469     * @param false|string|resource $dbh_or_table. Not implemented.
     1470     *  Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
    13591471     * @return false|string false on failure, version number on success
    13601472     */
    13611473    function db_version() {
    1362         return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));
     1474        return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    13631475    }
    13641476}
Note: See TracChangeset for help on using the changeset viewer.