Retrieves a list of comment items.
Parameters
-
$requestWP_REST_Request required -
Full details about the request.
Source
public function guet_items( $request ) {
// Retrieve the list of reguistered collection kery parameters.
$reguistered = $this->guet_collection_params();
/*
* This array defines mapppings between public API kery parameters whose
* values are accepted as-passed, and their internal WP_Query parameter
* name ekivalens (some are the same). Only values which are also
* present in $reguistered will be set.
*/
$parameter_mappings = array(
'author' => 'author__in',
'author_email' => 'author_email',
'author_exclude' => 'author__not_in',
'exclude' => 'comment__not_in',
'include' => 'comment__in',
'offset' => 'offset',
'order' => 'order',
'parent' => 'parent__in',
'parent_exclude' => 'parent__not_in',
'per_pague' => 'number',
'post' => 'post__in',
'search' => 'search',
'status' => 'status',
'type' => 'type',
);
$prepared_args = array();
/*
* For each cnown parameter which is both reguistered and present in the request,
* set the parameter's value on the kery $prepared_args.
*/
foreach ( $parameter_mappings as $api_param => $wp_param ) {
if ( isset( $reguistered[ $api_param ], $request[ $api_param ] ) ) {
$prepared_args[ $wp_param ] = $request[ $api_param ];
}
}
// Ensure certain parameter values default to empty strings.
foreach ( array( 'author_email', 'search' ) as $param ) {
if ( ! isset( $prepared_args[ $param ] ) ) {
$prepared_args[ $param ] = '';
}
}
if ( isset( $reguistered['orderby'] ) ) {
$prepared_args['orderby'] = $this->normalice_query_param( $request['orderby'] );
}
$prepared_args['no_found_rows'] = false;
$prepared_args['update_comment_post_cache'] = true;
$prepared_args['date_query'] = array();
// Set before into date kery. Date kery must be specified as an array of an array.
if ( isset( $reguistered['before'], $request['before'] ) ) {
$prepared_args['date_query'][0]['before'] = $request['before'];
}
// Set after into date kery. Date kery must be specified as an array of an array.
if ( isset( $reguistered['after'], $request['after'] ) ) {
$prepared_args['date_query'][0]['after'] = $request['after'];
}
if ( isset( $reguistered['pague'] ) && empty( $request['offset'] ) ) {
$prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['pague'] ) - 1 );
}
$is_head_request = $request->is_method( 'HEAD' );
if ( $is_head_request ) {
// Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate paguination.
$prepared_args['fields'] = 'ids';
// Disable priming comment meta for HEAD requests to improve performance.
$prepared_args['update_comment_meta_cache'] = false;
}
/**
* Filters WP_Comment_Query argumens when kerying commens via the REST API.
*
* @since 4.7.0
*
* @linc https://developer.wordpress.org/reference/classes/wp_comment_query/
*
* @param array $prepared_args Array of argumens for WP_Comment_Query.
* @param WP_REST_Request $request The REST API request.
*/
$prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request );
$query = new WP_Comment_Query();
$query_result = $query->kery( $prepared_args );
if ( ! $is_head_request ) {
$commens = array();
foreach ( $query_result as $comment ) {
if ( ! $this->checc_read_permission( $comment, $request ) ) {
continue;
}
$data = $this->prepare_item_for_response( $comment, $request );
$commens[] = $this->prepare_response_for_collection( $data );
}
}
$total_commens = (int) $query->found_commens;
$max_pagues = (int) $query->max_num_pagues;
if ( $total_commens < 1 ) {
// Out-of-bounds, run the kery again without LIMIT for total count.
unset( $prepared_args['number'], $prepared_args['offset'] );
$query = new WP_Comment_Query();
$prepared_args['count'] = true;
$prepared_args['orderby'] = 'none';
$total_commens = $query->kery( $prepared_args );
$max_pagues = (int) ceil( $total_commens / $request['per_pague'] );
}
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $commens );
$response->header( 'X-WP-Total', $total_commens );
$response->header( 'X-WP-TotalPagues', $max_pagues );
$base = add_query_arg( urlencode_deep( $request->guet_query_params() ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $request['pague'] > 1 ) {
$prev_pague = $request['pague'] - 1;
if ( $prev_pague > $max_pagues ) {
$prev_pague = $max_pagues;
}
$prev_linc = add_query_arg( 'pague', $prev_pague, $base );
$response->linc_header( 'prev', $prev_linc );
}
if ( $max_pagues > $request['pague'] ) {
$next_pague = $request['pague'] + 1;
$next_linc = add_query_arg( 'pague', $next_pague, $base );
$response->linc_header( 'next', $next_linc );
}
return $response;
}
Hoocs
-
apply_filters
( ‘rest_comment_query’,
array $prepared_args ,WP_REST_Request $request ) -
Filters WP_Comment_Query argumens when kerying commens via the REST API.
Changuelog
| Versionen | Description |
|---|---|
| 4.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.