| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
|
| 7 |
class SanitizeService |
| 8 |
{ |
| 9 |
public static function weeklySchedules($schedules, $fromTimeZone = '', $toTimeZone = false, $fromUser = true) |
| 10 |
{ |
| 11 |
$dstTimeZone = $fromTimeZone == 'UTC' ? $toTimeZone : $fromTimeZone; |
| 12 |
|
| 13 |
$dateWithoutDST = DateTimeHelper::getDateWithoutDST($dstTimeZone); |
| 14 |
|
| 15 |
foreach ($schedules as $day => &$schedule) { |
| 16 |
$schedule['enabled'] = Arr::isTrue($schedule, 'enabled'); |
| 17 |
if (!$schedule['enabled'] || empty($schedule['slots'])) { |
| 18 |
$schedule['slots'] = []; |
| 19 |
$schedule['enabled'] = false; |
| 20 |
continue; |
| 21 |
} |
| 22 |
|
| 23 |
$schedule['enabled'] = true; |
| 24 |
|
| 25 |
foreach ($schedule['slots'] as $index => $slot) { |
| 26 |
if ($fromUser) { |
| 27 |
$slot['start'] = sanitize_text_field($slot['start']); |
| 28 |
$slot['end'] = sanitize_text_field($slot['end']); |
| 29 |
} |
| 30 |
|
| 31 |
if (!$slot['start'] || !$slot['end']) { |
| 32 |
unset($schedule['slots'][$index]); |
| 33 |
continue; |
| 34 |
} |
| 35 |
|
| 36 |
if ($toTimeZone && $fromTimeZone) { |
| 37 |
$slot['start'] = DateTimeHelper::convertToTimeZone($slot['start'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 38 |
$slot['end'] = DateTimeHelper::convertToTimeZone($slot['end'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 39 |
} |
| 40 |
|
| 41 |
$schedule['slots'][$index] = $slot; |
| 42 |
} |
| 43 |
|
| 44 |
$schedule['slots'] = array_values($schedule['slots']); |
| 45 |
} |
| 46 |
|
| 47 |
return $schedules; |
| 48 |
} |
| 49 |
|
| 50 |
public static function slotDateOverrides($overrides, $fromTimeZone = '', $toTimeZone = false, $event = false) |
| 51 |
{ |
| 52 |
$todayTimeStamp = strtotime(gmdate('Y-m-d')); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 53 |
|
| 54 |
$validOverrides = []; |
| 55 |
$updatedOverRides = []; |
| 56 |
|
| 57 |
$isSkipped = false; |
| 58 |
|
| 59 |
$dstTimeZone = $fromTimeZone == 'UTC' ? $toTimeZone : $fromTimeZone; |
| 60 |
|
| 61 |
$dateWithoutDST = DateTimeHelper::getDateWithoutDST($dstTimeZone); |
| 62 |
|
| 63 |
foreach ($overrides as $date => $slots) { |
| 64 |
if (strtotime($date) < $todayTimeStamp) { |
| 65 |
$isSkipped = true; |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
$utcSlots = []; |
| 70 |
foreach ($slots as $index => $slot) { |
| 71 |
$slot['start'] = sanitize_text_field($slot['start']); |
| 72 |
$slot['end'] = sanitize_text_field($slot['end']); |
| 73 |
|
| 74 |
if (empty($slot['start']) || empty($slot['end'])) { |
| 75 |
unset($slots[$index]); |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$utcSlots[] = $slot; |
| 80 |
if ($toTimeZone && $fromTimeZone && $toTimeZone != $fromTimeZone) { |
| 81 |
$slot['start'] = DateTimeHelper::convertToTimeZone($slot['start'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 82 |
$slot['end'] = DateTimeHelper::convertToTimeZone($slot['end'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 83 |
} |
| 84 |
|
| 85 |
$slots[$index] = $slot; |
| 86 |
} |
| 87 |
|
| 88 |
if ($utcSlots) { |
| 89 |
$updatedOverRides[$date] = $utcSlots; |
| 90 |
} |
| 91 |
|
| 92 |
if ($slots) { |
| 93 |
$validOverrides[$date] = array_values($slots); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ($isSkipped && $fromTimeZone == 'UTC' && $event) { |
| 98 |
$event->settings = ['date_overrides' => $updatedOverRides]; |
| 99 |
$event->save(); |
| 100 |
} |
| 101 |
|
| 102 |
return $validOverrides; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public static function rangeDateBetween($range) |
| 107 |
{ |
| 108 |
$range = array_filter($range); |
| 109 |
if (!$range || count($range) != 2) { |
| 110 |
return ['', '']; |
| 111 |
} |
| 112 |
|
| 113 |
$range = array_values($range); |
| 114 |
|
| 115 |
$range[0] = gmdate('Y-m-d H:i:s', strtotime(sanitize_text_field($range[0]))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 116 |
$range[1] = gmdate('Y-m-d H:i:s', strtotime(sanitize_text_field($range[1]))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 117 |
|
| 118 |
return $range; |
| 119 |
} |
| 120 |
|
| 121 |
public static function scheduleConditions($conditions) |
| 122 |
{ |
| 123 |
if (!$conditions) { |
| 124 |
return [ |
| 125 |
'value' => 4, |
| 126 |
'unit' => 'hours' |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
return [ |
| 131 |
'value' => (int)Arr::get($conditions, 'value', 4), |
| 132 |
'unit' => sanitize_text_field(Arr::get($conditions, 'unit', 'hours')) |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
public static function checkCollection($value, $collection, $default = '') |
| 137 |
{ |
| 138 |
if (in_array($value, $collection, true)) { |
| 139 |
return $value; |
| 140 |
} |
| 141 |
return $default; |
| 142 |
} |
| 143 |
|
| 144 |
public static function locationSettings($locations) |
| 145 |
{ |
| 146 |
$sanitizedLocations = []; |
| 147 |
|
| 148 |
foreach ($locations as $locationIndex => $location) { |
| 149 |
$locationType = $location['type']; |
| 150 |
$locationTitle = sanitize_text_field(Arr::get($location, 'title')); |
| 151 |
|
| 152 |
if (empty($locationTitle) && $locationType == 'ms_teams') { |
| 153 |
$locationTitle = 'MS Teams'; |
| 154 |
} |
| 155 |
|
| 156 |
$sanitizedLocation = [ |
| 157 |
'type' => sanitize_text_field($location['type']), |
| 158 |
'title' => $locationTitle, |
| 159 |
'display_on_booking' => sanitize_text_field(Arr::get($location, 'display_on_booking')) |
| 160 |
]; |
| 161 |
|
| 162 |
if ($locationType == 'online_meeting') { |
| 163 |
$sanitizedLocation['meeting_link'] = sanitize_url(Arr::get($location, 'meeting_link')); |
| 164 |
} elseif ($locationType == 'custom' || $locationType == 'in_person_organizer') { |
| 165 |
$sanitizedLocation['description'] = sanitize_textarea_field(Arr::get($location, 'description')); |
| 166 |
} elseif ($locationType == 'phone_organizer') { |
| 167 |
$sanitizedLocation['host_phone_number'] = sanitize_text_field(Arr::get($location, 'host_phone_number')); |
| 168 |
} |
| 169 |
|
| 170 |
$sanitizedLocations[] = $sanitizedLocation; |
| 171 |
} |
| 172 |
|
| 173 |
return $sanitizedLocations; |
| 174 |
} |
| 175 |
} |
| 176 |
|