Make WordPress Core

Changeset 33021


Ignore:
Timestamp:
07/01/2015 12:07:28 PM (9 years ago)
Author:
boonebgorges
Message:

Allow 'comment_agent' and 'comment_author_IP' to be set via wp_new_comment().

Props mrutz, wonderboymusic, rachelbaker.
Fixes #14601.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r32964 r33021  
    22722272 *
    22732273 * @since 1.5.0
     2274 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
    22742275 *
    22752276 * @see wp_insert_comment()
     
    23102311    $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
    23112312
    2312     $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
    2313     $commentdata['comment_agent']     = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
     2313    if ( ! isset( $commentdata['comment_author_IP'] ) ) {
     2314        $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
     2315    }
     2316    $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
     2317
     2318    if ( ! isset( $commentdata['comment_agent'] ) ) {
     2319        $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: '';
     2320    }
     2321    $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
    23142322
    23152323    if ( empty( $commentdata['comment_date'] ) ) {
  • trunk/tests/phpunit/tests/comment.php

    r32692 r33021  
    114114    }
    115115
     116    /**
     117     * @ticket 14601
     118     */
     119    public function test_wp_new_comment_respects_author_ip() {
     120        $u = $this->factory->user->create();
     121        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     122
     123        $data = array(
     124            'comment_post_ID'      => $post_id,
     125            'comment_author'       => rand_str(),
     126            'comment_author_IP'    => '192.168.1.1',
     127            'comment_author_url'   => '',
     128            'comment_author_email' => '',
     129            'comment_type'         => '',
     130            'comment_content'      => rand_str(),
     131        );
     132
     133        $id = wp_new_comment( $data );
     134
     135        $comment = get_comment( $id );
     136
     137        $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
     138    }
     139
     140    /**
     141     * @ticket 14601
     142     */
     143    public function test_wp_new_comment_respects_author_ip_empty_string() {
     144        $u = $this->factory->user->create();
     145        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     146
     147        $data = array(
     148            'comment_post_ID'      => $post_id,
     149            'comment_author'       => rand_str(),
     150            'comment_author_IP'    => '',
     151            'comment_author_url'   => '',
     152            'comment_author_email' => '',
     153            'comment_type'         => '',
     154            'comment_content'      => rand_str(),
     155        );
     156
     157        $id = wp_new_comment( $data );
     158
     159        $comment = get_comment( $id );
     160
     161        $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
     162    }
     163
     164    /**
     165     * @ticket 14601
     166     */
     167    public function test_wp_new_comment_respects_comment_agent() {
     168        $u = $this->factory->user->create();
     169        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     170
     171        $data = array(
     172            'comment_post_ID'      => $post_id,
     173            'comment_author'       => rand_str(),
     174            'comment_author_IP'    => '',
     175            'comment_author_url'   => '',
     176            'comment_author_email' => '',
     177            'comment_agent'        => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53',
     178            'comment_type'         => '',
     179            'comment_content'      => rand_str(),
     180        );
     181
     182        $id = wp_new_comment( $data );
     183
     184        $comment = get_comment( $id );
     185
     186        $this->assertEquals( $data['comment_agent'], $comment->comment_agent );
     187    }
     188
     189    /**
     190     * @ticket 14601
     191     */
     192    public function test_wp_new_comment_should_trim_provided_comment_agent_to_254_chars() {
     193        $u = $this->factory->user->create();
     194        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     195
     196        $data = array(
     197            'comment_post_ID'      => $post_id,
     198            'comment_author'       => rand_str(),
     199            'comment_author_IP'    => '',
     200            'comment_author_url'   => '',
     201            'comment_author_email' => '',
     202            'comment_agent'        => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre',
     203            'comment_type'         => '',
     204            'comment_content'      => rand_str(),
     205        );
     206
     207        $id = wp_new_comment( $data );
     208
     209        $comment = get_comment( $id );
     210
     211        $this->assertEquals( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent );
     212    }
     213
     214    /**
     215     * @ticket 14601
     216     */
     217    public function test_wp_new_comment_respects_comment_agent_empty_string() {
     218        $u = $this->factory->user->create();
     219        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     220
     221        $data = array(
     222            'comment_post_ID'      => $post_id,
     223            'comment_author'       => rand_str(),
     224            'comment_author_IP'    => '',
     225            'comment_author_url'   => '',
     226            'comment_author_email' => '',
     227            'comment_agent'        => '',
     228            'comment_type'         => '',
     229            'comment_content'      => rand_str(),
     230        );
     231
     232        $id = wp_new_comment( $data );
     233
     234        $comment = get_comment( $id );
     235
     236        $this->assertEquals( $data['comment_agent'], $comment->comment_agent );
     237    }
     238
     239
    116240    public function test_comment_field_lengths() {
    117241        // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices.
Note: See TracChangeset for help on using the changeset viewer.