Make WordPress Core


Ignore:
Timestamp:
08/24/2011 07:32:59 PM (13 years ago)
Author:
ryan
Message:

Introduce metadata_exists(), WP_User::get_data_by(), WP_User::get(), WP_User::has_prop(). Don't fill user objects with meta. Eliminate data duplication in cache and memory. Props scribu. see #15458

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/meta.php

    r18500 r18597  
    7474
    7575    wp_cache_delete($object_id, $meta_type . '_meta');
    76     // users cache stores usermeta that must be cleared.
    77     if ( 'user' == $meta_type )
    78         clean_user_cache($object_id);
    7976
    8077    do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
     
    150147
    151148    do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
    152    
     149
    153150    if ( 'post' == $meta_type )
    154         do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );   
     151        do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    155152
    156153    $wpdb->update( $table, $data, $where );
    157154
    158155    wp_cache_delete($object_id, $meta_type . '_meta');
    159     // users cache stores usermeta that must be cleared.
    160     if ( 'user' == $meta_type )
    161         clean_user_cache($object_id);
    162156
    163157    do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
    164    
     158
    165159    if ( 'post' == $meta_type )
    166         do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );     
     160        do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    167161
    168162    return true;
     
    211205    $_meta_value = $meta_value;
    212206    $meta_value = maybe_serialize( $meta_value );
    213        
     207
    214208    $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
    215209
     
    228222
    229223    do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    230    
     224
    231225    if ( 'post' == $meta_type )
    232226        do_action( 'delete_postmeta', $meta_ids );
     
    247241    }
    248242
    249     // users cache stores usermeta that must be cleared.
    250     if ( 'user' == $meta_type )
    251         clean_user_cache($object_id);
    252 
    253243    do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    254    
     244
    255245    if ( 'post' == $meta_type )
    256246        do_action( 'deleted_postmeta', $meta_ids );
     
    311301
    312302/**
     303 * Determine if a meta key is set for a given object
     304 *
     305 * @since 3.3.0
     306 *
     307 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
     308 * @param int $object_id ID of the object metadata is for
     309 * @param string $meta_key Metadata key.
     310 * @return boolean true of the key is set, false if not.
     311 */
     312function metadata_exists( $meta_type, $object_id, $meta_key ) {
     313    if ( ! $meta_type )
     314        return false;
     315
     316    if ( ! $object_id = absint( $object_id ) )
     317        return false;
     318
     319    $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
     320    if ( null !== $check )
     321        return true;
     322
     323    $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
     324
     325    if ( !$meta_cache ) {
     326        $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
     327        $meta_cache = $meta_cache[$object_id];
     328    }
     329
     330    if ( isset( $meta_cache[ $meta_key ] ) )
     331        return true;
     332
     333    return false;
     334}
     335
     336/**
    313337 * Get meta data by meta ID
    314338 *
     
    407431        if ( 'post' == $meta_type )
    408432            do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    409        
     433
    410434        // Run the update query, all fields in $data are %s, $where is a %d.
    411435        $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
     
    413437        // Clear the caches.
    414438        wp_cache_delete($object_id, $meta_type . '_meta');
    415        
    416         // Users cache stores usermeta that must be cleared.
    417         if ( 'user' == $meta_type )
    418             clean_user_cache($object_id);
    419439
    420440        do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     
    425445        return $result;
    426446    }
    427    
     447
    428448    // And if the meta was not found.
    429449    return false;
     
    444464function delete_metadata_by_mid( $meta_type, $meta_id ) {
    445465    global $wpdb;
    446    
     466
    447467    // Make sure everything is valid.
    448468    if ( ! $meta_type )
     
    454474    if ( ! $table = _get_meta_table( $meta_type ) )
    455475        return false;
    456    
     476
    457477    // object and id columns
    458478    $column = esc_sql($meta_type . '_id');
     
    474494        wp_cache_delete($object_id, $meta_type . '_meta');
    475495
    476         // Users cache stores usermeta that must be cleared.
    477         if ( 'user' == $meta_type )
    478             clean_user_cache($object_id);
    479 
    480496        do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
    481497
     
    486502
    487503    }
    488    
     504
    489505    // Meta id was not found.
    490506    return false;
     
    819835/**
    820836 * Register meta key
    821  * 
     837 *
    822838 * @since 3.3.0
    823839 *
Note: See TracChangeset for help on using the changeset viewer.