From 89c4da675f45da76d4e78cad4b04aa4cb323914a Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Thu, 18 Jul 2024 17:58:15 +0000 Subject: [PATCH] Comments: Fix fatal error when get_comment_author() receives an object 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]. Reviewed by SergeyBiryukov, jorbin. Merges [58755,58756] to the 6.6 branch. Props ambrosiawt, hellofromTonya, jorbin, mukesh27, SergeyBiryukov. Fixes #61681. Built from https://develop.svn.wordpress.org/branches/6.6@58762 git-svn-id: http://core.svn.wordpress.org/branches/6.6@58164 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment-template.php | 8 +++++++- wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 8c56097333..fed6568af3 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -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; diff --git a/wp-includes/version.php b/wp-includes/version.php index 215963486b..bf7657f5ba 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6.1-alpha-58761'; +$wp_version = '6.6.1-alpha-58762'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.