From 957a800bd31e818407449c260ca5118e0254e254 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 31 Oct 2015 20:12:16 +0000 Subject: [PATCH] Comments: don't auto-close comments on draft posts. Adds unit tests. Props solarissmoke, MikeHansenMe, nacin, rachelbaker. Fixes #20262. git-svn-id: https://develop.svn.wordpress.org/trunk@35475 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-functions.php | 5 +++++ tests/phpunit/tests/comment.php | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index e1975f62e6..ae40a8a7f9 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/wp-includes/comment-functions.php @@ -2592,6 +2592,11 @@ function _close_comments_for_old_post( $open, $post_id ) { if ( ! in_array( $post->post_type, $post_types ) ) return $open; + // Undated drafts should not show up as comments closed. + if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { + return $open; + } + if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) return false; diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 8faa0b7fcf..4fd1df119e 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -567,4 +567,26 @@ class Tests_Comment extends WP_UnitTestCase { return $email_sent_when_comment_approved || $email_sent_when_comment_added; } + + public function test_close_comments_for_old_post() { + update_option( 'close_comments_for_old_posts', true ); + // Close comments more than one day old. + update_option( 'close_comments_days_old', 1 ); + + $old_date = strtotime( '-25 hours' ); + $old_post_id = self::factory()->post->create( array( 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $old_date ) ) ); + + $old_post_comment_status = _close_comments_for_old_post( true, $old_post_id ); + $this->assertFalse( $old_post_comment_status ); + + $new_post_comment_status = _close_comments_for_old_post( true, self::$post_id ); + $this->assertTrue( $new_post_comment_status ); + } + + public function test_close_comments_for_old_post_undated_draft() { + $draft_id = self::factory()->post->create( array( 'post_status' => 'draft', 'post_type' => 'post' ) ); + $draft_comment_status = _close_comments_for_old_post( true, $draft_id ); + + $this->assertTrue( $draft_comment_status ); + } }