diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 986639af13..1f7596dc55 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2275,6 +2275,11 @@ function comment_form( $args = array(), $post_id = null ) { $consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"'; $fields['cookies'] = '
'; + + // Ensure that the passed fields include cookies consent. + if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) { + $args['fields']['cookies'] = $fields['cookies']; + } } $required_text = sprintf( ' ' . __( 'Required fields are marked %s' ), '*' ); diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index b1bde4c792..0fabd97a6a 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -80,4 +80,24 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { unset( $defaults['submit_button'] ); return $defaults; } + + /** + * @ticket 44126 + */ + public function test_fields_should_include_cookies_consent() { + $p = self::factory()->post->create(); + + add_filter( 'option_show_comments_cookies_opt_in', '__return_true' ); + + $args = array( + 'fields' => array( + 'author' => 'Hello World!', + ), + ); + $form = get_echo( 'comment_form', array( $args, $p ) ); + + remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' ); + + $this->assertRegExp( '| |', $form ); + } }