PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
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 3 weeks ago Apps.php 3 weeks ago Collection.php 1 month ago Comments.php 2 months ago DynamicContent.php 1 month ago ExportImport.php 3 weeks ago Form.php 3 weeks ago Media.php 3 weeks ago Page.php 3 weeks ago PageSettings.php 3 weeks ago RBAC.php 3 weeks ago Symbol.php 3 weeks ago Taxonomy.php 2 months ago TemplateExportImport.php 2 months ago UserData.php 3 weeks ago Users.php 2 months ago Walkthrough.php 2 months ago WordpressData.php 2 months ago WpAdmin.php 2 months ago
Users.php
234 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 ( $sorting ) {
144 if ( isset( $sorting['order'] ) ) {
145 $args['order'] = $sorting['order'];
146 }
147 if ( isset( $sorting['orderby'] ) ) {
148 $args['orderby'] = $sorting['orderby'];
149 }
150 }
151
152 $users = get_users( $args );
153
154 // To get total users based on filters, exclude pagination from $args
155 $args_for_count = array_merge(
156 $args,
157 array(
158 'number' => '',
159 'paged' => '',
160 'count_total' => true,
161 )
162 );
163
164 // Fetch total users count
165 $total_users = count( get_users( $args_for_count ) );
166 $total_users = $total_users - $offset;
167
168 // Calculate pagination
169 $total_pages = ceil( $total_users / $item_per_page );
170 $previous_page = ( $current_page > 1 ) ? $current_page - 1 : null;
171 $next_page = ( $current_page < $total_pages ) ? $current_page + 1 : null;
172
173 // Prepare pagination array
174 $pagination = array(
175 'per_page' => $item_per_page,
176 'current_page' => $current_page,
177 'total_pages' => $total_pages,
178 'previous' => $previous_page,
179 'next' => $next_page,
180 'total_count' => $total_users,
181 );
182
183 // Return the users and pagination info
184 return array(
185 'data' => self::get_formatted_users_data( $users ),
186 'pagination' => $pagination,
187 );
188 }
189
190 public static function get_formatted_users_data( $users ) {
191 // Initialize an empty array to store formatted user data
192 $formatted_users = array();
193
194 // Loop through each user and format the data
195 foreach ( $users as $user ) {
196 // Add user data to the array
197 $formatted_users[] = self::get_format_single_user_data( $user );
198 }
199
200 return $formatted_users;
201 }
202
203 /**
204 * The function `get_format_single_user_data` retrieves and formats specific user data including ID,
205 * display name, email, profile URL, and avatar URL.
206 */
207 public static function get_format_single_user_data( $user ) {
208 // Get the user's avatar (profile image) URL
209 $avatar_url = get_avatar_url( $user->user_email );
210
211 // Get the user's profile page URL
212 $profile_url = get_author_posts_url( $user->ID );
213
214 return array(
215 'ID' => $user->ID,
216 'display_name' => $user->display_name,
217 'user_email' => $user->user_email,
218 'user_nicename' => $user->user_nicename,
219 'user_registered' => $user->user_registered,
220 'user_status' => $user->user_status,
221 'user_url' => $profile_url,
222 'profile_image' => $avatar_url, // Only the main profile image
223 'roles' => $user->roles,
224 );
225 }
226
227 public static function get_user_by_id( $user_id ) {
228 $user = get_user_by( 'id', $user_id );
229
230 $format_user = self::get_format_single_user_data( $user );
231 return $format_user;
232 }
233 }
234