[wp-trac] [WordPress Trac] #54159: comments_open() & pings_open() trigger PHP Notice when called on a page without a $post

WordPress Trac noreply at wordpress.org
Fri Nov 19 20:22:57 UTC 2021


#54159: comments_open() & pings_open() trigger PHP Notice when called on a page
without a $post
---------------------------------------------+-----------------------------
 Reporter:  dd32                             |       Owner:  hellofromTonya
     Type:  defect (bug)                     |      Status:  closed
 Priority:  normal                           |   Milestone:  5.9
Component:  Comments                         |     Version:
 Severity:  normal                           |  Resolution:  fixed
 Keywords:  has-patch commit has-unit-tests  |     Focuses:
---------------------------------------------+-----------------------------
Changes (by hellofromTonya):

 * status:  accepted => closed
 * resolution:   => fixed


Comment:

 In [changeset:"52223" 52223]:
 {{{
 #!CommitTicketReference repository="" revision="52223"
 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.
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54159#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list