Make WordPress Core


Ignore:
Timestamp:
09/25/2015 05:10:40 AM (9 years ago)
Author:
boonebgorges
Message:

Use WP_Comment_Query in get_page_of_comment().

This change allows get_page_of_comment() to use WP_Comment_Query's native
caching mechanisms.

Props boonebgorges, Viper007Bond, wmertens, jeremyfelt.
Fixes #11334.

File:
1 edited

Legend:

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

    r34534 r34535  
    822822 *
    823823 * @global wpdb $wpdb
    824  *
    825  * @param int $comment_ID Comment ID.
    826  * @param array $args Optional args.
     824 * @param int   $comment_ID Comment ID.
     825 * @param array $args {
     826 *      Array of optional arguments.
     827 *      @type string     $type      Limit paginated comments to those matching a given type. Accepts 'comment',
     828 *                                  'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
     829 *                                  Default is 'all'.
     830 *      @type int        $per_page  Per-page count to use when calculating pagination. Defaults to the value of the
     831 *                                  'comments_per_page' option.
     832 *      @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
     833 *                                  `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
     834 * } *
    827835 * @return int|null Comment page number or null on error.
    828836 */
     
    856864        return get_page_of_comment( $comment->comment_parent, $args );
    857865
    858     $allowedtypes = array(
    859         'comment' => '',
    860         'pingback' => 'pingback',
    861         'trackback' => 'trackback',
     866    $comment_args = array(
     867        'type'       => $args['type'],
     868        'post_ID'    => $comment->comment_post_ID,
     869        'fields'     => 'ids',
     870        'status'     => 'approve',
     871        'date_query' => array(
     872            array(
     873                'column' => "$wpdb->comments.comment_date_gmt",
     874                'before' => $comment->comment_date_gmt,
     875            )
     876        ),
    862877    );
    863878
    864     $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    865 
    866     // Count comments older than this one
    867     $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
     879    $older_comment_ids = get_comments( $comment_args );
     880    $older_comment_count = count( $older_comment_ids );
    868881
    869882    // No older comments? Then it's page #1.
    870     if ( 0 == $oldercoms )
     883    if ( 0 == $older_comment_count )
    871884        return 1;
    872885
    873886    // Divide comments older than this one by comments per page to get this comment's page number
    874     return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     887    return ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
    875888}
    876889
Note: See TracChangeset for help on using the changeset viewer.