WordPress.org

Make WordPress Core

Changeset 34533


Ignore:
Timestamp:
09/25/2015 04:40:30 AM (4 years ago)
Author:
boonebgorges
Message:

Allow metadata to be attached to comment at time of creation.

The new $comment_meta parameter of wp_insert_comment() allows an array of
key/value pairs to be passed when creating a comment. These pairs are then
stored as commentmeta when the comment has been created.

Props tellyworth, wonderboymusic.
Fixes #12431.

Location:
trunk
Files:
2 edited

Legend:

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

    r34524 r34533  
    13781378 *
    13791379 * @since 2.0.0
     1380 * @since 4.4.0 Introduced `$comment_meta` argument.
    13801381 *
    13811382 * @global wpdb $wpdb WordPress database abstraction object.
     
    14021403 *                                            Default empty.
    14031404 *     @type string     $comment_type         Comment type. Default empty.
     1405 *     @type array      $comment_meta         Optional. Array of key/value pairs to be stored in commentmeta for the
     1406 *                                            new comment.
    14041407 *     @type int        $user_id              ID of the user who submitted the comment. Default 0.
    14051408 * }
     
    14391442    }
    14401443    $comment = get_comment( $id );
     1444
     1445    // If metadata is provided, store it.
     1446    if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
     1447        foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
     1448            add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
     1449        }
     1450    }
    14411451
    14421452    /**
  • trunk/tests/phpunit/tests/comment.php

    r34250 r34533  
    274274        $this->assertFalse( $sent );
    275275    }
     276
     277    /**
     278     * @ticket 12431
     279     */
     280    public function test_wp_new_comment_with_meta() {
     281        $c = $this->factory->comment->create( array(
     282            'comment_approved' => '1',
     283            'comment_meta' => array(
     284                'food' => 'taco',
     285                'sauce' => 'fire'
     286            )
     287        ) );
     288
     289        $this->assertEquals( 'fire', get_comment_meta( $c, 'sauce', true ) );
     290    }
    276291}
Note: See TracChangeset for help on using the changeset viewer.