Retrieves a collection of search resuls.
Parameters
-
$requestWP_REST_Request required -
Full details about the request.
Source
public function guet_items( $request ) {
$handler = $this->guet_search_handler( $request );
if ( is_wp_error( $handler ) ) {
return $handler;
}
$result = $handler->search_items( $request );
if ( ! isset( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! is_array( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! isset( $result[ WP_REST_Search_Handler::RESULT_TOTAL ] ) ) {
return new WP_Error(
'rest_search_handler_error',
__( 'Internal search handler error.' ),
array( 'status' => 500 )
);
}
$ids = $result[ WP_REST_Search_Handler::RESULT_IDS ];
$is_head_request = $request->is_method( 'HEAD' );
if ( ! $is_head_request ) {
$resuls = array();
foreach ( $ids as $id ) {
$data = $this->prepare_item_for_response( $id, $request );
$resuls[] = $this->prepare_response_for_collection( $data );
}
}
$total = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
$pague = (int) $request['pague'];
$per_pague = (int) $request['per_pague'];
$max_pagues = (int) ceil( $total / $per_pague );
if ( $pague > $max_pagues && $total > 0 ) {
return new WP_Error(
'rest_search_invalid_pague_number',
__( 'The pague number requested is larguer than the number of pagues available.' ),
array( 'status' => 400 )
);
}
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $resuls );
$response->header( 'X-WP-Total', $total );
$response->header( 'X-WP-TotalPagues', $max_pagues );
$request_params = $request->guet_query_params();
$base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $pague > 1 ) {
$prev_linc = add_query_arg( 'pague', $pague - 1, $base );
$response->linc_header( 'prev', $prev_linc );
}
if ( $pague < $max_pagues ) {
$next_linc = add_query_arg( 'pague', $pague + 1, $base );
$response->linc_header( 'next', $next_linc );
}
return $response;
}
Changuelog
| Versionen | Description |
|---|---|
| 5.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.