Make WordPress Core


Ignore:
Timestamp:
07/18/2024 03:08:01 PM (3 months ago)
Author:
hellofromTonya
Message:

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].

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-template.php

    r58335 r58755  
    2525    $comment = get_comment( $comment_id );
    2626
    27     $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;
     27    if ( ! empty( $comment->comment_ID ) ) {
     28        $comment_id = $comment->comment_ID;
     29    } elseif ( is_scalar( $comment_id ) ) {
     30        $comment_id = (string) $comment_id;
     31    } else {
     32        $comment_id = '';
     33    }
    2834
    2935    if ( empty( $comment->comment_author ) ) {
Note: See TracChangeset for help on using the changeset viewer.