Comments: Return early from comment_form() if an invalid post ID is passed.

If an invalid post ID is passed to the function, `comments_open()` should return `false`, and no comment form be displayed. This commit restores the previous behavior that was unintentionally changed when standardizing on the `$post` parameter name.

Follow-up to [53715].

Props peterwilsoncc.
Fixes #56243.

git-svn-id: https://develop.svn.wordpress.org/trunk@54488 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-10-11 15:43:04 +00:00
parent 20bccea8a7
commit 645f474fc0
2 changed files with 46 additions and 4 deletions

View File

@ -2324,13 +2324,14 @@ function wp_list_comments( $args = array(), $comments = null ) {
function comment_form( $args = array(), $post = null ) { function comment_form( $args = array(), $post = null ) {
$post = get_post( $post ); $post = get_post( $post );
$post_id = $post ? $post->ID : get_the_ID(); // Exit the function if the post is invalid or comments are closed.
if ( ! $post || ! comments_open( $post ) ) {
// Exit the function when comments for the post are closed.
if ( ! comments_open( $post_id ) ) {
/** /**
* Fires after the comment form if comments are closed. * Fires after the comment form if comments are closed.
* *
* For backward compatibility, this action also fires if comment_form()
* is called with an invalid post object or ID.
*
* @since 3.0.0 * @since 3.0.0
*/ */
do_action( 'comment_form_comments_closed' ); do_action( 'comment_form_comments_closed' );
@ -2338,6 +2339,7 @@ function comment_form( $args = array(), $post = null ) {
return; return;
} }
$post_id = $post->ID;
$commenter = wp_get_current_commenter(); $commenter = wp_get_current_commenter();
$user = wp_get_current_user(); $user = wp_get_current_user();
$user_identity = $user->exists() ? $user->display_name : ''; $user_identity = $user->exists() ? $user->display_name : '';

View File

@ -153,4 +153,44 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$expected = '<a rel="nofollow" id="cancel-comment-reply-link" href="#respond" style="display:none;">Cancel reply</a>'; $expected = '<a rel="nofollow" id="cancel-comment-reply-link" href="#respond" style="display:none;">Cancel reply</a>';
$this->assertStringNotContainsString( $expected, $form ); $this->assertStringNotContainsString( $expected, $form );
} }
/**
* @ticket 56243
*/
public function test_comment_form_should_not_display_for_global_post_when_called_with_invalid_id() {
// Go to permalink to ensure global post ID is set.
$this->go_to( get_permalink( self::$post_id ) );
$impossibly_high_post_id = PHP_INT_MAX;
$form = get_echo( 'comment_form', array( array(), $impossibly_high_post_id ) );
$this->assertEmpty( $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_global_post_with_falsey_post_id() {
$post_id = self::$post_id;
$this->go_to( get_permalink( $post_id ) );
$form = get_echo( 'comment_form', array( array(), false ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_specified_post_when_passed_a_valid_post_id() {
$post_id = self::$post_id;
$form = get_echo( 'comment_form', array( array(), $post_id ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
} }