From 8dbaaf9927c05eca8acae9d02e1e575219d69dc8 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 1 Nov 2014 02:38:19 +0000 Subject: [PATCH] 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 --- src/wp-includes/meta.php | 23 +++++++++++++++++------ tests/phpunit/tests/meta.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index d56d81bbc9..5541d12227 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -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; diff --git a/tests/phpunit/tests/meta.php b/tests/phpunit/tests/meta.php index 7ad5fb8e90..cd134cf2ee 100644 --- a/tests/phpunit/tests/meta.php +++ b/tests/phpunit/tests/meta.php @@ -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; + } }