PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.21
Timetics – Appointment Booking Calendar & Scheduling v1.0.21
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 / staffs / staff.php

staff.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.21, at core/staffs/staff.php

484 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Staff Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Staffs;
8
9 use WP_User_Query;
10
11 /**
12 * Class Staff
13 */
14 class Staff {
15 /**
16 * Store staff role
17 *
18 * @var string
19 */
20 protected $role = 'timetics-staff';
21
22 /**
23 * Store meta prefix
24 *
25 * @var string
26 */
27 protected $meta_prefix = '_staff_';
28
29 /**
30 * Store staff id
31 *
32 * @var integer
33 */
34 protected $id;
35
36 /**
37 * Store staff data
38 *
39 * @var array
40 */
41 protected $data = [
42 'first_name' => '',
43 'last_name' => '',
44 'user_email' => '',
45 'image' => '',
46 'user_login' => '',
47 'phone' => '',
48 'schedule' => [],
49 'status' => 0,
50 ];
51
52 /**
53 * Store WP_Error
54 *
55 * @var Object
56 */
57 public $error;
58
59 /**
60 * Staff Constructor
61 *
62 * @return void
63 */
64 public function __construct( $staff = 0 ) {
65 if ( $staff instanceof self ) {
66 $this->set_id( $staff->get_id() );
67 } elseif ( ! empty( $staff->ID ) ) {
68 $this->set_id( $staff->ID );
69 } elseif ( is_numeric( $staff ) && $staff > 0 ) {
70 $this->set_id( $staff );
71 }
72 }
73
74 /**
75 * Get user id
76 *
77 * @return integer
78 */
79 public function get_id() {
80 return $this->id;
81 }
82
83 /**
84 * Get first name
85 *
86 * @return string
87 */
88 public function get_first_name() {
89 return get_user_meta( $this->id, 'first_name', true );
90 }
91
92 /**
93 * Get last name
94 *
95 * @return string
96 */
97 public function get_last_name() {
98 return get_user_meta( $this->id, 'last_name', true );
99 }
100
101 /**
102 * Get password
103 *
104 * @return string
105 */
106 public function get_password() {
107 $user = get_user_by( 'id', $this->id );
108 return $user->user_pass;
109 }
110
111 /**
112 * Get staff display name
113 *
114 * @return string
115 */
116 public function get_display_name() {
117 $user = get_userdata( $this->id );
118 $display_name = '';
119
120 if ( $user ) {
121 $display_name = $user->display_name;
122 }
123
124 return $display_name;
125 }
126
127 /**
128 * Get user email
129 *
130 * @return string
131 */
132 public function get_email() {
133 $staff = get_userdata( $this->id );
134
135 if ( $staff ) {
136 return $staff->user_email;
137 }
138
139 return '';
140 }
141
142 /**
143 * Get phone
144 *
145 * @return string
146 */
147 public function get_phone() {
148 return $this->get_prop( 'phone', '' );
149 }
150
151 /**
152 * Get image
153 *
154 * @return string
155 */
156 public function get_image() {
157 $image = $this->get_prop( 'image' );
158
159 if ( filter_var( $image, FILTER_VALIDATE_URL ) ) {
160 return $image;
161 }
162
163
164 if ( ! $image ) {
165 return false;
166 }
167
168 return wp_get_attachment_image_url( $image );
169 }
170
171 /**
172 * Get user name
173 *
174 * @return string
175 */
176 public function get_user_name() {
177 return get_userdata($this->id)->user_login;
178 }
179
180 /**
181 * Get services
182 *
183 * @return array
184 */
185 public function get_service() {
186 return $this->get_prop( 'service' );
187 }
188
189 /**
190 * Get schedule
191 *
192 * @return array
193 */
194 public function get_schedule() {
195 return $this->get_prop( 'schedule', []);
196 }
197
198 /**
199 * Get staff status
200 *
201 * @return mixed
202 */
203 public function get_status() {
204 $user = get_userdata( $this->id );
205
206 if ( user_can( $user, 'manage_options' ) ) {
207 return 1;
208 }
209
210 return $this->get_prop( 'status' );
211 }
212
213 /**
214 * Get full name;
215 *
216 * @return string
217 */
218 public function get_full_name() {
219 $first_name = $this->get_first_name();
220 $last_name = $this->get_last_name();
221
222 if ( ! $first_name ) {
223 return $this->get_display_name();
224 }
225
226 if ( ! $last_name ) {
227 return $first_name;
228 }
229
230
231
232 return $first_name . ' ' . $last_name;
233 }
234
235 /**
236 * Get all data for a staff
237 *
238 * @return array
239 */
240 public function get_data() {
241 return [
242 'id' => $this->get_id(),
243 'full_name' => $this->get_full_name(),
244 'first_name' => $this->get_first_name(),
245 'last_name' => $this->get_last_name(),
246 'user_name' => $this->get_user_name(),
247 'email' => $this->get_email(),
248 'phone' => $this->get_phone(),
249 'image' => $this->get_image(),
250 'status' => $this->get_status(),
251 'service' => $this->get_service(),
252 'schedule' => $this->get_schedule(),
253 ];
254 }
255
256 /**
257 * Get appointment data
258 *
259 * @param string $prop
260 *
261 * @return mixed
262 */
263 private function get_prop( $prop = '', $default = false ) {
264 $data = $this->get_metadata( $prop );
265
266 if ( ! $data ) {
267 return $default;
268 }
269
270 return $data;
271 }
272
273 /**
274 * Get metadata
275 *
276 * @param string $prop
277 *
278 * @return mixed
279 */
280 private function get_metadata( $prop = '' ) {
281 $meta_key = $this->meta_prefix . $prop;
282
283 return get_user_meta( $this->id, $meta_key, true );
284 }
285
286 /**
287 * Set id
288 *
289 * @param integer $id User ID
290 *
291 * @return void
292 */
293 public function set_id( $id ) {
294 $this->id = $id;
295 }
296
297 /**
298 * Set props
299 *
300 * @param array $args User Data
301 *
302 * @return void
303 */
304 public function set_props( $args = [] ) {
305 $this->data = wp_parse_args( $args, $this->data );
306 }
307
308 /**
309 * Create staff
310 *
311 * @param array $args Staff data
312 *
313 * @return bool
314 */
315 public function create( $args = [] ) {
316 $defaults = [
317 'first_name' => '',
318 'last_name' => '',
319 'user_login' => '',
320 'user_email' => '',
321 'user_pass' => wp_generate_password(),
322 'role' => 'timetics-staff',
323 ];
324
325 $args = wp_parse_args( $args, $defaults );
326
327 $email = ! empty( $args['user_email'] ) ? $args['user_email'] : '';
328
329 if ( $email ) {
330 $user['user_email'] = $email;
331 }
332
333 $user = get_user_by( 'email', $email );
334
335 if ( $user && ! in_array( 'timetics-staff', $user->roles, true ) ) {
336 $user->add_role( 'timetics-staff' );
337
338 return $user->ID;
339 }
340
341 $user_id = wp_insert_user( $args );
342
343 if ( ! is_wp_error( $user_id ) ) {
344 $this->set_id( $user_id );
345 $this->save_metadata( $args );
346 $this->retrieve_password();
347 }
348
349 return $user_id;
350 }
351
352 /**
353 * Update staff data
354 *
355 * @return void
356 */
357 public function update( $args = [] ) {
358 $user = get_userdata( $this->id )->to_array();
359 $email = ! empty( $args['user_email'] ) ? $args['user_email'] : '';
360
361 if ( ! empty( $args['user_pass'] ) ) {
362 $user['user_pass'] = $args['user_pass'];
363 }
364
365 if ( $email ) {
366 $user['user_email'] = $email;
367 }
368
369 $user_data = get_user_by( 'email', $email );
370
371 if ( $user_data && ! in_array( 'timetics-staff', $user_data->roles, true ) ) {
372 $user_data->add_role( 'timetics-staff' );
373
374 return $user_data->ID;
375 }
376
377 $updated = wp_update_user( $user );
378
379 if ( ! is_wp_error( $updated ) ) {
380
381 $this->save_metadata( $args );
382 }
383
384 return $updated;
385 }
386
387 /**
388 * Update meta data
389 *
390 * @return void
391 */
392 public function save_metadata( $data = []) {
393 foreach ( $data as $key => $value ) {
394 if ( ! isset( $this->data[ $key ] )) {
395 continue;
396 }
397
398 // Prepare meta key.
399 if( $key == 'first_name' || $key == 'last_name' ) {
400 $meta_key = $key;
401 } else {
402 $meta_key = $this->meta_prefix . $key;
403 }
404
405 // Update appointment meta data.
406 update_user_meta( $this->id, $meta_key, $value );
407 }
408 }
409
410 /**
411 * Send password reset email
412 *
413 * @return void
414 */
415 public function retrieve_password() {
416 if ( 0 === intval( $this->get_status() ) ) {
417 retrieve_password( $this->get_email() );
418 }
419 }
420
421 /**
422 * Delete staff
423 *
424 * @return bool
425 */
426 public function delete() {
427 require_once ABSPATH . 'wp-admin/includes/user.php';
428 $user = get_userdata( $this->id );
429
430 if ( count( $user->roles ) > 1 ) {
431 $user->remove_role('timetics-staff');
432
433 return true;
434 }
435
436 if ( is_multisite() ) {
437 require_once ABSPATH . 'wp-admin/includes/ms.php';
438
439 return wpmu_delete_user( $this->id );
440 }
441
442 return wp_delete_user( $this->id );
443 }
444
445 /**
446 * Check user is valid staff
447 *
448 * @return bool
449 */
450 public function is_staff() {
451 $user = get_userdata( $this->id );
452
453 if ( $user ) {
454 return in_array( 'timetics-staff', $user->roles, true );
455 }
456
457 return false;
458 }
459
460 /**
461 * Get all appointments
462 *
463 * @param array $args Appointments args
464 *
465 * @return array
466 */
467 public static function all( $args = [] ) {
468 $defaults = [
469 'role' => 'timetics-staff',
470 'number' => 20,
471 'paged' => 1,
472 ];
473
474 $args = wp_parse_args( $args, $defaults );
475
476 $users = new WP_User_Query( $args );
477
478 return [
479 'total' => $users->get_total(),
480 'items' => $users->get_results(),
481 ];
482 }
483 }
484