| | 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 | if ( ! $meta_type ) |
| | 344 | return false; |
| | 345 | |
| | 346 | if ( ! $meta_id = absint( $meta_id ) ) |
| | 347 | return false; |
| | 348 | |
| | 349 | if ( ! $table = _get_meta_table( $meta_type ) ) |
| | 350 | return false; |
| | 351 | |
| | 352 | $column = esc_sql($meta_type . '_id'); |
| | 353 | $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
| | 354 | |
| | 355 | // Fetch the meta and go on if it's found. |
| | 356 | if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
| | 357 | $original_key = $meta->meta_key; |
| | 358 | |
| | 359 | // If a new meta_key (last parameter) was specified, change the meta key, |
| | 360 | // otherwise use the original key in the update statement. |
| | 361 | if ( false === $meta_key ) { |
| | 362 | $meta_key = $original_key; |
| | 363 | } elseif ( is_string( $meta_key ) ) { |
| | 364 | $meta_key = stripslashes( $meta_key ); |
| | 365 | } else { |
| | 366 | return false; |
| | 367 | } |
| | 368 | |
| | 369 | // Sanitize the meta |
| | 370 | $meta_value = stripslashes_deep( $meta_value ); |
| | 371 | $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
| | 372 | $meta_value = maybe_serialize( $meta_value ); |
| | 373 | |
| | 374 | // Run the query. |
| | 375 | return (bool) $wpdb->query( $wpdb->prepare( "UPDATE $table SET meta_key = %s, meta_value = %s WHERE $id_column = %d LIMIT 1;", $meta_key, $meta_value, $meta_id ) ); |
| | 376 | } |
| | 377 | |
| | 378 | // And if the meta was not found. |
| | 379 | return false; |
| | 380 | } |
| | 381 | |
| | 382 | /** |