From 1061a15eafaa7714e8a8612f1230b88bfacd3f1b Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 23 May 2014 17:43:03 +0000 Subject: [PATCH] Allow `get_comments_number()` to officially accept `$post` or `$post_id`. Adds unit tests. Props coffee2code, JanHenkG. Fixes #26240. git-svn-id: https://develop.svn.wordpress.org/trunk@28558 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 11 +++----- tests/phpunit/tests/comment/template.php | 33 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/tests/comment/template.php diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 18f3fdff4a..0c7f40981a 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -682,16 +682,13 @@ function comments_link( $deprecated = '', $deprecated_2 = '' ) { * @return int The number of comments a post has. */ function get_comments_number( $post_id = 0 ) { - $post_id = absint( $post_id ); + $post = get_post( $post_id ); - if ( !$post_id ) - $post_id = get_the_ID(); - - $post = get_post($post_id); - if ( ! isset($post->comment_count) ) + if ( ! isset( $post->comment_count ) ) { $count = 0; - else + } else { $count = $post->comment_count; + } /** * Filter the returned comment count for a post. diff --git a/tests/phpunit/tests/comment/template.php b/tests/phpunit/tests/comment/template.php new file mode 100644 index 0000000000..1ffdc0cbfe --- /dev/null +++ b/tests/phpunit/tests/comment/template.php @@ -0,0 +1,33 @@ +factory->post->create(); + + $this->assertEquals( 0, get_comments_number( 0 ) ); + $this->assertEquals( 0, get_comments_number( $post_id ) ); + $this->assertEquals( 0, get_comments_number( get_post( $post_id ) ) ); + + $this->factory->comment->create_post_comments( $post_id, 12 ); + + $this->assertEquals( 12, get_comments_number( $post_id ) ); + $this->assertEquals( 12, get_comments_number( get_post( $post_id ) ) ); + } + + function test_get_comments_number_without_arg() { + $post_id = $this->factory->post->create(); + $permalink = get_permalink( $post_id ); + $this->go_to( $permalink ); + + $this->assertEquals( 0, get_comments_number() ); + + $this->factory->comment->create_post_comments( $post_id, 12 ); + $this->go_to( $permalink ); + + $this->assertEquals( 12, get_comments_number() ); + } + +} \ No newline at end of file