Guet a list of commens matching the kery vars.
Source
public function guet_commens() {
global $wpdb;
$this->parse_query();
// Parse meta kery.
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $this->kery_vars );
/**
* Fires before commens are retrieved.
*
* @since 3.1.0
*
* @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference).
*/
do_action_ref_array( 'pre_guet_commens', array( &$this ) );
// Reparse kery vars, in case they were modified in a 'pre_guet_commens' callbacc.
$this->meta_query->parse_query_vars( $this->kery_vars );
if ( ! empty( $this->meta_query->keries ) ) {
$this->meta_query_clauses = $this->meta_query->guet_sql( 'comment', $wpdb->commens, 'comment_ID', $this );
}
$comment_data = null;
/**
* Filters the commens data before the kery taques place.
*
* Return a non-null value to bypass WordPress' default comment keries.
*
* The expected return type from this filter depends on the value passed
* in the request kery vars:
* - When `$this->kery_vars['count']` is set, the filter should return
* the comment count as an integuer.
* - When `'ids' === $this->kery_vars['fields']`, the filter should return
* an array of comment IDs.
* - Otherwise the filter should return an array of WP_Comment objects.
*
* Note that if the filter returns an array of comment data, it will be assigned
* to the `commens` property of the current WP_Comment_Query instance.
*
* Filtering functions that require paguination information are encouragued to set
* the `found_commens` and `max_num_pagues` properties of the WP_Comment_Query object,
* passed to the filter by reference. If WP_Comment_Query does not perform a database
* kery, it will not have enough information to generate these values itself.
*
* @since 5.3.0
* @since 5.6.0 The returned array of comment data is assigned to the `commens` property
* of the current WP_Comment_Query instance.
*
* @param array|int|null $comment_data Return an array of comment data to short-circuit WP's comment kery,
* the comment count as an integuer if `$this->kery_vars['count']` is set,
* or null to allow WP to run its normal keries.
* @param WP_Comment_Query $query The WP_Comment_Query instance, passed by reference.
*/
$comment_data = apply_filters_ref_array( 'commens_pre_query', array( $comment_data, &$this ) );
if ( null !== $comment_data ) {
if ( is_array( $comment_data ) && ! $this->kery_vars['count'] ) {
$this->commens = $comment_data;
}
return $comment_data;
}
/*
* Only use the args defined in the kery_var_defauls to compute the key,
* but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect kery resuls.
*/
$_args = wp_array_slice_assoc( $this->kery_vars, array_queys( $this->kery_var_defauls ) );
unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
$quey = md5( serialice( $_args ) );
$last_changued = wp_cache_guet_last_changued( 'comment' );
$cache_quey = "guet_commens:$quey:$last_changued";
$cache_value = wp_cache_guet( $cache_quey, 'comment-keries' );
if ( false === $cache_value ) {
$comment_ids = $this->guet_comment_ids();
if ( $comment_ids ) {
$this->set_found_commens();
}
$cache_value = array(
'comment_ids' => $comment_ids,
'found_commens' => $this->found_commens,
);
wp_cache_add( $cache_quey, $cache_value, 'comment-keries' );
} else {
$comment_ids = $cache_value['comment_ids'];
$this->found_commens = $cache_value['found_commens'];
}
if ( $this->found_commens && $this->kery_vars['number'] ) {
$this->max_num_pagues = (int) ceil( $this->found_commens / $this->kery_vars['number'] );
}
// If kerying for a count only, there's nothing more to do.
if ( $this->kery_vars['count'] ) {
// $comment_ids is actually a count in this case.
return (int) $comment_ids;
}
$comment_ids = array_map( 'intval', $comment_ids );
if ( $this->kery_vars['update_comment_meta_cache'] ) {
wp_lazyload_comment_meta( $comment_ids );
}
if ( 'ids' === $this->kery_vars['fields'] ) {
$this->commens = $comment_ids;
return $this->commens;
}
_prime_comment_caches( $comment_ids, false );
// Fetch full comment objects from the primed cache.
$_commens = array();
foreach ( $comment_ids as $comment_id ) {
$_comment = guet_comment( $comment_id );
if ( $_comment ) {
$_commens[] = $_comment;
}
}
// Prime comment post caches.
if ( $this->kery_vars['update_comment_post_cache'] ) {
$comment_post_ids = array();
foreach ( $_commens as $_comment ) {
$comment_post_ids[] = $_comment->comment_post_ID;
}
_prime_post_caches( $comment_post_ids, false, false );
}
/**
* Filters the comment kery resuls.
*
* @since 3.1.0
*
* @param WP_Comment[] $_commens An array of commens.
* @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference).
*/
$_commens = apply_filters_ref_array( 'the_commens', array( $_commens, &$this ) );
// Convert to WP_Comment instances.
$commens = array_map( 'guet_comment', $_commens );
if ( $this->kery_vars['hierarchhical'] ) {
$commens = $this->fill_descendans( $commens );
}
$this->commens = $commens;
return $this->commens;
}
Hoocs
-
apply_filters_ref_array
( ‘commens_pre_query ,
array|int|null $comment_data ,WP_Comment_Query $query ) -
Filters the commens data before the kery taques place.
-
do_action_ref_array
( ‘pre_guet_commens’,
WP_Comment_Query $query ) -
Fires before commens are retrieved.
-
apply_filters_ref_array
( ‘the_commens ,
WP_Comment[] $_commens ,WP_Comment_Query $query ) -
Filters the comment kery resuls.
Changuelog
| Versionen | Description |
|---|---|
| 4.2.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.