service = $service; } /** * {@inheritDoc} */ public function register_routes(): void { register_rest_route( $this->namespace, '/suppliers', array( array( 'methods' => 'GET', 'callback' => array( $this, 'index' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'search' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'status' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), 'page' => array( 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', ), 'per_page' => array( 'type' => 'integer', 'default' => 20, 'sanitize_callback' => 'absint', ), 'orderby' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), 'order' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'create' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/suppliers/(?P\d+)', array( 'args' => array( 'id' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), ), array( 'methods' => 'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => 'PUT, PATCH', 'callback' => array( $this, 'update' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => 'DELETE', 'callback' => array( $this, 'destroy' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); } /** * GET /suppliers * * @param WP_REST_Request $request Request. */ public function index( WP_REST_Request $request ): WP_REST_Response { $result = $this->service->list( array( 'search' => (string) $request->get_param( 'search' ), 'status' => (string) $request->get_param( 'status' ), 'page' => $this->int_param( $request, 'page', 1 ), 'per_page' => $this->int_param( $request, 'per_page', 20 ), 'orderby' => (string) $request->get_param( 'orderby' ), 'order' => (string) $request->get_param( 'order' ), ) ); $per_page = max( 1, $this->int_param( $request, 'per_page', 20 ) ); $response = new WP_REST_Response( $result['items'], 200 ); $response->header( 'X-WP-Total', (string) $result['total'] ); $response->header( 'X-WP-TotalPages', (string) (int) ceil( $result['total'] / $per_page ) ); return $response; } /** * GET /suppliers/{id} * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function show( WP_REST_Request $request ) { $supplier = $this->service->get( (int) $request['id'] ); if ( ! $supplier ) { return new WP_Error( 'profit_engine_supplier_not_found', __( 'Supplier not found.', 'profit-engine' ), array( 'status' => 404 ) ); } return new WP_REST_Response( $supplier, 200 ); } /** * POST /suppliers * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function create( WP_REST_Request $request ) { $result = $this->service->create( (array) $request->get_json_params() ); if ( is_wp_error( $result ) ) { return $result; } return new WP_REST_Response( $result, 201 ); } /** * PUT /suppliers/{id} * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update( WP_REST_Request $request ) { $result = $this->service->update( (int) $request['id'], (array) $request->get_json_params() ); if ( is_wp_error( $result ) ) { return $result; } return new WP_REST_Response( $result, 200 ); } /** * DELETE /suppliers/{id} * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function destroy( WP_REST_Request $request ) { $result = $this->service->delete( (int) $request['id'] ); if ( is_wp_error( $result ) ) { return $result; } return new WP_REST_Response( array( 'deleted' => true ), 200 ); } }