/
class.bt-booking-event.php
411 lines (343 loc) · 8.96 KB
/
class.bt-booking-event.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
<?php
/**
* @file
* @brief Implements the BTB_Event 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_Event object.
*/
class BTB_Event {
/**
* Event ID.
*
* @var int $ID
*/
public $ID;
/**
* ID of the author of this event.
*
* @var int $post_author
*/
public $post_author = 0;
/**
* Name of the event.
*
* @var string $name
*/
public $name = '';
/**
* The post type. Should be btb_event.
*
* @var string $post_type
*/
public $post_type = 'btb_event';
/**
* The status is publish by default.
*
* @var string $post_status
*/
public $post_status = 'publish';
/**
* Stores the event object's sanitization level.
*
* @var string $filter
*/
public $filter;
/**
* Defines the creation time of the event.
*
* @var string $post_date
*/
public $post_date = '';
/**
* Defines the creation time of the event at GMT.
*
* @var string $post_date_gmt
*/
public $post_date_gmt = '';
/**
* Sanitized version of the event title/name.
*
* @var string $post_name
*/
public $post_name = '';
/**
* Last event modification time.
*
* @var string $post_modified
*/
public $post_modified = '';
/**
* Last event modificatin time at GMT.
*
* @var string $post_modified_gmt
*/
public $post_modified_gmt = '';
/**
* Global Uniquie ID for referencing the event.
*
* @var string $guid
*/
public $guid = '';
/**
* ID of the description page for this event.
*
* A description page can be used as a separate page,
* describing the event.
*
* @var int $desc_page
*/
public $desc_page = 0;
/**
* General price for this event.
*
* The general price can be overwritten by per time prices.
*
* @var float $price
*/
public $price = 0.0;
/**
* Hint how the price has to be interpreted.
*
* @var string $price_hint
*/
public $price_hint = '';
/**
* ID of the venue this event is located at.
*
* @var int $venue
*/
public $venue = 0;
/**
* Detailed escription of this event.
*
* @var string $description
*/
public $description = '';
/**
* Excerpt of the event description (aka. short description).
*
* @var string $short_desc
*/
public $short_desc = '';
/**
* Type of the event. Used for schema.org.
*
* @var string $event_type
*/
public $event_type = '';
/**
* Type of the struct data. Used for schema.org.
*
* Possible values: disabled, event, product
*
* @var string $struct_data_type
*/
public $struct_data_type = '';
/**
* Retrieve BTB_Event instance.
*
* @param int $event_id Event ID.
* @return BTB_Event|false BTB_Event object, false otherwise.
*/
public static function get_instance($event_id) {
global $wpdb;
$event_id = (int) $event_id;
if (!$event_id) {
return false;
}
$_event = wp_cache_get($event_id, 'btb_events');
if (!$_event) {
$_event = $wpdb->get_row($wpdb->prepare(
"
SELECT ev.ID, ev.post_author, ev.post_title AS name, ev.post_status, ev.post_date, ev.post_date_gmt, ev.post_name,
ev.post_modified, ev.post_modified_gmt, ev.guid, pr.meta_value AS price, ph.meta_value AS price_hint, ev.post_parent AS venue,
dp.meta_value AS desc_page, ev.post_content AS description, ev.post_excerpt AS short_desc, et.meta_value AS event_type, sd.meta_value AS struct_data_type
FROM $wpdb->posts ev
LEFT JOIN $wpdb->postmeta pr
ON pr.post_id = ev.ID
AND pr.meta_key = 'btb_price'
LEFT JOIN $wpdb->postmeta ph
ON ph.post_id = ev.ID
AND ph.meta_key = 'btb_price_hint'
LEFT JOIN $wpdb->postmeta dp
ON dp.post_id = ev.ID
AND dp.meta_key = 'btb_desc_page'
LEFT JOIN $wpdb->postmeta et
ON et.post_id = ev.ID
AND et.meta_key = 'btb_event_type'
LEFT JOIN $wpdb->postmeta sd
ON sd.post_id = ev.ID
AND sd.meta_key = 'btb_struct_data_type'
WHERE ev.ID = %d AND ev.post_type = 'btb_event' LIMIT 1
",
$event_id
));
if (!$_event) {
return false;
}
$_event = btb_sanitize_event($_event, 'raw');
wp_cache_add($_event->ID, 'btb_events');
} elseif (empty($_event->filter)) {
$_event = btb_sanitize_event($_event, 'raw');
}
return new BTB_Event($_event);
}
/**
* Constructor
*
* @param BTB_Event|obejct $event Event object.
*/
public function __construct($event = null) {
if ($event) {
foreach(get_object_vars($event) 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->venue;
}
$value = get_post_meta($this->ID, $key, true);
if ($this->filter) {
$value = btb_sanitize_event_field($key, $value, $this->ID, $this->filter);
}
return $value;
}
/**
* Returns a filtered version of this event.
*
* @param string $filter The filter context.
* @return self|array|bool|object|BTB_Event
*/
public function filter($filter) {
if ($this->filter == $filter) {
return $this;
}
if ($filter == 'raw') {
return self::get_instance($this->ID);
}
return btb_sanitize_event($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' => $this->description,
'post_content_filtered' => '',
'post_title' => $this->name,
'post_excerpt' =>$this->short_desc,
'post_status' => $this->post_status,
'post_type' => 'btb_event',
'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->venue,
'menu_order' => 0,
'post_mime_type' => '',
'guid' => $this->guid,
'tax_input' => array(),
'meta_input' => array(
'btb_desc_page' => $this->desc_page,
'btb_price' => $this->price,
'btb_price_hint' => $this->price_hint,
'btb_event_type' => $this->event_type,
'btb_struct_data_type' => $this->struct_data_type
)
);
}
return get_object_vars($this);
}
/**
* @brief Loads the data for a single event 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->description = $response->content->rendered;
$this->price = $response->btb_price;
$this->price_hint = $response->btb_price_hint;
}
}
}