WordPress.org

Make WordPress Core

Ticket #7540: 7540.diff

File 7540.diff, 7.3 KB (added by Denis-de-Bernardy, 9 years ago)
  • wp-includes/version.php

     
    1515 *
    1616 * @global int $wp_db_version
    1717 */
    18 $wp_db_version = 10850;
     18$wp_db_version = 11029;
    1919
    20 ?>
     20?>
     21 No newline at end of file
  • wp-includes/user.php

     
    282282        return $users;
    283283}
    284284
     285
    285286//
    286287// User meta functions
    287288//
    288289
    289290/**
     291 * Add meta data field to a user.
     292 *
     293 *
     294 * @since 2.8
     295 * @uses $wpdb
     296 * @link http://codex.wordpress.org/Function_Reference/add_user_meta
     297 *
     298 * @param int $user_id User ID.
     299 * @param string $key Metadata name.
     300 * @param mixed $value Metadata value.
     301 * @param bool $single Optional, default is false. Whether the same key should not be added.
     302 * @return bool False for failure. True for success.
     303 */
     304function add_user_meta($user_id, $meta_key, $meta_value, $single = false) {
     305        global $wpdb;
     306
     307        $user_id = (int) $user_id;
     308       
     309        // expected_slashed ($meta_key)
     310        $meta_key = stripslashes($meta_key);
     311
     312        $user = new WP_User($user_id);
     313       
     314        if ( $single ) {
     315                if ( isset($user->{$meta_key}) )
     316                        return false;
     317        }
     318
     319        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     320        $single = (int) $single;
     321
     322        $wpdb->insert( $wpdb->usermeta, compact( 'user_id', 'meta_key', 'meta_value', 'single' ) );
     323
     324        if ( !$single && isset($user->meta_key) ) {
     325                // make sure an initially single-row meta gets treated as a multi-row meta from this point onward
     326                $wpdb->update( $wpdb->usermeta, compact('single'), compact('user_id', 'meta_key'));
     327        }
     328
     329        wp_cache_delete($user_id, 'users');
     330
     331        return true;
     332}
     333
     334/**
     335 * Remove matching metadata from a user.
     336 *
     337 * You can match based on the key, or key and value. Removing based on key and
     338 * value, will keep from removing duplicate metadata with the same key. It also
     339 * allows removing all metadata matching key, if needed.
     340 *
     341 * @since 2.8
     342 * @uses $wpdb
     343 * @link http://codex.wordpress.org/Function_Reference/delete_user_meta
     344 *
     345 * @param int $user_id User ID
     346 * @param string $meta_key Metadata name.
     347 * @param mixed $meta_value Optional. Metadata value.
     348 * @return bool False for failure. True for success.
     349 */
     350function delete_user_meta($user_id, $meta_key, $meta_value = '') {
     351        global $wpdb;
     352
     353        $user_id = (int) $user_id;
     354        // expected_slashed ($meta_key, $meta_value)
     355        $meta_key = stripslashes( $meta_key );
     356        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     357
     358        if ( empty( $meta_value ) ) {
     359                $user = new WP_User($user_id);
     360                if ( !isset($user->{$meta_key}) )
     361                        return false;
     362        } else {
     363                $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $user_id, $meta_key, $meta_value ) );
     364                if ( !$meta_id )
     365                        return false;
     366        }
     367
     368        if ( empty( $meta_value ) )
     369                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key ) );
     370        else
     371                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $user_id, $meta_key, $meta_value ) );
     372
     373        wp_cache_delete($user_id, 'users');
     374
     375        return true;
     376}
     377
     378/**
     379 * Retrieve user meta field for a user.
     380 *
     381 * @since 2.8
     382 * @uses $wpdb
     383 * @link http://codex.wordpress.org/Function_Reference/get_user_meta
     384 *
     385 * @param int $user_id User ID.
     386 * @param string $key The meta key to retrieve.
     387 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
     388 */
     389function get_user_meta($user_id, $key) {
     390        $user_id = (int) $user_id;
     391
     392        $user = new WP_User($user_id);
     393       
     394        return isset($user->{$key}) ? $user->{$key} : '';
     395}
     396
     397/**
     398 * Update user meta field based on User ID.
     399 *
     400 * Use the $prev_value parameter to differentiate between meta fields with the
     401 * same key and User ID.
     402 *
     403 * If the meta field for the user does not exist, it will be added.
     404 *
     405 * @since 2.8
     406 * @uses $wpdb
     407 * @link http://codex.wordpress.org/Function_Reference/update_user_meta
     408 *
     409 * @param int $user_id User ID.
     410 * @param string $key Metadata key.
     411 * @param mixed $value Metadata value.
     412 * @param mixed $prev_value Optional. Previous value to check before removing.
     413 * @return bool False on failure, true if success.
     414 */
     415function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
     416        global $wpdb;
     417
     418        // expected_slashed ($meta_key)
     419        $meta_key = stripslashes($meta_key);
     420
     421        if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE meta_key = %s AND user_id = %d", $meta_key, $user_id ) ) ) {
     422                return add_user_meta($user_id, $meta_key, $meta_value, empty($prev_value));
     423        }
     424
     425        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     426        $single = empty($prev_value);
     427
     428        $data  = compact( 'meta_value', 'single' );
     429        $where = compact( 'meta_key', 'user_id' );
     430
     431        if ( !empty( $prev_value ) ) {
     432                $prev_value = maybe_serialize($prev_value);
     433                $where['meta_value'] = $prev_value;
     434        }
     435
     436        $wpdb->update( $wpdb->usermeta, $data, $where );
     437        wp_cache_delete($user_id, 'users');
     438        return true;
     439}
     440
     441/**
     442 * Delete everything from user meta matching meta key.
     443 *
     444 * @since 2.8
     445 * @uses $wpdb
     446 *
     447 * @param string $user_meta_key Key to search for when deleting.
     448* @return bool Whether the user meta key was deleted from the database
     449 */
     450function delete_user_meta_by_key($user_meta_key) {
     451        global $wpdb;
     452
     453        $user_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $user_meta_key));
     454
     455        if ( $user_ids ) {
     456                $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $user_meta_key));
     457                foreach ( $user_ids as $user_id )
     458                        wp_cache_delete($user_id, 'users');
     459
     460                return true;
     461        }
     462       
     463        return false;
     464}
     465
     466/**
    290467 * Remove user meta data.
    291468 *
    292469 * @since 2.0.0
     
    572749        global $wpdb;
    573750
    574751        $show = $wpdb->hide_errors();
    575         $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user->ID));
     752        $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, single FROM $wpdb->usermeta WHERE user_id = %d", $user->ID));
    576753        $wpdb->show_errors($show);
    577754
    578755        if ( $metavalues ) {
    579756                foreach ( (array) $metavalues as $meta ) {
    580757                        $value = maybe_unserialize($meta->meta_value);
    581                         $user->{$meta->meta_key} = $value;
     758                        if ( $meta->single ) {
     759                                $user->{$meta->meta_key} = $value;
     760                        } else {
     761                                $user->{$meta->meta_key}[] = $value;
     762                        }
     763                       
    582764                }
    583765        }
    584766
     
    600782        wp_cache_add($user->user_nicename, $user->ID, 'userslugs');
    601783}
    602784
    603 ?>
     785?>
     786 No newline at end of file
  • wp-admin/includes/schema.php

     
    158158CREATE TABLE $wpdb->usermeta (
    159159  umeta_id bigint(20) unsigned NOT NULL auto_increment,
    160160  user_id bigint(20) unsigned NOT NULL default '0',
     161  single tinyint(1) unsigned NOT NULL default '1',
    161162  meta_key varchar(255) default NULL,
    162163  meta_value longtext,
    163164  PRIMARY KEY  (umeta_id),
     
    576577        }
    577578}
    578579
    579 ?>
     580?>
     581 No newline at end of file