Comments: Only type cast a scalar `$comment_id` in `get_comment_author_link()`.

This aims to resolve a fatal error when the incoming `$comment_id` is an instance of `WP_Comment` (or any object) without a `comment_ID` property defined, or if it's empty:
{{{
Object of class WP_Comment could not be converted to string
}}}

This commit mirrors the changes previously made for a similar code fragment in `get_comment_author()`.

Includes:
* Unit tests to demonstrate the fatal error and validate the fix.
* Changing the default value for a non-existent comment ID in `get_comment_author()` from an empty string to zero as a numeric string, for consistency with `get_comment_ID()`.

Follow-up to [52818], [55289], [58335], [58755].

Props narenin, mukesh27, iflairwebtechnologies, umeshsinghin, SergeyBiryukov.
Fixes #61715.
Built from https://develop.svn.wordpress.org/trunk@58809


git-svn-id: http://core.svn.wordpress.org/trunk@58205 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2024-07-25 22:38:16 +00:00
parent cd610922d0
commit bb26471543
2 changed files with 9 additions and 3 deletions

View File

@ -29,7 +29,7 @@ function get_comment_author( $comment_id = 0 ) {
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
} else {
$comment_id = '';
$comment_id = '0';
}
if ( empty( $comment->comment_author ) ) {
@ -233,7 +233,13 @@ function get_comment_author_email_link( $link_text = '', $before = '', $after =
function get_comment_author_link( $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 = '0';
}
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-alpha-58807';
$wp_version = '6.7-alpha-58809';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.