/
class.bt-booking-time.php
426 lines (357 loc) · 8.47 KB
/
class.bt-booking-time.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* @file
* @brief Implements the BTB_Time class.
* @author Matthias Fehring
* @version 1.0.0
* @date 2016
*
* @copyright
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
defined( 'ABSPATH' ) or die (' Am Arsch die Räuber! ');
/**
* Core class used to implement the BTB_Time object.
*
* @since 1.0.0
*/
class BTB_Time {
/**
* Time ID.
*
* @var int $ID
*/
public $ID;
/**
* Time name.
*
* This is normally a combination of start and end date.
*
* @var string $name
*/
public $name = '';
/**
* Start time and date as unix time stamp.
*
* @var int $start
*/
public $start = 0;
/**
* End time and date as unix time stamp.
*
* @var int $end
*/
public $end = 0;
/**
* Amount of slots for this time.
*
* @var int $slots
*/
public $slots = 0;
/**
* The price for this time.
*
* @var float $price
*/
public $price = 0.0;
/**
* Should only the date be valid, not the time.
*
* @var bool $date_only
*/
public $date_only = false;
/**
* ID of the event this time belongs to.
*
* @var int $event
*/
public $event = 0;
/**
* The post type. Should be btb_time.
*
* @var string $post_type
*/
public $post_type = 'btb_time';
/**
* The status is publish by default.
*
* @var string $post_status
*/
public $post_status = 'publish';
/**
* Stores the time object's sanitization level.
*
* @var string $filter
*/
public $filter;
/**
* Defines the creation time of the booking.
*
* @var string $post_date
*/
public $post_date = '';
/**
* Defines the creation time of the booking at GMT.
*
* @var string $post_date_gmt
*/
public $post_date_gmt = '';
/**
* Sanitized version of the post_title/booking code.
*
* @var string $post_name
*/
public $post_name = '';
/**
* Last booking modification time.
*
* @var string $post_modified
*/
public $post_modified = '';
/**
* Last booking modificatin time at GMT.
*
* @var string $post_modified_gmt
*/
public $post_modified_gmt = '';
/**
* Global Uniquie ID for referencing the booking.
*
* @var string $guid
*/
public $guid = '';
/**
* ID of the user that created this time.
*
* @var int $post_author
*/
public $post_author = 0;
/**
* Number of free slots for this time.
*
* @var int $free_slots
*/
public $free_slots = 0;
/**
* Number of booked slots for this time.
*
* @var int $booked_slots
*/
public $booked_slots = 0;
/**
* Number of pre booked slots for this time.
*
* @var int $prebooked_slots
*/
public $prebooked_slots = 0;
/**
* Retrieve BTB_Time instance.
*
* @param int $time_id Time ID.
* @return BTB_Time|false BTB_Time object, false otherwise.
*/
public static function get_instance($time_id) {
global $wpdb;
$time_id = (int) $time_id;
if (!$time_id) {
return false;
}
$_time = wp_cache_get($time_id, 'btb_times');
if (!$_time) {
$_time = $wpdb->get_row($wpdb->prepare(
"
SELECT ti.ID, ti.post_author, ti.post_date, ti.post_date_gmt, ti.post_title AS name, ti.post_status, ti.post_name,
ti.post_modified, ti.post_modified_gmt, ti.post_parent AS event, ti.guid, sl.meta_value AS slots, pr.meta_value AS price,
dato.meta_value AS date_only, st.meta_value AS start, et.meta_value AS end
FROM $wpdb->posts ti
LEFT JOIN $wpdb->postmeta sl
ON sl.post_id = ti.ID
AND sl.meta_key = 'btb_slots'
LEFT JOIN $wpdb->postmeta pr
ON pr.post_id = ti.ID
AND pr.meta_key = 'btb_price'
LEFT JOIN $wpdb->postmeta dato
ON dato.post_id = ti.ID
AND dato.meta_key = 'btb_date_only'
LEFT JOIN $wpdb->postmeta st
ON st.post_id = ti.ID
AND st.meta_key = 'btb_start'
LEFT JOIN $wpdb->postmeta et
ON et.post_id = ti.ID
AND et.meta_key = 'btb_end'
WHERE ti.ID = %d AND ti.post_type = 'btb_time' LIMIT 1;
",
$time_id
));
if (!$_time) {
return false;
}
$_time = btb_sanitize_time($_time, 'raw');
wp_cache_add($_time->ID, $_time, 'btb_times');
} elseif (empty($_time->filter)) {
$_time = btb_sanitize_time($_time, 'raw');
}
return new BTB_Time($_time);
}
/**
* Constructor
*
* @param BTB_Time|object $time Time object.
*/
public function __construct($time = null) {
if ($time) {
foreach(get_object_vars($time) as $key => $value) {
$this->$key = $value;
}
}
}
/**
* Isset-er.
*
* @param string $key Property to check if set.
* @return bool
*/
public function __isset($key) {
if ('ancestors' == $key) {
return true;
}
if ('page_template' == $key) {
return false;
}
if ('post_category' == $key) {
return true;
}
if ('tags_input' == $key) {
return true;
}
if ('post_title' == $key) {
return isset($this->name);
}
if ('post_parent' == $key) {
return true;
}
return metadata_exists('post', $this->ID, $key);
}
/**
* Getter
*
* @param string $key Key to get.
* @return mixed
*/
public function __get($key) {
if ('page_template' == $key && $this->__isset($key)) {
return get_post_meta( $this->ID, '_wp_page_template', true );
}
if ('post_category' == $key) {
return array();
}
if ('tags_input' == $key) {
return array();
}
if ('ancestors' == $key) {
return array();
}
if ('post_title' == $key) {
return $this->name;
}
if ('post_parent' == $key) {
return $this->event;
}
$value = get_post_meta($this->ID, $key, true);
if ($this->filter) {
$value = btb_sanitize_time_field($key, $value, $this->ID, $this->filter);
}
return $value;
}
/**
* Returns a filtered version of this time.
*
* @param string $filter The filter context.
* @return self|array|bool|object|BTB_Booking
*/
public function filter($filter) {
if ($this->filter == $filter) {
return $this;
}
if ($filter == 'raw') {
return self::get_instance($this->ID);
}
return btb_sanitize_time($this, $filter);
}
/**
* @brief Convert object to array.
*
* @param bool $wp_post_array If true, the array will be compatible to the WP_Post object/array.
* @return array Object as Array.
*/
public function to_array($wp_post_array = false) {
if ($wp_post_array) {
return array(
'ID' => $this->ID,
'post_author' => $this->post_author,
'post_date' => $this->post_date,
'post_date_gmt' => $this->post_date_gmt,
'post_content' => '',
'post_content_filtered' => '',
'post_title' => $this->name,
'post_excerpt' => '',
'post_status' => $this->post_status,
'post_type' => 'btb_time',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => $this->post_name,
'to_ping' => '',
'pinged' => '',
'post_modified' => $this->post_modified,
'post_modified_gmt' => $this->post_modified_gmt,
'post_parent' => $this->booked_time,
'menu_order' => 0,
'post_mime_type' => '',
'guid' => $this->guid,
'tax_input' => array(),
'meta_input' => array(
'btb_start' => $this->start,
'btb_end' => $this->end,
'btb_slots' => $this->slots,
'btb_price' => $this->price,
'btb_date_only' => $this->date_only
)
);
}
return get_object_vars($this);
}
/**
* @brief Calculates the values for the booked, prebooked and free slots.
*/
public function calc_slots() {
$this->prebooked_slots = btb_get_time_prebooked_slots($this->ID);
$this->booked_slots = btb_get_time_booked_slots($this->ID);
$this->free_slots = ($this->slots - $this->prebooked_slots - $this->booked_slots);
}
/**
* @brief Loads the data for a single time from a API response.
*
* @param object $reponse JSON object from the API response.
*/
public function from_api_response($response) {
if (is_object($response)) {
$this->ID = $response->id;
$this->post_date = $response->date;
$this->post_date_gmt = $response->date_gmt;
$this->guid = $response->guid->rendered;
$this->post_modified = $response->modified;
$this->post_modified_gmt = $response->modified_gmt;
$this->post_name = $response->slug;
$this->name = $response->title->rendered;
$this->price = (float)$response->btb_price;
$this->event = (int)$response->btb_event_id;
$this->start = (int)$response->btb_start;
$this->end = (int)$response->btb_end;
$this->date_only = ($response->btb_date_only == "1");
$this->free_slots = $response->btb_free_slots;
}
}
}