PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.0
Timetics – Appointment Booking Calendar & Scheduling v1.0.0
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.0, 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 get_user_meta( $this->id, 'first_name', true );
82 }
83
84 /**
85 * Get last name
86 *
87 * @return string
88 */
89 public function get_last_name() {
90 return get_user_meta( $this->id, 'last_name', true );
91 }
92
93 /**
94 * Get customer display name
95 *
96 * @return string
97 */
98 public function get_display_name() {
99 $user = get_userdata( $this->id );
100
101 $display_name = '';
102
103 if ( $user ) {
104 $display_name = $user->display_name;
105 }
106
107 return $display_name;
108 }
109
110 /**
111 * Get email
112 *
113 * @return string
114 */
115 public function get_email() {
116 return get_userdata( $this->id )->user_email;
117 }
118
119 /**
120 * Get phone
121 *
122 * @return string
123 */
124 public function get_phone() {
125 return $this->get_prop( 'phone' );
126 }
127
128 /**
129 * Get customer image
130 *
131 * @return string
132 */
133 public function get_image() {
134 $image = $this->get_prop( 'image' );
135
136 if ( ! $image ) {
137 return get_avatar_url( $this->id );
138 }
139
140 return wp_get_attachment_image_url( $image );
141 }
142
143 /**
144 * Get prop
145 *
146 * @param string
147 *
148 * @return string
149 */
150 public function get_prop( $prop = '' ) {
151 return $this->get_metadata( $prop );
152 }
153
154 /**
155 * Get customer metadata
156 *
157 * @param string $prop
158 *
159 * @return string
160 */
161 public function get_metadata( $prop = '' ) {
162 $meta_key = $this->meta_prefix . $prop;
163
164 return get_user_meta( $this->id, $meta_key, true );
165 }
166
167 // Setters.
168 /**
169 * Set customer id
170 *
171 * @param integer $id
172 *
173 * @return void
174 */
175 public function set_id( $id ) {
176 $this->id = $id;
177 }
178
179 /**
180 * Set props
181 *
182 * @param array $args Customer Data
183 *
184 * @return void
185 */
186 public function set_props( $args = [] ) {
187 $this->data = wp_parse_args( $args, $this->data );
188 }
189
190 /**
191 * Update meta data
192 *
193 * @return void
194 */
195 public function save_metadata() {
196 foreach ( $this->data as $key => $value ) {
197 // Prepare meta key.
198 $meta_key = $this->meta_prefix . $key;
199
200 // Update appointment meta data.
201 update_user_meta( $this->id, $meta_key, $value );
202 }
203 }
204
205 /**
206 * Check user is valid staff
207 *
208 * @return bool
209 */
210 public function is_customer() {
211 $user = get_userdata( $this->id );
212
213 if ( $user ) {
214 return in_array( 'timetics-customer', $user->roles, true );
215 }
216
217 return true;
218 }
219
220 /**
221 * Save customer
222 *
223 * @return void
224 */
225 public function save() {
226 $args = [
227 'first_name' => $this->data['first_name'],
228 'last_name' => $this->data['last_name'],
229 'user_login' => $this->data['email'],
230 'user_email' => $this->data['email'],
231 'user_pass' => wp_generate_password(),
232 'role' => 'timetics-customer',
233 ];
234
235 if ( $this->id ) {
236 $args['ID'] = $this->id;
237 unset( $args['user_email'] );
238 }
239
240 $customer_id = wp_insert_user( $args );
241
242 if ( is_wp_error( $customer_id ) ) {
243 $this->error = $customer_id;
244 return;
245 }
246
247 $this->set_id( $customer_id );
248 $this->save_metadata();
249 }
250
251 /**
252 * Delete customer
253 *
254 * @return bool
255 */
256 public function delete() {
257 require_once ABSPATH . 'wp-admin/includes/user.php';
258
259 // Get all bookings for this customer.
260 $bookings = $this->get_bookings();
261
262 // Delete all bookings for this customer.
263 foreach ( $bookings as $booking ) {
264 $booking = new Booking( $booking );
265 $booking->delete();
266 }
267
268 return wp_delete_user( $this->id );
269 }
270
271 /**
272 * Make customer using email
273 *
274 * @param array Customer data
275 *
276 * @return Customer
277 */
278 public function make( $args = [] ) {
279 $defaults = [
280 'first_name' => '',
281 'last_name' => '',
282 'email' => '',
283 'phone' => '',
284 ];
285
286 $args = wp_parse_args( $args, $defaults );
287
288 if ( email_exists( $args['email'] ) ) {
289 $user = get_user_by( 'email', $args['email'] );
290
291 if ( ! $this->is_customer() ) {
292 $user->add_role( 'timetics-customer' );
293 }
294 $this->set_id( $user->ID );
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