| @@ -102,17 +102,30 @@ | ||
| 102 | 102 | * |
| 103 | 103 | * @return []|array|bool|WP_Error |
| 104 | 104 | */ |
| 105 | 105 | private function prepare_item_to_save( array $data ) { |
| 106 | - $validated = booktics_validate( | |
| 107 | - $data, array( | |
| 108 | - 'date' => array( 'required' ), | |
| 109 | - 'start_time' => array( 'required' ), | |
| 110 | - 'service_id' => array( 'required' ), | |
| 111 | - 'team_member_id' => array( 'required' ), | |
| 112 | - ) | |
| 106 | + $item_rules = array( | |
| 107 | + 'date' => array( 'required' ), | |
| 108 | + 'start_time' => array( 'required' ), | |
| 109 | + 'service_id' => array( 'required' ), | |
| 110 | + 'team_member_id' => array( 'required' ), | |
| 113 | 111 | ); |
| 114 | 112 | |
| 113 | + /** | |
| 114 | + * Filters the validation rules applied to a cart item. | |
| 115 | + * | |
| 116 | + * Extensions that assign a team member server-side (Booktics Pro | |
| 117 | + * round robin) drop the team_member_id requirement here. | |
| 118 | + * | |
| 119 | + * @since 1.0.26 | |
| 120 | + * | |
| 121 | + * @param array $item_rules Validation rules. | |
| 122 | + * @param array $data The cart item being validated. | |
| 123 | + */ | |
| 124 | + $item_rules = apply_filters( 'booktics_cart_item_validation_rules', $item_rules, $data ); | |
| 125 | + | |
| 126 | + $validated = booktics_validate( $data, $item_rules ); | |
| 127 | + | |
| 115 | 128 | if ( is_wp_error( $validated ) ) { |
| 116 | 129 | return $validated; |
| 117 | 130 | } |
| 118 | 131 | |
| @@ -164,9 +177,9 @@ | ||
| 164 | 177 | * |
| 165 | 178 | * @return array|\WP_HTTP_Response |
| 166 | 179 | */ |
| 167 | 180 | public function delete_from_cart( $request ) { |
| 168 | - $uuid = $request->get_param( 'uuid' ); | |
| 181 | + $uuid = sanitize_text_field( $request->get_param( 'uuid' ) ); | |
| 169 | 182 | |
| 170 | 183 | if ( empty( $uuid ) ) { |
| 171 | 184 | return $this->error( |
| 172 | 185 | __( 'UUID is required', 'booktics' ), |
| @@ -245,11 +258,16 @@ | ||
| 245 | 258 | if ( ! $service ) { |
| 246 | 259 | $error->add( 'invalid_service', __( 'Invalid service selected', 'booktics' ) ); |
| 247 | 260 | } |
| 248 | 261 | |
| 249 | - $team_member = ( new Team_Member_Model() )->find( $data['team_member_id'] ); | |
| 250 | - if ( empty( $team_member ) ) { | |
| 251 | - $error->add( 'invalid_team_member', __( 'Invalid team member selected', 'booktics' ) ); | |
| 262 | + // A cart item may legitimately arrive without a team member when the | |
| 263 | + // service assigns one server-side. Only verify a member that was | |
| 264 | + // actually supplied; the booking is refused later if none can be found. | |
| 265 | + if ( ! empty( $data['team_member_id'] ) ) { | |
| 266 | + $team_member = ( new Team_Member_Model() )->find( $data['team_member_id'] ); | |
| 267 | + if ( empty( $team_member ) ) { | |
| 268 | + $error->add( 'invalid_team_member', __( 'Invalid team member selected', 'booktics' ) ); | |
| 269 | + } | |
| 252 | 270 | } |
| 253 | 271 | |
| 254 | 272 | $service_price = 0; |
| 255 | 273 | $addition_durations = $service->additional_durations; |
| @@ -274,9 +292,9 @@ | ||
| 274 | 292 | * |
| 275 | 293 | * @return array|\WP_HTTP_Response |
| 276 | 294 | */ |
| 277 | 295 | public function edit_cart_item( $request ) { |
| 278 | - $uuid = $request->get_param( 'uuid' ); | |
| 296 | + $uuid = sanitize_text_field( (string) $request->get_param( 'uuid' ) ); | |
| 279 | 297 | $params = json_decode( $request->get_body(), true ); |
| 280 | 298 | |
| 281 | 299 | if ( empty( $uuid ) ) { |
| 282 | 300 | return $this->error( |
| @@ -298,8 +316,12 @@ | ||
| 298 | 316 | |
| 299 | 317 | // Add UUID to validated data |
| 300 | 318 | $validated['uuid'] = $uuid; |
| 301 | 319 | |
| 320 | + // Calculate end time from start time and total duration | |
| 321 | + $total_duration_min = $this->calculate_total_duration( $validated ); | |
| 322 | + $validated['end_time'] = $this->get_endtime_from_starttime( $validated['start_time'], $total_duration_min ); | |
| 323 | + | |
| 302 | 324 | // Update the cart item |
| 303 | 325 | $updated_item = $this->cart_manager->edit_cart_item( $uuid, $validated ); |
| 304 | 326 | |
| 305 | 327 | if ( empty( $updated_item ) ) { |
| @@ -399,7 +421,7 @@ | ||
| 399 | 421 | private function get_endtime_from_starttime( $start_time, $total_duration_min ) { |
| 400 | 422 | $start_time = strtotime( $start_time ); |
| 401 | 423 | $end_time = strtotime( '+' . $total_duration_min . ' minutes', $start_time ); |
| 402 | 424 | |
| 403 | - return date( 'g:i A', $end_time ); | |
| 425 | + return gmdate( 'H:i', $end_time ); | |
| 404 | 426 | } |
| 405 | 427 | } |