Make WordPress Core

Changeset 40981


Ignore:
Timestamp:
07/01/2017 01:35:38 PM (8 years ago)
Author:
boonebgorges
Message:

Allow metadata to be updated via wp_update_comment().

Passing an array of comment_meta into wp_update_comment() will
now update corresponding metadata. Similar functionality already
exists in wp_insert_comment().

Props dshanske, kraftbj.
Fixes #36784.

Location:
trunk
Files:
2 edited

Legend:

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

    r40667 r40981  
    21272127 *
    21282128 * @since 2.0.0
     2129 * @since 4.9.0 Add updating comment meta during comment update.
    21292130 *
    21302131 * @global wpdb $wpdb WordPress database abstraction object.
     
    21992200
    22002201    $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
     2202
     2203    // If metadata is provided, store it.
     2204    if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) {
     2205        foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) {
     2206            update_comment_meta( $comment_ID, $meta_key, $meta_value );
     2207        }
     2208    }
    22012209
    22022210    clean_comment_cache( $comment_ID );
  • trunk/tests/phpunit/tests/comment.php

    r38834 r40981  
    5454
    5555    /**
     56     * @ticket 36784
     57     */
     58    function test_wp_update_comment_updates_comment_meta() {
     59        $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     60        wp_update_comment( array(
     61            'comment_ID' => $comment_id,
     62            'comment_meta' => array(
     63                'food' => 'taco',
     64                'sauce' => 'fire',
     65            ),
     66        ) );
     67        $this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) );
     68    }
     69
     70    /**
    5671     * @ticket 30307
    5772     */
Note: See TracChangeset for help on using the changeset viewer.