PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.10
Timetics – Appointment Booking Calendar & Scheduling v1.0.10
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 1.0.23 1.0.24 All 62 releases
timetics / core / customers / customer.php

customer.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.10, at core/customers/customer.php

397 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cutomer Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Customers;
8
9 use Timetics\Core\Bookings\Booking;
10
11 /**
12 * Customer class
13 *
14 * @since 1.0.0
15 */
16 class Customer {
17 /**
18 * Store customer id
19 *
20 * @var integer
21 */
22 protected $id;
23
24 /**
25 * Store meta prefix
26 *
27 * @var string
28 */
29 protected $meta_prefix = '_timetics_customer_';
30
31 /**
32 * Store error
33 *
34 * @var Object
35 */
36 public $error;
37
38 /**
39 * Customer data
40 *
41 * @var array
42 */
43 protected $data = [
44 'image' => '',
45 'first_name' => '',
46 'last_name' => '',
47 'email' => '',
48 'phone' => '',
49 'password' => '',
50 ];
51
52 /**
53 * Customer constructor
54 *
55 * @return void
56 */
57 public function __construct( $customer = 0 ) {
58 if ( $customer instanceof self ) {
59 $this->set_id( $customer->get_id() );
60 } elseif ( ! empty( $customer->ID ) ) {
61 $this->set_id( $customer->ID );
62 } elseif ( is_numeric( $customer ) && $customer > 0 ) {
63 $this->set_id( $customer );
64 }
65 }
66
67 /**
68 * Get customer id
69 *
70 * @return integer
71 */
72 public function get_id() {
73 return $this->id;
74 }
75
76 /**
77 * Get first name
78 *
79 * @return string
80 */
81 public function get_first_name() {
82 return get_user_meta( $this->id, 'first_name', true );
83 }
84
85 /**
86 * Get last name
87 *
88 * @return string
89 */
90 public function get_last_name() {
91 return get_user_meta( $this->id, 'last_name', true );
92 }
93
94 /**
95 * Get customer display name
96 *
97 * @return string
98 */
99 public function get_display_name() {
100 return $this->get_first_name() . ' ' . $this->get_last_name();
101 }
102
103 /**
104 * Get email
105 *
106 * @return string
107 */
108 public function get_email() {
109 $customer = get_userdata( $this->id );
110
111 if ( $customer ) {
112 return $customer->user_email;
113 }
114
115 return '';
116 }
117
118 /**
119 * Get phone
120 *
121 * @return string
122 */
123 public function get_phone() {
124 return $this->get_prop( 'phone' );
125 }
126
127 /**
128 * Get customer image
129 *
130 * @return string
131 */
132 public function get_image() {
133 $image = $this->get_prop( 'image' );
134
135 if ( ! $image ) {
136 return get_avatar_url( $this->id );
137 }
138
139 return wp_get_attachment_image_url( $image );
140 }
141
142 /**
143 * Get password
144 *
145 * @return string
146 */
147 public function get_password() {
148 $user = get_user_by( 'id', $this->id );
149 return $user->user_pass;
150 }
151
152 /**
153 * Get prop
154 *
155 * @param string
156 *
157 * @return string
158 */
159 public function get_prop( $prop = '' ) {
160 return $this->get_metadata( $prop );
161 }
162
163 /**
164 * Get customer metadata
165 *
166 * @param string $prop
167 *
168 * @return string
169 */
170 public function get_metadata( $prop = '' ) {
171 if( $prop == 'first_name' || $prop == 'last_name' ) {
172 $meta_key = $prop;
173 } else {
174 $meta_key = $this->meta_prefix . $prop;
175 }
176
177 return get_user_meta( $this->id, $meta_key, true );
178
179 }
180
181 // Setters.
182 /**
183 * Set customer id
184 *
185 * @param integer $id
186 *
187 * @return void
188 */
189 public function set_id( $id ) {
190 $this->id = $id;
191 }
192
193 /**
194 * Set props
195 *
196 * @param array $args Customer Data
197 *
198 * @return void
199 */
200 public function set_props( $args = [] ) {
201 $this->data = wp_parse_args( $args, $this->data );
202 }
203
204 /**
205 * Update meta data
206 *
207 * @return void
208 */
209 public function save_metadata() {
210 foreach ( $this->data as $key => $value ) {
211 // Prepare meta key.
212 if( $key == 'first_name' || $key == 'last_name' ) {
213 $meta_key = $key;
214 } else {
215 $meta_key = $this->meta_prefix . $key;
216 }
217
218 // Update appointment meta data.
219 update_user_meta( $this->id, $meta_key, $value );
220 }
221 }
222
223 /**
224 * Check user is valid staff
225 *
226 * @return bool
227 */
228 public function is_customer() {
229 $user = get_userdata( $this->id );
230
231 if ( $user ) {
232 return in_array( 'timetics-customer', $user->roles, true );
233 }
234
235 return true;
236 }
237
238 /**
239 * Save customer
240 *
241 * @return void
242 */
243 public function save() {
244 $args = [
245 'first_name' => $this->data['first_name'],
246 'last_name' => $this->data['last_name'],
247 'user_login' => $this->data['email'],
248 'user_email' => $this->data['email'],
249 ];
250
251 if ( $this->id ) {
252 $args['ID'] = $this->id;
253 $args['user_pass'] = $this->data['password'];
254 $customer_id = wp_update_user( $args );
255 } else {
256 $args['role'] = 'timetics-customer';
257 $args['user_pass'] = wp_generate_password();
258 $customer_id = wp_insert_user( $args );
259 }
260
261 if ( is_wp_error( $customer_id ) ) {
262 $this->error = $customer_id;
263 return;
264 }
265
266 $this->set_id( $customer_id );
267 $this->save_metadata();
268 }
269
270 /**
271 * Delete customer
272 *
273 * @return bool
274 */
275 public function delete() {
276 require_once ABSPATH . 'wp-admin/includes/user.php';
277
278 // Get all bookings for this customer.
279 $bookings = $this->get_bookings();
280
281 // Delete all bookings for this customer.
282 foreach ( $bookings as $booking ) {
283 $booking = new Booking( $booking );
284 $booking->delete();
285 }
286
287 return wp_delete_user( $this->id );
288 }
289
290 /**
291 * Make customer using email
292 *
293 * @param array Customer data
294 *
295 * @return Customer
296 */
297 public function make( $args = [] ) {
298 $defaults = [
299 'first_name' => '',
300 'last_name' => '',
301 'email' => '',
302 'phone' => '',
303 'password' => '',
304 ];
305
306 $args = wp_parse_args( $args, $defaults );
307
308 if ( email_exists( $args['email'] ) ) {
309 $user = get_user_by( 'email', $args['email'] );
310
311 if ( ! $this->is_customer() ) {
312 $user->add_role( 'timetics-customer' );
313 }
314 $this->set_id( $user->ID );
315 $this->set_props( $args );
316 $this->save_metadata();
317 } else {
318 $this->set_props( $args );
319 $this->save();
320 }
321 }
322
323 /**
324 * Get total booking for a customer
325 *
326 * @return integer
327 */
328 public function get_total_booking() {
329 $bookings = $this->booking_query();
330
331 return $bookings->found_posts;
332 }
333
334 /**
335 * Get all bookings by customer
336 *
337 * @return array
338 */
339 public function get_bookings() {
340 $bookings = $this->booking_query();
341 $data = [];
342
343 if ( ! empty( $bookings->posts ) ) {
344 foreach( $bookings->posts as $booking ) {
345 $object = new Booking( $booking->ID );
346 $data[] = $object->to_array();
347 }
348 }
349
350 return $data;
351 }
352
353 /**
354 * Get query for bookings
355 *
356 * @return WP_Query
357 */
358 private function booking_query() {
359 $query = new \WP_Query(
360 [
361 'post_type' => 'timetics-booking',
362 'post_status' => 'completed',
363 // @codingStandardsIgnoreStart
364 'meta_key' => '_tt_booking_customer',
365 'meta_value' => $this->id,
366 // @codingStandardsIgnoreEnd
367 ]
368 );
369
370 return $query;
371 }
372
373 /**
374 * Get all customer
375 *
376 * @param array $args Customer
377 *
378 * @return array
379 */
380 public static function all( $args = [] ) {
381 $defaults = [
382 'role' => 'timetics-customer',
383 'number' => 20,
384 'paged' => 1,
385 ];
386
387 $args = wp_parse_args( $args, $defaults );
388
389 $users = new \WP_User_Query( $args );
390
391 return [
392 'total' => $users->get_total(),
393 'items' => $users->get_results(),
394 ];
395 }
396 }
397