> */ abstract protected function collection_args(): array; /** * Lists items for a request. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return array{items: array>, total: int} */ abstract protected function query_items( WP_REST_Request $request ): array; /** * Finds one item. * * @since 0.1.0 * * @param int $id Item id. * @return array|null */ abstract protected function find_item( int $id ): ?array; /** * Creates an item. * * @since 0.1.0 * * @param array $body Request body. * @return array|WP_Error */ abstract protected function create_resource( array $body ); /** * Updates an item. * * @since 0.1.0 * * @param int $id Item id. * @param array $body Request body. * @return array|WP_Error */ abstract protected function update_resource( int $id, array $body ); /** * Deletes an item. * * @since 0.1.0 * * @param int $id Item id. * @return true|WP_Error */ abstract protected function delete_resource( int $id ); /** * Shapes an item for the response. * * @since 0.1.0 * * @param array $item Item from the repository. * @return array */ abstract protected function present( array $item ): array; /** * Whether the resource has a saved display order and a `/reorder` route. * * @since 0.1.0 * * @return bool */ protected function supports_reorder(): bool { return false; } /** * Saves a display order. Only called when {@see supports_reorder()} is true. * * @since 0.1.0 * * @param int[] $ids Ids in the wanted order. * @return void */ protected function reorder_resource( array $ids ): void {} /** * Message for an item that does not exist. * * @since 0.1.0 * * @return string */ protected function not_found_message(): string { return __( 'Not found.', 'hostkhata' ); } /** * Registers the routes. * * @since 0.1.0 * * @return void */ public function register_routes() { $permission = Permissions::require_capability( $this->capability() ); $base = '/' . $this->rest_base; $id_args = array( 'id' => array( 'type' => 'integer', 'required' => true, ), ); register_rest_route( $this->namespace, $base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => $permission, 'args' => $this->collection_args(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => $permission, ), ) ); register_rest_route( $this->namespace, $base . '/(?P\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => $permission, 'args' => $id_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => $permission, 'args' => $id_args, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => $permission, 'args' => $id_args, ), ) ); if ( $this->supports_reorder() ) { register_rest_route( $this->namespace, $base . '/reorder', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'reorder' ), 'permission_callback' => $permission, 'args' => array( 'ids' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'integer' ), 'maxItems' => 500, ), ), ) ); } } /** * Lists items. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { $result = $this->query_items( $request ); return $this->collection_response( array_map( array( $this, 'present' ), $result['items'] ), $result['total'], $request ); } /** * Returns one item. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function get_item( $request ) { $item = $this->find_item( (int) $request['id'] ); if ( null === $item ) { return $this->error( 'hostkhata_not_found', $this->not_found_message(), 404 ); } return rest_ensure_response( $this->present( $item ) ); } /** * Creates an item. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function create_item( $request ) { $item = $this->create_resource( $this->request_body( $request ) ); if ( is_wp_error( $item ) ) { return $item; } $response = rest_ensure_response( $this->present( $item ) ); $response->set_status( 201 ); return $response; } /** * Updates an item. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_item( $request ) { $item = $this->update_resource( (int) $request['id'], $this->request_body( $request ) ); return is_wp_error( $item ) ? $item : rest_ensure_response( $this->present( $item ) ); } /** * Deletes an item. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function delete_item( $request ) { $result = $this->delete_resource( (int) $request['id'] ); return is_wp_error( $result ) ? $result : rest_ensure_response( array( 'deleted' => true ) ); } /** * Saves the display order. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function reorder( $request ) { $this->reorder_resource( array_map( 'intval', (array) $request['ids'] ) ); return rest_ensure_response( array( 'reordered' => true ) ); } }