From 58d931dfc71589945a605ab209dd0f86cca3b508 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 16 Jan 2015 02:38:30 +0000 Subject: [PATCH] Allow `comment_post_ID` to be passed to `wp_update_comment()`, so that a comment can be moved to a different post. Props tyxla, rachelbaker Fixes #30946 git-svn-id: https://develop.svn.wordpress.org/trunk@31195 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 8 +++++++- tests/phpunit/tests/comment.php | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 01c15a75f3..a4f104f41b 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2205,6 +2205,12 @@ function wp_update_comment($commentarr) { if ( empty( $comment ) ) { return 0; } + + // Make sure that the comment post ID is valid (if specified). + if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { + return 0; + } + // Escape data pulled from DB. $comment = wp_slash($comment); @@ -2239,7 +2245,7 @@ function wp_update_comment($commentarr) { $comment_ID = $data['comment_ID']; $comment_post_ID = $data['comment_post_ID']; - $keys = array( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' ); + $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' ); $data = wp_array_slice_assoc( $data, $keys ); $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c1f9b10b90..0bd3920b5a 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -6,6 +6,7 @@ class Tests_Comment extends WP_UnitTestCase { function test_wp_update_comment() { $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) ); + $post2 = $this->factory->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) ); $comments = $this->factory->comment->create_post_comments( $post->ID, 5 ); $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) ); $this->assertEquals( 1, $result ); @@ -13,6 +14,9 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertEquals( $comments[1], $comment->comment_parent ); $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) ); $this->assertEquals( 0, $result ); + $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_post_ID' => $post2->ID ) ); + $comment = get_comment( $comments[0] ); + $this->assertEquals( $post2->ID, $comment->comment_post_ID ); } /**