PluginProbe
Support Genix – Helpdesk, AI Chatbot, Knowledge Base & Customer Support Ticketing System / trunk
Support Genix – Helpdesk, AI Chatbot, Knowledge Base & Customer Support Ticketing System vtrunk
1.4.54 1.4.53 1.4.52 1.4.51 1.4.50 1.4.49 1.4.48 1.4.47 1.4.46 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 102 releases
support-genix-lite / modules / Apbd_wps_users.php

Apbd_wps_users.php in Support Genix – Helpdesk, AI Chatbot, Knowledge Base & Customer Support Ticketing System trunk, at modules/Apbd_wps_users.php

934 lines 34.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Users.
5 */
6
7 defined('ABSPATH') || exit;
8
9 class Apbd_wps_users extends ApbdWpsBaseModuleLite
10 {
11 public function initialize()
12 {
13 parent::initialize();
14 $this->disableDefaultForm();
15 $this->AddAjaxAction("add", [$this, "add"]);
16 $this->AddAjaxAction("data_search", [$this, "data_search"]);
17
18 $this->AddPortalAjaxAction("add", [$this, "add"]);
19 $this->AddPortalAjaxAction("data_search", [$this, "data_search"]);
20 $this->AddPortalAjaxAction("logout", [$this, "logout"]);
21 $this->AddPortalAjaxAction("update", [$this, "update"]);
22 $this->AddPortalAjaxAction("change_password", [$this, "change_password"]);
23
24 $this->AddPortalAjaxNoPrivAction("add_guest", [$this, "add_guest"]);
25 $this->AddPortalAjaxNoPrivAction("login", [$this, "login"]);
26 $this->AddPortalAjaxNoPrivAction("register", [$this, "register"]);
27 $this->AddPortalAjaxNoPrivAction("reset_password", [$this, "reset_password"]);
28 }
29
30 public function OnInit()
31 {
32 parent::OnInit();
33
34 /* Multiple user roles */
35 $multi_role_enabled = $this->is_multi_role_enbled();
36
37 if ($multi_role_enabled) {
38 add_action('user_new_form', [$this, 'new_profile_fields']);
39 add_action('show_user_profile', [$this, 'edit_profile_fields']);
40 add_action('edit_user_profile', [$this, 'edit_profile_fields']);
41 add_action('user_register', [$this, 'user_role_register'], 5);
42 add_action('profile_update', [$this, 'profile_role_update'], 10, 2);
43 }
44 }
45
46 public function add()
47 {
48 $apiResponse = new Apbd_Wps_APIResponse();
49 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
50
51 $data = [];
52 $hasError = false;
53
54 if (ApbdWps_IsPostBack && current_user_can('create-ticket-user')) {
55 $email = sanitize_email(ApbdWps_PostValue('email', ''));
56 $first_name = sanitize_text_field(ApbdWps_PostValue('first_name', ''));
57 $last_name = sanitize_text_field(ApbdWps_PostValue('last_name', ''));
58 $notify = sanitize_text_field(ApbdWps_PostValue('notify', ''));
59
60 $notify = 'Y' === $notify ? 'Y' : 'N';
61
62 if (
63 (1 > strlen($email)) ||
64 (1 > strlen($first_name))
65 ) {
66 $hasError = true;
67 }
68
69 $userObj = $this->CreateUser($email, $first_name, $last_name, $notify);
70
71 if ($userObj) {
72 $data = [
73 'id' => strval(absint($userObj->ID)),
74 'name' => $userObj->display_name,
75 'email' => $userObj->user_email,
76 'avatar' => get_avatar_url($userObj->ID),
77 ];
78
79 $userId = absint($userObj->ID);
80 $userObj = get_user_by("ID", $userId);
81
82 if ($userObj) {
83 $custom_fields_json = ApbdWps_PostValue('custom_fields', '');
84 $custom_fields = !empty($custom_fields_json) ? json_decode(stripslashes($custom_fields_json), true) : [];
85
86 if (!empty($custom_fields) && is_array($custom_fields)) {
87 $custom_fields = array_map(function ($value) {
88 return !is_bool($value) ? sanitize_text_field($value) : $value;
89 }, $custom_fields);
90 }
91
92 $userData = Apbd_Wps_User::get_user_data($userObj);
93 do_action('apbd-wps/action/user-created', $userData, $custom_fields);
94 }
95 }
96
97 if (!$hasError) {
98 if (!empty($data)) {
99 $apiResponse->SetResponse(true, $this->__('Successfully added.'), $data);
100 } else {
101 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
102 }
103 } else {
104 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
105 }
106 }
107
108 echo wp_json_encode($apiResponse);
109 }
110
111 public function add_guest()
112 {
113 $apiResponse = new Apbd_Wps_APIResponse();
114 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
115
116 $is_disabled = Apbd_wps_settings::GetModuleOption('disable_guest_ticket_creation', 'N');
117
118 if ('Y' === $is_disabled) {
119 wp_send_json_error([
120 'message' => $this->__('Invalid request.'),
121 'status' => 403
122 ], 403);
123 }
124
125 if (!Apbd_wps_settings::RegistrationAllowed()) {
126 wp_send_json_error([
127 'message' => $this->__('Sorry, you are not allowed to do that.'),
128 'status' => 403
129 ], 403);
130 }
131
132 $hasError = false;
133
134 if (ApbdWps_IsPostBack) {
135 $grcToken = ApbdWps_PostValue('grcToken', '');
136
137 $user = ApbdWps_PostValue('user', '');
138 $user = !empty($user) ? json_decode(stripslashes($user), true) : [];
139 $user = wp_parse_args($user, [
140 'email' => '',
141 'first_name' => '',
142 'last_name' => '',
143 'custom_fields' => [],
144 ]);
145
146 $ticket = ApbdWps_PostValue('ticket', '');
147 $ticket = !empty($ticket) ? json_decode(stripslashes($ticket), true) : [];
148 $ticket = wp_parse_args($ticket, [
149 'cat_id' => '',
150 'title' => '',
151 'ticket_body' => '',
152 'is_public' => 'N',
153 'custom_fields' => [],
154 ]);
155
156 $email = sanitize_email($user['email']);
157 $first_name = sanitize_text_field($user['first_name']);
158 $last_name = sanitize_text_field($user['last_name']);
159 $user_custom_fields = $user['custom_fields'];
160
161 if (is_array($user_custom_fields)) {
162 $user_custom_fields = array_map(function ($value) {
163 return !is_bool($value) ? sanitize_text_field($value) : $value;
164 }, $user_custom_fields);
165 } else {
166 $user_custom_fields = [];
167 }
168
169 $password = wp_generate_password();
170
171 $username = ApbdWps_GenerateBaseUsername($first_name, $last_name, '', $email);
172 $username = ApbdWps_GenerateUniqueUsername($username);
173
174 $cat_id = sanitize_text_field($ticket['cat_id']);
175 $title = sanitize_text_field($ticket['title']);
176 $ticket_body = sanitize_text_field($ticket['ticket_body']);
177 $is_public = sanitize_text_field($ticket['is_public']);
178 $ticket_custom_fields = $ticket['custom_fields'];
179
180 if (is_array($ticket_custom_fields)) {
181 $ticket_custom_fields = array_map(function ($value) {
182 return !is_bool($value) ? sanitize_text_field($value) : $value;
183 }, $ticket_custom_fields);
184 } else {
185 $ticket_custom_fields = [];
186 }
187
188 $cat_id = strval($cat_id);
189 $ticket_body = stripslashes($ticket_body);
190 $check__ticket_body = sanitize_text_field($ticket_body);
191 $is_public = 'Y' === $is_public ? 'Y' : 'N';
192
193 if (
194 (1 > strlen($email)) ||
195 (1 > strlen($first_name)) ||
196 (1 > strlen($password)) ||
197 (1 > strlen($username)) ||
198 (1 > strlen($title)) ||
199 (1 > strlen($check__ticket_body))
200 ) {
201 $hasError = true;
202 }
203
204 if (!$hasError) {
205 $userObj = get_user_by('email', $email);
206
207 if (!$userObj) {
208 $namespace = ApbdWps_SupportLite::getNamespaceStr();
209 $apiObj = new ApbdWpsAPI_User($namespace, false);
210
211 $apiObj->SetPayload('grcToken', $grcToken);
212
213 $apiObj->SetPayload('user', [
214 'email' => $email,
215 'first_name' => $first_name,
216 'last_name' => $last_name,
217 'username' => $username,
218 'password' => $password,
219 'custom_fields' => $user_custom_fields,
220 ]);
221
222 $apiObj->SetPayload('ticket', [
223 'cat_id' => $cat_id,
224 'title' => $title,
225 'ticket_body' => $ticket_body,
226 'is_public' => $is_public,
227 'custom_fields' => $ticket_custom_fields,
228 ]);
229
230 $resObj = $apiObj->create_user();
231 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
232
233 if ($resStatus) {
234 $apiResponse->SetResponse(true, $this->__('Successfully created.'));
235 } else {
236 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
237 }
238 } else {
239 $allCustomFields = $ticket_custom_fields;
240
241 if (! empty($user_custom_fields) && is_array($user_custom_fields)) {
242 $allCustomFields = array_merge($user_custom_fields, $allCustomFields);
243 }
244
245 if (! empty($allCustomFields)) {
246 $isValidCustomField = apply_filters('apbd-wps/filter/ticket-custom-field-valid', true, $allCustomFields, $email);
247
248 if (! $isValidCustomField) {
249 $msg = ApbdWps_GetMsgAPI();
250
251 if (empty($msg)) {
252 $msg = $this->__('Ticket creation failed.');
253 }
254
255 $apiResponse->SetResponse(false, $msg);
256 echo wp_json_encode($apiResponse);
257 return;
258 }
259 }
260
261 $namespace = ApbdWps_SupportLite::getNamespaceStr();
262 $apiObj = new ApbdWpsAPI_Ticket($namespace, false);
263
264 $ticket_user = isset($userObj->ID) ? absint($userObj->ID) : 0;
265
266 $apiObj->SetPayload('cat_id', $cat_id);
267 $apiObj->SetPayload('ticket_user', $ticket_user);
268 $apiObj->SetPayload('title', $title);
269 $apiObj->SetPayload('ticket_body', $ticket_body);
270 $apiObj->SetPayload('is_public', $is_public);
271 $apiObj->SetPayload('custom_fields', $ticket_custom_fields);
272
273 $resObj = $apiObj->create_ticket();
274 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
275
276 if ($resStatus) {
277 $apiResponse->SetResponse(true, $this->__('Successfully created.'));
278 } else {
279 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
280 }
281 }
282 } else {
283 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
284 }
285 }
286
287 echo wp_json_encode($apiResponse);
288 }
289
290 public function update()
291 {
292 $apiResponse = new Apbd_Wps_APIResponse();
293 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
294
295 $param_id = get_current_user_id();
296
297 $hasError = false;
298
299 if (ApbdWps_IsPostBack && !empty($param_id)) {
300 $first_name = sanitize_text_field(ApbdWps_PostValue('first_name', ''));
301 $last_name = sanitize_text_field(ApbdWps_PostValue('last_name', ''));
302 $custom_fields = ApbdWps_PostValue('custom_fields', '');
303
304 if (!empty($custom_fields)) {
305 $custom_fields = json_decode(stripslashes($custom_fields), true);
306
307 if (is_array($custom_fields)) {
308 $custom_fields = array_map(function ($value) {
309 return !is_bool($value) ? sanitize_text_field($value) : $value;
310 }, $custom_fields);
311 }
312 }
313
314 $custom_fields = is_array($custom_fields) ? $custom_fields : [];
315
316 if (1 > strlen($first_name)) {
317 $hasError = true;
318 }
319
320 if (!$hasError) {
321 $userObj = get_user_by('id', $param_id);
322
323 if ($userObj) {
324 $namespace = ApbdWps_SupportLite::getNamespaceStr();
325 $apiObj = new ApbdWpsAPI_User($namespace, false);
326
327 $apiObj->SetPayload('id', $param_id);
328 $apiObj->SetPayload('first_name', $first_name);
329 $apiObj->SetPayload('last_name', $last_name);
330 $apiObj->SetPayload('custom_fields', $custom_fields);
331 $apiObj->SetPayload('username', $userObj->user_login);
332 $apiObj->SetPayload('email', $userObj->user_email);
333
334 $resObj = $apiObj->update_client();
335 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
336
337 if ($resStatus) {
338 $apiResponse->SetResponse(true, $this->__('Successfully updated.'));
339 } else {
340 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
341 }
342 } else {
343 $apiResponse->SetResponse(false, $this->__('Invalid user.'));
344 }
345 } else {
346 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
347 }
348 }
349
350 echo wp_json_encode($apiResponse);
351 }
352
353 public function login()
354 {
355 $apiResponse = new Apbd_Wps_APIResponse();
356 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
357
358 $hasError = false;
359
360 if (ApbdWps_IsPostBack) {
361 $grcToken = ApbdWps_PostValue('grcToken', '');
362 $username = sanitize_text_field(ApbdWps_PostValue('username', ''));
363 $password = strval(ApbdWps_PostValue('password', ''));
364 $remember = ApbdWps_PostValue('remember', '');
365
366 if (
367 (1 > strlen($username)) ||
368 (1 > strlen($password))
369 ) {
370 $hasError = true;
371 }
372
373 if (!$hasError) {
374 $user = wp_authenticate($username, $password);
375
376 if (!is_wp_error($user)) {
377 $namespace = ApbdWps_SupportLite::getNamespaceStr();
378 $apiObj = new ApbdWpsAPI_User($namespace, false);
379
380 $apiObj->SetPayload('grcToken', $grcToken);
381 $apiObj->SetPayload('username', $username);
382 $apiObj->SetPayload('password', $password);
383 $apiObj->SetPayload('remember', $remember);
384
385 $resObj = $apiObj->user_login();
386 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
387
388 if ($resStatus) {
389 $apiResponse->SetResponse(true, $this->__('Login successful.'));
390 } else {
391 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
392 }
393 } else {
394 $apiResponse->SetResponse(false, $this->__('Invalid username or password.'));
395 }
396 } else {
397 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
398 }
399 }
400
401 echo wp_json_encode($apiResponse);
402 }
403
404 public function logout()
405 {
406 $apiResponse = new Apbd_Wps_APIResponse();
407 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
408
409 if (ApbdWps_IsPostBack) {
410 $namespace = ApbdWps_SupportLite::getNamespaceStr();
411 $apiObj = new ApbdWpsAPI_User($namespace, false);
412
413 $resObj = $apiObj->user_logout();
414 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
415
416 if ($resStatus) {
417 $apiResponse->SetResponse(true, $this->__('Logout successful.'));
418 } else {
419 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
420 }
421 }
422
423 echo wp_json_encode($apiResponse);
424 }
425
426 public function register()
427 {
428 $apiResponse = new Apbd_Wps_APIResponse();
429 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
430
431 $is_disabled = Apbd_wps_settings::GetModuleOption('disable_registration_form', 'N');
432
433 if ('Y' === $is_disabled) {
434 wp_send_json_error([
435 'message' => $this->__('Invalid request.'),
436 'status' => 403
437 ], 403);
438 }
439
440 if (!Apbd_wps_settings::RegistrationAllowed()) {
441 wp_send_json_error([
442 'message' => $this->__('Sorry, you are not allowed to do that.'),
443 'status' => 403
444 ], 403);
445 }
446
447 $hasError = false;
448
449 if (ApbdWps_IsPostBack) {
450 $grcToken = ApbdWps_PostValue('grcToken', '');
451 $email = sanitize_email(ApbdWps_PostValue('email', ''));
452 $first_name = sanitize_text_field(ApbdWps_PostValue('first_name', ''));
453 $last_name = sanitize_text_field(ApbdWps_PostValue('last_name', ''));
454 $password = strval(ApbdWps_PostValue('password', ''));
455 $custom_fields = ApbdWps_PostValue('custom_fields', '');
456
457 if (!empty($custom_fields)) {
458 $custom_fields = json_decode(stripslashes($custom_fields), true);
459
460 if (is_array($custom_fields)) {
461 $custom_fields = array_map(function ($value) {
462 return !is_bool($value) ? sanitize_text_field($value) : $value;
463 }, $custom_fields);
464 }
465 }
466
467 $custom_fields = is_array($custom_fields) ? $custom_fields : [];
468
469 $username = ApbdWps_GenerateBaseUsername($first_name, $last_name, '', $email);
470 $username = ApbdWps_GenerateUniqueUsername($username);
471
472 if (
473 (1 > strlen($email)) ||
474 (1 > strlen($first_name)) ||
475 (1 > strlen($last_name)) ||
476 (1 > strlen($password)) ||
477 (1 > strlen($username))
478 ) {
479 $hasError = true;
480 }
481
482 if (!$hasError) {
483 $userObj = get_user_by('email', $email);
484
485 if (!$userObj) {
486 $namespace = ApbdWps_SupportLite::getNamespaceStr();
487 $apiObj = new ApbdWpsAPI_User($namespace, false);
488
489 $apiObj->SetPayload('grcToken', $grcToken);
490 $apiObj->SetPayload('id', null);
491 $apiObj->SetPayload('email', $email);
492 $apiObj->SetPayload('first_name', $first_name);
493 $apiObj->SetPayload('last_name', $last_name);
494 $apiObj->SetPayload('username', $username);
495 $apiObj->SetPayload('password', $password);
496 $apiObj->SetPayload('custom_fields', $custom_fields);
497 $apiObj->SetPayload('image', '');
498 $apiObj->SetPayload('role', '');
499
500 $resObj = $apiObj->create_client();
501 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
502
503 if ($resStatus) {
504 $apiResponse->SetResponse(true, $this->__('Registration successful.'));
505 } else {
506 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
507 }
508 } else {
509 $apiResponse->SetResponse(false, $this->__('User already exists.'));
510 }
511 } else {
512 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
513 }
514 }
515
516 echo wp_json_encode($apiResponse);
517 }
518
519 public function reset_password()
520 {
521 $apiResponse = new Apbd_Wps_APIResponse();
522 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
523
524 $hasError = false;
525
526 if (ApbdWps_IsPostBack) {
527 $grcToken = ApbdWps_PostValue('grcToken', '');
528 $username = sanitize_text_field(ApbdWps_PostValue('username', ''));
529
530 if (1 > strlen($username)) {
531 $hasError = true;
532 }
533
534 if (!$hasError) {
535 $userObj = get_user_by('email', $username);
536
537 if (!$userObj) {
538 $userObj = get_user_by('login', $username);
539 }
540
541 if ($userObj) {
542 $namespace = ApbdWps_SupportLite::getNamespaceStr();
543 $apiObj = new ApbdWpsAPI_User($namespace, false);
544
545 $apiObj->SetPayload('grcToken', $grcToken);
546 $apiObj->SetPayload('username', $username);
547
548 $resObj = $apiObj->reset_password();
549 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
550
551 if ($resStatus) {
552 $apiResponse->SetResponse(true, $this->__('Check your email for the confirmation link, then visit the login page.'));
553 } else {
554 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
555 }
556 } else {
557 $apiResponse->SetResponse(false, $this->__('Invalid username or email address.'));
558 }
559 } else {
560 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
561 }
562 }
563
564 echo wp_json_encode($apiResponse);
565 }
566
567 public function change_password()
568 {
569 $apiResponse = new Apbd_Wps_APIResponse();
570 $apiResponse->SetResponse(false, $this->__('Invalid request.'));
571
572 $hasError = false;
573
574 if (ApbdWps_IsPostBack) {
575 $old_password = strval(ApbdWps_PostValue('old_password', ''));
576 $new_password = strval(ApbdWps_PostValue('new_password', ''));
577
578 if (
579 (1 > strlen($old_password)) ||
580 (1 > strlen($new_password))
581 ) {
582 $hasError = true;
583 }
584
585 if (!$hasError) {
586 $namespace = ApbdWps_SupportLite::getNamespaceStr();
587 $apiObj = new ApbdWpsAPI_User($namespace, false);
588
589 $apiObj->SetPayload('oldPass', $old_password);
590 $apiObj->SetPayload('newPass', $new_password);
591
592 $resObj = $apiObj->change_pass();
593 $resStatus = isset($resObj->status) ? rest_sanitize_boolean($resObj->status) : false;
594
595 if ($resStatus) {
596 $apiResponse->SetResponse(true, $this->__('Successfully updated.'));
597 } else {
598 $apiResponse->SetResponse(false, $this->__('Something went wrong.'));
599 }
600 } else {
601 $apiResponse->SetResponse(false, $this->__('Invalid data.'));
602 }
603 }
604
605 echo wp_json_encode($apiResponse);
606 }
607
608 public function data_search($term = '')
609 {
610 $apiResponse = new Apbd_Wps_APIResponse();
611 $apiResponse->SetResponse(true, "", []);
612
613 $term = ApbdWps_GetValue("term", '');
614 $term = sanitize_text_field($term);
615
616 $sort = ApbdWps_GetValue("sort");
617 $page = ApbdWps_GetValue("page");
618 $limit = ApbdWps_GetValue("limit");
619
620 $orderBy = 'id';
621 $order = 'ASC';
622
623 if ($sort) {
624 $sort = explode('-', $sort);
625
626 if (isset($sort[0]) && !empty($sort[0])) {
627 $orderBy = sanitize_key($sort[0]);
628 }
629
630 if (isset($sort[1]) && !empty($sort[1])) {
631 $order = 'asc' === sanitize_key($sort[1]) ? 'ASC' : 'DESC';
632 }
633 }
634
635 $page = max(absint($page), 1);
636 $limit = max(absint($limit), 10);
637 $offset = ($limit * ($page - 1));
638
639 $queryArgs = [
640 'search' => '*' . esc_attr($term) . '*',
641 'number' => $limit,
642 'offset' => $offset,
643 'orderby' => $orderBy,
644 'order' => $order
645 ];
646
647 $sortArgs = apply_filters('support_genix_sort_args_for_ticket_user_query', ['orderby' => $orderBy, 'order' => $order]);
648 $queryArgs = apply_filters('support_genix_query_args_for_ticket_user_fetch', array_merge($queryArgs, $sortArgs));
649
650 $result = get_users($queryArgs);
651
652 $data = [];
653
654 if (is_array($result) && !empty($result)) {
655 foreach ($result as $userObj) {
656 $data[] = [
657 'id' => strval(absint($userObj->ID)),
658 'name' => $userObj->display_name,
659 'email' => $userObj->user_email,
660 'avatar' => get_avatar_url($userObj->ID),
661 ];
662 }
663 }
664
665 $apiResponse->SetResponse(true, "", $data);
666
667 echo wp_json_encode($apiResponse);
668 }
669
670 public function CreateUser($email, $firstName, $lastName, $notify = 'N')
671 {
672 $userObj = get_user_by("email", $email);
673
674 if (!$userObj) {
675 $username = ApbdWps_GenerateBaseUsername($firstName, $lastName, '', $email);
676 $username = ApbdWps_GenerateUniqueUsername($username);
677
678 $password = wp_generate_password();
679
680 $userId = wp_create_user($username, $password, $email);
681 $userId = !is_wp_error($userId) ? $userId : 0;
682
683 if (!empty($userId)) {
684 $display_name = trim($firstName . " " . $lastName);
685
686 wp_update_user([
687 "ID" => $userId,
688 "first_name" => $firstName,
689 "last_name" => $lastName,
690 "display_name" => $display_name,
691 ]);
692
693 if ('Y' === $notify) {
694 wp_send_new_user_notifications($userId, 'user');
695 }
696
697 $userObj = get_user_by("id", $userId);
698 }
699 }
700
701 return $userObj;
702 }
703
704 /* Multiple user roles */
705
706 public function new_profile_fields()
707 {
708 if (!current_user_can('promote_users')) {
709 return;
710 }
711
712 $creating = isset($_POST['createuser']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Read-only field renderer gated by current_user_can('promote_users'); reads $_POST only to preselect the default role, no state change.
713 $user_roles = $creating && isset($_POST['role']) ? sanitize_text_field(wp_unslash($_POST['role'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Read-only field renderer gated by current_user_can('promote_users'); reads $_POST only to preselect the default role, no state change.
714
715 if (!$user_roles) {
716 $user_roles = get_option('default_role');
717 }
718
719 $roles = get_editable_roles();
720 $user_roles = is_array($user_roles) ? $user_roles : array($user_roles);
721 $opts_roles = array_map(function ($role) {
722 return translate_user_role($role['name']);
723 }, $roles);
724
725 wp_nonce_field('sgenix_wp_user_roles', 'sgenix_wp_user_roles_nonce');
726 ?>
727 <table class="form-table">
728 <tr>
729 <th><?php $this->_e('User Roles'); ?></th>
730 <td>
731 <div class="wp-tab-panel sgenix-wp-user-roles-tab-panel">
732 <ul>
733 <?php foreach ($opts_roles as $role_key => $role_name): ?>
734 <li>
735 <label title="<?php echo esc_attr($role_key); ?>">
736 <input type="checkbox" name="sgenix_wp_user_roles[]" value="<?php echo esc_attr($role_key); ?>" <?php checked(in_array($role_key, $user_roles)); ?> />
737 <?php echo esc_html($role_name); ?>
738 </label>
739 </li>
740 <?php endforeach; ?>
741 </ul>
742 </div>
743 </td>
744 </tr>
745 </table>
746 <style type="text/css">
747 .sgenix-wp-user-roles-tab-panel {
748 min-height: unset !important;
749 max-height: unset !important;
750 }
751
752 @media only screen and (min-width: 783px) {
753 .sgenix-wp-user-roles-tab-panel {
754 max-width: calc(500px - 1.8em) !important;
755 }
756 }
757 </style>
758 <script type="text/javascript">
759 jQuery(document).ready(function() {
760 jQuery('select#role').closest('tr').remove();
761 });
762 </script>
763 <?php
764 }
765
766 public function edit_profile_fields($user)
767 {
768 if (
769 !current_user_can('promote_users') ||
770 !current_user_can('edit_user', $user->ID)
771 ) {
772 return;
773 }
774
775 $roles = get_editable_roles();
776 $user_roles = array_intersect(array_values($user->roles), array_keys($roles));
777 $opts_roles = array_map(function ($role) {
778 return translate_user_role($role['name']);
779 }, $roles);
780
781 wp_nonce_field('sgenix_wp_user_roles', 'sgenix_wp_user_roles_nonce');
782 ?>
783 <h2><?php $this->_e('Roles'); ?></h2>
784 <table class="form-table">
785 <tr>
786 <th><?php $this->_e('User Roles'); ?></th>
787 <td>
788 <div class="wp-tab-panel sgenix-wp-user-roles-tab-panel">
789 <ul>
790 <?php foreach ($opts_roles as $role_key => $role_name): ?>
791 <li>
792 <label title="<?php echo esc_attr($role_key); ?>">
793 <input type="checkbox" name="sgenix_wp_user_roles[]" value="<?php echo esc_attr($role_key); ?>" <?php checked(in_array($role_key, $user_roles)); ?> />
794 <?php echo esc_html($role_name); ?>
795 </label>
796 </li>
797 <?php endforeach; ?>
798 </ul>
799 </div>
800 </td>
801 </tr>
802 </table>
803 <style type="text/css">
804 .user-role-wrap {
805 display: none !important;
806 }
807
808 .sgenix-wp-user-roles-tab-panel {
809 min-height: unset !important;
810 max-height: unset !important;
811 }
812
813 @media only screen and (min-width: 783px) {
814 .sgenix-wp-user-roles-tab-panel {
815 max-width: calc(500px - 1.8em) !important;
816 }
817 }
818 </style>
819 <script type="text/javascript">
820 jQuery(document).ready(function() {
821 jQuery('.user-role-wrap').remove();
822 });
823 </script>
824 <?php
825 }
826
827 public function user_role_register($user_id)
828 {
829 if (
830 !current_user_can('promote_users') ||
831 !isset($_POST['sgenix_wp_user_roles_nonce']) ||
832 !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['sgenix_wp_user_roles_nonce'])), 'sgenix_wp_user_roles')
833 ) {
834 return;
835 }
836
837 $user = new \WP_User($user_id);
838
839 $roles = get_editable_roles();
840 $key_roles = array_keys($roles);
841 $old_roles = array_intersect(array_values($user->roles), $key_roles);
842
843 if (!empty($_POST['sgenix_wp_user_roles'])) {
844 $new_roles = array_map([$this, 'sanitize_user_role_key'], wp_unslash((array) $_POST['sgenix_wp_user_roles'])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each element sanitized via sanitize_user_role_key() callback.
845
846 foreach ($new_roles as $new_role) {
847 if (
848 in_array($new_role, $key_roles) &&
849 !in_array($new_role, $old_roles)
850 ) {
851 $user->add_role($new_role);
852 }
853 }
854
855 foreach ($old_roles as $old_role) {
856 if (
857 in_array($old_role, $key_roles) &&
858 !in_array($old_role, $new_roles)
859 ) {
860 $user->remove_role($old_role);
861 }
862 }
863 } else {
864 foreach ((array) $user->roles as $old_role) {
865 if (in_array($old_role, $key_roles)) {
866 $user->remove_role($old_role);
867 }
868 }
869 }
870 }
871
872 public function profile_role_update($user_id, $old_user)
873 {
874 if (
875 !current_user_can('promote_users') ||
876 !current_user_can('edit_user', $user_id) ||
877 !isset($_POST['sgenix_wp_user_roles_nonce']) ||
878 !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['sgenix_wp_user_roles_nonce'])), 'sgenix_wp_user_roles')
879 ) {
880 return;
881 }
882
883 $roles = get_editable_roles();
884 $key_roles = array_keys($roles);
885 $old_roles = array_intersect(array_values($old_user->roles), $key_roles);
886
887 if (!empty($_POST['sgenix_wp_user_roles'])) {
888 $new_roles = array_map([$this, 'sanitize_user_role_key'], wp_unslash((array) $_POST['sgenix_wp_user_roles'])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each element sanitized via sanitize_user_role_key() callback.
889
890 foreach ($new_roles as $new_role) {
891 if (
892 in_array($new_role, $key_roles) &&
893 !in_array($new_role, $old_roles)
894 ) {
895 $old_user->add_role($new_role);
896 }
897 }
898
899 foreach ($old_roles as $old_role) {
900 if (
901 in_array($old_role, $key_roles) &&
902 !in_array($old_role, $new_roles)
903 ) {
904 $old_user->remove_role($old_role);
905 }
906 }
907 } else {
908 foreach ($old_roles as $old_role) {
909 if (in_array($old_role, $key_roles)) {
910 $old_user->remove_role($old_role);
911 }
912 }
913 }
914 }
915
916 public function sanitize_user_role_key($role)
917 {
918 $_role = strtolower($role);
919 $_role = preg_replace('/[^a-z0-9_\-\s]/', '', $_role);
920 $_role = str_replace(' ', '_', $_role);
921
922 return $_role;
923 }
924
925 public function is_multi_role_enbled()
926 {
927 $is_enabled = Apbd_wps_settings::GetModuleOption('user_multi_role_field');
928 $is_enabled = sanitize_text_field($is_enabled);
929 $is_enabled = (('Y' === $is_enabled) ? true : false);
930
931 return $is_enabled;
932 }
933 }
934