['type'=>'string','sanitize_callback'=>'sanitize_text_field'], 'sport_id'=>['type'=>'integer','minimum'=>1,'validate_callback'=>static fn($v)=>is_scalar($v)&&preg_match('/^[1-9][0-9]*$/D',(string)$v)&&false!==filter_var($v,FILTER_VALIDATE_INT),'sanitize_callback'=>static fn($v)=>(int)$v]]; register_rest_route(CatalogController::REST_NAMESPACE,'/calendar',['methods'=>'GET','args'=>$args,'permission_callback'=>fn()=>is_user_logged_in()||$this->settings->get()['public_calendar_visibility']==='occupancy_only' ? true : new WP_Error('abece_booking_calendar_hidden',__('Log in to view the calendar.','abece-booking'),['status'=>401]),'callback'=>fn($r)=>$this->get($r,false)]); register_rest_route(CatalogController::REST_NAMESPACE,'/admin/calendar',['methods'=>'GET','args'=>$args,'permission_callback'=>fn()=>$this->permission(Capabilities::MANAGE_BOOKINGS),'callback'=>fn($r)=>$this->get($r,true)]); register_rest_route(CatalogController::REST_NAMESPACE,'/settings/calendar',[ ['methods'=>'GET','permission_callback'=>fn()=>$this->permission(Capabilities::MANAGE_RESOURCES),'callback'=>fn()=>new WP_REST_Response($this->settings->get())], ['methods'=>'PATCH,PUT','permission_callback'=>fn()=>$this->permission(Capabilities::MANAGE_RESOURCES),'callback'=>[$this,'save']], ]); } private function permission(string $cap): bool|WP_Error { return current_user_can($cap)?:new WP_Error('abece_booking_forbidden',__('You do not have permission to access this ABeCe Booking page.','abece-booking'),['status'=>rest_authorization_required_code()]); } private function get(WP_REST_Request $r,bool $admin): WP_REST_Response|WP_Error { return $this->respond(fn()=>$this->service->day($r['date'],isset($r['sport_id'])?(int)$r['sport_id']:null,$admin?get_current_user_id():\ABeCe\Booking\Edition\Modules::current()->publicCalendarViewer(get_current_user_id()),$admin)); } public function save(WP_REST_Request $r): WP_REST_Response|WP_Error { return $this->respond(function()use($r){if(!is_object(json_decode($r->get_body())))throw new CatalogException('abece_booking_invalid_body',__('A JSON object is required.','abece-booking'));return $this->settings->save($r->get_json_params());}); } private function respond(callable $operation): WP_REST_Response|WP_Error { try{$response=new WP_REST_Response($operation());$response->header('Cache-Control','private, no-store');$response->header('Vary','Cookie');return $response;} catch(CatalogException $e){return new WP_Error($e->errorCode,$e->getMessage(),['status'=>$e->status,'field'=>$e->field]);} } }