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

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

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