Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: class-wp-comment-query.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Comment API: WP_Comment_Query class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Comments
[5] Fix | Delete
* @since 4.4.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class used for querying comments.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 3.1.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_Comment_Query::__construct() for accepted arguments.
[14] Fix | Delete
*/
[15] Fix | Delete
#[AllowDynamicProperties]
[16] Fix | Delete
class WP_Comment_Query {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* SQL for database query.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 4.0.1
[22] Fix | Delete
* @var string
[23] Fix | Delete
*/
[24] Fix | Delete
public $request;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Metadata query container
[28] Fix | Delete
*
[29] Fix | Delete
* @since 3.5.0
[30] Fix | Delete
* @var WP_Meta_Query A meta query instance.
[31] Fix | Delete
*/
[32] Fix | Delete
public $meta_query = false;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Metadata query clauses.
[36] Fix | Delete
*
[37] Fix | Delete
* @since 4.4.0
[38] Fix | Delete
* @var array
[39] Fix | Delete
*/
[40] Fix | Delete
protected $meta_query_clauses;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* SQL query clauses.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 4.4.0
[46] Fix | Delete
* @var array
[47] Fix | Delete
*/
[48] Fix | Delete
protected $sql_clauses = array(
[49] Fix | Delete
'select' => '',
[50] Fix | Delete
'from' => '',
[51] Fix | Delete
'where' => array(),
[52] Fix | Delete
'groupby' => '',
[53] Fix | Delete
'orderby' => '',
[54] Fix | Delete
'limits' => '',
[55] Fix | Delete
);
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* SQL WHERE clause.
[59] Fix | Delete
*
[60] Fix | Delete
* Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses.
[61] Fix | Delete
*
[62] Fix | Delete
* @since 4.4.2
[63] Fix | Delete
* @var string
[64] Fix | Delete
*/
[65] Fix | Delete
protected $filtered_where_clause;
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Date query container
[69] Fix | Delete
*
[70] Fix | Delete
* @since 3.7.0
[71] Fix | Delete
* @var WP_Date_Query A date query instance.
[72] Fix | Delete
*/
[73] Fix | Delete
public $date_query = false;
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Query vars set by the user.
[77] Fix | Delete
*
[78] Fix | Delete
* @since 3.1.0
[79] Fix | Delete
* @var array
[80] Fix | Delete
*/
[81] Fix | Delete
public $query_vars;
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Default values for query vars.
[85] Fix | Delete
*
[86] Fix | Delete
* @since 4.2.0
[87] Fix | Delete
* @var array
[88] Fix | Delete
*/
[89] Fix | Delete
public $query_var_defaults;
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* List of comments located by the query.
[93] Fix | Delete
*
[94] Fix | Delete
* @since 4.0.0
[95] Fix | Delete
* @var int[]|WP_Comment[]
[96] Fix | Delete
*/
[97] Fix | Delete
public $comments;
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* The amount of found comments for the current query.
[101] Fix | Delete
*
[102] Fix | Delete
* @since 4.4.0
[103] Fix | Delete
* @var int
[104] Fix | Delete
*/
[105] Fix | Delete
public $found_comments = 0;
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* The number of pages.
[109] Fix | Delete
*
[110] Fix | Delete
* @since 4.4.0
[111] Fix | Delete
* @var int
[112] Fix | Delete
*/
[113] Fix | Delete
public $max_num_pages = 0;
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Make private/protected methods readable for backward compatibility.
[117] Fix | Delete
*
[118] Fix | Delete
* @since 4.0.0
[119] Fix | Delete
*
[120] Fix | Delete
* @param string $name Method to call.
[121] Fix | Delete
* @param array $arguments Arguments to pass when calling.
[122] Fix | Delete
* @return mixed|false Return value of the callback, false otherwise.
[123] Fix | Delete
*/
[124] Fix | Delete
public function __call( $name, $arguments ) {
[125] Fix | Delete
if ( 'get_search_sql' === $name ) {
[126] Fix | Delete
return $this->get_search_sql( ...$arguments );
[127] Fix | Delete
}
[128] Fix | Delete
return false;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
/**
[132] Fix | Delete
* Constructor.
[133] Fix | Delete
*
[134] Fix | Delete
* Sets up the comment query, based on the query vars passed.
[135] Fix | Delete
*
[136] Fix | Delete
* @since 4.2.0
[137] Fix | Delete
* @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
[138] Fix | Delete
* @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
[139] Fix | Delete
* `$hierarchical`, and `$update_comment_post_cache` were added.
[140] Fix | Delete
* @since 4.5.0 Introduced the `$author_url` argument.
[141] Fix | Delete
* @since 4.6.0 Introduced the `$cache_domain` argument.
[142] Fix | Delete
* @since 4.9.0 Introduced the `$paged` argument.
[143] Fix | Delete
* @since 5.1.0 Introduced the `$meta_compare_key` argument.
[144] Fix | Delete
* @since 5.3.0 Introduced the `$meta_type_key` argument.
[145] Fix | Delete
*
[146] Fix | Delete
* @param string|array $query {
[147] Fix | Delete
* Optional. Array or query string of comment query parameters. Default empty.
[148] Fix | Delete
*
[149] Fix | Delete
* @type string $author_email Comment author email address. Default empty.
[150] Fix | Delete
* @type string $author_url Comment author URL. Default empty.
[151] Fix | Delete
* @type int[] $author__in Array of author IDs to include comments for. Default empty.
[152] Fix | Delete
* @type int[] $author__not_in Array of author IDs to exclude comments for. Default empty.
[153] Fix | Delete
* @type int[] $comment__in Array of comment IDs to include. Default empty.
[154] Fix | Delete
* @type int[] $comment__not_in Array of comment IDs to exclude. Default empty.
[155] Fix | Delete
* @type bool $count Whether to return a comment count (true) or array of
[156] Fix | Delete
* comment objects (false). Default false.
[157] Fix | Delete
* @type array $date_query Date query clauses to limit comments by. See WP_Date_Query.
[158] Fix | Delete
* Default null.
[159] Fix | Delete
* @type string $fields Comment fields to return. Accepts 'ids' for comment IDs
[160] Fix | Delete
* only or empty for all fields. Default empty.
[161] Fix | Delete
* @type array $include_unapproved Array of IDs or email addresses of users whose unapproved
[162] Fix | Delete
* comments will be returned by the query regardless of
[163] Fix | Delete
* `$status`. Default empty.
[164] Fix | Delete
* @type int $karma Karma score to retrieve matching comments for.
[165] Fix | Delete
* Default empty.
[166] Fix | Delete
* @type string|string[] $meta_key Meta key or keys to filter by.
[167] Fix | Delete
* @type string|string[] $meta_value Meta value or values to filter by.
[168] Fix | Delete
* @type string $meta_compare MySQL operator used for comparing the meta value.
[169] Fix | Delete
* See WP_Meta_Query::__construct() for accepted values and default value.
[170] Fix | Delete
* @type string $meta_compare_key MySQL operator used for comparing the meta key.
[171] Fix | Delete
* See WP_Meta_Query::__construct() for accepted values and default value.
[172] Fix | Delete
* @type string $meta_type MySQL data type that the meta_value column will be CAST to for comparisons.
[173] Fix | Delete
* See WP_Meta_Query::__construct() for accepted values and default value.
[174] Fix | Delete
* @type string $meta_type_key MySQL data type that the meta_key column will be CAST to for comparisons.
[175] Fix | Delete
* See WP_Meta_Query::__construct() for accepted values and default value.
[176] Fix | Delete
* @type array $meta_query An associative array of WP_Meta_Query arguments.
[177] Fix | Delete
* See WP_Meta_Query::__construct() for accepted values.
[178] Fix | Delete
* @type int $number Maximum number of comments to retrieve.
[179] Fix | Delete
* Default empty (no limit).
[180] Fix | Delete
* @type int $paged When used with `$number`, defines the page of results to return.
[181] Fix | Delete
* When used with `$offset`, `$offset` takes precedence. Default 1.
[182] Fix | Delete
* @type int $offset Number of comments to offset the query. Used to build
[183] Fix | Delete
* LIMIT clause. Default 0.
[184] Fix | Delete
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
[185] Fix | Delete
* Default: true.
[186] Fix | Delete
* @type string|array $orderby Comment status or array of statuses. To use 'meta_value'
[187] Fix | Delete
* or 'meta_value_num', `$meta_key` must also be defined.
[188] Fix | Delete
* To sort by a specific `$meta_query` clause, use that
[189] Fix | Delete
* clause's array key. Accepts:
[190] Fix | Delete
* - 'comment_agent'
[191] Fix | Delete
* - 'comment_approved'
[192] Fix | Delete
* - 'comment_author'
[193] Fix | Delete
* - 'comment_author_email'
[194] Fix | Delete
* - 'comment_author_IP'
[195] Fix | Delete
* - 'comment_author_url'
[196] Fix | Delete
* - 'comment_content'
[197] Fix | Delete
* - 'comment_date'
[198] Fix | Delete
* - 'comment_date_gmt'
[199] Fix | Delete
* - 'comment_ID'
[200] Fix | Delete
* - 'comment_karma'
[201] Fix | Delete
* - 'comment_parent'
[202] Fix | Delete
* - 'comment_post_ID'
[203] Fix | Delete
* - 'comment_type'
[204] Fix | Delete
* - 'user_id'
[205] Fix | Delete
* - 'comment__in'
[206] Fix | Delete
* - 'meta_value'
[207] Fix | Delete
* - 'meta_value_num'
[208] Fix | Delete
* - The value of `$meta_key`
[209] Fix | Delete
* - The array keys of `$meta_query`
[210] Fix | Delete
* - false, an empty array, or 'none' to disable `ORDER BY` clause.
[211] Fix | Delete
* Default: 'comment_date_gmt'.
[212] Fix | Delete
* @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'.
[213] Fix | Delete
* Default: 'DESC'.
[214] Fix | Delete
* @type int $parent Parent ID of comment to retrieve children of.
[215] Fix | Delete
* Default empty.
[216] Fix | Delete
* @type int[] $parent__in Array of parent IDs of comments to retrieve children for.
[217] Fix | Delete
* Default empty.
[218] Fix | Delete
* @type int[] $parent__not_in Array of parent IDs of comments *not* to retrieve
[219] Fix | Delete
* children for. Default empty.
[220] Fix | Delete
* @type int[] $post_author__in Array of author IDs to retrieve comments for.
[221] Fix | Delete
* Default empty.
[222] Fix | Delete
* @type int[] $post_author__not_in Array of author IDs *not* to retrieve comments for.
[223] Fix | Delete
* Default empty.
[224] Fix | Delete
* @type int $post_id Limit results to those affiliated with a given post ID.
[225] Fix | Delete
* Default 0.
[226] Fix | Delete
* @type int[] $post__in Array of post IDs to include affiliated comments for.
[227] Fix | Delete
* Default empty.
[228] Fix | Delete
* @type int[] $post__not_in Array of post IDs to exclude affiliated comments for.
[229] Fix | Delete
* Default empty.
[230] Fix | Delete
* @type int $post_author Post author ID to limit results by. Default empty.
[231] Fix | Delete
* @type string|string[] $post_status Post status or array of post statuses to retrieve
[232] Fix | Delete
* affiliated comments for. Pass 'any' to match any value.
[233] Fix | Delete
* Default empty.
[234] Fix | Delete
* @type string|string[] $post_type Post type or array of post types to retrieve affiliated
[235] Fix | Delete
* comments for. Pass 'any' to match any value. Default empty.
[236] Fix | Delete
* @type string $post_name Post name to retrieve affiliated comments for.
[237] Fix | Delete
* Default empty.
[238] Fix | Delete
* @type int $post_parent Post parent ID to retrieve affiliated comments for.
[239] Fix | Delete
* Default empty.
[240] Fix | Delete
* @type string $search Search term(s) to retrieve matching comments for.
[241] Fix | Delete
* Default empty.
[242] Fix | Delete
* @type string|array $status Comment statuses to limit results by. Accepts an array
[243] Fix | Delete
* or space/comma-separated list of 'hold' (`comment_status=0`),
[244] Fix | Delete
* 'approve' (`comment_status=1`), 'all', or a custom
[245] Fix | Delete
* comment status. Default 'all'.
[246] Fix | Delete
* @type string|string[] $type Include comments of a given type, or array of types.
[247] Fix | Delete
* Accepts 'comment', 'pings' (includes 'pingback' and
[248] Fix | Delete
* 'trackback'), or any custom type string. Default empty.
[249] Fix | Delete
* @type string[] $type__in Include comments from a given array of comment types.
[250] Fix | Delete
* Default empty.
[251] Fix | Delete
* @type string[] $type__not_in Exclude comments from a given array of comment types.
[252] Fix | Delete
* Default empty.
[253] Fix | Delete
* @type int $user_id Include comments for a specific user ID. Default empty.
[254] Fix | Delete
* @type bool|string $hierarchical Whether to include comment descendants in the results.
[255] Fix | Delete
* - 'threaded' returns a tree, with each comment's children
[256] Fix | Delete
* stored in a `children` property on the `WP_Comment` object.
[257] Fix | Delete
* - 'flat' returns a flat array of found comments plus
[258] Fix | Delete
* their children.
[259] Fix | Delete
* - Boolean `false` leaves out descendants.
[260] Fix | Delete
* The parameter is ignored (forced to `false`) when
[261] Fix | Delete
* `$fields` is 'ids' or 'counts'. Accepts 'threaded',
[262] Fix | Delete
* 'flat', or false. Default: false.
[263] Fix | Delete
* @type string $cache_domain Unique cache key to be produced when this query is stored in
[264] Fix | Delete
* an object cache. Default is 'core'.
[265] Fix | Delete
* @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments.
[266] Fix | Delete
* Default true.
[267] Fix | Delete
* @type bool $update_comment_post_cache Whether to prime the cache for comment posts.
[268] Fix | Delete
* Default false.
[269] Fix | Delete
* }
[270] Fix | Delete
*/
[271] Fix | Delete
public function __construct( $query = '' ) {
[272] Fix | Delete
$this->query_var_defaults = array(
[273] Fix | Delete
'author_email' => '',
[274] Fix | Delete
'author_url' => '',
[275] Fix | Delete
'author__in' => '',
[276] Fix | Delete
'author__not_in' => '',
[277] Fix | Delete
'include_unapproved' => '',
[278] Fix | Delete
'fields' => '',
[279] Fix | Delete
'ID' => '',
[280] Fix | Delete
'comment__in' => '',
[281] Fix | Delete
'comment__not_in' => '',
[282] Fix | Delete
'karma' => '',
[283] Fix | Delete
'number' => '',
[284] Fix | Delete
'offset' => '',
[285] Fix | Delete
'no_found_rows' => true,
[286] Fix | Delete
'orderby' => '',
[287] Fix | Delete
'order' => 'DESC',
[288] Fix | Delete
'paged' => 1,
[289] Fix | Delete
'parent' => '',
[290] Fix | Delete
'parent__in' => '',
[291] Fix | Delete
'parent__not_in' => '',
[292] Fix | Delete
'post_author__in' => '',
[293] Fix | Delete
'post_author__not_in' => '',
[294] Fix | Delete
'post_ID' => '',
[295] Fix | Delete
'post_id' => 0,
[296] Fix | Delete
'post__in' => '',
[297] Fix | Delete
'post__not_in' => '',
[298] Fix | Delete
'post_author' => '',
[299] Fix | Delete
'post_name' => '',
[300] Fix | Delete
'post_parent' => '',
[301] Fix | Delete
'post_status' => '',
[302] Fix | Delete
'post_type' => '',
[303] Fix | Delete
'status' => 'all',
[304] Fix | Delete
'type' => '',
[305] Fix | Delete
'type__in' => '',
[306] Fix | Delete
'type__not_in' => '',
[307] Fix | Delete
'user_id' => '',
[308] Fix | Delete
'search' => '',
[309] Fix | Delete
'count' => false,
[310] Fix | Delete
'meta_key' => '',
[311] Fix | Delete
'meta_value' => '',
[312] Fix | Delete
'meta_query' => '',
[313] Fix | Delete
'date_query' => null, // See WP_Date_Query.
[314] Fix | Delete
'hierarchical' => false,
[315] Fix | Delete
'cache_domain' => 'core',
[316] Fix | Delete
'update_comment_meta_cache' => true,
[317] Fix | Delete
'update_comment_post_cache' => false,
[318] Fix | Delete
);
[319] Fix | Delete
[320] Fix | Delete
if ( ! empty( $query ) ) {
[321] Fix | Delete
$this->query( $query );
[322] Fix | Delete
}
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
/**
[326] Fix | Delete
* Parse arguments passed to the comment query with default query parameters.
[327] Fix | Delete
*
[328] Fix | Delete
* @since 4.2.0 Extracted from WP_Comment_Query::query().
[329] Fix | Delete
*
[330] Fix | Delete
* @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct() for accepted arguments.
[331] Fix | Delete
*/
[332] Fix | Delete
public function parse_query( $query = '' ) {
[333] Fix | Delete
if ( empty( $query ) ) {
[334] Fix | Delete
$query = $this->query_vars;
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
$this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
[338] Fix | Delete
[339] Fix | Delete
/**
[340] Fix | Delete
* Fires after the comment query vars have been parsed.
[341] Fix | Delete
*
[342] Fix | Delete
* @since 4.2.0
[343] Fix | Delete
*
[344] Fix | Delete
* @param WP_Comment_Query $query The WP_Comment_Query instance (passed by reference).
[345] Fix | Delete
*/
[346] Fix | Delete
do_action_ref_array( 'parse_comment_query', array( &$this ) );
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/**
[350] Fix | Delete
* Sets up the WordPress query for retrieving comments.
[351] Fix | Delete
*
[352] Fix | Delete
* @since 3.1.0
[353] Fix | Delete
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
[354] Fix | Delete
* 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
[355] Fix | Delete
* 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
[356] Fix | Delete
* arguments to $query_vars.
[357] Fix | Delete
* @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query().
[358] Fix | Delete
*
[359] Fix | Delete
* @param string|array $query Array or URL query string of parameters.
[360] Fix | Delete
* @return array|int List of comments, or number of comments when 'count' is passed as a query var.
[361] Fix | Delete
*/
[362] Fix | Delete
public function query( $query ) {
[363] Fix | Delete
$this->query_vars = wp_parse_args( $query );
[364] Fix | Delete
return $this->get_comments();
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* Get a list of comments matching the query vars.
[369] Fix | Delete
*
[370] Fix | Delete
* @since 4.2.0
[371] Fix | Delete
*
[372] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[373] Fix | Delete
*
[374] Fix | Delete
* @return int|int[]|WP_Comment[] List of comments or number of found comments if `$count` argument is true.
[375] Fix | Delete
*/
[376] Fix | Delete
public function get_comments() {
[377] Fix | Delete
global $wpdb;
[378] Fix | Delete
[379] Fix | Delete
$this->parse_query();
[380] Fix | Delete
[381] Fix | Delete
// Parse meta query.
[382] Fix | Delete
$this->meta_query = new WP_Meta_Query();
[383] Fix | Delete
$this->meta_query->parse_query_vars( $this->query_vars );
[384] Fix | Delete
[385] Fix | Delete
/**
[386] Fix | Delete
* Fires before comments are retrieved.
[387] Fix | Delete
*
[388] Fix | Delete
* @since 3.1.0
[389] Fix | Delete
*
[390] Fix | Delete
* @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference).
[391] Fix | Delete
*/
[392] Fix | Delete
do_action_ref_array( 'pre_get_comments', array( &$this ) );
[393] Fix | Delete
[394] Fix | Delete
// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
[395] Fix | Delete
$this->meta_query->parse_query_vars( $this->query_vars );
[396] Fix | Delete
if ( ! empty( $this->meta_query->queries ) ) {
[397] Fix | Delete
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
$comment_data = null;
[401] Fix | Delete
[402] Fix | Delete
/**
[403] Fix | Delete
* Filters the comments data before the query takes place.
[404] Fix | Delete
*
[405] Fix | Delete
* Return a non-null value to bypass WordPress' default comment queries.
[406] Fix | Delete
*
[407] Fix | Delete
* The expected return type from this filter depends on the value passed
[408] Fix | Delete
* in the request query vars:
[409] Fix | Delete
* - When `$this->query_vars['count']` is set, the filter should return
[410] Fix | Delete
* the comment count as an integer.
[411] Fix | Delete
* - When `'ids' === $this->query_vars['fields']`, the filter should return
[412] Fix | Delete
* an array of comment IDs.
[413] Fix | Delete
* - Otherwise the filter should return an array of WP_Comment objects.
[414] Fix | Delete
*
[415] Fix | Delete
* Note that if the filter returns an array of comment data, it will be assigned
[416] Fix | Delete
* to the `comments` property of the current WP_Comment_Query instance.
[417] Fix | Delete
*
[418] Fix | Delete
* Filtering functions that require pagination information are encouraged to set
[419] Fix | Delete
* the `found_comments` and `max_num_pages` properties of the WP_Comment_Query object,
[420] Fix | Delete
* passed to the filter by reference. If WP_Comment_Query does not perform a database
[421] Fix | Delete
* query, it will not have enough information to generate these values itself.
[422] Fix | Delete
*
[423] Fix | Delete
* @since 5.3.0
[424] Fix | Delete
* @since 5.6.0 The returned array of comment data is assigned to the `comments` property
[425] Fix | Delete
* of the current WP_Comment_Query instance.
[426] Fix | Delete
*
[427] Fix | Delete
* @param array|int|null $comment_data Return an array of comment data to short-circuit WP's comment query,
[428] Fix | Delete
* the comment count as an integer if `$this->query_vars['count']` is set,
[429] Fix | Delete
* or null to allow WP to run its normal queries.
[430] Fix | Delete
* @param WP_Comment_Query $query The WP_Comment_Query instance, passed by reference.
[431] Fix | Delete
*/
[432] Fix | Delete
$comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) );
[433] Fix | Delete
[434] Fix | Delete
if ( null !== $comment_data ) {
[435] Fix | Delete
if ( is_array( $comment_data ) && ! $this->query_vars['count'] ) {
[436] Fix | Delete
$this->comments = $comment_data;
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
return $comment_data;
[440] Fix | Delete
}
[441] Fix | Delete
[442] Fix | Delete
/*
[443] Fix | Delete
* Only use the args defined in the query_var_defaults to compute the key,
[444] Fix | Delete
* but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results.
[445] Fix | Delete
*/
[446] Fix | Delete
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
[447] Fix | Delete
unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
[448] Fix | Delete
[449] Fix | Delete
$key = md5( serialize( $_args ) );
[450] Fix | Delete
$last_changed = wp_cache_get_last_changed( 'comment' );
[451] Fix | Delete
[452] Fix | Delete
$cache_key = "get_comments:$key";
[453] Fix | Delete
$cache_value = wp_cache_get_salted( $cache_key, 'comment-queries', $last_changed );
[454] Fix | Delete
if ( false === $cache_value ) {
[455] Fix | Delete
$comment_ids = $this->get_comment_ids();
[456] Fix | Delete
if ( $comment_ids ) {
[457] Fix | Delete
$this->set_found_comments();
[458] Fix | Delete
}
[459] Fix | Delete
[460] Fix | Delete
$cache_value = array(
[461] Fix | Delete
'comment_ids' => $comment_ids,
[462] Fix | Delete
'found_comments' => $this->found_comments,
[463] Fix | Delete
);
[464] Fix | Delete
wp_cache_set_salted( $cache_key, $cache_value, 'comment-queries', $last_changed );
[465] Fix | Delete
} else {
[466] Fix | Delete
$comment_ids = $cache_value['comment_ids'];
[467] Fix | Delete
$this->found_comments = $cache_value['found_comments'];
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
if ( $this->found_comments && $this->query_vars['number'] ) {
[471] Fix | Delete
$this->max_num_pages = (int) ceil( $this->found_comments / $this->query_vars['number'] );
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
// If querying for a count only, there's nothing more to do.
[475] Fix | Delete
if ( $this->query_vars['count'] ) {
[476] Fix | Delete
// $comment_ids is actually a count in this case.
[477] Fix | Delete
return (int) $comment_ids;
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
$comment_ids = array_map( 'intval', $comment_ids );
[481] Fix | Delete
[482] Fix | Delete
if ( $this->query_vars['update_comment_meta_cache'] ) {
[483] Fix | Delete
wp_lazyload_comment_meta( $comment_ids );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
if ( 'ids' === $this->query_vars['fields'] ) {
[487] Fix | Delete
$this->comments = $comment_ids;
[488] Fix | Delete
return $this->comments;
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
_prime_comment_caches( $comment_ids, false );
[492] Fix | Delete
[493] Fix | Delete
// Fetch full comment objects from the primed cache.
[494] Fix | Delete
$_comments = array();
[495] Fix | Delete
foreach ( $comment_ids as $comment_id ) {
[496] Fix | Delete
$_comment = get_comment( $comment_id );
[497] Fix | Delete
if ( $_comment ) {
[498] Fix | Delete
$_comments[] = $_comment;
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function