From 919f08fd2d15bc831a9f40daf0a52bc00f93bf64 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 26 Oct 2016 14:48:29 +0000 Subject: [PATCH] Comments: In `comment_form()`, bail early if comments for the post are closed. Props jipmoors. Fixes #38514. Built from https://develop.svn.wordpress.org/trunk@38959 git-svn-id: http://core.svn.wordpress.org/trunk@38902 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment-template.php | 329 ++++++++++++++++--------------- wp-includes/version.php | 2 +- 2 files changed, 167 insertions(+), 164 deletions(-) diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index dc63e3653f..b6f27a28cb 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -2167,6 +2167,18 @@ function comment_form( $args = array(), $post_id = null ) { if ( null === $post_id ) $post_id = get_the_ID(); + // Exit the function when comments for the post are closed. + if ( ! comments_open( $post_id ) ) { + /** + * Fires after the comment form if comments are closed. + * + * @since 3.0.0 + */ + do_action( 'comment_form_comments_closed' ); + + return; + } + $commenter = wp_get_current_commenter(); $user = wp_get_current_user(); $user_identity = $user->exists() ? $user->display_name : ''; @@ -2252,209 +2264,200 @@ function comment_form( $args = array(), $post_id = null ) { // Ensure that the filtered args contain all required default values. $args = array_merge( $defaults, $args ); - if ( comments_open( $post_id ) ) : ?> + /** + * Fires before the comment form. + * + * @since 3.0.0 + */ + do_action( 'comment_form_before' ); + ?> +
-
- +
> + - > - $args['comment_field'] ) + (array) $args['fields']; + echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); /** - * Filters the comment form fields, including the textarea. + * Fires after the is_user_logged_in() check in the comment form. * - * @since 4.4.0 + * @since 3.0.0 * - * @param array $comment_fields The comment fields. + * @param array $commenter An array containing the comment author's + * username, email, and URL. + * @param string $user_identity If the commenter is a registered user, + * the display name, blank otherwise. */ - $comment_fields = apply_filters( 'comment_form_fields', $comment_fields ); + do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); - // Get an array of field names, excluding the textarea - $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) ); + else : - // Get the first and the last field name, excluding the textarea - $first_field = reset( $comment_field_keys ); - $last_field = end( $comment_field_keys ); + echo $args['comment_notes_before']; - foreach ( $comment_fields as $name => $field ) { + endif; - if ( 'comment' === $name ) { + // Prepare an array of all fields, including the textarea + $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields']; + /** + * Filters the comment form fields, including the textarea. + * + * @since 4.4.0 + * + * @param array $comment_fields The comment fields. + */ + $comment_fields = apply_filters( 'comment_form_fields', $comment_fields ); + + // Get an array of field names, excluding the textarea + $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) ); + + // Get the first and the last field name, excluding the textarea + $first_field = reset( $comment_field_keys ); + $last_field = end( $comment_field_keys ); + + foreach ( $comment_fields as $name => $field ) { + + if ( 'comment' === $name ) { + + /** + * Filters the content of the comment textarea field for display. + * + * @since 3.0.0 + * + * @param string $args_comment_field The content of the comment textarea field. + */ + echo apply_filters( 'comment_form_field_comment', $field ); + + echo $args['comment_notes_after']; + + } elseif ( ! is_user_logged_in() ) { + + if ( $first_field === $name ) { /** - * Filters the content of the comment textarea field for display. + * Fires before the comment fields in the comment form, excluding the textarea. * * @since 3.0.0 - * - * @param string $args_comment_field The content of the comment textarea field. */ - echo apply_filters( 'comment_form_field_comment', $field ); + do_action( 'comment_form_before_fields' ); + } - echo $args['comment_notes_after']; - - } elseif ( ! is_user_logged_in() ) { - - if ( $first_field === $name ) { - /** - * Fires before the comment fields in the comment form, excluding the textarea. - * - * @since 3.0.0 - */ - do_action( 'comment_form_before_fields' ); - } + /** + * Filters a comment form field for display. + * + * The dynamic portion of the filter hook, `$name`, refers to the name + * of the comment form field. Such as 'author', 'email', or 'url'. + * + * @since 3.0.0 + * + * @param string $field The HTML-formatted output of the comment form field. + */ + echo apply_filters( "comment_form_field_{$name}", $field ) . "\n"; + if ( $last_field === $name ) { /** - * Filters a comment form field for display. - * - * The dynamic portion of the filter hook, `$name`, refers to the name - * of the comment form field. Such as 'author', 'email', or 'url'. + * Fires after the comment fields in the comment form, excluding the textarea. * * @since 3.0.0 - * - * @param string $field The HTML-formatted output of the comment form field. */ - echo apply_filters( "comment_form_field_{$name}", $field ) . "\n"; - - if ( $last_field === $name ) { - /** - * Fires after the comment fields in the comment form, excluding the textarea. - * - * @since 3.0.0 - */ - do_action( 'comment_form_after_fields' ); - } + do_action( 'comment_form_after_fields' ); } } + } - $submit_button = sprintf( - $args['submit_button'], - esc_attr( $args['name_submit'] ), - esc_attr( $args['id_submit'] ), - esc_attr( $args['class_submit'] ), - esc_attr( $args['label_submit'] ) - ); + $submit_button = sprintf( + $args['submit_button'], + esc_attr( $args['name_submit'] ), + esc_attr( $args['id_submit'] ), + esc_attr( $args['class_submit'] ), + esc_attr( $args['label_submit'] ) + ); - /** - * Filters the submit button for the comment form to display. - * - * @since 4.2.0 - * - * @param string $submit_button HTML markup for the submit button. - * @param array $args Arguments passed to `comment_form()`. - */ - $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args ); + /** + * Filters the submit button for the comment form to display. + * + * @since 4.2.0 + * + * @param string $submit_button HTML markup for the submit button. + * @param array $args Arguments passed to `comment_form()`. + */ + $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args ); - $submit_field = sprintf( - $args['submit_field'], - $submit_button, - get_comment_id_fields( $post_id ) - ); + $submit_field = sprintf( + $args['submit_field'], + $submit_button, + get_comment_id_fields( $post_id ) + ); - /** - * Filters the submit field for the comment form to display. - * - * The submit field includes the submit button, hidden fields for the - * comment form, and any wrapper markup. - * - * @since 4.2.0 - * - * @param string $submit_field HTML markup for the submit field. - * @param array $args Arguments passed to comment_form(). - */ - echo apply_filters( 'comment_form_submit_field', $submit_field, $args ); + /** + * Filters the submit field for the comment form to display. + * + * The submit field includes the submit button, hidden fields for the + * comment form, and any wrapper markup. + * + * @since 4.2.0 + * + * @param string $submit_field HTML markup for the submit field. + * @param array $args Arguments passed to comment_form(). + */ + echo apply_filters( 'comment_form_submit_field', $submit_field, $args ); - /** - * Fires at the bottom of the comment form, inside the closing
tag. - * - * @since 1.5.0 - * - * @param int $post_id The post ID. - */ - do_action( 'comment_form', $post_id ); - ?> - - -
- tag. + * + * @since 1.5.0 + * + * @param int $post_id The post ID. + */ + do_action( 'comment_form', $post_id ); + ?> + + +
+