WordPress.org

Make WordPress Core

Changeset 30140


Ignore:
Timestamp:
11/01/2014 02:38:19 AM (7 years ago)
Author:
boonebgorges
Message:

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

Props wonderboymusic.
Fixes #11683.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r30122 r30140  
    197197    }
    198198
    199     if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
     199    $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
     200    if ( empty( $meta_ids ) ) {
    200201        return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
     202    }
    201203
    202204    $_meta_value = $meta_value;
     
    224226     * @param mixed  $meta_value Meta value.
    225227     */
    226     do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
    227 
    228     if ( 'post' == $meta_type )
     228    foreach ( $meta_ids as $meta_id ) {
     229        do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     230    }
     231
     232    if ( 'post' == $meta_type ) {
    229233        /**
    230234         * Fires immediately before updating a post's metadata.
     
    237241         * @param mixed  $meta_value Meta value.
    238242         */
    239         do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
     243        foreach ( $meta_ids as $meta_id ) {
     244            do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
     245        }
     246    }
    240247
    241248    $result = $wpdb->update( $table, $data, $where );
     
    258265     * @param mixed  $meta_value Meta value.
    259266     */
    260     do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     267    foreach ( $meta_ids as $meta_id ) {
     268        do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     269    }
    261270
    262271    if ( 'post' == $meta_type ) {
     
    271280         * @param mixed  $meta_value Meta value.
    272281         */
    273         do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
     282        foreach ( $meta_ids as $meta_id ) {
     283            do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
     284        }
    274285    }
    275286
  • trunk/tests/phpunit/tests/meta.php

    r30115 r30140  
    55 */
    66class Tests_Meta extends WP_UnitTestCase {
     7    protected $updated_mids = array();
     8
    79    function setUp() {
    810        parent::setUp();
     
    7577        $second = get_user_meta( $meta->user_id, $meta->meta_key );
    7678        $this->assertFalse( $first === $second );
     79    }
     80
     81    /**
     82     * @ticket 11683
     83     */
     84    public function test_update_metadata_hooks_for_multiple_updated_rows() {
     85        add_metadata( 'post', 1, 'test_key', 'value_1' );
     86        add_metadata( 'post', 1, 'test_key', 'value_2' );
     87        add_action( 'update_post_meta', array( $this, 'updated_meta' ) );
     88        add_action( 'update_postmeta', array( $this, 'updated_meta' ) );
     89        add_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
     90        add_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
     91
     92        update_metadata( 'post', 1, 'test_key', 'value_3' );
     93
     94        remove_action( 'update_post_meta', array( $this, 'updated_meta' ) );
     95        remove_action( 'update_postmeta', array( $this, 'updated_meta' ) );
     96        remove_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
     97        remove_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
     98
     99        $found = $this->updated_mids;
     100        $this->updated_mids = array();
     101
     102        foreach ( $found as $action => $mids ) {
     103            $this->assertSame( 2, count( $mids ) );
     104        }
    77105    }
    78106
     
    301329        $this->assertSame( array( $data ), $found['foo'] );
    302330    }
     331
     332    /** Helpers **********************************************************/
     333
     334    public function updated_meta( $meta_id ) {
     335        $this->updated_mids[ current_action() ][] = $meta_id;
     336    }
    303337}
Note: See TracChangeset for help on using the changeset viewer.