Pass all updated meta IDs to filters in update_metadata().

Props wonderboymusic.
Fixes #11683.

git-svn-id: https://develop.svn.wordpress.org/trunk@30140 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-01 02:38:19 +00:00
parent 17e9c6f403
commit 8dbaaf9927
2 changed files with 51 additions and 6 deletions

View File

@ -196,8 +196,10 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
}
}
if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
if ( empty( $meta_ids ) ) {
return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
@ -223,9 +225,11 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
foreach ( $meta_ids as $meta_id ) {
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
}
if ( 'post' == $meta_type )
if ( 'post' == $meta_type ) {
/**
* Fires immediately before updating a post's metadata.
*
@ -236,7 +240,10 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
foreach ( $meta_ids as $meta_id ) {
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
$result = $wpdb->update( $table, $data, $where );
if ( ! $result )
@ -257,7 +264,9 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
foreach ( $meta_ids as $meta_id ) {
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
}
if ( 'post' == $meta_type ) {
/**
@ -270,7 +279,9 @@ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_v
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
foreach ( $meta_ids as $meta_id ) {
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
return true;

View File

@ -4,6 +4,8 @@
* @group meta
*/
class Tests_Meta extends WP_UnitTestCase {
protected $updated_mids = array();
function setUp() {
parent::setUp();
$this->author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
@ -76,6 +78,32 @@ class Tests_Meta extends WP_UnitTestCase {
$this->assertFalse( $first === $second );
}
/**
* @ticket 11683
*/
public function test_update_metadata_hooks_for_multiple_updated_rows() {
add_metadata( 'post', 1, 'test_key', 'value_1' );
add_metadata( 'post', 1, 'test_key', 'value_2' );
add_action( 'update_post_meta', array( $this, 'updated_meta' ) );
add_action( 'update_postmeta', array( $this, 'updated_meta' ) );
add_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
add_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
update_metadata( 'post', 1, 'test_key', 'value_3' );
remove_action( 'update_post_meta', array( $this, 'updated_meta' ) );
remove_action( 'update_postmeta', array( $this, 'updated_meta' ) );
remove_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
remove_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
$found = $this->updated_mids;
$this->updated_mids = array();
foreach ( $found as $action => $mids ) {
$this->assertSame( 2, count( $mids ) );
}
}
function test_metadata_exists() {
$this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) );
$this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) );
@ -300,4 +328,10 @@ class Tests_Meta extends WP_UnitTestCase {
$this->assertSame( array( $data ), $found['foo'] );
}
/** Helpers **********************************************************/
public function updated_meta( $meta_id ) {
$this->updated_mids[ current_action() ][] = $meta_id;
}
}