PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.1
Timetics – Appointment Booking Calendar & Scheduling v1.0.1
1.0.64 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 All 65 releases
timetics / core / customers / customer.php

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

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