/
class.bt-booking-venue.php
427 lines (363 loc) · 8.64 KB
/
class.bt-booking-venue.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
427
<?php
/**
* @file
* @brief Implements the BTB_Venue 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_Venue object.
*/
class BTB_Venue {
/**
* Venue ID.
*
* @var int $ID
*/
public $ID;
/**
* Venue name.
*
* @var string $name
*/
public $name = '';
/**
* Description of the venue.
*
* @var string $description
*/
public $description = '';
/**
* Short description of the venue (aka. excerpt).
*
* @var string $short_desc
*/
public $short_desc = '';
/**
* Street part of the venue's address.
*
* @var string $street
*/
public $street = '';
/**
* House number part of the venue's address.
*
* @var string $house_number
*/
public $house_number = '';
/**
* Postal code part of the venue's address.
*
* @var string $postal_code
*/
public $postal_code = '';
/**
* City part of the venue's address.
*
* @var string $city
*/
public $city = '';
/**
* Region part of the venue's address.
*
* @var string $region
*/
public $region = '';
/**
* Country part of the venue's address.
*
* ISO 3166-1 alpha2 two-letter code.
*
* @var string $country
*/
public $country = '';
/**
* True to use map coordinates if any.
*
* @var bool $use_map_coords
*/
public $use_map_coords = false;
/**
* Latitude of the venue location.
*
* @var float $latitude
*/
public $latitude = 0.0;
/**
* Longitude of the venue location.
*
* @var float $longitude
*/
public $longitude = 0.0;
/**
* Stores the venue object's sanitization level.
*
* @var string $filter
*/
public $filter;
/**
* Defines the creation time of the venue.
*
* @var string $post_date
*/
public $post_date = '';
/**
* Defines the creation time of the venue at GMT.
*
* @var string $post_date_gmt
*/
public $post_date_gmt = '';
/**
* Sanitized version of the post_title/name.
*
* @var string $post_name
*/
public $post_name = '';
/**
* Last venue modification time.
*
* @var string $post_modified
*/
public $post_modified = '';
/**
* Last venue modificatin time at GMT.
*
* @var string $post_modified_gmt
*/
public $post_modified_gmt = '';
/**
* Global Uniquie ID for referencing the venue.
*
* @var string $guid
*/
public $guid = '';
/**
* ID of the author that created this venue.
*
* @var int $post_author
*/
public $post_author = '';
/**
* Status of the venue. Default 'publish'.
*
* @var string $post_status
*/
public $post_status = 'publish';
/**
* @brief Retrieve BTB_Venue instance.
*
* @param int $venue_id Venue ID.
* @return BTB_Venue|false Venue object, false otherwise.
*/
public static function get_instance($venue_id) {
global $wpdb;
$venue_id = (int) $venue_id;
if (!$venue_id) {
return false;
}
$_venue = wp_cache_get($venue_id, 'btb_venues');
if (!$_venue) {
$_venue = $wpdb->get_row($wpdb->prepare(
"
SELECT ve.ID, ve.post_author, ve.post_date, ve.post_date_gmt, ve.post_content AS description, ve.post_title AS name, ve.post_excerpt AS short_desc,
ve.post_status, ve.post_name, ve.post_modified, ve.post_modified_gmt, ve.guid, street.meta_value AS street, hn.meta_value AS house_number,
zip.meta_value AS postal_code, ci.meta_value AS city, re.meta_value AS region, cc.meta_value AS country, umc.meta_value AS use_map_coords,
lat.meta_value AS latitude, lon.meta_value AS longitude
FROM $wpdb->posts ve
LEFT JOIN $wpdb->postmeta street
ON street.post_id = ve.ID
AND street.meta_key = 'btb_address_street'
LEFT JOIN $wpdb->postmeta hn
ON hn.post_id = ve.ID
AND hn.meta_key = 'btb_address_number'
LEFT JOIN $wpdb->postmeta zip
ON zip.post_id = ve.ID
AND zip.meta_key = 'btb_address_zip'
LEFT JOIN $wpdb->postmeta ci
ON ci.post_id = ve.ID
AND ci.meta_key = 'btb_address_city'
LEFT JOIN $wpdb->postmeta re
ON re.post_id = ve.ID
AND re.meta_key = 'btb_address_region'
LEFT JOIN $wpdb->postmeta cc
ON cc.post_id = ve.ID
AND cc.meta_key = 'btb_address_country'
LEFT JOIN $wpdb->postmeta umc
ON umc.post_id = ve.ID
AND umc.meta_key = 'btb_use_coordinates'
LEFT JOIN $wpdb->postmeta lat
ON lat.post_id = ve.ID
AND lat.meta_key = 'btb_address_lat'
LEFT JOIN $wpdb->postmeta lon
ON lon.post_id = ve.ID
AND lon.meta_key = 'btb_address_lon'
WHERE ve.ID = %d AND ve.post_type = 'btb_venue';
",
$venue_id
));
if (!$_venue) {
return false;
}
$_venue = btb_sanitize_venue($_venue, 'raw');
wp_cache_add($_venue->ID, $_venue, 'btb_venues');
} elseif (empty($_venue->filter)) {
$_venue = btb_sanitize_venue($_venue, 'raw');
}
return new BTB_Venue($_venue);
}
/**
* Constructor
*
* @param BTB_Venue|object $venue Event object.
*/
public function __construct($venue) {
foreach(get_object_vars($venue) 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->code);
}
if ('post_type' == $key) {
return true;
}
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->code;
}
if ('post_type' == $key) {
return 'btb_venue';
}
if ('post_parent' == $key) {
return 0;
}
$value = get_post_meta($this->ID, $key, true);
if ($this->filter) {
$value = btb_sanitize_venue_field($key, $value, $this->ID, $this->filter);
}
return $value;
}
/**
* Returns a filtered version of this venue.
*
* @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_venue($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_venue',
'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_address_street' => $this->street,
'btb_address_number' => $this->house_number,
'btb_address_zip' => $this->postal_code,
'btb_address_city' => $this->city,
'btb_address_region' => $this->region,
'btb_address_country' => $this->country,
'btb_use_coordinates' => $this->use_map_coords,
'btb_address_lat' => $this->latitude,
'btb_address_lon' => $this->longitude
)
);
}
return get_object_vars($this);
}
/*!
* @brief Returns a combination from street and house number.
*
* @return string
*/
public function streetAndNumber() {
if (!empty($this->street) && !empty($this->house_number)) {
return $this->street . ' ' . $this->house_number;
} else {
return $this->street . $this->house_number;
}
}
}