Ticket #20302: 20302.2.diff
File 20302.2.diff, 5.4 KB (added by , 9 years ago) |
---|
-
src/wp-includes/comment-template.php
1242 1242 * @global int $user_ID 1243 1243 * @global string $user_identity 1244 1244 * @global bool $overridden_cpage 1245 * @global bool $withcomments 1245 * @global bool $withcomments 1246 1246 * 1247 1247 * @param string $file Optional. The file to load. Default '/comments.php'. 1248 1248 * @param bool $separate_comments Optional. Whether to separate the comments by comment type. … … 2026 2026 * fields. Default: '<p class="form-submit">%1$s %2$s</a>', where %1$s is the 2027 2027 * submit button markup and %2$s is the comment hidden fields. 2028 2028 * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'. 2029 * @type array $attributes_form Additional attributes to apply to 2029 2030 * } 2030 2031 * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post. 2031 2032 */ … … 2089 2090 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />', 2090 2091 'submit_field' => '<p class="form-submit">%1$s %2$s</p>', 2091 2092 'format' => 'xhtml', 2093 'attributes_form' => array(), 2092 2094 ); 2093 2095 2094 2096 /** … … 2105 2107 // Ensure that the filtered args contain all required default values. 2106 2108 $args = array_merge( $defaults, $args ); 2107 2109 2110 // Build the <form> attributes. 2111 $attributes = array( 2112 'action' => site_url( '/wp-comments-post.php' ), 2113 'method' => 'post', 2114 'id' => $args['id_form'], 2115 'class' => $args['class_form'], 2116 'novalidate' => $html5, 2117 ); 2118 2119 if ( is_array( $args['attributes_form'] ) ) { 2120 $attributes = array_merge( $attributes, $args['attributes_form'] ); 2121 } 2122 2108 2123 if ( comments_open( $post_id ) ) : ?> 2109 2124 <?php 2110 2125 /** … … 2137 2152 */ 2138 2153 do_action( 'comment_form_must_log_in_after' ); 2139 2154 else : ?> 2140 <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="<?php echo esc_attr( $args['class_form'] ); ?>"<?php echo $html5 ? ' novalidate' : ''; ?>> 2155 <?php 2156 echo '<form'; 2157 2158 // Build the form attributes. 2159 foreach ( $attributes as $attr_name => $attr_val ) { 2160 if ( true === $attr_val ) { 2161 echo ' ' . esc_attr( $attr_name ); 2162 } else { 2163 printf( ' %s="%s"', esc_attr( $attr_name ), esc_attr( $attr_val ) ); 2164 } 2165 } 2166 2167 echo '>'; 2168 ?> 2141 2169 <?php 2142 2170 /** 2143 2171 * Fires at the top of the comment form, inside the form tag. -
tests/phpunit/tests/comment/commentsTemplate.php
685 685 $this->assertSame( array( $comment_4, $comment_3 ), $found_cids ); 686 686 } 687 687 688 /** 689 * Validate that the current attributes (action, method, id, class, and novalidate) are 690 * still present after moving to an array-based build. 691 * 692 * @ticket 20302 693 */ 694 public function test_building_comment_form_attributes_from_array() { 695 $p = self::factory()->post->create(); 696 697 add_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); 698 $this->go_to( get_permalink( $p ) ); 699 $template = get_echo( 'comments_template' ); 700 remove_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); 701 702 $this->assertEquals( 1, preg_match( '|\saction="(.+)/wp-comments-post.php"|i', $template ) ); 703 $this->assertContains( 'method="post"', $template ); 704 $this->assertContains( 'id="commentform"', $template ); 705 $this->assertContains( 'class="comment-form"', $template ); 706 } 707 708 /** 709 * @ticket 20302 710 */ 711 public function test_extra_comment_form_attributes() { 712 $p = self::factory()->post->create(); 713 714 /** 715 * Anonymous function to alter the 'attributes' value for the 'comment_form_defaults' filter. 716 */ 717 add_filter( 'comment_form_defaults', array( $this, 'extra_comment_form_attributes_cb' ) ); 718 719 add_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); 720 $this->go_to( get_permalink( $p ) ); 721 $template = get_echo( 'comments_template' ); 722 remove_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) ); 723 724 // Validate that default attributes aren't overwritten. 725 $this->assertContains( 'method="post"', $template ); 726 $this->assertContains( 'id="commentform"', $template ); 727 $this->assertContains( 'class="comment-form"', $template ); 728 729 // Validate our custom attribute. 730 $this->assertContains( 'data-test-attribute="hooray!"', $template ); 731 $this->assertContains( 'onclick="dosomething()"', $template ); 732 } 733 734 /** 735 * Callback for test_extra_comment_form_attributes(). 736 * 737 * @ticket 20302 738 */ 739 public function extra_comment_form_attributes_cb( $defaults ) { 740 $defaults['attributes_form'] = array( 741 'data-test-attribute' => 'hooray!', 742 'onclick' => 'dosomething()', 743 ); 744 745 return $defaults; 746 } 747 688 748 public function fake_current_commenter( $commenter ) { 689 749 $commenter['comment_author_email'] = 'foo@example.com'; 690 750 return $commenter; 691 751 } 752 692 753 }