Skip to content

Commit

Permalink
Comments: Fix fatal error when get_comment_author() receives an objec…
Browse files Browse the repository at this point in the history
…t with no comment_id.

[58335] introduced `(string)` type casting of the passed in `$comment_id` value. If `$comment_id` is a scalar, it works as expected. But if it's an `object`, the following fatal error is thrown:

{{{
Object of class WP_Comment could not be converted to string
}}}

This fatal error happens when the incoming `$comment_id` is an instance of `WP_Comment` (or any object) without a `comment_ID` (empty). 

This changeset adds tests to demonstrate the fatal error and validate the fix.

It fixes the fatal error by restructuring the ternary checks into an `if/elseif/else` structure for the 3 paths:

- When `$comment->comment_ID` is not empty, then it uses the property.
- When `$comment_id` is scalar, then it type casts it to a `string`.
- Else, the default is an empty `string`.

Follow-up to [58335], [41127], [52818].

Props ambrosiawt, hellofromTonya, jorbin, mukesh27, SergeyBiryukov.
Fixes #61681.

git-svn-id: https://develop.svn.wordpress.org/trunk@58755 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Jul 18, 2024
1 parent 8d2c73b commit 9aa777d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
function get_comment_author( $comment_id = 0 ) {
$comment = get_comment( $comment_id );

$comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;
if ( ! empty( $comment->comment_ID ) ) {
$comment_id = $comment->comment_ID;
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
} else {
$comment_id = '';
}

if ( empty( $comment->comment_author ) ) {
$user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
Expand Down
54 changes: 54 additions & 0 deletions tests/phpunit/tests/comment/getCommentAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,58 @@ public function test_comment_author_passes_correct_comment_id_for_non_existent_c

get_comment_author( self::$non_existent_comment_id ); // Non-existent comment ID.
}

/**
* @ticket 61681
*
* @dataProvider data_should_return_author_when_given_object_without_comment_id
*
* @param stdClass $comment_props Comment properties test data.
* @param string $expected The expected result.
* @param array $user_data Optional. User data for creating an author. Default empty array.
*/
public function test_should_return_author_when_given_object_without_comment_id( $comment_props, $expected, $user_data = array() ) {
if ( ! empty( $comment_props->user_id ) ) {
$user = self::factory()->user->create_and_get( $user_data );
$comment_props->user_id = $user->ID;
}
$comment = new WP_Comment( $comment_props );
$this->assertSame( $expected, get_comment_author( $comment ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_should_return_author_when_given_object_without_comment_id() {
return array(
'with no author' => array(
'comment_props' => new stdClass(),
'expected' => 'Anonymous',
),
'with author name' => array(
'comment_props' => (object) array(
'comment_author' => 'tester1',
),
'expected' => 'tester1',
),
'with author name, empty ID' => array(
'comment_props' => (object) array(
'comment_author' => 'tester2',
'comment_ID' => '',
),
'expected' => 'tester2',
),
'with author ID' => array(
'comment_props' => (object) array(
'user_id' => 1, // populates in the test with an actual user ID.
),
'expected' => 'Tester3',
'user_data' => array(
'display_name' => 'Tester3',
),
),
);
}
}

0 comments on commit 9aa777d

Please sign in to comment.