diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 200b2e095f..5a1b2e22eb 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1241,7 +1241,7 @@ function comments_open( $post_id = null ) { $_post = get_post( $post_id ); $post_id = $_post ? $_post->ID : 0; - $open = ( 'open' === $_post->comment_status ); + $open = ( $_post && ( 'open' === $_post->comment_status ) ); /** * Filters whether the current post is open for comments. @@ -1271,7 +1271,7 @@ function pings_open( $post_id = null ) { $_post = get_post( $post_id ); $post_id = $_post ? $_post->ID : 0; - $open = ( 'open' === $_post->ping_status ); + $open = ( $_post && ( 'open' === $_post->ping_status ) ); /** * Filters whether the current post is open for pings. diff --git a/tests/phpunit/tests/comment/commentsOpen.php b/tests/phpunit/tests/comment/commentsOpen.php new file mode 100644 index 0000000000..54e08ea8ed --- /dev/null +++ b/tests/phpunit/tests/comment/commentsOpen.php @@ -0,0 +1,33 @@ +assertFalse( comments_open( 99999 ) ); + } + + /** + * @ticket 54159 + */ + public function test_post_exist_status_open() { + $post = $this->factory->post->create_and_get(); + $this->assertTrue( comments_open( $post ) ); + } + + /** + * @ticket 54159 + */ + public function test_post_exist_status_closed() { + $post = $this->factory->post->create_and_get(); + $post->comment_status = 'closed'; + + $this->assertFalse( comments_open( $post ) ); + } +} diff --git a/tests/phpunit/tests/comment/pingsOpen.php b/tests/phpunit/tests/comment/pingsOpen.php new file mode 100644 index 0000000000..2ef7029804 --- /dev/null +++ b/tests/phpunit/tests/comment/pingsOpen.php @@ -0,0 +1,33 @@ +assertFalse( pings_open( 99999 ) ); + } + + /** + * @ticket 54159 + */ + public function test_post_exist_status_open() { + $post = $this->factory->post->create_and_get(); + $this->assertTrue( pings_open( $post ) ); + } + + /** + * @ticket 54159 + */ + public function test_post_exist_status_closed() { + $post = $this->factory->post->create_and_get(); + $post->ping_status = 'closed'; + + $this->assertFalse( pings_open( $post ) ); + } +}