Make WordPress Core


Ignore:
Timestamp:
09/23/2015 06:15:39 PM (10 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

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

    r34019 r34460  
    1010 * Determine if a comment exists based on author and date.
    1111 *
     12 * For best performance, use `$timezone = 'gmt'`, which queries a field that is properly indexed. The default value
     13 * for `$timezone` is 'blog' for legacy reasons.
     14 *
    1215 * @since 2.0.0
     16 * @since 4.4.0 Added the `$timezone` parameter.
    1317 *
    1418 * @global wpdb $wpdb WordPress database abstraction object.
    1519 *
    16  * @param string $comment_author Author of the comment
    17  * @param string $comment_date Date of the comment
     20 * @param string $comment_author Author of the comment.
     21 * @param string $comment_date   Date of the comment.
     22 * @param string $timezone       Timezone. Accepts 'blog' or 'gmt'. Default 'blog'.
     23 *
    1824 * @return mixed Comment post ID on success.
    1925 */
    20 function comment_exists($comment_author, $comment_date) {
     26function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
    2127    global $wpdb;
    2228
     29    $date_field = 'comment_date';
     30    if ( 'gmt' === $timezone ) {
     31        $date_field = 'comment_date_gmt';
     32    }
     33
    2334    return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
    24             WHERE comment_author = %s AND comment_date = %s",
     35            WHERE comment_author = %s AND $date_field = %s",
    2536            stripslashes( $comment_author ),
    2637            stripslashes( $comment_date )
Note: See TracChangeset for help on using the changeset viewer.