| 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 | */ |
| 338 | function 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 | $object_id = $meta->{$column}; |
| 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 | return false; |
| 365 | } |
| 366 | |
| 367 | // Sanitize the meta |
| 368 | $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
| 369 | $meta_value = maybe_serialize( $meta_value ); |
| 370 | |
| 371 | // Format the data query arguments. |
| 372 | $data = array( |
| 373 | 'meta_key' => $meta_key, |
| 374 | 'meta_value' => $meta_value |
| 375 | ); |
| 376 | |
| 377 | // Format the where query arguments. |
| 378 | $where = array(); |
| 379 | $where[$id_column] = $meta_id; |
| 380 | |
| 381 | // Run the update query, all fields in $data are %s, $where is a %d. |
| 382 | $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' ); |
| 383 | |
| 384 | // Clear the caches. |
| 385 | wp_cache_delete($object_id, $meta_type . '_meta'); |
| 386 | |
| 387 | // Users cache stores usermeta that must be cleared. |
| 388 | if ( 'user' == $meta_type ) |
| 389 | clean_user_cache($object_id); |
| 390 | |
| 391 | return $result; |
| 392 | } |
| 393 | |
| 394 | // And if the meta was not found. |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Delete meta data by meta ID |
| 400 | * |
| 401 | * @since 3.3.0 |
| 402 | * |
| 403 | * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value |
| 404 | * and object_id of the given meta_id. |
| 405 | * |
| 406 | * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
| 407 | * @param int $meta_id ID for a specific meta row |
| 408 | * @return bool True on successful delete, false on failure. |
| 409 | */ |
| 410 | function delete_metadata_by_mid( $meta_type, $meta_id ) { |
| 411 | global $wpdb; |
| 412 | |
| 413 | // Make sure everything is valid. |
| 414 | if ( ! $meta_type ) |
| 415 | return false; |
| 416 | |
| 417 | if ( ! $meta_id = absint( $meta_id ) ) |
| 418 | return false; |
| 419 | |
| 420 | if ( ! $table = _get_meta_table( $meta_type ) ) |
| 421 | return false; |
| 422 | |
| 423 | // object and id columns |
| 424 | $column = esc_sql($meta_type . '_id'); |
| 425 | $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
| 426 | |
| 427 | // Fetch the meta and go on if it's found. |
| 428 | if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
| 429 | $object_id = $meta->{$column}; |
| 430 | |
| 431 | // Run the query, will return true if deleted, false otherwise |
| 432 | $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) ); |
| 433 | |
| 434 | // Clear the caches. |
| 435 | wp_cache_delete($object_id, $meta_type . '_meta'); |
| 436 | |
| 437 | // Users cache stores usermeta that must be cleared. |
| 438 | if ( 'user' == $meta_type ) |
| 439 | clean_user_cache($object_id); |
| 440 | |
| 441 | return $result; |
| 442 | |
| 443 | } |
| 444 | |
| 445 | // Meta id was not found. |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | /** |