PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.2
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.2
6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / Ajax / Users.php
kirki / includes / Ajax Last commit date
Collaboration 2 weeks ago Apps.php 1 month ago Collection.php 2 months ago Comments.php 3 months ago DynamicContent.php 1 week ago ExportImport.php 1 week ago Form.php 1 month ago Media.php 3 weeks ago Page.php 2 weeks ago PageSettings.php 1 month ago RBAC.php 1 month ago Symbol.php 1 month ago Taxonomy.php 3 months ago TemplateExportImport.php 3 months ago UserData.php 1 month ago Users.php 4 days ago Walkthrough.php 2 weeks ago WordpressData.php 3 months ago WpAdmin.php 3 weeks ago
Users.php
236 lines
1 <?php
2
3 namespace Kirki\Ajax;
4
5 use Kirki\HelperFunctions;
6
7 class Users {
8 /**
9 * Get users data based on the query
10 *
11 * @return void wp_send_json($users);
12 */
13
14 public static function get_users_of_collection() {
15 $inherit = HelperFunctions::sanitize_text( $_GET['inherit'] ?? null );
16 $post_parent = HelperFunctions::sanitize_text( $_GET['post_parent'] ?? null );
17
18 $sorting_param = HelperFunctions::sanitize_text( $_GET['sorting'] ?? null );
19 $filter_param = HelperFunctions::sanitize_text( $_GET['filters'] ?? null );
20
21 $item_per_page = HelperFunctions::sanitize_text( $_GET['items'] ?? 3 );
22 $current_page = HelperFunctions::sanitize_text( $_GET['current_page'] ?? 1 );
23 $offset = HelperFunctions::sanitize_text( $_GET['offset'] ?? 0 );
24 $query = HelperFunctions::sanitize_text( isset( $_GET['q'] ) ? $_GET['q'] : '' );
25
26 $sorting = null;
27 $filters = null;
28
29 if ( isset( $sorting_param ) ) {
30 $sorting = json_decode( stripslashes( $sorting_param ), true );
31 }
32
33 if ( isset( $filter_param ) ) {
34 $filters = json_decode( stripslashes( $filter_param ), true );
35 }
36
37 $users = self::get_users(
38 array(
39 'inherit' => $inherit,
40 'post_parent' => $post_parent,
41 'sorting' => $sorting,
42 'filters' => $filters,
43 'item_per_page' => $item_per_page,
44 'current_page' => $current_page,
45 'offset' => $offset,
46 'q' => $query,
47 )
48 );
49
50 wp_send_json( $users );
51 }
52
53 public static function get_users( $params ) {
54 $inherit = (bool) ( $params['inherit'] ?? false );
55 $post_parent = (int) ( $params['post_parent'] ?? 0 );
56
57 $sorting = isset( $params['sorting'] ) ? $params['sorting'] : null;
58 $filters = isset( $params['filters'] ) ? $params['filters'] : null;
59 $item_per_page = isset( $params['item_per_page'] ) ? $params['item_per_page'] : 3;
60 $current_page = isset( $params['current_page'] ) ? $params['current_page'] : 1;
61 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
62 $query = isset( $params['q'] ) ? $params['q'] : '';
63
64 // Calculate the offset
65 $offset_cal = ( $current_page - 1 ) * $item_per_page + $offset;
66
67 if ( ! $query ) {
68 $query = HelperFunctions::sanitize_text( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' );
69 }
70
71 $args = array(
72 'number' => $item_per_page,
73 'paged' => $current_page,
74 'offset' => $offset_cal,
75 'search' => '*' . $query . '*', // The dynamic search query
76 'search_columns' => array( 'user_nicename', 'user_email', 'display_name' ), // Fields to search in
77 );
78
79 if ( isset( $filters ) && is_array( $filters ) ) {
80 foreach ( $filters as $filter_item ) {
81 $field_name = isset( $filter_item['id'] ) ? $filter_item['id'] : '';
82
83 if ( ! $field_name ) {
84 continue;
85 }
86
87 switch ( $field_name ) {
88 case 'date_query': {
89 /**
90 * $filter_item['items'] max contain one array.
91 * in array may contain start-date, end-date
92 * Like: [{"start-date": "2020-01-01","end-date": "2020-01-02"}]
93 */
94
95 if ( isset( $filter_item['items'], $filter_item['items'][0] ) ) {
96 $items = $filter_item['items'];
97 $item = $items[0]; // Get first item in array.
98
99 $date_query = array();
100 $date_query['inclusive'] = true;
101
102 if ( ! empty( $item['start-date'] ) ) {
103 $date_query['after'] = $item['start-date'];
104 }
105
106 if ( ! empty( $item['end-date'] ) ) {
107 $date_query['before'] = $item['end-date'];
108 }
109
110 $args['date_query'] = $date_query;
111 }
112
113 break;
114 }
115
116 case 'role': {
117 /**
118 * $filter_item['items'] must not contain more than 2 array of conditions.
119 * 1 array may contain 'in' conditions and another for 'not-in' conditions
120 * And values of 'in' and 'not-in' conditions should not collide
121 * Like: the condition should not be author 'in' [1, 2, 3] and 'not-in' [2, 4, 5]
122 */
123 $items = $filter_item['items'];
124
125 foreach ( $items as $item ) {
126 if ( isset( $item['condition'], $item['values'] ) && is_array( $item['values'] ) ) {
127 if ( $item['condition'] === 'in' ) {
128 $args['role__in'] = $item['values'];
129 }
130
131 if ( $item['condition'] === 'not-in' ) {
132 $args['role__not_in'] = $item['values'];
133 }
134 }
135 }
136
137 break;
138 }
139 }
140 }
141 }
142
143 if ( ! empty( $sorting ) && is_array( $sorting ) ) {
144 $order = $sorting['order'] ?? $sorting['type'] ?? null;
145 $orderby = $sorting['orderby'] ?? $sorting['value'] ?? null;
146 if ( $order ) {
147 $args['order'] = $order;
148 }
149 if ( $orderby ) {
150 $args['orderby'] = $orderby;
151 }
152 }
153
154 $users = get_users( $args );
155
156 // To get total users based on filters, exclude pagination from $args
157 $args_for_count = array_merge(
158 $args,
159 array(
160 'number' => '',
161 'paged' => '',
162 'count_total' => true,
163 )
164 );
165
166 // Fetch total users count
167 $total_users = count( get_users( $args_for_count ) );
168 $total_users = $total_users - $offset;
169
170 // Calculate pagination
171 $total_pages = ceil( $total_users / $item_per_page );
172 $previous_page = ( $current_page > 1 ) ? $current_page - 1 : null;
173 $next_page = ( $current_page < $total_pages ) ? $current_page + 1 : null;
174
175 // Prepare pagination array
176 $pagination = array(
177 'per_page' => $item_per_page,
178 'current_page' => $current_page,
179 'total_pages' => $total_pages,
180 'previous' => $previous_page,
181 'next' => $next_page,
182 'total_count' => $total_users,
183 );
184
185 // Return the users and pagination info
186 return array(
187 'data' => self::get_formatted_users_data( $users ),
188 'pagination' => $pagination,
189 );
190 }
191
192 public static function get_formatted_users_data( $users ) {
193 // Initialize an empty array to store formatted user data
194 $formatted_users = array();
195
196 // Loop through each user and format the data
197 foreach ( $users as $user ) {
198 // Add user data to the array
199 $formatted_users[] = self::get_format_single_user_data( $user );
200 }
201
202 return $formatted_users;
203 }
204
205 /**
206 * The function `get_format_single_user_data` retrieves and formats specific user data including ID,
207 * display name, email, profile URL, and avatar URL.
208 */
209 public static function get_format_single_user_data( $user ) {
210 // Get the user's avatar (profile image) URL
211 $avatar_url = get_avatar_url( $user->user_email );
212
213 // Get the user's profile page URL
214 $profile_url = get_author_posts_url( $user->ID );
215
216 return array(
217 'ID' => $user->ID,
218 'display_name' => $user->display_name,
219 'user_email' => $user->user_email,
220 'user_nicename' => $user->user_nicename,
221 'user_registered' => $user->user_registered,
222 'user_status' => $user->user_status,
223 'user_url' => $profile_url,
224 'profile_image' => $avatar_url, // Only the main profile image
225 'roles' => $user->roles,
226 );
227 }
228
229 public static function get_user_by_id( $user_id ) {
230 $user = get_user_by( 'id', $user_id );
231
232 $format_user = self::get_format_single_user_data( $user );
233 return $format_user;
234 }
235 }
236