From b70967302b5cb9e6ee9231a124c3baa704468f0d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 11 May 2014 00:25:29 +0000 Subject: [PATCH] In `get_the_author_posts()`, if there is no current `$post`, return `0` and bail. Props krogsgard, aaroncampbell. Fixes #27998. git-svn-id: https://develop.svn.wordpress.org/trunk@28362 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/author-template.php | 6 +++++- tests/phpunit/tests/user/author.php | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index b7361a0244..80fd6166ba 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -206,7 +206,11 @@ function the_author_link() { * @return int The number of posts by the author. */ function get_the_author_posts() { - return count_user_posts( get_post()->post_author ); + $post = get_post(); + if ( ! $post ) { + return 0; + } + return count_user_posts( $post->post_author ); } /** diff --git a/tests/phpunit/tests/user/author.php b/tests/phpunit/tests/user/author.php index 67b9aba756..8646a9ba42 100644 --- a/tests/phpunit/tests/user/author.php +++ b/tests/phpunit/tests/user/author.php @@ -75,4 +75,11 @@ class Tests_User_Author extends WP_UnitTestCase { $this->assertEquals( '', get_the_author_meta( 'user_login' ) ); $this->assertEquals( '', get_the_author_meta( 'does_not_exist' ) ); } -} \ No newline at end of file + + function test_get_the_author_posts() { + // Test with no global post, result should be 0 because no author is found + $this->assertEquals( 0, get_the_author_posts() ); + $GLOBALS['post'] = $this->post_id; + $this->assertEquals( 1, get_the_author_posts() ); + } +}