Make WordPress Core

Changeset 58809


Ignore:
Timestamp:
07/25/2024 10:36:13 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r58755 r58809  
    3030                $comment_id = (string) $comment_id;
    3131        } else {
    32                 $comment_id = '';
     32                $comment_id = '0';
    3333        }
    3434
     
    234234        $comment = get_comment( $comment_id );
    235235
    236         $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;
     236        if ( ! empty( $comment->comment_ID ) ) {
     237                $comment_id = $comment->comment_ID;
     238        } elseif ( is_scalar( $comment_id ) ) {
     239                $comment_id = (string) $comment_id;
     240        } else {
     241                $comment_id = '0';
     242        }
    237243
    238244        $comment_author_url = get_comment_author_url( $comment );
Note: See TracChangeset for help on using the changeset viewer.