From d13adcc9bfd89170e16b04041a4e77219da047b4 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Thu, 10 Dec 2015 15:16:51 +0000 Subject: [PATCH] =?UTF-8?q?Comments:=20Comments=20don=E2=80=99t=20need=20n?= =?UTF-8?q?o=20Post=20ID=20when=20created,=20so=20they=20don=E2=80=99t=20b?= =?UTF-8?q?e=20needing=20one=20to=20be=20edited.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In `wp_update_comment()` only check if the given `comment_post_ID` is valid if it isn’t `0`. This allows comments that were created programmatically via `wp_insert_comment()` without the (optional) `comment_post_ID` parameter to be edited. Props subharanjan for the initial patch. Fixes #34954 git-svn-id: https://develop.svn.wordpress.org/trunk@35853 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 2 +- tests/phpunit/tests/comment.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 5de9531322..c62a920839 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -1904,7 +1904,7 @@ function wp_update_comment($commentarr) { } // Make sure that the comment post ID is valid (if specified). - if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { + if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { return 0; } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 4fd1df119e..844e4d12b2 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -70,6 +70,21 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertEquals( 1, $comment->user_id ); } + /** + * @ticket 34954 + */ + function test_wp_update_comment_with_no_post_id() { + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => 0 ) ); + + $updated_comment_text = 'I should be able to update a comment with a Post ID of zero'; + + $update = wp_update_comment( array( 'comment_ID' => $comment_id, 'comment_content' => $updated_comment_text, 'comment_post_ID' => 0 ) ); + $this->assertSame( 1, $update ); + + $comment = get_comment( $comment_id ); + $this->assertEquals( $updated_comment_text, $comment->comment_content ); + } + public function test_get_approved_comments() { $ca1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1'