From 30c9e4338ddc78075544f5c550d2babf5fb2d601 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Fri, 19 Nov 2021 20:24:00 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@52223 git-svn-id: http://core.svn.wordpress.org/trunk@51815 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment-template.php | 4 ++-- wp-includes/version.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 200b2e095f..5a1b2e22eb 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -1241,7 +1241,7 @@ function comments_open( $post_id = null ) { $_post = get_post( $post_id ); $post_id = $_post ? $_post->ID : 0; - $open = ( 'open' === $_post->comment_status ); + $open = ( $_post && ( 'open' === $_post->comment_status ) ); /** * Filters whether the current post is open for comments. @@ -1271,7 +1271,7 @@ function pings_open( $post_id = null ) { $_post = get_post( $post_id ); $post_id = $_post ? $_post->ID : 0; - $open = ( 'open' === $_post->ping_status ); + $open = ( $_post && ( 'open' === $_post->ping_status ) ); /** * Filters whether the current post is open for pings. diff --git a/wp-includes/version.php b/wp-includes/version.php index b03b9a995f..a47de199bf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52222'; +$wp_version = '5.9-alpha-52223'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.