PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.2
Timetics – Appointment Booking Calendar & Scheduling v1.0.2
1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 All 64 releases
timetics / core / bookings / booking-entry.php

booking-entry.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.2, at core/bookings/booking-entry.php

363 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Booking entry
4 *
5 * @package Timetics
6 */
7
8 namespace Timetics\Core\Bookings;
9
10 /**
11 * Class Booking Entry
12 */
13 class Booking_Entry {
14
15 /**
16 * Store post type
17 *
18 * @var string
19 */
20 private $post_type = 'timetics-schedule';
21
22 /**
23 * Store prefix
24 *
25 * @var string
26 */
27 private $prefix = '_timetics_schedule_';
28
29 /**
30 * Store entry id
31 *
32 * @var string
33 */
34 private $id;
35
36 /**
37 * Store entries
38 *
39 * @var array
40 */
41 private $entries = [];
42
43 /**
44 * Store entry data
45 *
46 * @var array
47 */
48 private $data = [
49 'meeting_id' => '',
50 'staff_id' => '',
51 'customer_id' => '',
52 'booking_id' => '',
53 'booked' => '',
54 'date' => '',
55 'start' => '',
56 'end' => '',
57 'seats' => '',
58 ];
59
60 /**
61 * Constructor for Booking Entry
62 *
63 * @param integer | Object $entry
64 *
65 * @return void
66 */
67 public function __construct( $entry = 0 ) {
68 if ( $entry instanceof self ) {
69 $this->set_id( $entry->get_id() );
70 } elseif ( ! empty( $entry->ID ) ) {
71 $this->set_id( $entry->ID );
72 } elseif ( is_numeric( $entry ) && $entry > 0 ) {
73 $this->set_id( $entry );
74 }
75 }
76
77 /**
78 * Get booking entry id
79 *
80 * @return integer
81 */
82 public function get_id() {
83 return $this->id;
84 }
85
86 /**
87 * Get meeting id
88 *
89 * @return integer
90 */
91 public function get_meeting_id() {
92 return $this->get_prop( 'meeting_id' );
93 }
94
95 /**
96 * Get staff id
97 *
98 * @return integer
99 */
100 public function get_staff_id() {
101 return $this->get_prop( 'staff_id' );
102 }
103
104 /**
105 * Get customer id
106 *
107 * @return integer
108 */
109 public function get_customer_id() {
110 return $this->get_prop( 'customer_id' );
111 }
112
113 /**
114 * Get booking id
115 *
116 * @return integer
117 */
118 public function get_booking_id() {
119 return $this->get_prop( 'booking_id' );
120 }
121
122 /**
123 * Get booked
124 *
125 * @return integer
126 */
127 public function get_booked() {
128 return $this->get_prop( 'booked' );
129 }
130
131 /**
132 * Get start time
133 *
134 * @return string
135 */
136 public function get_start() {
137 return $this->get_prop( 'start' );
138 }
139
140 /**
141 * Get end time
142 *
143 * @return string
144 */
145 public function get_end() {
146 return $this->get_prop( 'end' );
147 }
148
149 /**
150 * Get date
151 *
152 * @return string
153 */
154 public function get_date() {
155 return $this->get_prop( 'date' );
156 }
157
158 /**
159 * Get seats
160 *
161 * @return array
162 */
163 public function get_seats() {
164 return $this->get_prop( 'seats' );
165 }
166
167 /**
168 * Get entry data
169 *
170 * @return array
171 */
172 public function get_data() {
173 return [
174 'id' => $this->get_id(),
175 'meeting_id' => $this->get_meeting_id(),
176 'staff_id' => $this->get_staff_id(),
177 'booking_id' => $this->get_booking_id(),
178 'customer_id' => $this->get_customer_id(),
179 'booked' => $this->get_booked(),
180 'date' => $this->get_date(),
181 'start' => $this->get_start(),
182 'end' => $this->get_end(),
183 ];
184 }
185
186 /**
187 * Get prop
188 *
189 * @param string $name
190 *
191 * @return mixed
192 */
193 public function get_prop( $name ) {
194 $key = $this->prefix . $name;
195
196 return get_post_meta( $this->id, $key, true );
197 }
198
199 /**
200 * Find booking entry
201 *
202 * @param array $args
203 *
204 * @return array
205 */
206 public function find( $args = [] ) {
207 $data = [
208 'post_type' => $this->post_type,
209 'post_status' => 'publish',
210 'numberposts' => -1,
211 ];
212
213 if ( $args ) {
214 $query = [
215 'relation' => 'AND',
216 ];
217 foreach ( $args as $key => $value ) {
218 $meta_key = $this->prefix . $key;
219 $meta_query = [
220 'key' => $meta_key,
221 'value' => $value,
222 'compare' => '=',
223 ];
224
225 $query[] = $meta_query;
226 }
227
228 // @codingStandardsIgnoreStart
229 $data['meta_query'] = $query;
230 // @codingStandardsIgnoreEnd
231 }
232
233 $entries = $this->collection( get_posts( $data ) );
234
235 $this->entries = $entries;
236
237 return $entries;
238 }
239
240 /**
241 * Get all booking entries
242 *
243 * @return array
244 */
245 public function all() {
246 $entries = get_posts(
247 [
248 'post_type' => $this->post_type,
249 'post_status' => 'publish',
250 'numberposts' => -1,
251 ]
252 );
253
254 $this->entries = $entries;
255 $entries = $this->collection( $entries );
256
257 return $entries;
258 }
259
260 /**
261 * Get first entry from the entries
262 *
263 * @return Object | null
264 */
265 public function first() {
266 if ( $this->entries ) {
267 return $this->entries[0];
268 }
269
270 return null;
271 }
272
273 /**
274 * Get instance of the current entry
275 *
276 * @param mixed $entry
277 *
278 * @return Object
279 */
280 private function get_instance( $entry ) {
281 return new self( $entry );
282 }
283
284 /**
285 * Get collection of object
286 *
287 * @param array $data
288 *
289 * @return array
290 */
291 private function collection( $data = [] ) {
292 return array_map( [$this, 'get_instance'], $data );
293 }
294
295 /**
296 * Set entry id
297 *
298 * @param integer $id
299 *
300 * @return void
301 */
302 private function set_id( $id ) {
303 $this->id = $id;
304 }
305
306 /**
307 * Create booking entry
308 *
309 * @param array $args
310 *
311 * @return integer
312 */
313 public function create( $args = [] ) {
314 $args = wp_parse_args( $args, $this->data );
315
316 $entry = wp_insert_post(
317 [
318 'post_title' => $this->post_type . $args['start'],
319 'post_type' => $this->post_type,
320 'post_status' => 'publish',
321 'post_author' => 1,
322 ]
323 );
324
325 if ( ! is_wp_error( $entry ) ) {
326 $this->set_id( $entry );
327 $this->update_metadata( $args );
328 }
329
330 return $entry;
331 }
332
333 /**
334 * Update booking entry
335 *
336 * @param array $args
337 *
338 * @return void
339 */
340 public function update( $args ) {
341 $this->update_metadata( $args );
342 }
343
344 /**
345 * Update metadata
346 *
347 * @param array $data
348 *
349 * @return void
350 */
351 private function update_metadata( $data = [] ) {
352 foreach ( $data as $key => $value ) {
353 if ( ! array_key_exists( $key, $this->data ) ) {
354 continue;
355 }
356
357 $meta_key = $this->prefix . $key;
358
359 update_post_meta( $this->id, $meta_key, $value );
360 }
361 }
362 }
363