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 / customers / customer.php

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

367 lines 7.3 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 ];
50
51 /**
52 * Customer constructor
53 *
54 * @return void
55 */
56 public function __construct( $customer = 0 ) {
57 if ( $customer instanceof self ) {
58 $this->set_id( $customer->get_id() );
59 } elseif ( ! empty( $customer->ID ) ) {
60 $this->set_id( $customer->ID );
61 } elseif ( is_numeric( $customer ) && $customer > 0 ) {
62 $this->set_id( $customer );
63 }
64 }
65
66 /**
67 * Get customer id
68 *
69 * @return integer
70 */
71 public function get_id() {
72 return $this->id;
73 }
74
75 /**
76 * Get first name
77 *
78 * @return string
79 */
80 public function get_first_name() {
81 return $this->get_prop( 'first_name' );
82 }
83
84 /**
85 * Get last name
86 *
87 * @return string
88 */
89 public function get_last_name() {
90 return $this->get_prop( 'last_name' );
91 }
92
93 /**
94 * Get customer display name
95 *
96 * @return string
97 */
98 public function get_display_name() {
99 return $this->get_first_name() . ' ' . $this->get_last_name();
100 }
101
102 /**
103 * Get email
104 *
105 * @return string
106 */
107 public function get_email() {
108 $customer = get_userdata( $this->id );
109
110 if ( $customer ) {
111 return $customer->user_email;
112 }
113
114 return '';
115 }
116
117 /**
118 * Get phone
119 *
120 * @return string
121 */
122 public function get_phone() {
123 return $this->get_prop( 'phone' );
124 }
125
126 /**
127 * Get customer image
128 *
129 * @return string
130 */
131 public function get_image() {
132 $image = $this->get_prop( 'image' );
133
134 if ( ! $image ) {
135 return get_avatar_url( $this->id );
136 }
137
138 return wp_get_attachment_image_url( $image );
139 }
140
141 /**
142 * Get prop
143 *
144 * @param string
145 *
146 * @return string
147 */
148 public function get_prop( $prop = '' ) {
149 return $this->get_metadata( $prop );
150 }
151
152 /**
153 * Get customer metadata
154 *
155 * @param string $prop
156 *
157 * @return string
158 */
159 public function get_metadata( $prop = '' ) {
160 $meta_key = $this->meta_prefix . $prop;
161
162 return get_user_meta( $this->id, $meta_key, true );
163 }
164
165 // Setters.
166 /**
167 * Set customer id
168 *
169 * @param integer $id
170 *
171 * @return void
172 */
173 public function set_id( $id ) {
174 $this->id = $id;
175 }
176
177 /**
178 * Set props
179 *
180 * @param array $args Customer Data
181 *
182 * @return void
183 */
184 public function set_props( $args = [] ) {
185 $this->data = wp_parse_args( $args, $this->data );
186 }
187
188 /**
189 * Update meta data
190 *
191 * @return void
192 */
193 public function save_metadata() {
194 foreach ( $this->data as $key => $value ) {
195 // Prepare meta key.
196 $meta_key = $this->meta_prefix . $key;
197
198 // Update appointment meta data.
199 update_user_meta( $this->id, $meta_key, $value );
200 }
201 }
202
203 /**
204 * Check user is valid staff
205 *
206 * @return bool
207 */
208 public function is_customer() {
209 $user = get_userdata( $this->id );
210
211 if ( $user ) {
212 return in_array( 'timetics-customer', $user->roles, true );
213 }
214
215 return true;
216 }
217
218 /**
219 * Save customer
220 *
221 * @return void
222 */
223 public function save() {
224 $args = [
225 'first_name' => $this->data['first_name'],
226 'last_name' => $this->data['last_name'],
227 'user_login' => $this->data['email'],
228 'user_email' => $this->data['email'],
229 'user_pass' => wp_generate_password(),
230 'role' => 'timetics-customer',
231 ];
232
233 if ( $this->id ) {
234 $args['ID'] = $this->id;
235 unset( $args['user_email'] );
236 }
237
238 $customer_id = wp_insert_user( $args );
239
240 if ( is_wp_error( $customer_id ) ) {
241 $this->error = $customer_id;
242 return;
243 }
244
245 $this->set_id( $customer_id );
246 $this->save_metadata();
247 }
248
249 /**
250 * Delete customer
251 *
252 * @return bool
253 */
254 public function delete() {
255 require_once ABSPATH . 'wp-admin/includes/user.php';
256
257 // Get all bookings for this customer.
258 $bookings = $this->get_bookings();
259
260 // Delete all bookings for this customer.
261 foreach ( $bookings as $booking ) {
262 $booking = new Booking( $booking );
263 $booking->delete();
264 }
265
266 return wp_delete_user( $this->id );
267 }
268
269 /**
270 * Make customer using email
271 *
272 * @param array Customer data
273 *
274 * @return Customer
275 */
276 public function make( $args = [] ) {
277 $defaults = [
278 'first_name' => '',
279 'last_name' => '',
280 'email' => '',
281 'phone' => '',
282 ];
283
284 $args = wp_parse_args( $args, $defaults );
285
286 if ( email_exists( $args['email'] ) ) {
287 $user = get_user_by( 'email', $args['email'] );
288
289 if ( ! $this->is_customer() ) {
290 $user->add_role( 'timetics-customer' );
291 }
292 $this->set_id( $user->ID );
293 $this->set_props( $args );
294 $this->save_metadata();
295 } else {
296 $this->set_props( $args );
297 $this->save();
298 }
299 }
300
301 /**
302 * Get total booking for a customer
303 *
304 * @return integer
305 */
306 public function get_total_booking() {
307 $bookings = $this->booking_query();
308
309 return $bookings->found_posts;
310 }
311
312 /**
313 * Get all bookings by customer
314 *
315 * @return array
316 */
317 public function get_bookings() {
318 $bookings = $this->booking_query();
319
320 return $bookings->posts;
321 }
322
323 /**
324 * Get query for bookings
325 *
326 * @return WP_Query
327 */
328 private function booking_query() {
329 $query = new \WP_Query(
330 [
331 'post_type' => 'timetics-booking',
332 'post_status' => 'publish',
333 // @codingStandardsIgnoreStart
334 'meta_key' => '_tt_booking_customer',
335 'meta_value' => $this->id,
336 // @codingStandardsIgnoreEnd
337 ]
338 );
339
340 return $query;
341 }
342
343 /**
344 * Get all customer
345 *
346 * @param array $args Customer
347 *
348 * @return array
349 */
350 public static function all( $args = [] ) {
351 $defaults = [
352 'role' => 'timetics-customer',
353 'number' => 20,
354 'paged' => 1,
355 ];
356
357 $args = wp_parse_args( $args, $defaults );
358
359 $users = new \WP_User_Query( $args );
360
361 return [
362 'total' => $users->get_total(),
363 'items' => $users->get_results(),
364 ];
365 }
366 }
367