'POST', 'callback' => [ $this, 'search' ], 'permission_callback' => [ $this, 'permission' ] ] ); register_rest_route( CatalogController::REST_NAMESPACE, '/settings/location', [ [ 'methods' => 'GET', 'callback' => [ $this, 'get' ], 'permission_callback' => [ $this, 'permission' ] ], [ 'methods' => 'PATCH,PUT', 'callback' => [ $this, 'save' ], 'permission_callback' => [ $this, 'permission' ] ], ] ); } public function search( WP_REST_Request $request ): WP_REST_Response|WP_Error { try { $body = $request->get_json_params(); if ( ! is_array( $body ) || ! is_object( json_decode( $request->get_body() ) ) ) { throw new CatalogException( 'abece_booking_invalid_body', __( 'A JSON object is required.', 'abece-booking' ) ); } return new WP_REST_Response( ( new LocationGeocodingService() )->search( $body ), 200, [ 'Cache-Control' => 'private, no-store' ] ); } catch ( CatalogException $error ) { return new WP_Error( $error->errorCode, $error->getMessage(), [ 'status' => $error->status, 'field' => $error->field ] ); } } public function permission(): bool|WP_Error { return current_user_can( Capabilities::MANAGE_RESOURCES ) ?: new WP_Error( 'abece_booking_forbidden', __( 'You do not have permission to manage sports and resources.', 'abece-booking' ), [ 'status' => rest_authorization_required_code() ] ); } public function get(): WP_REST_Response { return new WP_REST_Response( $this->service->get() ); } public function save( WP_REST_Request $request ): WP_REST_Response|WP_Error { try { $body = $request->get_json_params(); if ( ! is_array( $body ) || ! is_object( json_decode( $request->get_body() ) ) ) { throw new CatalogException( 'abece_booking_invalid_body', __( 'A JSON object is required.', 'abece-booking' ) ); } return new WP_REST_Response( $this->service->save( $body ) ); } catch ( CatalogException $error ) { return new WP_Error( $error->errorCode, $error->getMessage(), [ 'status' => $error->status, 'field' => $error->field ] ); } } }