+ if ( 'id' == $field ) {
+ // Make sure the value is numeric to avoid casting objects, for example,
+ // to int 1.
+ if ( ! is_numeric( $value ) )
+ return false;
+ $value = absint( $value );
+ } else {
+ $value = trim( $value );
+ }
+
+ if ( !$value )
+ return false;
+
+ switch ( $field ) {
+ case 'id':
+ $user_id = $value;
+ $db_field = 'ID';
+ break;
+ case 'slug':
+ $user_id = wp_cache_get($value, 'userslugs');
+ $db_field = 'user_nicename';
+ break;
+ case 'email':
+ $user_id = wp_cache_get($value, 'useremail');
+ $db_field = 'user_email';
+ break;
+ case 'login':
+ $value = sanitize_user( $value );
+ $user_id = wp_cache_get($value, 'userlogins');
+ $db_field = 'user_login';
+ break;
+ default:
+ return false;
+ }
+
+ if ( false !== $user_id ) {
+ if ( $user = wp_cache_get( $user_id, 'users' ) )
+ return $user;
+ }
+
+ if ( !$user = $wpdb->get_row( $wpdb->prepare(
+ "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
+ ) ) )
+ return false;
+
+ update_user_caches( $user );
+
+ return $user;
+ }
+
+ /**
+ * Magic method for checking the existence of a certain custom field
+ *
+ * @since 3.3.0
+ */
+ function __isset( $key ) {
+ if ( 'id' == $key ) {
+ _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
+ $key = 'ID';
+ }
+
+ if ( isset( $this->data->$key ) )
+ return true;
+
+ if ( isset( self::$back_compat_keys[ $key ] ) )
+ $key = self::$back_compat_keys[ $key ];
+
+ return metadata_exists( 'user', $this->ID, $key );
+ }
+
+ /**
+ * Magic method for accessing custom fields
+ *
+ * @since 3.3.0
+ */
+ function __get( $key ) {
+ if ( 'id' == $key ) {
+ _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
+ return $this->ID;
+ }
+
+ if ( isset( $this->data->$key ) ) {
+ $value = $this->data->$key;
+ } else {
+ if ( isset( self::$back_compat_keys[ $key ] ) )
+ $key = self::$back_compat_keys[ $key ];
+ $value = get_user_meta( $this->ID, $key, true );
+ }
+
+ if ( $this->filter ) {
+ $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );