| 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 = false) |
| 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 |
if ($fromUser) { |
| 26 |
usort($schedule['slots'], function ($a, $b) { |
| 27 |
return strcmp($a['start'], $b['start']); |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
$cleanedSlots = []; |
| 32 |
foreach ($schedule['slots'] as $slot) { |
| 33 |
if ($fromUser) { |
| 34 |
$slot['start'] = sanitize_text_field($slot['start']); |
| 35 |
$slot['end'] = sanitize_text_field($slot['end']); |
| 36 |
} |
| 37 |
|
| 38 |
if (!$slot['start'] || !$slot['end']) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
if ($toTimeZone && $fromTimeZone) { |
| 43 |
$slot['start'] = DateTimeHelper::convertToTimeZone($slot['start'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 44 |
$slot['end'] = DateTimeHelper::convertToTimeZone($slot['end'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 45 |
} |
| 46 |
|
| 47 |
$cleanedSlots[] = $slot; |
| 48 |
} |
| 49 |
|
| 50 |
$schedule['slots'] = array_values($cleanedSlots); |
| 51 |
} |
| 52 |
|
| 53 |
return $schedules; |
| 54 |
} |
| 55 |
|
| 56 |
public static function slotDateOverrides($overrides, $fromTimeZone = '', $toTimeZone = false, $event = null, $fromUser = false) |
| 57 |
{ |
| 58 |
$todayTimeStamp = strtotime(gmdate('Y-m-d')); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 59 |
|
| 60 |
$validOverrides = []; |
| 61 |
$updatedOverRides = []; |
| 62 |
|
| 63 |
$isSkipped = false; |
| 64 |
|
| 65 |
$dstTimeZone = $fromTimeZone == 'UTC' ? $toTimeZone : $fromTimeZone; |
| 66 |
|
| 67 |
$dateWithoutDST = DateTimeHelper::getDateWithoutDST($dstTimeZone); |
| 68 |
|
| 69 |
foreach ($overrides as $date => $slots) { |
| 70 |
if (strtotime($date) < $todayTimeStamp) { |
| 71 |
$isSkipped = true; |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
if ($fromUser) { |
| 76 |
usort($slots, function ($a, $b) { |
| 77 |
return strcmp($a['start'], $b['start']); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
$utcSlots = []; |
| 82 |
foreach ($slots as $index => $slot) { |
| 83 |
if ($fromUser) { |
| 84 |
$slot['start'] = sanitize_text_field($slot['start']); |
| 85 |
$slot['end'] = sanitize_text_field($slot['end']); |
| 86 |
} |
| 87 |
|
| 88 |
if (empty($slot['start']) || empty($slot['end'])) { |
| 89 |
unset($slots[$index]); |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
$utcSlots[] = $slot; |
| 94 |
if ($toTimeZone && $fromTimeZone && $toTimeZone != $fromTimeZone) { |
| 95 |
$slot['start'] = DateTimeHelper::convertToTimeZone($slot['start'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 96 |
$slot['end'] = DateTimeHelper::convertToTimeZone($slot['end'], $fromTimeZone, $toTimeZone, 'H:i', $dateWithoutDST); |
| 97 |
} |
| 98 |
|
| 99 |
$slots[$index] = $slot; |
| 100 |
} |
| 101 |
|
| 102 |
if ($utcSlots) { |
| 103 |
$updatedOverRides[$date] = $utcSlots; |
| 104 |
} |
| 105 |
|
| 106 |
if (!$slots) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$validOverrides[$date] = array_values($slots); |
| 111 |
} |
| 112 |
|
| 113 |
if ($isSkipped && $fromTimeZone == 'UTC' && $event) { |
| 114 |
$event->settings = ['date_overrides' => $updatedOverRides]; |
| 115 |
$event->save(); |
| 116 |
} |
| 117 |
|
| 118 |
return $validOverrides; |
| 119 |
} |
| 120 |
|
| 121 |
public static function rangeDateBetween($range) |
| 122 |
{ |
| 123 |
$range = array_filter($range); |
| 124 |
if (!$range || count($range) != 2) { |
| 125 |
return ['', '']; |
| 126 |
} |
| 127 |
|
| 128 |
$range = array_values($range); |
| 129 |
|
| 130 |
$range[0] = gmdate('Y-m-d H:i:s', strtotime(sanitize_text_field($range[0]))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 131 |
$range[1] = gmdate('Y-m-d H:i:s', strtotime(sanitize_text_field($range[1]))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 132 |
|
| 133 |
return $range; |
| 134 |
} |
| 135 |
|
| 136 |
public static function scheduleConditions($conditions) |
| 137 |
{ |
| 138 |
if (!$conditions) { |
| 139 |
return [ |
| 140 |
'value' => 4, |
| 141 |
'unit' => 'hours' |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
return [ |
| 146 |
'value' => max(0, (int)Arr::get($conditions, 'value', 4)), |
| 147 |
'unit' => sanitize_text_field(Arr::get($conditions, 'unit', 'hours')) |
| 148 |
]; |
| 149 |
} |
| 150 |
|
| 151 |
public static function checkCollection($value, $collection, $default = '') |
| 152 |
{ |
| 153 |
if (in_array($value, $collection, true)) { |
| 154 |
return $value; |
| 155 |
} |
| 156 |
return $default; |
| 157 |
} |
| 158 |
|
| 159 |
public static function locationSettings($locations) |
| 160 |
{ |
| 161 |
$sanitizedLocations = []; |
| 162 |
|
| 163 |
foreach ($locations as $locationIndex => $location) { |
| 164 |
$locationType = $location['type']; |
| 165 |
$locationTitle = sanitize_text_field(Arr::get($location, 'title')); |
| 166 |
|
| 167 |
if (empty($locationTitle) && $locationType == 'ms_teams') { |
| 168 |
$locationTitle = 'MS Teams'; |
| 169 |
} |
| 170 |
|
| 171 |
$sanitizedLocation = [ |
| 172 |
'type' => sanitize_text_field($location['type']), |
| 173 |
'title' => $locationTitle, |
| 174 |
'display_on_booking' => sanitize_text_field(Arr::get($location, 'display_on_booking')) |
| 175 |
]; |
| 176 |
|
| 177 |
if ($locationType == 'online_meeting') { |
| 178 |
$sanitizedLocation['meeting_link'] = sanitize_url(Arr::get($location, 'meeting_link')); |
| 179 |
} elseif ($locationType == 'custom' || $locationType == 'in_person_organizer') { |
| 180 |
$sanitizedLocation['description'] = sanitize_textarea_field(Arr::get($location, 'description')); |
| 181 |
} elseif ($locationType == 'phone_organizer') { |
| 182 |
$sanitizedLocation['host_phone_number'] = sanitize_text_field(Arr::get($location, 'host_phone_number')); |
| 183 |
} |
| 184 |
|
| 185 |
$sanitizedLocations[] = $sanitizedLocation; |
| 186 |
} |
| 187 |
|
| 188 |
return $sanitizedLocations; |
| 189 |
} |
| 190 |
|
| 191 |
public static function sanitizeUtmData($value) |
| 192 |
{ |
| 193 |
return sanitize_text_field($value); |
| 194 |
} |
| 195 |
|
| 196 |
public static function sanitizeAddons($inputs) |
| 197 |
{ |
| 198 |
return array_keys(array_filter([ |
| 199 |
'fluent-crm' => Arr::get($inputs, 'install_fluentcrm', 'no') == 'yes', |
| 200 |
'fluent-smtp' => Arr::get($inputs, 'install_fluentsmtp', 'no') == 'yes', |
| 201 |
'fluent-cart' => Arr::get($inputs, 'install_fluentcart', 'no') == 'yes', |
| 202 |
])); |
| 203 |
} |
| 204 |
} |
| 205 |
|