Make WordPress Core

Changeset 52818


Ignore:
Timestamp:
03/03/2022 08:32:20 PM (3 years ago)
Author:
davidbaumwald
Message:

Comments: Guard against potential PHP notices in get_comment_author and get_comment_ID.

In both get_comment_author and get_comment_ID, it's possible that these functions are called without a comment context. Specifically, get_comment_author can be called without passing the $comment_ID parameter, and get_comment_ID relies on the comment global if there's no $comment parameter supplied. This leads to a PHP notice of "Trying to get property of a non-object."

This change adds a check to both functions to ensure the $comment->comment_ID property is not empty before actually using its value.

Follow-up to [52223].

Props dd32.
Fixes #54379.

File:
1 edited

Legend:

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

    r52223 r52818  
    2323 */
    2424function get_comment_author( $comment_ID = 0 ) {
    25     $comment = get_comment( $comment_ID );
     25    $comment    = get_comment( $comment_ID );
     26    $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_ID;
    2627
    2728    if ( empty( $comment->comment_author ) ) {
    28         $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
     29        $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
    2930        if ( $user ) {
    3031            $author = $user->display_name;
     
    4647     * @param WP_Comment $comment    The comment object.
    4748     */
    48     return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
     49    return apply_filters( 'get_comment_author', $author, $comment_ID, $comment );
    4950}
    5051
     
    664665 */
    665666function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
    666     $comment = get_comment();
     667    $comment    = get_comment();
     668    $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : '0';
    667669
    668670    /**
     
    675677     * @param WP_Comment $comment    The comment object.
    676678     */
    677     return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
     679    return apply_filters( 'get_comment_ID', $comment_ID, $comment );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
    678680}
    679681
Note: See TracChangeset for help on using the changeset viewer.