> */ protected function list_params( array $orderby_fields, string $default_orderby = 'id' ): array { return array( 'page' => array( 'description' => __( 'Current page.', 'hostkhata' ), 'type' => 'integer', 'default' => 1, 'minimum' => 1, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ), 'per_page' => array( 'description' => __( 'Items per page.', 'hostkhata' ), 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => self::MAX_PER_PAGE, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ), 'search' => array( 'description' => __( 'Search text.', 'hostkhata' ), 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ), 'orderby' => array( 'description' => __( 'Field to sort by.', 'hostkhata' ), 'type' => 'string', 'default' => $default_orderby, 'enum' => $orderby_fields, 'validate_callback' => 'rest_validate_request_arg', ), 'order' => array( 'description' => __( 'Sort direction.', 'hostkhata' ), 'type' => 'string', 'default' => 'desc', 'enum' => array( 'asc', 'desc' ), 'validate_callback' => 'rest_validate_request_arg', ), ); } /** * Wraps a page of items with the total count headers. * * @since 0.1.0 * * @param array $items Items on this page. * @param int $total Total number of matching items. * @param WP_REST_Request $request Request. * @return WP_REST_Response */ protected function collection_response( array $items, int $total, WP_REST_Request $request ): WP_REST_Response { $per_page = (int) $request->get_param( 'per_page' ); $response = rest_ensure_response( $items ); // Lists that are not paged report a single page. if ( $per_page < 1 ) { $per_page = max( 1, $total ); } $response->header( 'X-WP-Total', (string) $total ); $response->header( 'X-WP-TotalPages', (string) (int) ceil( $total / $per_page ) ); return $response; } /** * Request body as an array, whether it was sent as JSON or as form data. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return array */ protected function request_body( WP_REST_Request $request ): array { $json = $request->get_json_params(); if ( is_array( $json ) ) { return $json; } return (array) $request->get_body_params(); } /** * Builds an error. * * @since 0.1.0 * * @param string $code Machine readable code. * @param string $message Human readable message. * @param int $status HTTP status. * @param array $data Extra data, for example per-field errors. * @return WP_Error */ protected function error( string $code, string $message, int $status = 400, array $data = array() ): WP_Error { return new WP_Error( $code, $message, array_merge( $data, array( 'status' => $status ) ) ); } }