diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index b1828c3fde..ada21ec8dd 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2212,8 +2212,13 @@ function wp_new_comment( $commentdata ) { $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ); $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : ''; - $commentdata['comment_date'] = current_time('mysql'); - $commentdata['comment_date_gmt'] = current_time('mysql', 1); + if ( empty( $commentdata['comment_date'] ) ) { + $commentdata['comment_date'] = current_time('mysql'); + } + + if ( empty( $commentdata['comment_date_gmt'] ) ) { + $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); + } $commentdata = wp_filter_comment($commentdata); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 0bd3920b5a..ff3cc1fc2d 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -72,4 +72,44 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertSame( array(), $found ); } + + /** + * @ticket 14279 + */ + public function test_wp_new_comment_respects_dates() { + // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices. + if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { + $remote_addr = $_SERVER['REMOTE_ADDR']; + } else { + $_SERVER['REMOTE_ADDR'] = ''; + } + + $u = $this->factory->user->create(); + $post_id = $this->factory->post->create( array( 'post_author' => $u ) ); + + $data = array( + 'comment_post_ID' => $post_id, + 'comment_author' => rand_str(), + 'comment_author_url' => '', + 'comment_author_email' => '', + 'comment_type' => '', + 'comment_content' => rand_str(), + 'comment_date' => '2011-01-01 10:00:00', + 'comment_date_gmt' => '2011-01-01 10:00:00', + ); + + $id = wp_new_comment( $data ); + + $comment = get_comment( $id ); + + $this->assertEquals( $data['comment_date'], $comment->comment_date ); + $this->assertEquals( $data['comment_date_gmt'], $comment->comment_date_gmt ); + + // Cleanup. + if ( isset( $remote_addr ) ) { + $_SERVER['REMOTE_ADDR'] = $remote_addr; + } else { + unset( $_SERVER['REMOTE_ADDR'] ); + } + } }