diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index dfb39220ab..32cb56b5fb 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -9,19 +9,30 @@ /** * Determine if a comment exists based on author and date. * + * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value + * for `$timezone` is 'blog' for legacy reasons. + * * @since 2.0.0 + * @since 4.4.0 Added the `$timezone` parameter. * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $comment_author Author of the comment - * @param string $comment_date Date of the comment + * @param string $comment_author Author of the comment. + * @param string $comment_date Date of the comment. + * @param string $timezone Timezone. Accepts 'blog' or 'gmt'. Default 'blog'. + * * @return mixed Comment post ID on success. */ -function comment_exists($comment_author, $comment_date) { +function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) { global $wpdb; + $date_field = 'comment_date'; + if ( 'gmt' === $timezone ) { + $date_field = 'comment_date_gmt'; + } + return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments - WHERE comment_author = %s AND comment_date = %s", + WHERE comment_author = %s AND $date_field = %s", stripslashes( $comment_author ), stripslashes( $comment_date ) ) ); diff --git a/tests/phpunit/tests/admin/includesComment.php b/tests/phpunit/tests/admin/includesComment.php index 5b7b01b904..fe9b86cb72 100644 --- a/tests/phpunit/tests/admin/includesComment.php +++ b/tests/phpunit/tests/admin/includesComment.php @@ -23,4 +23,64 @@ class Tests_Admin_IncludesComment extends WP_UnitTestCase { $this->assertNull( comment_exists( 1, '2004-01-02 12:00:00' ) ); $this->assertEquals( $p1, comment_exists( 1, '2014-05-06 12:00:00' ) ); } + + /** + * @ticket 33871 + */ + public function test_default_value_of_timezone_should_be_blog() { + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( + 'comment_author' => 1, + 'comment_post_ID' => $p, + 'comment_date' => '2014-05-06 12:00:00', + 'comment_date_gmt' => '2014-05-06 07:00:00', + ) ); + + $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00' ) ); + } + + /** + * @ticket 33871 + */ + public function test_should_respect_timezone_blog() { + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( + 'comment_author' => 1, + 'comment_post_ID' => $p, + 'comment_date' => '2014-05-06 12:00:00', + 'comment_date_gmt' => '2014-05-06 07:00:00', + ) ); + + $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'blog' ) ); + } + + /** + * @ticket 33871 + */ + public function test_should_respect_timezone_gmt() { + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( + 'comment_author' => 1, + 'comment_post_ID' => $p, + 'comment_date' => '2014-05-06 12:00:00', + 'comment_date_gmt' => '2014-05-06 07:00:00', + ) ); + + $this->assertEquals( $p, comment_exists( 1, '2014-05-06 07:00:00', 'gmt' ) ); + } + + /** + * @ticket 33871 + */ + public function test_invalid_timezone_should_fall_back_on_blog() { + $p = $this->factory->post->create(); + $c = $this->factory->comment->create( array( + 'comment_author' => 1, + 'comment_post_ID' => $p, + 'comment_date' => '2014-05-06 12:00:00', + 'comment_date_gmt' => '2014-05-06 07:00:00', + ) ); + + $this->assertEquals( $p, comment_exists( 1, '2014-05-06 12:00:00', 'not_a_valid_value' ) ); + } }