From 3c028a80d421d65c4080f7a1d362795ed2fb3b99 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 23 Sep 2015 18:16:26 +0000 Subject: [PATCH] Allow `comment_exists()` to match based on GMT date. The `comment_date_gmt` field of the `wp_comments` table is indexed, which makes `WHERE` matches against the field much faster than against the unindexed `comment_date`. For bulk operations like data import, the speed difference can be meaningful. We continue to default to 'blog' for `$timezone`, to preserve compatibility with existing uses. Props apokalyptik. Fixes #33871. Built from https://develop.svn.wordpress.org/trunk@34460 git-svn-id: http://core.svn.wordpress.org/trunk@34424 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/comment.php | 19 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/wp-admin/includes/comment.php b/wp-admin/includes/comment.php index dfb39220ab..32cb56b5fb 100644 --- a/wp-admin/includes/comment.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 1387e19a9a..24af754281 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34459'; +$wp_version = '4.4-alpha-34460'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.