Skip to content
Closed
Prev Previous commit
Next Next commit
Break off into it's own method.
  • Loading branch information
spacedmonkey committed Feb 1, 2022
commit 2f9e17dd0c1bd1fad5d0aca9a0c5fd51a1cd6c52
75 changes: 38 additions & 37 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,42 @@ public function parse_tax_query( &$q ) {
do_action( 'parse_tax_query', $this );
}


/**
* Generates SQL for the WHERE clause based on passed search terms.
*
* @since 3.7.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $comments_request SQL to run.
* @return array Array of comments.
*/
protected function get_comments( $comments_request ) {
global $wpdb;

$key = md5( $comments_request );
$last_changed = wp_cache_get_last_changed( 'comment' ) . wp_cache_get_last_changed( 'posts' );

$cache_key = "comment_feed:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' );
if ( ! $comment_ids ) {
$comments = (array) $wpdb->get_results( $comments_request );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comments );
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
} else {
_prime_comment_caches( $comment_ids, false );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comment_ids );
}

return $comments;
}

/**
* Generates SQL for the WHERE clause based on passed search terms.
*
Expand Down Expand Up @@ -2721,25 +2757,7 @@ public function get_posts() {
$climits = ( ! empty( $climits ) ) ? $climits : '';

$comments_request = "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";

$key = md5( $comments_request );
$last_changed = wp_cache_get_last_changed( 'comment' ) . wp_cache_get_last_changed( 'posts' );

$cache_key = "comment_feed:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' );
if ( ! $comment_ids ) {
$comments = (array) $wpdb->get_results( $comments_request );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comments );
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
} else {
_prime_comment_caches( $comment_ids, false );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comment_ids );
}
$comments = $this->get_comments( $comments_request );

$this->comments = $comments;
$this->comment_count = count( $this->comments );
Expand Down Expand Up @@ -3168,24 +3186,7 @@ public function get_posts() {
$climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );

$comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$key = md5( $comments_request );
$last_changed = wp_cache_get_last_changed( 'comment' );

$cache_key = "comment_feed:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' );
if ( ! $comment_ids ) {
$comments = (array) $wpdb->get_results( $comments_request );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comments );
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
} else {
_prime_comment_caches( $comment_ids, false );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$comments = array_map( 'get_comment', $comment_ids );
}
$comments = $this->get_comments( $comments_request );

$this->comments = $comments;
$this->comment_count = count( $this->comments );
Expand Down