Make WordPress Core

Changeset 18494


Ignore:
Timestamp:
08/01/2011 05:01:54 PM (14 years ago)
Author:
ryan
Message:

update_metadata_by_mid() and delete_metadata_by_mid(). Props kovshenin. see #18195

File:
1 edited

Legend:

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

    r18445 r18494  
    320320
    321321    return $meta;
     322}
     323
     324/**
     325 * Update meta data by meta ID
     326 *
     327 * @since 3.3.0
     328 *
     329 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value
     330 *      and object_id of the given meta_id.
     331 *
     332 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
     333 * @param int $meta_id ID for a specific meta row
     334 * @param string $meta_value Metadata value
     335 * @param string $meta_key Optional, you can provide a meta key to update it
     336 * @return bool True on successful update, false on failure.
     337 */
     338function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
     339    global $wpdb;
     340
     341    // Make sure everything is valid.
     342    if ( ! $meta_type )
     343        return false;
     344
     345    if ( ! $meta_id = absint( $meta_id ) )
     346        return false;
     347
     348    if ( ! $table = _get_meta_table( $meta_type ) )
     349        return false;
     350
     351    $column = esc_sql($meta_type . '_id');
     352    $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     353
     354    // Fetch the meta and go on if it's found.
     355    if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
     356        $original_key = $meta->meta_key;
     357        $original_value = $meta->meta_value;
     358        $object_id = $meta->{$column};
     359
     360        // If a new meta_key (last parameter) was specified, change the meta key,
     361        // otherwise use the original key in the update statement.
     362        if ( false === $meta_key ) {
     363            $meta_key = $original_key;
     364        } elseif ( ! is_string( $meta_key ) ) {
     365            return false;
     366        }
     367
     368        // Sanitize the meta
     369        $_meta_value = $meta_value;
     370        $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
     371        $meta_value = maybe_serialize( $meta_value );
     372
     373        // Format the data query arguments.
     374        $data = array(
     375            'meta_key' => $meta_key,
     376            'meta_value' => $meta_value
     377        );
     378
     379        // Format the where query arguments.
     380        $where = array();
     381        $where[$id_column] = $meta_id;
     382
     383        do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     384       
     385        // Run the update query, all fields in $data are %s, $where is a %d.
     386        $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
     387
     388        // Clear the caches.
     389        wp_cache_delete($object_id, $meta_type . '_meta');
     390       
     391        // Users cache stores usermeta that must be cleared.
     392        if ( 'user' == $meta_type )
     393            clean_user_cache($object_id);
     394
     395        do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     396
     397        return $result;
     398    }
     399   
     400    // And if the meta was not found.
     401    return false;
     402}
     403
     404/**
     405 * Delete meta data by meta ID
     406 *
     407 * @since 3.3.0
     408 *
     409 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value
     410 *      and object_id of the given meta_id.
     411 *
     412 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
     413 * @param int $meta_id ID for a specific meta row
     414 * @return bool True on successful delete, false on failure.
     415 */
     416function delete_metadata_by_mid( $meta_type, $meta_id ) {
     417    global $wpdb;
     418   
     419    // Make sure everything is valid.
     420    if ( ! $meta_type )
     421        return false;
     422
     423    if ( ! $meta_id = absint( $meta_id ) )
     424        return false;
     425
     426    if ( ! $table = _get_meta_table( $meta_type ) )
     427        return false;
     428   
     429    // object and id columns
     430    $column = esc_sql($meta_type . '_id');
     431    $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     432
     433    // Fetch the meta and go on if it's found.
     434    if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
     435        $object_id = $meta->{$column};
     436
     437        do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
     438
     439        // Run the query, will return true if deleted, false otherwise
     440        $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) );
     441
     442        // Clear the caches.
     443        wp_cache_delete($object_id, $meta_type . '_meta');
     444       
     445        // Users cache stores usermeta that must be cleared.
     446        if ( 'user' == $meta_type )
     447            clean_user_cache($object_id);
     448
     449        do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
     450
     451        return $result;
     452
     453    }
     454   
     455    // Meta id was not found.
     456    return false;
    322457}
    323458
Note: See TracChangeset for help on using the changeset viewer.