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.
Built from https://develop.svn.wordpress.org/trunk@54488


git-svn-id: http://core.svn.wordpress.org/trunk@54047 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-10-11 15:45:11 +00:00
parent dd2cc423ef
commit 2fc21e6b79
2 changed files with 7 additions and 5 deletions

View File

@ -2324,13 +2324,14 @@ function wp_list_comments( $args = array(), $comments = null ) {
function comment_form( $args = array(), $post = null ) {
$post = get_post( $post );
$post_id = $post ? $post->ID : get_the_ID();
// Exit the function when comments for the post are closed.
if ( ! comments_open( $post_id ) ) {
// Exit the function if the post is invalid or comments are closed.
if ( ! $post || ! comments_open( $post ) ) {
/**
* 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
*/
do_action( 'comment_form_comments_closed' );
@ -2338,6 +2339,7 @@ function comment_form( $args = array(), $post = null ) {
return;
}
$post_id = $post->ID;
$commenter = wp_get_current_commenter();
$user = wp_get_current_user();
$user_identity = $user->exists() ? $user->display_name : '';

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-beta3-54487';
$wp_version = '6.1-beta3-54488';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.