+ /**
+ * Return only the main user fields
+ *
+ * @since 3.3.0
+ *
+ * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
+ * @param string|int $value The field value
+ * @return object|false Raw user object
+ */
+ public static function get_data_by( $field, $value ) {
+ global $wpdb;
+
+ 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 = intval( $value );
+ if ( $value < 1 )
+ return false;
+ } 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
+ * @param string $key
+ * @return bool
+ */
+ public 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
+ * @param string $key
+ * @return mixed
+ */
+ public 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 );
+ }
+
+ return $value;
+ }
+
+ /**
+ * Magic method for setting custom fields
+ *
+ * @since 3.3.0
+ */
+ public function __set( $key, $value ) {
+ if ( 'id' == $key ) {
+ _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
+ $this->ID = $value;
+ return;
+ }
+
+ $this->data->$key = $value;
+ }
+
+ /**
+ * Determine whether the user exists in the database.
+ *
+ * @since 3.4.0
+ * @access public
+ *
+ * @return bool True if user exists in the database, false if not.
+ */
+ public function exists() {
+ return ! empty( $this->ID );
+ }
+
+ /**
+ * Retrieve the value of a property or meta key.
+ *
+ * Retrieves from the users and usermeta table.
+ *
+ * @since 3.3.0
+ *
+ * @param string $key Property
+ */
+ public function get( $key ) {
+ return $this->__get( $key );
+ }
+
+ /**
+ * Determine whether a property or meta key is set
+ *
+ * Consults the users and usermeta tables.
+ *
+ * @since 3.3.0
+ *
+ * @param string $key Property
+ */
+ public function has_prop( $key ) {
+ return $this->__isset( $key );
+ }
+
+ /**
+ * Return an array representation.
+ *
+ * @since 3.5.0
+ *
+ * @return array Array representation.
+ */
+ public function to_array() {
+ return get_object_vars( $this->data );
+ }
+