From 99705b35b8e57a39a5ed7a7e8bd1e4b692b59764 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sun, 7 Nov 2021 21:13:11 +0000 Subject: [PATCH] Comments: Mark comment text field as required. Add required asterisk to the comment text field. Historically, the name and email fields are marked as required, but the comment text field is not, though it is actually a required field. Props infected, solarissmoke, rianrietveld, afercia, sabernhardt, strider72, mai21, audrasjb. Fixes #16206. git-svn-id: https://develop.svn.wordpress.org/trunk@52029 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 40 +++++++++++++++++----------- src/wp-includes/comment.php | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index e45088729f..1415dd1e2f 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2350,9 +2350,15 @@ function comment_form( $args = array(), $post_id = null ) { $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml'; } - $req = get_option( 'require_name_email' ); - $html_req = ( $req ? " required='required'" : '' ); - $html5 = 'html5' === $args['format']; + $req = get_option( 'require_name_email' ); + $html5 = 'html5' === $args['format']; + + // Define attributes in HTML5 or XHTML syntax. + $required_attribute = ( $html5 ? ' required' : ' required="required"' ); + $checked_attribute = ( $html5 ? ' checked' : ' checked="checked"' ); + + // Identify required fields visually. + $required_indicator = ' '; $fields = array( 'author' => sprintf( @@ -2360,12 +2366,12 @@ function comment_form( $args = array(), $post_id = null ) { sprintf( '', __( 'Name' ), - ( $req ? ' *' : '' ) + ( $req ? $required_indicator : '' ) ), sprintf( '', esc_attr( $commenter['comment_author'] ), - $html_req + ( $req ? $required_attribute : '' ) ) ), 'email' => sprintf( @@ -2373,13 +2379,13 @@ function comment_form( $args = array(), $post_id = null ) { sprintf( '', __( 'Email' ), - ( $req ? ' *' : '' ) + ( $req ? $required_indicator : '' ) ), sprintf( '', ( $html5 ? 'type="email"' : 'type="text"' ), esc_attr( $commenter['comment_author_email'] ), - $html_req + ( $req ? $required_attribute : '' ) ) ), 'url' => sprintf( @@ -2397,7 +2403,7 @@ function comment_form( $args = array(), $post_id = null ) { ); if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) { - $consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"'; + $consent = empty( $commenter['comment_author_email'] ) ? '' : $checked_attribute; $fields['cookies'] = sprintf( '', @@ -2419,8 +2425,8 @@ function comment_form( $args = array(), $post_id = null ) { $required_text = sprintf( /* translators: %s: Asterisk symbol (*). */ - ' ' . __( 'Required fields are marked %s' ), - '*' + ' ', + trim( $required_indicator ) ); /** @@ -2437,10 +2443,11 @@ function comment_form( $args = array(), $post_id = null ) { 'comment_field' => sprintf( '

%s %s

', sprintf( - '', - _x( 'Comment', 'noun' ) + '', + _x( 'Comment', 'noun' ), + $required_indicator ), - '' + '' ), 'must_log_in' => sprintf( '

%s

', @@ -2452,7 +2459,7 @@ function comment_form( $args = array(), $post_id = null ) { ) ), 'logged_in_as' => sprintf( - '

%s

', + '

%s%s

', sprintf( /* translators: 1: Edit user link, 2: Accessibility text, 3: User name, 4: Logout URL. */ __( 'Logged in as %3$s. Log out?' ), @@ -2462,7 +2469,8 @@ function comment_form( $args = array(), $post_id = null ) { $user_identity, /** This filter is documented in wp-includes/link-template.php */ wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) ) - ) + ), + $required_text ), 'comment_notes_before' => sprintf( '

%s%s

', @@ -2470,7 +2478,7 @@ function comment_form( $args = array(), $post_id = null ) { '%s', __( 'Your email address will not be published.' ) ), - ( $req ? $required_text : '' ) + $required_text ), 'comment_notes_after' => '', 'action' => site_url( '/wp-comments-post.php' ), diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 113bb2e719..c2892f16b1 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3549,7 +3549,7 @@ function wp_handle_comment_submission( $comment_data ) { if ( get_option( 'require_name_email' ) && ! $user->exists() ) { if ( '' == $comment_author_email || '' == $comment_author ) { - return new WP_Error( 'require_name_email', __( 'Error: Please fill the required fields (name, email).' ), 200 ); + return new WP_Error( 'require_name_email', __( 'Error: Please fill the required fields.' ), 200 ); } elseif ( ! is_email( $comment_author_email ) ) { return new WP_Error( 'require_valid_email', __( 'Error: Please enter a valid email address.' ), 200 ); }