| | 325 | * Update meta data by meta ID |
| | 326 | * |
| | 327 | * @since 3.3.0 |
| | 328 | * |
| | 329 | * @uses update_metadata() Calls update_metadata() to actually update the meta value after |
| | 330 | * the meta_key, object_id and prev_value are fetched. |
| | 331 | * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value |
| | 332 | * and object_id of the given meta_id. |
| | 333 | * |
| | 334 | * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
| | 335 | * @param int $meta_id ID for a specific meta row |
| | 336 | * @param string $meta_value Metadata value |
| | 337 | * @param string $meta_key Optional, you can provide a meta key to update it |
| | 338 | * @return bool True on successful update, false on failure. |
| | 339 | */ |
| | 340 | function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { |
| | 341 | global $wpdb; |
| | 342 | |
| | 343 | // Make sure everything is valid. |
| | 344 | if ( ! $meta_type ) |
| | 345 | return false; |
| | 346 | |
| | 347 | if ( ! $meta_id = absint( $meta_id ) ) |
| | 348 | return false; |
| | 349 | |
| | 350 | if ( ! $table = _get_meta_table( $meta_type ) ) |
| | 351 | return false; |
| | 352 | |
| | 353 | $column = esc_sql($meta_type . '_id'); |
| | 354 | $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
| | 355 | |
| | 356 | // Fetch the meta and go on if it's found. |
| | 357 | if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
| | 358 | $original_key = $meta->meta_key; |
| | 359 | $object_id = $meta->{$column}; |
| | 360 | |
| | 361 | // If a new meta_key (last parameter) was specified, change the meta key, |
| | 362 | // otherwise use the original key in the update statement. |
| | 363 | if ( false === $meta_key ) { |
| | 364 | $meta_key = $original_key; |
| | 365 | } elseif ( is_string( $meta_key ) ) { |
| | 366 | $meta_key = stripslashes( $meta_key ); |
| | 367 | } else { |
| | 368 | return false; |
| | 369 | } |
| | 370 | |
| | 371 | // Sanitize the meta |
| | 372 | $meta_value = stripslashes_deep( $meta_value ); |
| | 373 | $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
| | 374 | $meta_value = maybe_serialize( $meta_value ); |
| | 375 | |
| | 376 | // Format the data query arguments. |
| | 377 | $data = array( |
| | 378 | 'meta_key' => $meta_key, |
| | 379 | 'meta_value' => $meta_value |
| | 380 | ); |
| | 381 | |
| | 382 | // Format the where query arguments. |
| | 383 | $where = array(); |
| | 384 | $where[$id_column] = $meta_id; |
| | 385 | |
| | 386 | // Run the update query, all fields in $data are %s, $where is a %d. |
| | 387 | $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' ); |
| | 388 | |
| | 389 | // Clear the caches. |
| | 390 | wp_cache_delete($object_id, $meta_type . '_meta'); |
| | 391 | |
| | 392 | // Users cache stores usermeta that must be cleared. |
| | 393 | if ( 'user' == $meta_type ) |
| | 394 | clean_user_cache($object_id); |
| | 395 | |
| | 396 | return $result; |
| | 397 | } |
| | 398 | |
| | 399 | // And if the meta was not found. |
| | 400 | return false; |
| | 401 | } |
| | 402 | |
| | 403 | /** |