mirror of
git://develop.git.wordpress.org/
synced 2025-03-23 21:39:50 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@52223 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c99da727d9
commit
3442f776f3
@ -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.
|
||||
|
33
tests/phpunit/tests/comment/commentsOpen.php
Normal file
33
tests/phpunit/tests/comment/commentsOpen.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group comment
|
||||
* @covers ::comments_open
|
||||
*/
|
||||
class Tests_Comment_CommentsOpen extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_does_not_exist() {
|
||||
$this->assertFalse( comments_open( 99999 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_exist_status_open() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$this->assertTrue( comments_open( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_exist_status_closed() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$post->comment_status = 'closed';
|
||||
|
||||
$this->assertFalse( comments_open( $post ) );
|
||||
}
|
||||
}
|
33
tests/phpunit/tests/comment/pingsOpen.php
Normal file
33
tests/phpunit/tests/comment/pingsOpen.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group comment
|
||||
* @covers ::pings_open
|
||||
*/
|
||||
class Tests_Comment_PingsOpen extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_does_not_exist() {
|
||||
$this->assertFalse( pings_open( 99999 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_exist_status_open() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$this->assertTrue( pings_open( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 54159
|
||||
*/
|
||||
public function test_post_exist_status_closed() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$post->ping_status = 'closed';
|
||||
|
||||
$this->assertFalse( pings_open( $post ) );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user