WordPress.org

Make WordPress Core

Changeset 52223


Ignore:
Timestamp:
11/19/2021 08:22:43 PM (2 weeks ago)
Author:
hellofromTonya
Message:

Comments: Fix PHP Notice "trying to get property of non-object" in comments_open() and pings_open().

The post for the comments or pings is retrieved by get_post(). If the post exists, get_post() returns an instance of WP_Post; else, it returns null.

In both comments_open() and pings_open(), the returned value from get_post() is used without checking if the object returned, if the post exists. When the post does not exist, the following notices occur:

PHP Notice:  Trying to get property 'comment_status' of non-object in .../src/wp-includes/comment-template.php on line 1244

and

PHP Notice:  Trying to get property 'pings_open' of non-object in ../src/wp-includes/comment-template.php on line 1274

This commit fixes these notices by checking if the post has a non-falsey value before using it as an object to set the $open state. As the return from get_post() will only be an object or null, the truthy check is appropriate and slightly more performant.

Tests added to validate the fix.

Follow-up to [1964], [40666].

Props dd32, audrasjb, costdev, hellofromTonya, sergeybiryukov.
Fixes #54159.

Location:
trunk
Files:
2 added
1 edited

Legend:

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

    r52205 r52223  
    12421242
    12431243    $post_id = $_post ? $_post->ID : 0;
    1244     $open    = ( 'open' === $_post->comment_status );
     1244    $open    = ( $_post && ( 'open' === $_post->comment_status ) );
    12451245
    12461246    /**
     
    12721272
    12731273    $post_id = $_post ? $_post->ID : 0;
    1274     $open    = ( 'open' === $_post->ping_status );
     1274    $open    = ( $_post && ( 'open' === $_post->ping_status ) );
    12751275
    12761276    /**
Note: See TracChangeset for help on using the changeset viewer.