From e2c57a945101ac066e2f909bf127f9513f64fe87 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 25 Sep 2015 05:11:25 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@34535 git-svn-id: http://core.svn.wordpress.org/trunk@34499 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment-functions.php | 39 ++++++++++++++++++++----------- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/wp-includes/comment-functions.php b/wp-includes/comment-functions.php index 7499b97c57..810eef4ba3 100644 --- a/wp-includes/comment-functions.php +++ b/wp-includes/comment-functions.php @@ -821,9 +821,17 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded * @since 2.7.0 * * @global wpdb $wpdb - * - * @param int $comment_ID Comment ID. - * @param array $args Optional args. + * @param int $comment_ID Comment ID. + * @param array $args { + * Array of optional arguments. + * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment', + * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'. + * Default is 'all'. + * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the + * 'comments_per_page' option. + * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of + * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option. + * } * * @return int|null Comment page number or null on error. */ function get_page_of_comment( $comment_ID, $args = array() ) { @@ -855,23 +863,28 @@ function get_page_of_comment( $comment_ID, $args = array() ) { if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) return get_page_of_comment( $comment->comment_parent, $args ); - $allowedtypes = array( - 'comment' => '', - 'pingback' => 'pingback', - 'trackback' => 'trackback', + $comment_args = array( + 'type' => $args['type'], + 'post_ID' => $comment->comment_post_ID, + 'fields' => 'ids', + 'status' => 'approve', + 'date_query' => array( + array( + 'column' => "$wpdb->comments.comment_date_gmt", + 'before' => $comment->comment_date_gmt, + ) + ), ); - $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : ''; - - // Count comments older than this one - $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 ) ); + $older_comment_ids = get_comments( $comment_args ); + $older_comment_count = count( $older_comment_ids ); // No older comments? Then it's page #1. - if ( 0 == $oldercoms ) + if ( 0 == $older_comment_count ) return 1; // Divide comments older than this one by comments per page to get this comment's page number - return ceil( ( $oldercoms + 1 ) / $args['per_page'] ); + return ceil( ( $older_comment_count + 1 ) / $args['per_page'] ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index e321697d3e..ec8a4ec7b9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34534'; +$wp_version = '4.4-alpha-34535'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.