WordPress.org

Make WordPress Core

Changeset 31615


Ignore:
Timestamp:
03/05/2015 02:59:47 AM (6 years ago)
Author:
boonebgorges
Message:

Respect comment_date and comment_date_gmt params in wp_new_comment().

Props solarissmoke, oso96_2000.
Fixes #14279.

Location:
trunk
Files:
2 edited

Legend:

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

    r31553 r31615  
    22132213    $commentdata['comment_agent']     = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
    22142214
    2215     $commentdata['comment_date']     = current_time('mysql');
    2216     $commentdata['comment_date_gmt'] = current_time('mysql', 1);
     2215    if ( empty( $commentdata['comment_date'] ) ) {
     2216        $commentdata['comment_date'] = current_time('mysql');
     2217    }
     2218
     2219    if ( empty( $commentdata['comment_date_gmt'] ) ) {
     2220        $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
     2221    }
    22172222
    22182223    $commentdata = wp_filter_comment($commentdata);
  • trunk/tests/phpunit/tests/comment.php

    r31195 r31615  
    7373        $this->assertSame( array(), $found );
    7474    }
     75
     76    /**
     77     * @ticket 14279
     78     */
     79    public function test_wp_new_comment_respects_dates() {
     80        // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices.
     81        if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
     82            $remote_addr = $_SERVER['REMOTE_ADDR'];
     83        } else {
     84            $_SERVER['REMOTE_ADDR'] = '';
     85        }
     86
     87        $u = $this->factory->user->create();
     88        $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
     89
     90        $data = array(
     91            'comment_post_ID' => $post_id,
     92            'comment_author' => rand_str(),
     93            'comment_author_url' => '',
     94            'comment_author_email' => '',
     95            'comment_type' => '',
     96            'comment_content' => rand_str(),
     97            'comment_date' => '2011-01-01 10:00:00',
     98            'comment_date_gmt' => '2011-01-01 10:00:00',
     99        );
     100
     101        $id = wp_new_comment( $data );
     102
     103        $comment = get_comment( $id );
     104
     105        $this->assertEquals( $data['comment_date'], $comment->comment_date );
     106        $this->assertEquals( $data['comment_date_gmt'], $comment->comment_date_gmt );
     107
     108        // Cleanup.
     109        if ( isset( $remote_addr ) ) {
     110            $_SERVER['REMOTE_ADDR'] = $remote_addr;
     111        } else {
     112            unset( $_SERVER['REMOTE_ADDR'] );
     113        }
     114    }
    75115}
Note: See TracChangeset for help on using the changeset viewer.