getInputs(); $reservation_id = $inputs['reservation_id'] ?? '?'; $changes = []; if ( ! empty( $inputs['status'] ) ) { $changes[] = "status to \"{$inputs['status']}\""; } if ( ! empty( $inputs['date'] ) ) { $changes[] = "date to {$inputs['date']}"; } if ( ! empty( $inputs['start_time'] ) ) { $changes[] = "start time to {$inputs['start_time']}"; } if ( ! empty( $inputs['end_time'] ) ) { $changes[] = "end time to {$inputs['end_time']}"; } if ( ! empty( $inputs['total_guest'] ) ) { $changes[] = "guests to {$inputs['total_guest']}"; } if ( ! empty( $inputs['name'] ) ) { $changes[] = "name to \"{$inputs['name']}\""; } $summary = "Update reservation #{$reservation_id}"; if ( ! empty( $changes ) ) { $summary .= ': ' . implode( ', ', $changes ); } return $summary; } /** * Human-readable version of tool inputs for the approval card. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool * * @param int $reservation_id Reservation ID to update. * @param string|null $status New status. * @param string|null $name Updated name. * @param string|null $email Updated email. * @param string|null $phone Updated phone. * @param string|null $date New date (Y-m-d). * @param string|null $start_time New start time (HH:MM). * @param string|null $end_time New end time (HH:MM). * @param int|null $total_guest Updated guest count. * @param string|null $notes Updated notes. * @return array */ public function __invoke( int $reservation_id, ?string $status = null, ?string $name = null, ?string $email = null, ?string $phone = null, ?string $date = null, ?string $start_time = null, ?string $end_time = null, ?int $total_guest = null, ?string $notes = null ): array { $check = $this->guard( 'manage_woocommerce', 'manage_options', 'wpcafe_manage_reservations' ); if ( $check ) { return $check; } if ( $reservation_id <= 0 ) { return [ 'error' => 'reservation_id must be a positive integer.' ]; } try { $valid_statuses = [ 'pending', 'confirmed', 'cancelled' ]; if ( null !== $status && ! in_array( $status, $valid_statuses, true ) ) { return [ 'error' => "Invalid status '{$status}'. Accepted values: pending, confirmed, cancelled." ]; } if ( null !== $date ) { $parsed = \DateTime::createFromFormat( 'Y-m-d', $date ); if ( ! $parsed || $parsed->format( 'Y-m-d' ) !== $date ) { return [ 'error' => 'date must be in Y-m-d format.' ]; } } if ( null !== $start_time && ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $start_time ) ) { return [ 'error' => 'start_time must be a valid 24-hour time in HH:MM format (00:00-23:59).' ]; } if ( null !== $end_time && ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $end_time ) ) { return [ 'error' => 'end_time must be a valid 24-hour time in HH:MM format (00:00-23:59).' ]; } if ( null !== $total_guest && $total_guest < 1 ) { return [ 'error' => 'total_guest must be at least 1.' ]; } $body = []; if ( null !== $status ) { $body['status'] = $status; } if ( null !== $name ) { $body['name'] = sanitize_text_field( $name ); } if ( null !== $email ) { if ( ! is_email( $email ) ) { return [ 'error' => 'A valid customer email address is required.' ]; } $body['email'] = sanitize_email( $email ); } if ( null !== $phone ) { $body['phone'] = sanitize_text_field( $phone ); } if ( null !== $date ) { $body['date'] = $date; } if ( null !== $start_time ) { $body['start_time'] = $start_time; } if ( null !== $end_time ) { $body['end_time'] = $end_time; } if ( null !== $total_guest ) { $body['total_guest'] = $total_guest; } if ( null !== $notes ) { $body['notes'] = sanitize_textarea_field( $notes ); } if ( empty( $body ) ) { return [ 'error' => 'No fields provided to update.' ]; } $json = wp_json_encode( $body ); if ( false === $json ) { Log::error( 'Wpcafe_Update_Reservation_Tool JSON encoding failed', [ 'body_keys' => array_keys( $body ) ] ); return [ 'error' => 'Failed to encode request data. Ensure all input values are valid text.' ]; } $request = new \WP_REST_Request( 'PATCH', '/wpcafe/v2/reservations/' . $reservation_id ); $request->set_header( 'content-type', 'application/json' ); $request->set_url_params( [ 'id' => $reservation_id ] ); $request->set_body( $json ); $controller = new \WpCafe\Reservation\Controllers\Reservation_Controller(); $response = $controller->update_item( $request ); $data = $this->extract_response( $response, 'Failed to update reservation.' ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'reservation_id' => $reservation_id, 'updated_fields' => array_keys( $body ), 'message' => "Reservation #{$reservation_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_Update_Reservation_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update reservation: ' . $e->getMessage() ]; } } }