X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/9e77185fafaf4e60e2b73821e0e4b9b1a11fb85f..7f1521bf193b382565eb753043c161f4cb3fcda7:/wp-includes/user.php diff --git a/wp-includes/user.php b/wp-includes/user.php index af028645..e602a3ec 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -19,8 +19,8 @@ * * @since 2.5.0 * - * @param array $credentials Optional. User info in order to sign on. - * @param bool $secure_cookie Optional. Whether to use secure cookie. + * @param array $credentials Optional. User info in order to sign on. + * @param string|bool $secure_cookie Optional. Whether to use secure cookie. * @return WP_User|WP_Error WP_User on success, WP_Error on failure. */ function wp_signon( $credentials = array(), $secure_cookie = '' ) { @@ -112,7 +112,7 @@ function wp_signon( $credentials = array(), $secure_cookie = '' ) { * @return WP_User|WP_Error WP_User on success, WP_Error on failure. */ function wp_authenticate_username_password($user, $username, $password) { - if ( is_a( $user, 'WP_User' ) ) { + if ( $user instanceof WP_User ) { return $user; } @@ -134,7 +134,7 @@ function wp_authenticate_username_password($user, $username, $password) { $user = get_user_by('login', $username); if ( !$user ) - return new WP_Error( 'invalid_username', sprintf( __( 'ERROR: Invalid username. Lost your password?' ), wp_lostpassword_url() ) ); + return new WP_Error( 'invalid_username', sprintf( __( 'ERROR: Invalid username. Lost your password?' ), wp_lostpassword_url() ) ); /** * Filter whether the given user can be authenticated with the provided $password. @@ -150,7 +150,7 @@ function wp_authenticate_username_password($user, $username, $password) { return $user; if ( !wp_check_password($password, $user->user_pass, $user->ID) ) - return new WP_Error( 'incorrect_password', sprintf( __( 'ERROR: The password you entered for the username %1$s is incorrect. Lost your password?' ), + return new WP_Error( 'incorrect_password', sprintf( __( 'ERROR: The password you entered for the username %1$s is incorrect. Lost your password?' ), $username, wp_lostpassword_url() ) ); return $user; @@ -167,7 +167,7 @@ function wp_authenticate_username_password($user, $username, $password) { * @return WP_User|WP_Error WP_User on success, WP_Error on failure. */ function wp_authenticate_cookie($user, $username, $password) { - if ( is_a( $user, 'WP_User' ) ) { + if ( $user instanceof WP_User ) { return $user; } @@ -202,7 +202,7 @@ function wp_authenticate_cookie($user, $username, $password) { * @return WP_User|WP_Error WP_User on success, WP_Error if the user is considered a spammer. */ function wp_authenticate_spam_check( $user ) { - if ( $user && is_a( $user, 'WP_User' ) && is_multisite() ) { + if ( $user instanceof WP_User && is_multisite() ) { /** * Filter whether the user has been marked as a spammer. * @@ -229,7 +229,7 @@ function wp_authenticate_spam_check( $user ) { * * @since 3.9.0 * - * @param int|bool $user The user ID (or false) as received from the + * @param int|bool $user_id The user ID (or false) as received from the * determine_current_user filter. * @return int|bool User ID if validated, false otherwise. If a user ID from * an earlier filter callback is received, that value is returned. @@ -250,16 +250,18 @@ function wp_validate_logged_in_cookie( $user_id ) { * Number of posts user has written. * * @since 3.0.0 + * @since 4.1.0 Added `$post_type` argument. * * @global wpdb $wpdb WordPress database object for queries. * - * @param int $userid User ID. - * @return int Amount of posts user has written. + * @param int $userid User ID. + * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'. + * @return int Number of posts the user has written in this post type. */ -function count_user_posts($userid) { +function count_user_posts( $userid, $post_type = 'post' ) { global $wpdb; - $where = get_posts_by_author_sql('post', true, $userid); + $where = get_posts_by_author_sql( $post_type, true, $userid ); $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); @@ -267,11 +269,13 @@ function count_user_posts($userid) { * Filter the number of posts a user has written. * * @since 2.7.0 + * @since 4.1.0 Added `$post_type` argument. * - * @param int $count The user's post count. - * @param int $userid User ID. + * @param int $count The user's post count. + * @param int $userid User ID. + * @param string $post_type Post type to count the number of posts for. */ - return apply_filters( 'get_usernumposts', $count, $userid ); + return apply_filters( 'get_usernumposts', $count, $userid, $post_type ); } /** @@ -316,8 +320,6 @@ function count_many_users_posts( $users, $post_type = 'post', $public_only = fal * * @since MU * - * @uses wp_get_current_user - * * @return int The current user's ID */ function get_current_user_id() { @@ -343,7 +345,7 @@ function get_current_user_id() { * * @param string $option User option name. * @param int $user Optional. User ID. - * @param bool $deprecated Use get_option() to check for an option in the options table. + * @param string $deprecated Use get_option() to check for an option in the options table. * @return mixed User option value on success, false on failure. */ function get_user_option( $option, $user = 0, $deprecated = '' ) { @@ -369,7 +371,7 @@ function get_user_option( $option, $user = 0, $deprecated = '' ) { /** * Filter a specific user option value. * - * The dynamic portion of the hook name, $option, refers to the user option name. + * The dynamic portion of the hook name, `$option`, refers to the user option name. * * @since 2.5.0 * @@ -439,6 +441,8 @@ function delete_user_option( $user_id, $option_name, $global = false ) { * WordPress User Query class. * * @since 3.1.0 + * + * @see WP_User_Query::prepare_query() for information on accepted arguments. */ class WP_User_Query { @@ -469,6 +473,17 @@ class WP_User_Query { */ private $total_users = 0; + /** + * Metadata query container. + * + * @since 4.2.0 + * @access public + * @var object WP_Meta_Query + */ + public $meta_query = false; + + private $compat_fields = array( 'results', 'total_users' ); + // SQL clauses public $query_fields; public $query_from; @@ -481,8 +496,7 @@ class WP_User_Query { * * @since 3.1.0 * - * @param string|array $args Optional. The query variables. - * @return WP_User_Query + * @param null|string|array $args Optional. The query variables. */ public function __construct( $query = null ) { if ( ! empty( $query ) ) { @@ -495,8 +509,54 @@ class WP_User_Query { * Prepare the query variables. * * @since 3.1.0 + * @since 4.2.0 Added 'meta_value_num' support for `$orderby` parameter. Added multi-dimensional array syntax + * for `$orderby` parameter. + * @access public * - * @param string|array $args Optional. The query variables. + * @param string|array $query { + * Optional. Array or string of Query parameters. + * + * @type int $blog_id The site ID. Default is the global blog id. + * @type string $role Role name. Default empty. + * @type string $meta_key User meta key. Default empty. + * @type string $meta_value User meta value. Default empty. + * @type string $meta_compare Comparison operator to test the `$meta_value`. Accepts '=', '!=', + * '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', + * 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', + * or 'RLIKE'. Default '='. + * @type array $include An array of user IDs to include. Default empty array. + * @type array $exclude An array of user IDs to exclude. Default empty array. + * @type string $search Search keyword. Searches for possible string matches on columns. + * When `$search_columns` is left empty, it tries to determine which + * column to search in based on search string. Default empty. + * @type array $search_columns Array of column names to be searched. Accepts 'ID', 'login', + * 'nicename', 'email', 'url'. Default empty array. + * @type string|array $orderby Field(s) to sort the retrieved users by. May be a single value, + * an array of values, or a multi-dimensional array with fields as keys + * and orders ('ASC' or 'DESC') as values. Accepted values are'ID', + * 'display_name' (or 'name'), 'user_login' (or 'login'), + * 'user_nicename' (or 'nicename'), 'user_email' (or 'email'), + * 'user_url' (or 'url'), 'user_registered' (or 'registered'), + * 'post_count', 'meta_value', 'meta_value_num', the value of + * `$meta_key`, or an array key of `$meta_query`. To use 'meta_value' + * or 'meta_value_num', `$meta_key` must be also be defined. + * Default 'user_login'. + * @type string $order Designates ascending or descending order of users. Order values + * passed as part of an `$orderby` array take precedence over this + * parameter. Accepts 'ASC', 'DESC'. Default 'ASC'. + * @type int $offset Number of users to offset in retrieved results. Can be used in + * conjunction with pagination. Default 0. + * @type int $number Number of users to limit the query for. Can be used in conjunction + * with pagination. Value -1 (all) is not supported. + * Default empty (all users). + * @type bool $count_total Whether to count the total number of users found. If pagination is not + * needed, setting this to false can improve performance. Default true. + * @type string|array $fields Which fields to return. Single or all fields (string), or array + * of fields. Accepts 'ID', 'display_name', 'login', 'nicename', 'email', + * 'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to + * include meta fields. Default 'all'. + * @type string $who Type of users to query. Accepts 'authors'. Default empty (all users). + * } */ public function prepare_query( $query = array() ) { global $wpdb; @@ -559,43 +619,111 @@ class WP_User_Query { $this->query_from = "FROM $wpdb->users"; $this->query_where = "WHERE 1=1"; + // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below. + if ( ! empty( $qv['include'] ) ) { + $include = wp_parse_id_list( $qv['include'] ); + } else { + $include = false; + } + + $blog_id = 0; + if ( isset( $qv['blog_id'] ) ) { + $blog_id = absint( $qv['blog_id'] ); + } + + if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { + $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level'; + $qv['meta_value'] = 0; + $qv['meta_compare'] = '!='; + $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query + } + + // Meta query. + $this->meta_query = new WP_Meta_Query(); + $this->meta_query->parse_query_vars( $qv ); + + $role = ''; + if ( isset( $qv['role'] ) ) { + $role = trim( $qv['role'] ); + } + + if ( $blog_id && ( $role || is_multisite() ) ) { + $cap_meta_query = array(); + $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; + + if ( $role ) { + $cap_meta_query['value'] = '"' . $role . '"'; + $cap_meta_query['compare'] = 'like'; + } + + if ( empty( $this->meta_query->queries ) ) { + $this->meta_query->queries = array( $cap_meta_query ); + } elseif ( ! in_array( $cap_meta_query, $this->meta_query->queries, true ) ) { + // Append the cap query to the original queries and reparse the query. + $this->meta_query->queries = array( + 'relation' => 'AND', + array( $this->meta_query->queries, $cap_meta_query ), + ); + } + + $this->meta_query->parse_query_vars( $this->meta_query->queries ); + } + + if ( ! empty( $this->meta_query->queries ) ) { + $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this ); + $this->query_from .= $clauses['join']; + $this->query_where .= $clauses['where']; + + if ( 'OR' == $this->meta_query->relation ) { + $this->query_fields = 'DISTINCT ' . $this->query_fields; + } + } + // sorting - if ( isset( $qv['orderby'] ) ) { - if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { - $orderby = 'user_' . $qv['orderby']; - } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) { - $orderby = $qv['orderby']; - } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) { - $orderby = 'display_name'; - } elseif ( 'post_count' == $qv['orderby'] ) { - // todo: avoid the JOIN - $where = get_posts_by_author_sql('post'); - $this->query_from .= " LEFT OUTER JOIN ( - SELECT post_author, COUNT(*) as post_count - FROM $wpdb->posts - $where - GROUP BY post_author - ) p ON ({$wpdb->users}.ID = p.post_author) - "; - $orderby = 'post_count'; - } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { - $orderby = 'ID'; - } elseif ( 'meta_value' == $qv['orderby'] ) { - $orderby = "$wpdb->usermeta.meta_value"; + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; + $order = $this->parse_order( $qv['order'] ); + + if ( empty( $qv['orderby'] ) ) { + // Default order is by 'user_login'. + $ordersby = array( 'user_login' => $order ); + } else if ( is_array( $qv['orderby'] ) ) { + $ordersby = $qv['orderby']; + } else { + // 'orderby' values may be a comma- or space-separated list. + $ordersby = preg_split( '/[,\s]+/', $qv['orderby'] ); + } + + $orderby_array = array(); + foreach ( $ordersby as $_key => $_value ) { + if ( ! $_value ) { + continue; + } + + if ( is_int( $_key ) ) { + // Integer key means this is a flat array of 'orderby' fields. + $_orderby = $_value; + $_order = $order; } else { - $orderby = 'user_login'; + // Non-integer key means this the key is the field and the value is ASC/DESC. + $_orderby = $_key; + $_order = $_value; } + + $parsed = $this->parse_orderby( $_orderby ); + + if ( ! $parsed ) { + continue; + } + + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); } - if ( empty( $orderby ) ) - $orderby = 'user_login'; + // If no valid clauses were found, order by user_login. + if ( empty( $orderby_array ) ) { + $orderby_array[] = "user_login $order"; + } - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; - if ( 'ASC' == $qv['order'] ) - $order = 'ASC'; - else - $order = 'DESC'; - $this->query_orderby = "ORDER BY $orderby $order"; + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); // limit if ( isset( $qv['number'] ) && $qv['number'] ) { @@ -654,55 +782,21 @@ class WP_User_Query { $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); } - $blog_id = 0; - if ( isset( $qv['blog_id'] ) ) - $blog_id = absint( $qv['blog_id'] ); - - if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { - $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level'; - $qv['meta_value'] = 0; - $qv['meta_compare'] = '!='; - $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query - } - - $role = ''; - if ( isset( $qv['role'] ) ) - $role = trim( $qv['role'] ); - - if ( $blog_id && ( $role || is_multisite() ) ) { - $cap_meta_query = array(); - $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; - - if ( $role ) { - $cap_meta_query['value'] = '"' . $role . '"'; - $cap_meta_query['compare'] = 'like'; - } - - if ( empty( $qv['meta_query'] ) || ! in_array( $cap_meta_query, $qv['meta_query'], true ) ) { - $qv['meta_query'][] = $cap_meta_query; - } - } - - $meta_query = new WP_Meta_Query(); - $meta_query->parse_query_vars( $qv ); - - if ( !empty( $meta_query->queries ) ) { - $clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this ); - $this->query_from .= $clauses['join']; - $this->query_where .= $clauses['where']; - - if ( 'OR' == $meta_query->relation ) - $this->query_fields = 'DISTINCT ' . $this->query_fields; - } - - if ( ! empty( $qv['include'] ) ) { - $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); + if ( ! empty( $include ) ) { + // Sanitized earlier. + $ids = implode( ',', $include ); $this->query_where .= " AND $wpdb->users.ID IN ($ids)"; } elseif ( ! empty( $qv['exclude'] ) ) { $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)"; } + // Date queries are allowed for the user_registered field. + if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) { + $date_query = new WP_Date_Query( $qv['date_query'], 'user_registered' ); + $this->query_where .= $date_query->get_sql(); + } + /** * Fires after the WP_User_Query has been parsed, and before * the query is executed. @@ -743,7 +837,7 @@ class WP_User_Query { * * @since 3.2.0 * - * @global wpdb $wpdb WordPress database object. + * @global wpdb $wpdb WordPress database abstraction object. * * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. */ @@ -763,7 +857,7 @@ class WP_User_Query { $this->results = $r; } elseif ( 'all' == $qv['fields'] ) { foreach ( $this->results as $key => $user ) { - $this->results[ $key ] = new WP_User( $user ); + $this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] ); } } } @@ -846,12 +940,85 @@ class WP_User_Query { * @since 3.1.0 * @access public * - * @return array Array of total users. + * @return int Number of total users. */ public function get_total() { return $this->total_users; } + /** + * Parse and sanitize 'orderby' keys passed to the user query. + * + * @since 4.2.0 + * @access protected + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param string $orderby Alias for the field to order by. + * @return string|bool Value to used in the ORDER clause, if `$orderby` is valid. False otherwise. + */ + protected function parse_orderby( $orderby ) { + global $wpdb; + + $meta_query_clauses = $this->meta_query->get_clauses(); + + $_orderby = ''; + if ( in_array( $orderby, array( 'login', 'nicename', 'email', 'url', 'registered' ) ) ) { + $_orderby = 'user_' . $orderby; + } elseif ( in_array( $orderby, array( 'user_login', 'user_nicename', 'user_email', 'user_url', 'user_registered' ) ) ) { + $_orderby = $orderby; + } elseif ( 'name' == $orderby || 'display_name' == $orderby ) { + $_orderby = 'display_name'; + } elseif ( 'post_count' == $orderby ) { + // todo: avoid the JOIN + $where = get_posts_by_author_sql( 'post' ); + $this->query_from .= " LEFT OUTER JOIN ( + SELECT post_author, COUNT(*) as post_count + FROM $wpdb->posts + $where + GROUP BY post_author + ) p ON ({$wpdb->users}.ID = p.post_author) + "; + $_orderby = 'post_count'; + } elseif ( 'ID' == $orderby || 'id' == $orderby ) { + $_orderby = 'ID'; + } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) { + $_orderby = "$wpdb->usermeta.meta_value"; + } elseif ( 'meta_value_num' == $orderby ) { + $_orderby = "$wpdb->usermeta.meta_value+0"; + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { + $include = wp_parse_id_list( $this->query_vars['include'] ); + $include_sql = implode( ',', $include ); + $_orderby = "FIELD( $wpdb->users.ID, $include_sql )"; + } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { + $meta_clause = $meta_query_clauses[ $orderby ]; + $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); + } + + return $_orderby; + } + + /** + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. + * + * @since 4.2.0 + * @access protected + * + * @param string $order The 'order' query variable. + * @return string The sanitized 'order' query variable. + */ + protected function parse_order( $order ) { + if ( ! is_string( $order ) || empty( $order ) ) { + return 'DESC'; + } + + if ( 'ASC' === strtoupper( $order ) ) { + return 'ASC'; + } else { + return 'DESC'; + } + } + /** * Make private properties readable for backwards compatibility. * @@ -862,7 +1029,9 @@ class WP_User_Query { * @return mixed Property. */ public function __get( $name ) { - return $this->$name; + if ( in_array( $name, $this->compat_fields ) ) { + return $this->$name; + } } /** @@ -871,12 +1040,14 @@ class WP_User_Query { * @since 4.0.0 * @access public * - * @param string $name Property to set. + * @param string $name Property to check if set. * @param mixed $value Property value. * @return mixed Newly-set property. */ public function __set( $name, $value ) { - return $this->$name = $value; + if ( in_array( $name, $this->compat_fields ) ) { + return $this->$name = $value; + } } /** @@ -889,7 +1060,9 @@ class WP_User_Query { * @return bool Whether the property is set. */ public function __isset( $name ) { - return isset( $this->$name ); + if ( in_array( $name, $this->compat_fields ) ) { + return isset( $this->$name ); + } } /** @@ -901,7 +1074,9 @@ class WP_User_Query { * @param string $name Property to unset. */ public function __unset( $name ) { - unset( $this->$name ); + if ( in_array( $name, $this->compat_fields ) ) { + unset( $this->$name ); + } } /** @@ -915,7 +1090,10 @@ class WP_User_Query { * @return mixed|bool Return value of the callback, false otherwise. */ public function __call( $name, $arguments ) { - return call_user_func_array( array( $this, $name ), $arguments ); + if ( 'get_search_sql' === $name ) { + return call_user_func_array( array( $this, $name ), $arguments ); + } + return false; } } @@ -924,9 +1102,10 @@ class WP_User_Query { * * @since 3.1.0 * - * @uses WP_User_Query See for default arguments and information. + * @see WP_User_Query * - * @param array $args Optional. Array of arguments. + * @param array $args Optional. Arguments to retrieve users. See {@see WP_User_Query::prepare_query()} + * for more information on accepted arguments. * @return array List of users. */ function get_users( $args = array() ) { @@ -1045,7 +1224,6 @@ function get_blogs_of_user( $user_id, $all = false ) { * Find out whether a user is a member of a given blog. * * @since MU 1.1 - * @uses get_blogs_of_user() * * @param int $user_id Optional. The unique ID of the user. Defaults to the current user. * @param int $blog_id Optional. ID of the blog to check. Defaults to the current site. @@ -1071,8 +1249,7 @@ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { * Post meta data is called "Custom Fields" on the Administration Screens. * * @since 3.0.0 - * @uses add_metadata() - * @link http://codex.wordpress.org/Function_Reference/add_user_meta + * @link https://codex.wordpress.org/Function_Reference/add_user_meta * * @param int $user_id User ID. * @param string $meta_key Metadata name. @@ -1092,8 +1269,7 @@ function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) { * allows removing all metadata matching key, if needed. * * @since 3.0.0 - * @uses delete_metadata() - * @link http://codex.wordpress.org/Function_Reference/delete_user_meta + * @link https://codex.wordpress.org/Function_Reference/delete_user_meta * * @param int $user_id user ID * @param string $meta_key Metadata name. @@ -1108,8 +1284,7 @@ function delete_user_meta($user_id, $meta_key, $meta_value = '') { * Retrieve user meta field for a user. * * @since 3.0.0 - * @uses get_metadata() - * @link http://codex.wordpress.org/Function_Reference/get_user_meta + * @link https://codex.wordpress.org/Function_Reference/get_user_meta * * @param int $user_id User ID. * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. @@ -1130,8 +1305,7 @@ function get_user_meta($user_id, $key = '', $single = false) { * If the meta field for the user does not exist, it will be added. * * @since 3.0.0 - * @uses update_metadata - * @link http://codex.wordpress.org/Function_Reference/update_user_meta + * @link https://codex.wordpress.org/Function_Reference/update_user_meta * * @param int $user_id User ID. * @param string $meta_key Metadata key. @@ -1436,7 +1610,7 @@ function sanitize_user_field($field, $value, $user_id, $context) { /** * Filter a user field value in the 'edit' context. * - * The dynamic portion of the hook name, $field, refers to the prefixed user + * The dynamic portion of the hook name, `$field`, refers to the prefixed user * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. * * @since 2.9.0 @@ -1451,7 +1625,7 @@ function sanitize_user_field($field, $value, $user_id, $context) { $value = esc_html( $value ); // textarea_escaped? else $value = esc_attr($value); - } else if ( 'db' == $context ) { + } elseif ( 'db' == $context ) { if ( $prefixed ) { /** This filter is documented in wp-includes/post.php */ $value = apply_filters( "pre_{$field}", $value ); @@ -1460,7 +1634,7 @@ function sanitize_user_field($field, $value, $user_id, $context) { /** * Filter the value of a user field in the 'db' context. * - * The dynamic portion of the hook name, $field, refers to the prefixed user + * The dynamic portion of the hook name, `$field`, refers to the prefixed user * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. * * @since 2.9.0 @@ -1480,7 +1654,7 @@ function sanitize_user_field($field, $value, $user_id, $context) { /** * Filter the value of a user field in a standard context. * - * The dynamic portion of the hook name, $field, refers to the prefixed user + * The dynamic portion of the hook name, `$field`, refers to the prefixed user * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. * * @since 2.9.0 @@ -1496,11 +1670,11 @@ function sanitize_user_field($field, $value, $user_id, $context) { if ( 'user_url' == $field ) $value = esc_url($value); - if ( 'attribute' == $context ) - $value = esc_attr($value); - else if ( 'js' == $context ) - $value = esc_js($value); - + if ( 'attribute' == $context ) { + $value = esc_attr( $value ); + } elseif ( 'js' == $context ) { + $value = esc_js( $value ); + } return $value; } @@ -1570,10 +1744,9 @@ function email_exists( $email ) { } /** - * Checks whether an username is valid. + * Checks whether a username is valid. * * @since 2.0.1 - * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters * * @param string $username Username. * @return bool Whether username given is valid @@ -1593,7 +1766,7 @@ function validate_username( $username ) { } /** - * Insert an user into the database. + * Insert a user into the database. * * Most of the $userdata array fields have filters associated with the values. * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim', @@ -1624,7 +1797,7 @@ function validate_username( $username ) { * to build $display_name if unspecified. * @type string|bool $rich_editing Whether to enable the rich-editor for the user. False * if not empty. - * @type string $date_registered Date the user registered. Format is 'Y-m-d H:i:s'. + * @type string $user_registered Date the user registered. Format is 'Y-m-d H:i:s'. * @type string $role User's role. * @type string $jabber User's Jabber account username. * @type string $aim User's AIM account username. @@ -1636,9 +1809,9 @@ function validate_username( $username ) { function wp_insert_user( $userdata ) { global $wpdb; - if ( is_a( $userdata, 'stdClass' ) ) { + if ( $userdata instanceof stdClass ) { $userdata = get_object_vars( $userdata ); - } elseif ( is_a( $userdata, 'WP_User' ) ) { + } elseif ( $userdata instanceof WP_User ) { $userdata = $userdata->to_array(); } // Are we updating or creating? @@ -1676,12 +1849,17 @@ function wp_insert_user( $userdata ) { if ( ! $update && username_exists( $user_login ) ) { return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) ); } - if ( empty( $userdata['user_nicename'] ) ) { - $user_nicename = sanitize_title( $user_login ); + + // If a nicename is provided, remove unsafe user characters before + // using it. Otherwise build a nicename from the user_login. + if ( ! empty( $userdata['user_nicename'] ) ) { + $user_nicename = sanitize_user( $userdata['user_nicename'], true ); } else { - $user_nicename = $userdata['user_nicename']; + $user_nicename = $user_login; } + $user_nicename = sanitize_title( $user_nicename ); + // Store values to save in user meta. $meta = array(); @@ -1716,7 +1894,15 @@ function wp_insert_user( $userdata ) { */ $user_email = apply_filters( 'pre_user_email', $raw_user_email ); - if ( ! $update && ! defined( 'WP_IMPORTING' ) && email_exists( $user_email ) ) { + /* + * If there is no update, just check for `email_exists`. If there is an update, + * check if current email and new email are the same, or not, and check `email_exists` + * accordingly. + */ + if ( ( ! $update || ( ! empty( $old_user_data ) && $user_email !== $old_user_data->user_email ) ) + && ! defined( 'WP_IMPORTING' ) + && email_exists( $user_email ) + ) { return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); } $nickname = empty( $userdata['nickname'] ) ? $user_login : $userdata['nickname']; @@ -1818,6 +2004,9 @@ function wp_insert_user( $userdata ) { $data = wp_unslash( $compacted ); if ( $update ) { + if ( $user_email !== $old_user_data->user_email ) { + $data['user_activation_key'] = ''; + } $wpdb->update( $wpdb->users, $data, compact( 'ID' ) ); $user_id = (int) $ID; } else { @@ -1871,7 +2060,7 @@ function wp_insert_user( $userdata ) { } /** - * Update an user in the database. + * Update a user in the database. * * It is possible to update a user's password by specifying the 'user_pass' * value in the $userdata parameter array. @@ -1887,17 +2076,22 @@ function wp_insert_user( $userdata ) { * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated. */ function wp_update_user($userdata) { - if ( is_a( $userdata, 'stdClass' ) ) + if ( $userdata instanceof stdClass ) { $userdata = get_object_vars( $userdata ); - elseif ( is_a( $userdata, 'WP_User' ) ) + } elseif ( $userdata instanceof WP_User ) { $userdata = $userdata->to_array(); + } - $ID = (int) $userdata['ID']; + $ID = isset( $userdata['ID'] ) ? (int) $userdata['ID'] : 0; + if ( ! $ID ) { + return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); + } // First, get all of the original fields $user_obj = get_userdata( $ID ); - if ( ! $user_obj ) + if ( ! $user_obj ) { return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); + } $user = $user_obj->to_array(); @@ -1942,17 +2136,17 @@ function wp_update_user($userdata) { } /** - * A simpler way of inserting an user into the database. + * A simpler way of inserting a user into the database. * * Creates a new user with just the username, password, and email. For more - * complex user creation use wp_insert_user() to specify more information. + * complex user creation use {@see wp_insert_user()} to specify more information. * * @since 2.0.0 * @see wp_insert_user() More complete way to create a new user * * @param string $username The user's username. * @param string $password The user's password. - * @param string $email The user's email (optional). + * @param string $email Optional. The user's email. Default empty. * @return int The new user's ID. */ function wp_create_user($username, $password, $email = '') { @@ -1970,7 +2164,7 @@ function wp_create_user($username, $password, $email = '') { * @since 3.3.0 * @access private * - * @param object $user WP_User instance. + * @param WP_User $user WP_User instance. * @return array */ function _get_additional_user_keys( $user ) { @@ -2019,6 +2213,26 @@ function _wp_get_user_contactmethods( $user = null ) { return wp_get_user_contact_methods( $user ); } +/** + * Gets the text suggesting how to create strong passwords. + * + * @since 4.1.0 + * + * @return string The password hint text. + */ +function wp_get_password_hint() { + $hint = __( 'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ & ).' ); + + /** + * Filter the text describing the site's password complexity policy. + * + * @since 4.1.0 + * + * @param string $hint The password hint text. + */ + return apply_filters( 'password_hint', $hint ); +} + /** * Retrieves a user row based on password reset key and login * @@ -2027,7 +2241,8 @@ function _wp_get_user_contactmethods( $user = null ) { * hashing process. This field is now hashed; old values are no longer accepted * but have a different WP_Error code so good user feedback can be provided. * - * @global wpdb $wpdb WordPress database object for queries. + * @global wpdb $wpdb WordPress database object for queries. + * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. * * @param string $key Hash to validate sending user's password. * @param string $login The user login.