Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r41714 r42343  
    3232 */
    3333function wp_signon( $credentials = array(), $secure_cookie = '' ) {
    34         if ( empty($credentials) ) {
     34        if ( empty( $credentials ) ) {
    3535                $credentials = array(); // Back-compat for plugins passing an empty string.
    3636
    37                 if ( ! empty($_POST['log']) )
     37                if ( ! empty( $_POST['log'] ) ) {
    3838                        $credentials['user_login'] = $_POST['log'];
    39                 if ( ! empty($_POST['pwd']) )
     39                }
     40                if ( ! empty( $_POST['pwd'] ) ) {
    4041                        $credentials['user_password'] = $_POST['pwd'];
    41                 if ( ! empty($_POST['rememberme']) )
     42                }
     43                if ( ! empty( $_POST['rememberme'] ) ) {
    4244                        $credentials['remember'] = $_POST['rememberme'];
    43         }
    44 
    45         if ( !empty($credentials['remember']) )
     45                }
     46        }
     47
     48        if ( ! empty( $credentials['remember'] ) ) {
    4649                $credentials['remember'] = true;
    47         else
     50        } else {
    4851                $credentials['remember'] = false;
     52        }
    4953
    5054        /**
     
    6367        do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );
    6468
    65         if ( '' === $secure_cookie )
     69        if ( '' === $secure_cookie ) {
    6670                $secure_cookie = is_ssl();
     71        }
    6772
    6873        /**
     
    7378         * @param bool  $secure_cookie Whether to use a secure sign-on cookie.
    7479         * @param array $credentials {
    75          *     Array of entered sign-on data.
    76          *
    77          *     @type string $user_login    Username.
    78          *     @type string $user_password Password entered.
     80         *     Array of entered sign-on data.
     81         *
     82         *     @type string $user_login    Username.
     83         *     @type string $user_password Password entered.
    7984         *     @type bool   $remember      Whether to 'remember' the user. Increases the time
    8085         *                                 that the cookie will be kept. Default false.
    81          * }
     86         * }
    8287         */
    8388        $secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials );
     
    8691        $auth_secure_cookie = $secure_cookie;
    8792
    88         add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);
    89 
    90         $user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
    91 
    92         if ( is_wp_error($user) ) {
    93                 if ( $user->get_error_codes() == array('empty_username', 'empty_password') ) {
    94                         $user = new WP_Error('', '');
     93        add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 );
     94
     95        $user = wp_authenticate( $credentials['user_login'], $credentials['user_password'] );
     96
     97        if ( is_wp_error( $user ) ) {
     98                if ( $user->get_error_codes() == array( 'empty_username', 'empty_password' ) ) {
     99                        $user = new WP_Error( '', '' );
    95100                }
    96101
     
    98103        }
    99104
    100         wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
     105        wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie );
    101106        /**
    102107         * Fires after the user has successfully logged in.
     
    121126 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
    122127 */
    123 function wp_authenticate_username_password($user, $username, $password) {
     128function wp_authenticate_username_password( $user, $username, $password ) {
    124129        if ( $user instanceof WP_User ) {
    125130                return $user;
    126131        }
    127132
    128         if ( empty($username) || empty($password) ) {
    129                 if ( is_wp_error( $user ) )
     133        if ( empty( $username ) || empty( $password ) ) {
     134                if ( is_wp_error( $user ) ) {
    130135                        return $user;
     136                }
    131137
    132138                $error = new WP_Error();
    133139
    134                 if ( empty($username) )
    135                         $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
    136 
    137                 if ( empty($password) )
    138                         $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
     140                if ( empty( $username ) ) {
     141                        $error->add( 'empty_username', __( '<strong>ERROR</strong>: The username field is empty.' ) );
     142                }
     143
     144                if ( empty( $password ) ) {
     145                        $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) );
     146                }
    139147
    140148                return $error;
    141149        }
    142150
    143         $user = get_user_by('login', $username);
    144 
    145         if ( !$user ) {
    146                 return new WP_Error( 'invalid_username',
     151        $user = get_user_by( 'login', $username );
     152
     153        if ( ! $user ) {
     154                return new WP_Error(
     155                        'invalid_username',
    147156                        __( '<strong>ERROR</strong>: Invalid username.' ) .
    148157                        ' <a href="' . wp_lostpassword_url() . '">' .
     
    162171         */
    163172        $user = apply_filters( 'wp_authenticate_user', $user, $password );
    164         if ( is_wp_error($user) )
     173        if ( is_wp_error( $user ) ) {
    165174                return $user;
     175        }
    166176
    167177        if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
    168                 return new WP_Error( 'incorrect_password',
     178                return new WP_Error(
     179                        'incorrect_password',
    169180                        sprintf(
    170181                                /* translators: %s: user name */
     
    222233
    223234        if ( ! $user ) {
    224                 return new WP_Error( 'invalid_email',
     235                return new WP_Error(
     236                        'invalid_email',
    225237                        __( '<strong>ERROR</strong>: Invalid email address.' ) .
    226238                        ' <a href="' . wp_lostpassword_url() . '">' .
     
    238250
    239251        if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
    240                 return new WP_Error( 'incorrect_password',
     252                return new WP_Error(
     253                        'incorrect_password',
    241254                        sprintf(
    242255                                /* translators: %s: email address */
     
    265278 * @return WP_User|WP_Error WP_User on success, WP_Error on failure.
    266279 */
    267 function wp_authenticate_cookie($user, $username, $password) {
     280function wp_authenticate_cookie( $user, $username, $password ) {
    268281        if ( $user instanceof WP_User ) {
    269282                return $user;
    270283        }
    271284
    272         if ( empty($username) && empty($password) ) {
     285        if ( empty( $username ) && empty( $password ) ) {
    273286                $user_id = wp_validate_auth_cookie();
    274                 if ( $user_id )
    275                         return new WP_User($user_id);
     287                if ( $user_id ) {
     288                        return new WP_User( $user_id );
     289                }
    276290
    277291                global $auth_secure_cookie;
    278292
    279                 if ( $auth_secure_cookie )
     293                if ( $auth_secure_cookie ) {
    280294                        $auth_cookie = SECURE_AUTH_COOKIE;
    281                 else
     295                } else {
    282296                        $auth_cookie = AUTH_COOKIE;
    283 
    284                 if ( !empty($_COOKIE[$auth_cookie]) )
    285                         return new WP_Error('expired_session', __('Please log in again.'));
     297                }
     298
     299                if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) {
     300                        return new WP_Error( 'expired_session', __( 'Please log in again.' ) );
     301                }
    286302
    287303                // If the cookie is not set, be silent.
     
    312328                $spammed = apply_filters( 'check_is_user_spammed', is_user_spammy( $user ), $user );
    313329
    314                 if ( $spammed )
     330                if ( $spammed ) {
    315331                        return new WP_Error( 'spammer_account', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.' ) );
     332                }
    316333        }
    317334        return $user;
     
    338355        }
    339356
    340         if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) ) {
     357        if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
    341358                return false;
    342359        }
    343360
    344         return wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' );
     361        return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
    345362}
    346363
     
    398415
    399416        $count = array();
    400         if ( empty( $users ) || ! is_array( $users ) )
     417        if ( empty( $users ) || ! is_array( $users ) ) {
    401418                return $count;
     419        }
    402420
    403421        $userlist = implode( ',', array_map( 'absint', $users ) );
    404         $where = get_posts_by_author_sql( $post_type, true, null, $public_only );
     422        $where    = get_posts_by_author_sql( $post_type, true, null, $public_only );
    405423
    406424        $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
     
    410428
    411429        foreach ( $users as $id ) {
    412                 if ( ! isset( $count[ $id ] ) )
     430                if ( ! isset( $count[ $id ] ) ) {
    413431                        $count[ $id ] = 0;
     432                }
    414433        }
    415434
     
    429448 */
    430449function get_current_user_id() {
    431         if ( ! function_exists( 'wp_get_current_user' ) )
     450        if ( ! function_exists( 'wp_get_current_user' ) ) {
    432451                return 0;
     452        }
    433453        $user = wp_get_current_user();
    434454        return ( isset( $user->ID ) ? (int) $user->ID : 0 );
     
    457477        global $wpdb;
    458478
    459         if ( !empty( $deprecated ) )
     479        if ( ! empty( $deprecated ) ) {
    460480                _deprecated_argument( __FUNCTION__, '3.0.0' );
    461 
    462         if ( empty( $user ) )
     481        }
     482
     483        if ( empty( $user ) ) {
    463484                $user = get_current_user_id();
    464 
    465         if ( ! $user = get_userdata( $user ) )
     485        }
     486
     487        if ( ! $user = get_userdata( $user ) ) {
    466488                return false;
     489        }
    467490
    468491        $prefix = $wpdb->get_blog_prefix();
    469         if ( $user->has_prop( $prefix . $option ) ) // Blog specific
     492        if ( $user->has_prop( $prefix . $option ) ) { // Blog specific
    470493                $result = $user->get( $prefix . $option );
    471         elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
     494        } elseif ( $user->has_prop( $option ) ) { // User specific and cross-blog
    472495                $result = $user->get( $option );
    473         else
     496        } else {
    474497                $result = false;
     498        }
    475499
    476500        /**
     
    512536        global $wpdb;
    513537
    514         if ( !$global )
     538        if ( ! $global ) {
    515539                $option_name = $wpdb->get_blog_prefix() . $option_name;
     540        }
    516541
    517542        return update_user_meta( $user_id, $option_name, $newvalue );
     
    538563        global $wpdb;
    539564
    540         if ( !$global )
     565        if ( ! $global ) {
    541566                $option_name = $wpdb->get_blog_prefix() . $option_name;
     567        }
    542568        return delete_user_meta( $user_id, $option_name );
    543569}
     
    556582function get_users( $args = array() ) {
    557583
    558         $args = wp_parse_args( $args );
     584        $args                = wp_parse_args( $args );
    559585        $args['count_total'] = false;
    560586
    561         $user_search = new WP_User_Query($args);
     587        $user_search = new WP_User_Query( $args );
    562588
    563589        return (array) $user_search->get_results();
     
    584610
    585611        // Logged out users can't have sites
    586         if ( empty( $user_id ) )
     612        if ( empty( $user_id ) ) {
    587613                return array();
     614        }
    588615
    589616        /**
     
    607634
    608635        $keys = get_user_meta( $user_id );
    609         if ( empty( $keys ) )
     636        if ( empty( $keys ) ) {
    610637                return array();
     638        }
    611639
    612640        if ( ! is_multisite() ) {
    613                 $site_id = get_current_blog_id();
    614                 $sites = array( $site_id => new stdClass );
     641                $site_id                        = get_current_blog_id();
     642                $sites                          = array( $site_id => new stdClass );
    615643                $sites[ $site_id ]->userblog_id = $site_id;
    616                 $sites[ $site_id ]->blogname = get_option('blogname');
    617                 $sites[ $site_id ]->domain = '';
    618                 $sites[ $site_id ]->path = '';
    619                 $sites[ $site_id ]->site_id = 1;
    620                 $sites[ $site_id ]->siteurl = get_option('siteurl');
    621                 $sites[ $site_id ]->archived = 0;
    622                 $sites[ $site_id ]->spam = 0;
    623                 $sites[ $site_id ]->deleted = 0;
     644                $sites[ $site_id ]->blogname    = get_option( 'blogname' );
     645                $sites[ $site_id ]->domain      = '';
     646                $sites[ $site_id ]->path        = '';
     647                $sites[ $site_id ]->site_id     = 1;
     648                $sites[ $site_id ]->siteurl     = get_option( 'siteurl' );
     649                $sites[ $site_id ]->archived    = 0;
     650                $sites[ $site_id ]->spam        = 0;
     651                $sites[ $site_id ]->deleted     = 0;
    624652                return $sites;
    625653        }
     
    635663
    636664        foreach ( $keys as $key ) {
    637                 if ( 'capabilities' !== substr( $key, -12 ) )
     665                if ( 'capabilities' !== substr( $key, -12 ) ) {
    638666                        continue;
    639                 if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
     667                }
     668                if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
    640669                        continue;
     670                }
    641671                $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
    642                 if ( ! is_numeric( $site_id ) )
     672                if ( ! is_numeric( $site_id ) ) {
    643673                        continue;
     674                }
    644675
    645676                $site_ids[] = (int) $site_id;
     
    767798 * @return int|false Meta ID on success, false on failure.
    768799 */
    769 function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) {
    770         return add_metadata('user', $user_id, $meta_key, $meta_value, $unique);
     800function add_user_meta( $user_id, $meta_key, $meta_value, $unique = false ) {
     801        return add_metadata( 'user', $user_id, $meta_key, $meta_value, $unique );
    771802}
    772803
     
    786817 * @return bool True on success, false on failure.
    787818 */
    788 function delete_user_meta($user_id, $meta_key, $meta_value = '') {
    789         return delete_metadata('user', $user_id, $meta_key, $meta_value);
     819function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
     820        return delete_metadata( 'user', $user_id, $meta_key, $meta_value );
    790821}
    791822
     
    801832 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
    802833 */
    803 function get_user_meta($user_id, $key = '', $single = false) {
    804         return get_metadata('user', $user_id, $key, $single);
     834function get_user_meta( $user_id, $key = '', $single = false ) {
     835        return get_metadata( 'user', $user_id, $key, $single );
    805836}
    806837
     
    822853 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
    823854 */
    824 function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
    825         return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);
     855function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
     856        return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value );
    826857}
    827858
     
    853884        }
    854885        $blog_prefix = $wpdb->get_blog_prefix( $site_id );
    855         $result = array();
     886        $result      = array();
    856887
    857888        if ( 'time' == $strategy ) {
     
    867898                $select_count = array();
    868899                foreach ( $avail_roles as $this_role => $name ) {
    869                         $select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
     900                        $select_count[] = $wpdb->prepare( 'COUNT(NULLIF(`meta_value` LIKE %s, false))', '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%' );
    870901                }
    871902                $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
    872                 $select_count = implode(', ', $select_count);
     903                $select_count   = implode( ', ', $select_count );
    873904
    874905                // Add the meta_value index to the selection list, then run the query.
    875                 $row = $wpdb->get_row( "
     906                $row = $wpdb->get_row(
     907                        "
    876908                        SELECT {$select_count}, COUNT(*)
    877909                        FROM {$wpdb->usermeta}
    878910                        INNER JOIN {$wpdb->users} ON user_id = ID
    879911                        WHERE meta_key = '{$blog_prefix}capabilities'
    880                 ", ARRAY_N );
     912                ", ARRAY_N
     913                );
    881914
    882915                // Run the previous loop again to associate results with role names.
    883                 $col = 0;
     916                $col         = 0;
    884917                $role_counts = array();
    885918                foreach ( $avail_roles as $this_role => $name ) {
    886                         $count = (int) $row[$col++];
    887                         if ($count > 0) {
    888                                 $role_counts[$this_role] = $count;
     919                        $count = (int) $row[ $col++ ];
     920                        if ( $count > 0 ) {
     921                                $role_counts[ $this_role ] = $count;
    889922                        }
    890923                }
    891924
    892                 $role_counts['none'] = (int) $row[$col++];
     925                $role_counts['none'] = (int) $row[ $col++ ];
    893926
    894927                // Get the meta_value index from the end of the result set.
    895                 $total_users = (int) $row[$col];
     928                $total_users = (int) $row[ $col ];
    896929
    897930                $result['total_users'] = $total_users;
     
    902935                );
    903936
    904                 $users_of_blog = $wpdb->get_col( "
     937                $users_of_blog = $wpdb->get_col(
     938                        "
    905939                        SELECT meta_value
    906940                        FROM {$wpdb->usermeta}
    907941                        INNER JOIN {$wpdb->users} ON user_id = ID
    908942                        WHERE meta_key = '{$blog_prefix}capabilities'
    909                 " );
     943                "
     944                );
    910945
    911946                foreach ( $users_of_blog as $caps_meta ) {
    912                         $b_roles = maybe_unserialize($caps_meta);
    913                         if ( ! is_array( $b_roles ) )
     947                        $b_roles = maybe_unserialize( $caps_meta );
     948                        if ( ! is_array( $b_roles ) ) {
    914949                                continue;
     950                        }
    915951                        if ( empty( $b_roles ) ) {
    916952                                $avail_roles['none']++;
    917953                        }
    918954                        foreach ( $b_roles as $b_role => $val ) {
    919                                 if ( isset($avail_roles[$b_role]) ) {
    920                                         $avail_roles[$b_role]++;
     955                                if ( isset( $avail_roles[ $b_role ] ) ) {
     956                                        $avail_roles[ $b_role ]++;
    921957                                } else {
    922                                         $avail_roles[$b_role] = 1;
     958                                        $avail_roles[ $b_role ] = 1;
    923959                                }
    924960                        }
     
    953989 * @param int $for_user_id Optional. User ID to set up global data.
    954990 */
    955 function setup_userdata($for_user_id = '') {
     991function setup_userdata( $for_user_id = '' ) {
    956992        global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity;
    957993
    958         if ( '' == $for_user_id )
     994        if ( '' == $for_user_id ) {
    959995                $for_user_id = get_current_user_id();
     996        }
    960997        $user = get_userdata( $for_user_id );
    961998
    962999        if ( ! $user ) {
    963                 $user_ID = 0;
     1000                $user_ID    = 0;
    9641001                $user_level = 0;
    965                 $userdata = null;
     1002                $userdata   = null;
    9661003                $user_login = $user_email = $user_url = $user_identity = '';
    9671004                return;
    9681005        }
    9691006
    970         $user_ID    = (int) $user->ID;
    971         $user_level = (int) $user->user_level;
    972         $userdata   = $user;
    973         $user_login = $user->user_login;
    974         $user_email = $user->user_email;
    975         $user_url   = $user->user_url;
     1007        $user_ID       = (int) $user->ID;
     1008        $user_level    = (int) $user->user_level;
     1009        $userdata      = $user;
     1010        $user_login    = $user->user_login;
     1011        $user_email    = $user->user_email;
     1012        $user_url      = $user->user_url;
    9761013        $user_identity = $user->display_name;
    9771014}
     
    10421079function wp_dropdown_users( $args = '' ) {
    10431080        $defaults = array(
    1044                 'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
    1045                 'orderby' => 'display_name', 'order' => 'ASC',
    1046                 'include' => '', 'exclude' => '', 'multi' => 0,
    1047                 'show' => 'display_name', 'echo' => 1,
    1048                 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
    1049                 'blog_id' => get_current_blog_id(), 'who' => '', 'include_selected' => false,
    1050                 'option_none_value' => -1,
    1051                 'role' => '',
    1052                 'role__in' => array(),
    1053                 'role__not_in' => array(),
     1081                'show_option_all'         => '',
     1082                'show_option_none'        => '',
     1083                'hide_if_only_one_author' => '',
     1084                'orderby'                 => 'display_name',
     1085                'order'                   => 'ASC',
     1086                'include'                 => '',
     1087                'exclude'                 => '',
     1088                'multi'                   => 0,
     1089                'show'                    => 'display_name',
     1090                'echo'                    => 1,
     1091                'selected'                => 0,
     1092                'name'                    => 'user',
     1093                'class'                   => '',
     1094                'id'                      => '',
     1095                'blog_id'                 => get_current_blog_id(),
     1096                'who'                     => '',
     1097                'include_selected'        => false,
     1098                'option_none_value'       => -1,
     1099                'role'                    => '',
     1100                'role__in'                => array(),
     1101                'role__not_in'            => array(),
    10541102        );
    10551103
     
    10711119        $query_args['fields'] = $fields;
    10721120
    1073         $show_option_all = $r['show_option_all'];
    1074         $show_option_none = $r['show_option_none'];
     1121        $show_option_all   = $r['show_option_all'];
     1122        $show_option_none  = $r['show_option_none'];
    10751123        $option_none_value = $r['option_none_value'];
    10761124
     
    11031151                if ( $show_option_none ) {
    11041152                        $_selected = selected( $option_none_value, $r['selected'], false );
    1105                         $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
     1153                        $output   .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
    11061154                }
    11071155
    11081156                if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) {
    11091157                        $found_selected = false;
    1110                         $r['selected'] = (int) $r['selected'];
     1158                        $r['selected']  = (int) $r['selected'];
    11111159                        foreach ( (array) $users as $user ) {
    11121160                                $user->ID = (int) $user->ID;
     
    11321180
    11331181                        $_selected = selected( $user->ID, $r['selected'], false );
    1134                         $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
    1135                 }
    1136 
    1137                 $output .= "</select>";
     1182                        $output   .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
     1183                }
     1184
     1185                $output .= '</select>';
    11381186        }
    11391187
     
    11691217 * @return mixed Sanitized value.
    11701218 */
    1171 function sanitize_user_field($field, $value, $user_id, $context) {
    1172         $int_fields = array('ID');
    1173         if ( in_array($field, $int_fields) )
     1219function sanitize_user_field( $field, $value, $user_id, $context ) {
     1220        $int_fields = array( 'ID' );
     1221        if ( in_array( $field, $int_fields ) ) {
    11741222                $value = (int) $value;
    1175 
    1176         if ( 'raw' == $context )
     1223        }
     1224
     1225        if ( 'raw' == $context ) {
    11771226                return $value;
    1178 
    1179         if ( !is_string($value) && !is_numeric($value) )
     1227        }
     1228
     1229        if ( ! is_string( $value ) && ! is_numeric( $value ) ) {
    11801230                return $value;
     1231        }
    11811232
    11821233        $prefixed = false !== strpos( $field, 'user_' );
     
    12031254                }
    12041255
    1205                 if ( 'description' == $field )
     1256                if ( 'description' == $field ) {
    12061257                        $value = esc_html( $value ); // textarea_escaped?
    1207                 else
    1208                         $value = esc_attr($value);
     1258                } else {
     1259                        $value = esc_attr( $value );
     1260                }
    12091261        } elseif ( 'db' == $context ) {
    12101262                if ( $prefixed ) {
     
    12181270                         * The dynamic portion of the hook name, `$field`, refers to the prefixed user
    12191271                         * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.
    1220                          *
     1272                         *
    12211273                         * @since 2.9.0
    12221274                         *
     
    12491301        }
    12501302
    1251         if ( 'user_url' == $field )
    1252                 $value = esc_url($value);
     1303        if ( 'user_url' == $field ) {
     1304                $value = esc_url( $value );
     1305        }
    12531306
    12541307        if ( 'attribute' == $context ) {
     
    12771330        }
    12781331
    1279         wp_cache_add($user->ID, $user, 'users');
    1280         wp_cache_add($user->user_login, $user->ID, 'userlogins');
    1281         wp_cache_add($user->user_email, $user->ID, 'useremail');
    1282         wp_cache_add($user->user_nicename, $user->ID, 'userslugs');
     1332        wp_cache_add( $user->ID, $user, 'users' );
     1333        wp_cache_add( $user->user_login, $user->ID, 'userlogins' );
     1334        wp_cache_add( $user->user_email, $user->ID, 'useremail' );
     1335        wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' );
    12831336}
    12841337
     
    12921345 */
    12931346function clean_user_cache( $user ) {
    1294         if ( is_numeric( $user ) )
     1347        if ( is_numeric( $user ) ) {
    12951348                $user = new WP_User( $user );
    1296 
    1297         if ( ! $user->exists() )
     1349        }
     1350
     1351        if ( ! $user->exists() ) {
    12981352                return;
     1353        }
    12991354
    13001355        wp_cache_delete( $user->ID, 'users' );
     
    13491404 */
    13501405function email_exists( $email ) {
    1351         if ( $user = get_user_by( 'email', $email) ) {
     1406        if ( $user = get_user_by( 'email', $email ) ) {
    13521407                return $user->ID;
    13531408        }
     
    13661421function validate_username( $username ) {
    13671422        $sanitized = sanitize_user( $username, true );
    1368         $valid = ( $sanitized == $username && ! empty( $sanitized ) );
     1423        $valid     = ( $sanitized == $username && ! empty( $sanitized ) );
    13691424
    13701425        /**
     
    14441499        // Are we updating or creating?
    14451500        if ( ! empty( $userdata['ID'] ) ) {
    1446                 $ID = (int) $userdata['ID'];
    1447                 $update = true;
     1501                $ID            = (int) $userdata['ID'];
     1502                $update        = true;
    14481503                $old_user_data = get_userdata( $ID );
    14491504
     
    14781533        // user_login must be between 0 and 60 characters.
    14791534        if ( empty( $user_login ) ) {
    1480                 return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
     1535                return new WP_Error( 'empty_user_login', __( 'Cannot create a user with an empty login name.' ) );
    14811536        } elseif ( mb_strlen( $user_login ) > 60 ) {
    14821537                return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) );
     
    16361691        $meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true';
    16371692
    1638         $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
     1693        $admin_color         = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
    16391694        $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
    16401695
     
    16471702        $meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : '';
    16481703
    1649         $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
     1704        $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login ) );
    16501705
    16511706        if ( $user_nicename_check ) {
    16521707                $suffix = 2;
    1653                 while ($user_nicename_check) {
     1708                while ( $user_nicename_check ) {
    16541709                        // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
    1655                         $base_length = 49 - mb_strlen( $suffix );
    1656                         $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
    1657                         $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
     1710                        $base_length         = 49 - mb_strlen( $suffix );
     1711                        $alt_user_nicename   = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
     1712                        $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login ) );
    16581713                        $suffix++;
    16591714                }
     
    16621717
    16631718        $compacted = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
    1664         $data = wp_unslash( $compacted );
     1719        $data      = wp_unslash( $compacted );
    16651720
    16661721        if ( ! $update ) {
     
    17061761
    17071762        /**
    1708          * Filters a user's meta values and keys immediately after the user is created or updated
    1709          * and before any user meta is inserted or updated.
    1710          *
    1711          * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
    1712          *
    1713          * @since 4.4.0
    1714          *
    1715          * @param array $meta {
    1716          *     Default meta values and keys for the user.
    1717          *
    1718          *     @type string   $nickname             The user's nickname. Default is the user's username.
     1763         * Filters a user's meta values and keys immediately after the user is created or updated
     1764         * and before any user meta is inserted or updated.
     1765         *
     1766         * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
     1767         *
     1768         * @since 4.4.0
     1769         *
     1770         * @param array $meta {
     1771         *     Default meta values and keys for the user.
     1772         *
     1773         *     @type string   $nickname             The user's nickname. Default is the user's username.
    17191774         *     @type string   $first_name           The user's first name.
    17201775         *     @type string   $last_name            The user's last name.
     
    17281783         *     @type bool     $show_admin_bar_front Whether to show the admin bar on the front end for the user.
    17291784         *                                          Default true.
    1730          * }
     1785         * }
    17311786         * @param WP_User $user   User object.
    17321787         * @param bool    $update Whether the user is being updated rather than created.
    1733          */
     1788         */
    17341789        $meta = apply_filters( 'insert_user_meta', $meta, $user, $update );
    17351790
     
    17481803                $user->set_role( $userdata['role'] );
    17491804        } elseif ( ! $update ) {
    1750                 $user->set_role(get_option('default_role'));
     1805                $user->set_role( get_option( 'default_role' ) );
    17511806        }
    17521807        wp_cache_delete( $user_id, 'users' );
     
    17931848 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
    17941849 */
    1795 function wp_update_user($userdata) {
     1850function wp_update_user( $userdata ) {
    17961851        if ( $userdata instanceof stdClass ) {
    17971852                $userdata = get_object_vars( $userdata );
     
    18231878        if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
    18241879                // If password is changing, hash it now
    1825                 $plaintext_pass = $userdata['user_pass'];
     1880                $plaintext_pass        = $userdata['user_pass'];
    18261881                $userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
    18271882
     
    18361891                 * @param array $user     The original user array.
    18371892                 * @param array $userdata The updated user array.
    1838                  *
    18391893                 */
    18401894                $send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
     
    18521906                 * @param array $user     The original user array.
    18531907                 * @param array $userdata The updated user array.
    1854                  *
    18551908                 */
    18561909                $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
     
    18621915        // Merge old and new fields with new fields overwriting old ones.
    18631916        $userdata = array_merge( $user, $userdata );
    1864         $user_id = wp_insert_user( $userdata );
     1917        $user_id  = wp_insert_user( $userdata );
    18651918
    18661919        if ( ! is_wp_error( $user_id ) ) {
     
    18751928                if ( ! empty( $send_password_change_email ) ) {
    18761929                        /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    1877                         $pass_change_text = __( 'Hi ###USERNAME###,
     1930                        $pass_change_text = __(
     1931                                'Hi ###USERNAME###,
    18781932
    18791933This notice confirms that your password was changed on ###SITENAME###.
     
    18861940Regards,
    18871941All at ###SITENAME###
    1888 ###SITEURL###' );
     1942###SITEURL###'
     1943                        );
    18891944
    18901945                        $pass_change_email = array(
     
    19161971                         * @param array $user     The original user array.
    19171972                         * @param array $userdata The updated user array.
    1918                          *
    19191973                         */
    19201974                        $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
     
    19311985                if ( ! empty( $send_email_change_email ) ) {
    19321986                        /* translators: Do not translate USERNAME, ADMIN_EMAIL, NEW_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    1933                         $email_change_text = __( 'Hi ###USERNAME###,
     1987                        $email_change_text = __(
     1988                                'Hi ###USERNAME###,
    19341989
    19351990This notice confirms that your email address on ###SITENAME### was changed to ###NEW_EMAIL###.
     
    19421997Regards,
    19431998All at ###SITENAME###
    1944 ###SITEURL###' );
     1999###SITEURL###'
     2000                        );
    19452001
    19462002                        $email_change_email = array(
     
    19942050        $current_user = wp_get_current_user();
    19952051        if ( $current_user->ID == $ID ) {
    1996                 if ( isset($plaintext_pass) ) {
     2052                if ( isset( $plaintext_pass ) ) {
    19972053                        wp_clear_auth_cookie();
    19982054
    19992055                        // Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
    20002056                        // If it's greater than this, then we know the user checked 'Remember Me' when they logged in.
    2001                         $logged_in_cookie    = wp_parse_auth_cookie( '', 'logged_in' );
     2057                        $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' );
    20022058                        /** This filter is documented in wp-includes/pluggable.php */
    20032059                        $default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false );
     
    20262082 *                      be created.
    20272083 */
    2028 function wp_create_user($username, $password, $email = '') {
     2084function wp_create_user( $username, $password, $email = '' ) {
    20292085        $user_login = wp_slash( $username );
    2030         $user_email = wp_slash( $email    );
    2031         $user_pass = $password;
    2032 
    2033         $userdata = compact('user_login', 'user_email', 'user_pass');
    2034         return wp_insert_user($userdata);
     2086        $user_email = wp_slash( $email );
     2087        $user_pass  = $password;
     2088
     2089        $userdata = compact( 'user_login', 'user_email', 'user_pass' );
     2090        return wp_insert_user( $userdata );
    20352091}
    20362092
     
    20682124                        'aim'    => __( 'AIM' ),
    20692125                        'yim'    => __( 'Yahoo IM' ),
    2070                         'jabber' => __( 'Jabber / Google Talk' )
     2126                        'jabber' => __( 'Jabber / Google Talk' ),
    20712127                );
    20722128        }
     
    20782134         *
    20792135         * @param array   $methods Array of contact methods and their labels.
    2080          * @param WP_User $user    WP_User object.
     2136         * @param WP_User $user    WP_User object.
    20812137         */
    20822138        return apply_filters( 'user_contactmethods', $methods, $user );
     
    21932249                $wp_hasher = new PasswordHash( 8, true );
    21942250        }
    2195         $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
     2251        $hashed    = time() . ':' . $wp_hasher->HashPassword( $key );
    21962252        $key_saved = $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
    21972253        if ( false === $key_saved ) {
     
    22192275 * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys.
    22202276 */
    2221 function check_password_reset_key($key, $login) {
     2277function check_password_reset_key( $key, $login ) {
    22222278        global $wpdb, $wp_hasher;
    22232279
    2224         $key = preg_replace('/[^a-z0-9]/i', '', $key);
    2225 
    2226         if ( empty( $key ) || !is_string( $key ) )
    2227                 return new WP_Error('invalid_key', __('Invalid key'));
    2228 
    2229         if ( empty($login) || !is_string($login) )
    2230                 return new WP_Error('invalid_key', __('Invalid key'));
     2280        $key = preg_replace( '/[^a-z0-9]/i', '', $key );
     2281
     2282        if ( empty( $key ) || ! is_string( $key ) ) {
     2283                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     2284        }
     2285
     2286        if ( empty( $login ) || ! is_string( $login ) ) {
     2287                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     2288        }
    22312289
    22322290        $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) );
    2233         if ( ! $row )
    2234                 return new WP_Error('invalid_key', __('Invalid key'));
     2291        if ( ! $row ) {
     2292                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     2293        }
    22352294
    22362295        if ( empty( $wp_hasher ) ) {
     
    22502309        if ( false !== strpos( $row->user_activation_key, ':' ) ) {
    22512310                list( $pass_request_time, $pass_key ) = explode( ':', $row->user_activation_key, 2 );
    2252                 $expiration_time = $pass_request_time + $expiration_duration;
     2311                $expiration_time                      = $pass_request_time + $expiration_duration;
    22532312        } else {
    2254                 $pass_key = $row->user_activation_key;
     2313                $pass_key        = $row->user_activation_key;
    22552314                $expiration_time = false;
    22562315        }
     
    22702329
    22712330        if ( hash_equals( $row->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) {
    2272                 $return = new WP_Error( 'expired_key', __( 'Invalid key' ) );
     2331                $return  = new WP_Error( 'expired_key', __( 'Invalid key' ) );
    22732332                $user_id = $row->ID;
    22742333
     
    24032462        $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    24042463
    2405         if ( $errors->get_error_code() )
     2464        if ( $errors->get_error_code() ) {
    24062465                return $errors;
     2466        }
    24072467
    24082468        $user_pass = wp_generate_password( 12, false );
    2409         $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
     2469        $user_id   = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
    24102470        if ( ! $user_id || is_wp_error( $user_id ) ) {
    24112471                $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you&hellip; please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
     
    25302590        }
    25312591
    2532         $regex  = implode( '|', array_keys( $role_names ) );
    2533         $regex  = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
    2534         $users  = $wpdb->get_col( $wpdb->prepare( "
     2592        $regex = implode( '|', array_keys( $role_names ) );
     2593        $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
     2594        $users = $wpdb->get_col(
     2595                $wpdb->prepare(
     2596                        "
    25352597                SELECT user_id
    25362598                FROM $wpdb->usermeta
    25372599                WHERE meta_key = '{$prefix}capabilities'
    25382600                AND meta_value NOT REGEXP %s
    2539         ", $regex ) );
     2601        ", $regex
     2602                )
     2603        );
    25402604
    25412605        return $users;
     
    25712635                // Upgrade stdClass to WP_User
    25722636                if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
    2573                         $cur_id = $current_user->ID;
     2637                        $cur_id       = $current_user->ID;
    25742638                        $current_user = null;
    25752639                        wp_set_current_user( $cur_id );
     
    25832647        }
    25842648
    2585         if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
     2649        if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
    25862650                wp_set_current_user( 0 );
    25872651                return $current_user;
     
    26352699        if ( $current_user->user_email != $_POST['email'] ) {
    26362700                if ( ! is_email( $_POST['email'] ) ) {
    2637                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn&#8217;t correct." ), array(
    2638                                 'form-field' => 'email',
    2639                         ) );
     2701                        $errors->add(
     2702                                'user_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ), array(
     2703                                        'form-field' => 'email',
     2704                                )
     2705                        );
    26402706
    26412707                        return;
     
    26432709
    26442710                if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) {
    2645                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array(
    2646                                 'form-field' => 'email',
    2647                         ) );
     2711                        $errors->add(
     2712                                'user_email', __( '<strong>ERROR</strong>: The email address is already used.' ), array(
     2713                                        'form-field' => 'email',
     2714                                )
     2715                        );
    26482716                        delete_user_meta( $current_user->ID, '_new_email' );
    26492717
     
    26652733
    26662734                /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    2667                 $email_text = __( 'Howdy ###USERNAME###,
     2735                $email_text = __(
     2736                        'Howdy ###USERNAME###,
    26682737
    26692738You recently requested to have the email address on your account changed.
     
    26792748Regards,
    26802749All at ###SITENAME###
    2681 ###SITEURL###' );
     2750###SITEURL###'
     2751                );
    26822752
    26832753                /**
Note: See TracChangeset for help on using the changeset viewer.