PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
← All changes | app/Services/AvailabilityService.php +46 -23 1.5.0 → 2.5.0 View file →
@@ -9,18 +9,44 @@
9 9 use FluentBooking\Framework\Support\Arr;
10 10
11 11 class AvailabilityService
12 12 {
13 - public static function availabilitySchedules()
13 + public static function maybeCreateAvailability($calendar, $scheduleData)
14 14 {
15 - $permissions = ['read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars'];
15 + $userId = $calendar->user_id;
16 16
17 - $availabilities = Availability::when(
17 + $availability = self::getDefaultSchedule($userId);
18 +
19 + if ($availability) {
20 + return $availability;
21 + }
22 +
23 + $timezone = $calendar->author_timezone;
24 +
25 + $defaultSchedule = self::createScheduleSchema($userId, 'Weekly Hours', true, $timezone, 'UTC', $scheduleData);
26 +
27 + return Availability::create($defaultSchedule);
28 + }
29 +
30 + /**
31 + * Schedules the current user may read and attach, as a query so callers that only
32 + * need ids can avoid formatting every schedule.
33 + */
34 + public static function usableAvailabilityQuery()
35 + {
36 + $permissions = ['manage_all_data', 'read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars'];
37 +
38 + return Availability::when(
18 39 !PermissionManager::userCan($permissions),
19 40 function ($query) {
20 41 return $query->where('object_id', get_current_user_id());
21 - })->get()->toArray();
42 + });
43 + }
22 44
45 + public static function availabilitySchedules()
46 + {
47 + $availabilities = self::usableAvailabilityQuery()->get()->toArray();
48 +
23 49 $formattedSchedules = [];
24 50 foreach ($availabilities as $availability) {
25 51 $toTimezone = Arr::get($availability, 'value.timezone', 'UTC');
26 52 $formattedSchedules[] = [
@@ -87,9 +113,9 @@
87 113 $schedule = Availability::findOrFail($schedule->id);
88 114 $updatedSettings = [
89 115 'default' => false,
90 116 'timezone' => Arr::get($schedule, 'value.timezone', 'UTC'),
91 - 'date_overrides' => Arr::get($schedule, 'value.data_overrides', []),
117 + 'date_overrides' => Arr::get($schedule, 'value.date_overrides', []),
92 118 'weekly_schedules' => Arr::get($schedule, 'value.weekly_schedules'),
93 119 ];
94 120
95 121 $schedule->value = $updatedSettings;
@@ -98,9 +124,9 @@
98 124 }
99 125
100 126 public static function getScheduleOptions()
101 127 {
102 - $permissions = ['read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars'];
128 + $permissions = ['manage_all_data', 'read_and_use_other_availabilities', 'manage_other_availabilities', 'read_other_calendars', 'manage_other_calendars'];
103 129
104 130 $availabilities = Availability::when(
105 131 !PermissionManager::userCan($permissions),
106 132 function ($query) {
@@ -113,9 +139,9 @@
113 139 ->where('type', 'simple')
114 140 ->where('user_id', $availability->object_id)
115 141 ->first();
116 142
117 - if ($calendar) {
143 + if ($calendar && $calendar->user) {
118 144 $hostName = $calendar->user->full_name;
119 145 if ($calendar->user_id == get_current_user_id()) {
120 146 $hostName = __('My Schedules', 'fluent-booking');
121 147 }
@@ -123,14 +149,13 @@
123 149 $hostName = __('Deleted User', 'fluent-booking');
124 150 }
125 151
126 152 $scheduleOptions[$hostName] = $scheduleOptions[$hostName] ?? [];
127 -
128 - $default = Arr::isTrue($availability, 'value.default') ? ' (Default)' : '';
129 -
153 +
130 154 $scheduleOptions[$hostName][] = [
131 - 'label' => Arr::get($availability, 'key') . $default,
132 - 'value' => Arr::get($availability, 'id')
155 + 'label' => Arr::get($availability, 'key'),
156 + 'value' => Arr::get($availability, 'id'),
157 + 'default' => Arr::isTrue($availability, 'value.default')
133 158 ];
134 159 }
135 160
136 161 return apply_filters('fluent_booking/availability_schedule_options', $scheduleOptions);
@@ -137,16 +162,13 @@
137 162 }
138 163
139 164 public static function getDefaultSchedule($userId)
140 165 {
141 - $schedules = Availability::where('object_id', $userId)->get();
142 -
143 - foreach ($schedules as $schedule) {
144 - if (Arr::isTrue($schedule, 'value.default')) {
145 - return $schedule;
146 - }
147 - }
148 - return null;
166 + return Availability::where('object_id', $userId)
167 + ->get()
168 + ->first(function ($schedule) {
169 + return Arr::isTrue($schedule, 'value.default');
170 + });
149 171 }
150 172
151 173 public static function createScheduleSchema($userId, $title, $default, $fromTimezone, $toTimezone = 'UTC', $weeklySchedule = [], $dateOverrides = [])
152 174 {
@@ -151,9 +173,9 @@
151 173 public static function createScheduleSchema($userId, $title, $default, $fromTimezone, $toTimezone = 'UTC', $weeklySchedule = [], $dateOverrides = [])
152 174 {
153 175 $weeklySchedule = $weeklySchedule ? $weeklySchedule : Helper::getWeeklyScheduleSchema();
154 176
155 - $dateOverrides = $dateOverrides ? SanitizeService::slotDateOverrides($dateOverrides, $fromTimezone, $toTimezone) : [];
177 + $dateOverrides = $dateOverrides ? SanitizeService::slotDateOverrides($dateOverrides, $fromTimezone, $toTimezone, null, true) : [];
156 178
157 179 $defaultSchedule = [
158 180 'object_id' => $userId,
159 181 'key' => sanitize_text_field($title),
@@ -160,9 +182,9 @@
160 182 'value' => [
161 183 'default' => (bool)$default,
162 184 'timezone' => sanitize_text_field($fromTimezone),
163 185 'date_overrides' => $dateOverrides,
164 - 'weekly_schedules' => SanitizeService::weeklySchedules($weeklySchedule, $fromTimezone, $toTimezone),
186 + 'weekly_schedules' => SanitizeService::weeklySchedules($weeklySchedule, $fromTimezone, $toTimezone, true),
165 187 ]
166 188 ];
167 189 return $defaultSchedule;
168 190 }
@@ -198,9 +220,10 @@
198 220 continue;
199 221 }
200 222
201 223 $schedule['enabled'] = true;
202 -
224 +
225 + $nextDay = null;
203 226 $nextDayIndex = 0;
204 227 $dayIndex = array_search($day, $weekDays);
205 228 foreach ($schedule['slots'] as $index => $slot) {
206 229 if (!$slot['start'] || !$slot['end']) {