PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Permissions / SearchPermissionsUsersController.php

SearchPermissionsUsersController.php in The Innovative Form Builder – IvyForms 1.5, at backend/src/Controllers/Permissions/SearchPermissionsUsersController.php

92 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 */
6
7 namespace IvyForms\Controllers\Permissions;
8
9 // phpcs:disable PSR1.Files.SideEffects
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use IvyForms\Common\Exceptions\ForbiddenException;
15 use IvyForms\Common\Sanitizer\Sanitizer;
16 use IvyForms\Controllers\Controller;
17 use IvyForms\Services\Permissions\IvyFormsAccess;
18 use IvyForms\Services\Translations\BackendStrings;
19 use WP_REST_Request;
20 use WP_REST_Response;
21 use WP_User;
22 use WP_User_Query;
23
24 class SearchPermissionsUsersController extends Controller
25 {
26 private const USERS_RESULT_LIMIT = 50;
27
28 private const MIN_SEARCH_LENGTH = 2;
29
30 /**
31 * @throws ForbiddenException
32 */
33 public function handle(WP_REST_Request $data): WP_REST_Response
34 {
35 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
36 $search = sanitize_text_field((string) $data->get_param('search'));
37 $search = trim($search);
38
39 $args = [
40 'orderby' => 'user_email',
41 'order' => 'ASC',
42 'fields' => ['ID', 'user_email'],
43 'role__not_in' => ['administrator'],
44 'count_total' => false,
45 'number' => self::USERS_RESULT_LIMIT,
46 ];
47
48 if ($search !== '' && strlen($search) >= self::MIN_SEARCH_LENGTH) {
49 $args['search'] = '*' . $search . '*';
50 $args['search_columns'] = ['user_login', 'user_email', 'display_name'];
51 }
52
53 $query = new WP_User_Query($args);
54 $users = [];
55 foreach ($query->get_results() as $row) {
56 $userId = is_object($row) ? (int) ($row->ID ?? 0) : 0;
57 if ($userId <= 0) {
58 continue;
59 }
60
61 $user = get_userdata($userId);
62 if (!$user instanceof WP_User) {
63 continue;
64 }
65
66 // Exclude users who already have full / implicit IvyForms or site-admin access.
67 if (IvyFormsAccess::userShouldBeExcludedFromPermissionsPicker($user)) {
68 continue;
69 }
70
71 $email = trim((string) $user->user_email);
72 if ($email === '') {
73 continue;
74 }
75
76 $users[] = [
77 'id' => $userId,
78 'email' => $email,
79 'label' => $email,
80 ];
81 }
82
83 return new WP_REST_Response(
84 [
85 'message' => BackendStrings::getCommonStrings()['ok'],
86 'users' => $users,
87 ],
88 200
89 );
90 }
91 }
92