From f1e98d9609393c9a3bd8951b7b881e6358ace015 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 24 Jul 2018 12:42:33 +0000 Subject: [PATCH] Comments: Ensure that themes overriding default `comment_form()` fields still display the cookies consent checkbox. The `comment_form_default_fields` filter can be used to remove the checkbox. Props pross, SergeyBiryukov. Merges [43518] to the 4.9 branch. Fixes #44126. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43524 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 5 +++++ tests/phpunit/tests/comment/commentForm.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 661c82f891..5bcbcd674e 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2209,6 +2209,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 c6bb43fd96..288f2896d1 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 ); + } }