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 / staffs / staff.php

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

406 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 * 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 $this->get_prop( 'first_name', '' );
90 }
91
92 /**
93 * Get last name
94 *
95 * @return string
96 */
97 public function get_last_name() {
98 return $this->get_prop( 'last_name', '' );
99 }
100
101 /**
102 * Get staff display name
103 *
104 * @return string
105 */
106 public function get_display_name() {
107 $user = get_userdata( $this->id );
108 $display_name = '';
109
110 if ( $user ) {
111 $display_name = $user->display_name;
112 }
113
114 return $display_name;
115 }
116
117 /**
118 * Get user email
119 *
120 * @return string
121 */
122 public function get_email() {
123 $staff = get_userdata( $this->id );
124
125 if ( $staff ) {
126 return $staff->user_email;
127 }
128
129 return '';
130 }
131
132 /**
133 * Get phone
134 *
135 * @return string
136 */
137 public function get_phone() {
138 return $this->get_prop( 'phone', '' );
139 }
140
141 /**
142 * Get image
143 *
144 * @return string
145 */
146 public function get_image() {
147 $image = $this->get_prop( 'image' );
148
149 if ( ! $image ) {
150 return get_avatar_url( $this->id );
151 }
152
153 return wp_get_attachment_image_url( $image );
154 }
155
156 /**
157 * Get user name
158 *
159 * @return string
160 */
161 public function get_user_name() {
162 return get_userdata($this->id)->user_login;
163 }
164
165 /**
166 * Get services
167 *
168 * @return array
169 */
170 public function get_service() {
171 return $this->get_prop( 'service' );
172 }
173
174 /**
175 * Get schedule
176 *
177 * @return array
178 */
179 public function get_schedule() {
180 return $this->get_prop( 'schedule', []);
181 }
182
183 /**
184 * Get staff status
185 *
186 * @return mixed
187 */
188 public function get_status() {
189 return $this->get_prop( 'status' );
190 }
191
192 /**
193 * Get all data for a staff
194 *
195 * @return array
196 */
197 public function get_data() {
198 return [
199 'id' => $this->get_id(),
200 'full_name' => $this->get_display_name(),
201 'first_name' => $this->get_first_name(),
202 'last_name' => $this->get_last_name(),
203 'user_name' => $this->get_user_name(),
204 'email' => $this->get_email(),
205 'phone' => $this->get_phone(),
206 'image' => $this->get_image(),
207 'status' => $this->get_status(),
208 'service' => $this->get_service(),
209 'schedule' => $this->get_schedule(),
210 ];
211 }
212
213 /**
214 * Get appointment data
215 *
216 * @param string $prop
217 *
218 * @return mixed
219 */
220 private function get_prop( $prop = '', $default = false ) {
221 $data = $this->get_metadata( $prop );
222
223 if ( ! $data ) {
224 return $default;
225 }
226
227 return $data;
228 }
229
230 /**
231 * Get metadata
232 *
233 * @param string $prop
234 *
235 * @return mixed
236 */
237 private function get_metadata( $prop = '' ) {
238 $meta_key = $this->meta_prefix . $prop;
239
240 return get_user_meta( $this->id, $meta_key, true );
241 }
242
243 /**
244 * Set id
245 *
246 * @param integer $id User ID
247 *
248 * @return void
249 */
250 public function set_id( $id ) {
251 $this->id = $id;
252 }
253
254 /**
255 * Set props
256 *
257 * @param array $args User Data
258 *
259 * @return void
260 */
261 public function set_props( $args = [] ) {
262 $this->data = wp_parse_args( $args, $this->data );
263 }
264
265 /**
266 * Create staff
267 *
268 * @param array $args Staff data
269 *
270 * @return bool
271 */
272 public function create( $args = [] ) {
273 $defaults = [
274 'first_name' => '',
275 'last_name' => '',
276 'user_login' => '',
277 'user_email' => '',
278 'user_pass' => wp_generate_password(),
279 'role' => 'timetics-staff',
280 ];
281
282 $args = wp_parse_args( $args, $defaults );
283 $user_id = wp_insert_user( $args );
284
285 if ( ! is_wp_error( $user_id ) ) {
286 $this->set_id( $user_id );
287 $this->save_metadata( $args );
288 $this->retrieve_password();
289 }
290
291 return $user_id;
292 }
293
294 /**
295 * Update staff data
296 *
297 * @return void
298 */
299 public function update( $args = [] ) {
300 $user = get_userdata( $this->id )->to_array();
301
302 if ( ! empty( $args['user_pass'] ) ) {
303 $user['user_pass'] = $args['user_pass'];
304 }
305
306 if ( ! empty( $args['email'] ) ) {
307 $user['user_email'] = $args['email'];
308 }
309
310 $updated = wp_update_user( $user );
311
312 if ( ! is_wp_error( $updated ) ) {
313 $this->save_metadata( $args );
314 }
315
316 return $updated;
317 }
318
319 /**
320 * Update meta data
321 *
322 * @return void
323 */
324 public function save_metadata( $data = []) {
325 foreach ( $data as $key => $value ) {
326 if ( ! isset( $this->data[ $key ] )) {
327 continue;
328 }
329
330 // Prepare meta key.
331 $meta_key = $this->meta_prefix . $key;
332
333 // Update appointment meta data.
334 update_user_meta( $this->id, $meta_key, $value );
335 }
336 }
337
338 /**
339 * Send password reset email
340 *
341 * @return void
342 */
343 public function retrieve_password() {
344 if ( 0 === intval( $this->get_status() ) ) {
345 retrieve_password( $this->get_email() );
346 }
347 }
348
349 /**
350 * Delete staff
351 *
352 * @return bool
353 */
354 public function delete() {
355 require_once ABSPATH . 'wp-admin/includes/user.php';
356 $user = get_userdata( $this->id );
357
358 if ( count( $user->roles ) > 1 ) {
359 $user->remove_role('timetics-staff');
360
361 return true;
362 }
363
364 return wp_delete_user( $this->id );
365 }
366
367 /**
368 * Check user is valid staff
369 *
370 * @return bool
371 */
372 public function is_staff() {
373 $user = get_userdata( $this->id );
374
375 if ( $user ) {
376 return in_array( 'timetics-staff', $user->roles, true );
377 }
378
379 return false;
380 }
381
382 /**
383 * Get all appointments
384 *
385 * @param array $args Appointments args
386 *
387 * @return array
388 */
389 public static function all( $args = [] ) {
390 $defaults = [
391 'role' => 'timetics-staff',
392 'number' => 20,
393 'paged' => 1,
394 ];
395
396 $args = wp_parse_args( $args, $defaults );
397
398 $users = new WP_User_Query( $args );
399
400 return [
401 'total' => $users->get_total(),
402 'items' => $users->get_results(),
403 ];
404 }
405 }
406