From 107c93739444a87ead9eac02003603d42de7899f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 1 Jul 2017 13:35:38 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@40981 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 8 ++++++++ tests/phpunit/tests/comment.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index f098910ea5..7055db763f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2126,6 +2126,7 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 + * @since 4.9.0 Add updating comment meta during comment update. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -2199,6 +2200,13 @@ function wp_update_comment($commentarr) { $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); + // If metadata is provided, store it. + if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) { + foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) { + update_comment_meta( $comment_ID, $meta_key, $meta_value ); + } + } + clean_comment_cache( $comment_ID ); wp_update_comment_count( $comment_post_ID ); /** diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index d41b781fb9..9ecc5cd983 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -52,6 +52,21 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertEquals( 'pingback', $comment->comment_type ); } + /** + * @ticket 36784 + */ + function test_wp_update_comment_updates_comment_meta() { + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); + wp_update_comment( array( + 'comment_ID' => $comment_id, + 'comment_meta' => array( + 'food' => 'taco', + 'sauce' => 'fire', + ), + ) ); + $this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) ); + } + /** * @ticket 30307 */