| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
|
| 7 |
class ExportHelper |
| 8 |
{ |
| 9 |
public static function mapBooking(Booking $booking): array |
| 10 |
{ |
| 11 |
$event = $booking->calendar_event; |
| 12 |
$host = $booking->user; |
| 13 |
|
| 14 |
return [ |
| 15 |
'id' => (int) $booking->id, |
| 16 |
'group_id' => (int) $booking->group_id, |
| 17 |
'event_type' => self::sanitizeCell($booking->event_type), |
| 18 |
'event_title' => self::sanitizeCell($event ? $event->title : ''), |
| 19 |
'host_name' => self::sanitizeCell($host ? $host->display_name : ''), |
| 20 |
'first_name' => self::sanitizeCell($booking->first_name), |
| 21 |
'last_name' => self::sanitizeCell($booking->last_name), |
| 22 |
'email' => self::sanitizeCell($booking->email), |
| 23 |
'phone' => self::sanitizeCell($booking->phone), |
| 24 |
'message' => self::compactText($booking->message), |
| 25 |
'internal_note' => self::compactText($booking->internal_note), |
| 26 |
'status' => self::sanitizeCell($booking->status), |
| 27 |
'cancelled' => $booking->status === 'cancelled' ? 'Yes' : 'No', |
| 28 |
'cancelled_by' => self::sanitizeCell($booking->getMeta('cancelled_by_type', 'host')), |
| 29 |
'cancellation_reason' => self::compactText($booking->getCancelReason(true)), |
| 30 |
'person_time_zone' => self::sanitizeCell($booking->person_time_zone), |
| 31 |
'start_time' => (string) $booking->start_time, |
| 32 |
'end_time' => (string) $booking->end_time, |
| 33 |
'duration' => (int) $booking->slot_minutes, |
| 34 |
'meeting_location' => self::compactText($booking->getLocationAsText()), |
| 35 |
'source' => self::sanitizeCell($booking->source), |
| 36 |
'source_url' => self::sanitizeCell($booking->source_url), |
| 37 |
'utm_source' => self::sanitizeCell($booking->utm_source), |
| 38 |
'utm_medium' => self::sanitizeCell($booking->utm_medium), |
| 39 |
'utm_campaign' => self::sanitizeCell($booking->utm_campaign), |
| 40 |
'utm_term' => self::sanitizeCell($booking->utm_term), |
| 41 |
'utm_content' => self::sanitizeCell($booking->utm_content), |
| 42 |
'created_at' => (string) $booking->created_at, |
| 43 |
'event_price' => $event ? $event->getEventPrice($booking->slot_minutes) : 0, |
| 44 |
'payment_status' => self::sanitizeCell($booking->payment_status), |
| 45 |
'payment_method' => self::sanitizeCell($booking->payment_method), |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
public static function sanitizeCell($value) |
| 50 |
{ |
| 51 |
if (empty($value) && $value !== '0' && $value !== 0) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
|
| 55 |
$value = (string) $value; |
| 56 |
$value = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $value); |
| 57 |
|
| 58 |
$check = ltrim($value, " \t\xC2\xA0"); |
| 59 |
$first = isset($check[0]) ? $check[0] : ''; |
| 60 |
if (in_array($first, ['=', '+', '-', '@'], true)) { |
| 61 |
$value = "'" . $value; |
| 62 |
} |
| 63 |
|
| 64 |
return $value; |
| 65 |
} |
| 66 |
|
| 67 |
public static function compactText($value) |
| 68 |
{ |
| 69 |
if (empty($value) && $value !== '0' && $value !== 0) { |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
$value = wp_strip_all_tags((string) $value); |
| 74 |
$value = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'); |
| 75 |
$value = str_replace(["\r\n", "\r", "\n", "\t"], ' ', $value); |
| 76 |
$value = preg_replace('/\s+/', ' ', $value); |
| 77 |
$value = trim($value); |
| 78 |
|
| 79 |
return self::sanitizeCell($value); |
| 80 |
} |
| 81 |
} |
| 82 |
|