PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | includes/admin/tools/bulk-awards.php +646 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,646 @@
1 +<?php
2 +/**
3 + * Bulk Awards Tool
4 + *
5 + * @package GamiPress\Admin\Tools\Bulk_Awards
6 + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.4.1
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Register Bulk Awards Tool meta boxes
14 + *
15 + * @since 1.4.1
16 + *
17 + * @param array $meta_boxes
18 + *
19 + * @return array
20 + */
21 +function gamipress_bulk_awards_tool_meta_boxes( $meta_boxes ) {
22 +
23 + // Grab our points types as an array
24 + $points_types_options = array(
25 + '' => __( 'Choose a points type', 'gamipress' )
26 + );
27 +
28 + foreach( gamipress_get_points_types() as $slug => $data ) {
29 + $points_types_options[$slug] = $data['plural_name'];
30 + }
31 +
32 + // Grab our requirement types as an array
33 + $requirement_types_options = array(
34 + '' => __( 'Choose a requirement type', 'gamipress' )
35 + );
36 +
37 + foreach( gamipress_get_requirement_types() as $slug => $data ) {
38 + $requirement_types_options[$slug] = $data['singular_name'];
39 + }
40 +
41 + $meta_boxes['bulk-awards'] = array(
42 + 'title' => gamipress_dashicon( 'yes' ) . __( 'Bulk Awards', 'gamipress' ),
43 + 'fields' => apply_filters( 'gamipress_bulk_awards_tool_fields', array(
44 +
45 + // Points
46 +
47 + 'bulk_award_points' => array(
48 + 'name' => __( 'Points to Award', 'gamipress' ),
49 + 'tooltip' => __( 'Points amount to award (the amount will be added to the current user balance).', 'gamipress' ),
50 + 'label_cb' => 'cmb_tooltip_label_cb',
51 + 'type' => 'text_small',
52 + 'default' => '0',
53 + ),
54 + 'bulk_award_points_type' => array(
55 + 'name' => __( 'Points Type', 'gamipress' ),
56 + 'tooltip' => __( 'Points type of points amount to award.', 'gamipress' ),
57 + 'label_cb' => 'cmb_tooltip_label_cb',
58 + 'type' => 'select',
59 + 'options' => $points_types_options
60 + ),
61 + 'bulk_award_points_register_movement' => array(
62 + 'name' => __( 'Register on user earnings', 'gamipress' ),
63 + 'tooltip' => __( 'Check this option to register this balance movement on user earnings.', 'gamipress' ),
64 + 'label_cb' => 'cmb_tooltip_label_cb',
65 + 'type' => 'checkbox',
66 + 'classes' => 'gamipress-switch'
67 + ),
68 + 'bulk_award_points_earnings_text' => array(
69 + 'name' => __( 'Earning entry text', 'gamipress' ),
70 + 'tooltip' => __( 'Enter the line text to be displayed on user earnings.', 'gamipress' ),
71 + 'label_cb' => 'cmb_tooltip_label_cb',
72 + 'type' => 'text',
73 + 'show_if' => array( 'bulk_award_points_register_movement' => 'checked' ),
74 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
75 + 'default' => __( 'Manual balance adjustment', 'gamipress' ),
76 + ),
77 + 'bulk_award_points_all_users' => array(
78 + 'name' => __( 'Award to all users', 'gamipress' ),
79 + 'tooltip' => __( 'Check this option to award the points amount to all users.', 'gamipress' ),
80 + 'label_cb' => 'cmb_tooltip_label_cb',
81 + 'type' => 'checkbox',
82 + 'classes' => 'gamipress-switch'
83 + ),
84 + 'bulk_award_points_users' => array(
85 + 'name' => __( 'Users to award', 'gamipress' ),
86 + 'tooltip' => __( 'Choose users to award this points amount.', 'gamipress' ),
87 + 'label_cb' => 'cmb_tooltip_label_cb',
88 + 'type' => 'advanced_select',
89 + 'multiple' => true,
90 + 'custom_classes' => 'gamipress-user-selector',
91 + 'attributes' => array(
92 + 'data-placeholder' => __( 'Choose users(s)', 'gamipress' ),
93 + 'data-close-on-select' => 'false',
94 + ),
95 + 'options_cb' => 'gamipress_options_cb_users',
96 + 'hide_if' => array( 'bulk_award_points_all_users' => 'checked' ),
97 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
98 + ),
99 + 'bulk_award_points_roles' => array(
100 + 'name' => __( 'Roles to award', 'gamipress' ),
101 + 'tooltip' => __( 'Choose roles to award this points amount.', 'gamipress' ),
102 + 'label_cb' => 'cmb_tooltip_label_cb',
103 + 'type' => 'advanced_select',
104 + 'multiple' => true,
105 + 'custom_classes' => 'gamipress-selector',
106 + 'attributes' => array(
107 + 'data-placeholder' => __( 'Choose role(s)', 'gamipress' ),
108 + 'data-close-on-select' => 'false',
109 + ),
110 + 'options_cb' => 'gamipress_options_cb_roles',
111 + 'hide_if' => array( 'bulk_award_points_all_users' => 'checked' ),
112 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
113 + ),
114 + 'bulk_award_points_button' => array(
115 + 'label' => __( 'Award Points', 'gamipress' ),
116 + 'type' => 'button',
117 + 'button' => 'primary'
118 + ),
119 +
120 + // Achievements
121 +
122 + 'bulk_award_achievements' => array(
123 + 'name' => __( 'Achievements to Award', 'gamipress' ),
124 + 'tooltip' => __( 'Choose the achievements to award.', 'gamipress' ),
125 + 'label_cb' => 'cmb_tooltip_label_cb',
126 + 'type' => 'advanced_select',
127 + 'multiple' => true,
128 + 'classes' => 'gamipress-post-selector',
129 + 'attributes' => array(
130 + 'data-post-type' => implode( ',', gamipress_get_achievement_types_slugs() ),
131 + 'data-placeholder' => __( 'Choose achievement(s)', 'gamipress' ),
132 + 'data-close-on-select' => 'false',
133 + ),
134 + 'options_cb' => 'gamipress_options_cb_posts',
135 + ),
136 + 'bulk_award_achievements_all_users' => array(
137 + 'name' => __( 'Award to all users', 'gamipress' ),
138 + 'tooltip' => __( 'Check this option to award the achievements to all users.', 'gamipress' ),
139 + 'label_cb' => 'cmb_tooltip_label_cb',
140 + 'type' => 'checkbox',
141 + 'classes' => 'gamipress-switch'
142 + ),
143 + 'bulk_award_achievements_users' => array(
144 + 'name' => __( 'Users to award', 'gamipress' ),
145 + 'tooltip' => __( 'Choose users to award this achievements.', 'gamipress' ),
146 + 'label_cb' => 'cmb_tooltip_label_cb',
147 + 'type' => 'advanced_select',
148 + 'multiple' => true,
149 + 'custom_classes' => 'gamipress-user-selector',
150 + 'attributes' => array(
151 + 'data-placeholder' => __( 'Choose users(s)', 'gamipress' ),
152 + 'data-close-on-select' => 'false',
153 + ),
154 + 'options_cb' => 'gamipress_options_cb_users',
155 + 'hide_if' => array( 'bulk_award_achievements_all_users' => 'checked' ),
156 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
157 + ),
158 + 'bulk_award_achievements_roles' => array(
159 + 'name' => __( 'Roles to award', 'gamipress' ),
160 + 'tooltip' => __( 'Choose roles to award this achievements.', 'gamipress' ),
161 + 'label_cb' => 'cmb_tooltip_label_cb',
162 + 'type' => 'advanced_select',
163 + 'multiple' => true,
164 + 'custom_classes' => 'gamipress-selector',
165 + 'attributes' => array(
166 + 'data-placeholder' => __( 'Choose role(s)', 'gamipress' ),
167 + 'data-close-on-select' => 'false',
168 + ),
169 + 'options_cb' => 'gamipress_options_cb_roles',
170 + 'hide_if' => array( 'bulk_award_achievements_all_users' => 'checked' ),
171 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
172 + ),
173 + 'bulk_award_achievements_button' => array(
174 + 'label' => __( 'Award Achievements', 'gamipress' ),
175 + 'type' => 'button',
176 + 'button' => 'primary'
177 + ),
178 +
179 + // Ranks
180 +
181 + 'bulk_award_rank' => array(
182 + 'name' => __( 'Rank to Award', 'gamipress' ),
183 + 'tooltip' => __( 'Choose the rank to award.', 'gamipress' ),
184 + 'label_cb' => 'cmb_tooltip_label_cb',
185 + 'desc' => __( '<strong>Important!</strong> Users on higher rank will be downgrade to this rank.', 'gamipress' ),
186 + 'type' => 'advanced_select',
187 + 'classes' => 'gamipress-post-selector',
188 + 'attributes' => array(
189 + 'data-post-type' => implode( ',', gamipress_get_rank_types_slugs() ),
190 + 'data-placeholder' => __( 'Choose rank', 'gamipress' ),
191 + ),
192 + 'options_cb' => 'gamipress_options_cb_posts',
193 + ),
194 + 'bulk_award_rank_all_users' => array(
195 + 'name' => __( 'Award to all users', 'gamipress' ),
196 + 'tooltip' => __( 'Check this option to award the rank to all users.', 'gamipress' ),
197 + 'label_cb' => 'cmb_tooltip_label_cb',
198 + 'type' => 'checkbox',
199 + 'classes' => 'gamipress-switch'
200 + ),
201 + 'bulk_award_rank_users' => array(
202 + 'name' => __( 'Users to award', 'gamipress' ),
203 + 'tooltip' => __( 'Choose users to award this rank.', 'gamipress' ),
204 + 'label_cb' => 'cmb_tooltip_label_cb',
205 + 'type' => 'advanced_select',
206 + 'multiple' => true,
207 + 'custom_classes' => 'gamipress-user-selector',
208 + 'attributes' => array(
209 + 'data-placeholder' => __( 'Choose users(s)', 'gamipress' ),
210 + 'data-close-on-select' => 'false',
211 + ),
212 + 'options_cb' => 'gamipress_options_cb_users',
213 + 'hide_if' => array( 'bulk_award_rank_all_users' => 'checked' ),
214 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
215 + ),
216 + 'bulk_award_rank_roles' => array(
217 + 'name' => __( 'Roles to award', 'gamipress' ),
218 + 'tooltip' => __( 'Choose roles to award this rank.', 'gamipress' ),
219 + 'label_cb' => 'cmb_tooltip_label_cb',
220 + 'type' => 'advanced_select',
221 + 'multiple' => true,
222 + 'custom_classes' => 'gamipress-selector',
223 + 'attributes' => array(
224 + 'data-placeholder' => __( 'Choose role(s)', 'gamipress' ),
225 + 'data-close-on-select' => 'false',
226 + ),
227 + 'options_cb' => 'gamipress_options_cb_roles',
228 + 'hide_if' => array( 'bulk_award_rank_all_users' => 'checked' ),
229 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
230 + ),
231 + 'bulk_award_rank_button' => array(
232 + 'label' => __( 'Award Rank', 'gamipress' ),
233 + 'type' => 'button',
234 + 'button' => 'primary'
235 + ),
236 +
237 + // Requirements
238 +
239 + 'bulk_award_requirements_type' => array(
240 + 'name' => __( 'Requirement Type', 'gamipress' ),
241 + 'tooltip' => __( 'Choose the requirement type to award.', 'gamipress' ),
242 + 'label_cb' => 'cmb_tooltip_label_cb',
243 + 'type' => 'select',
244 + 'options' => $requirement_types_options,
245 + ),
246 + 'bulk_award_requirements' => array(
247 + 'name' => __( 'Requirements to Award', 'gamipress' ),
248 + 'tooltip' => __( 'Choose the requirements to award.', 'gamipress' ),
249 + 'label_cb' => 'cmb_tooltip_label_cb',
250 + 'type' => 'advanced_select',
251 + 'multiple' => true,
252 + 'custom_classes' => 'gamipress-post-selector',
253 + 'attributes' => array(
254 + 'data-action' => 'gamipress_get_requirements',
255 + 'data-post-type' => implode( ',', gamipress_get_requirement_types_slugs() ),
256 + 'data-placeholder' => __( 'Choose requirement(s)', 'gamipress' ),
257 + 'data-close-on-select' => 'false',
258 + ),
259 + 'options_cb' => 'gamipress_options_cb_posts',
260 + 'hide_if' => array( 'bulk_award_requirements_type' => '' ),
261 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
262 + ),
263 + 'bulk_award_requirements_all_users' => array(
264 + 'name' => __( 'Award to all users', 'gamipress' ),
265 + 'tooltip' => __( 'Check this option to award the requirements to all users.', 'gamipress' ),
266 + 'label_cb' => 'cmb_tooltip_label_cb',
267 + 'type' => 'checkbox',
268 + 'classes' => 'gamipress-switch'
269 + ),
270 + 'bulk_award_requirements_users' => array(
271 + 'name' => __( 'Users to award', 'gamipress' ),
272 + 'tooltip' => __( 'Choose users to award this requirements.', 'gamipress' ),
273 + 'label_cb' => 'cmb_tooltip_label_cb',
274 + 'type' => 'advanced_select',
275 + 'multiple' => true,
276 + 'custom_classes' => 'gamipress-user-selector',
277 + 'attributes' => array(
278 + 'data-placeholder' => __( 'Choose users(s)', 'gamipress' ),
279 + 'data-close-on-select' => 'false',
280 + ),
281 + 'options_cb' => 'gamipress_options_cb_users',
282 + 'hide_if' => array( 'bulk_award_requirements_all_users' => 'checked' ),
283 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
284 + ),
285 + 'bulk_award_requirements_roles' => array(
286 + 'name' => __( 'Roles to award', 'gamipress' ),
287 + 'tooltip' => __( 'Choose roles to award this requirements.', 'gamipress' ),
288 + 'label_cb' => 'cmb_tooltip_label_cb',
289 + 'type' => 'advanced_select',
290 + 'multiple' => true,
291 + 'custom_classes' => 'gamipress-selector',
292 + 'attributes' => array(
293 + 'data-placeholder' => __( 'Choose role(s)', 'gamipress' ),
294 + 'data-close-on-select' => 'false',
295 + ),
296 + 'options_cb' => 'gamipress_options_cb_roles',
297 + 'hide_if' => array( 'bulk_award_requirements_all_users' => 'checked' ),
298 + 'classes_cb' => 'cmb_conditional_fields_classes_cb',
299 + ),
300 + 'bulk_award_requirements_button' => array(
301 + 'label' => __( 'Award Requirements', 'gamipress' ),
302 + 'type' => 'button',
303 + 'button' => 'primary'
304 + ),
305 + ) ),
306 + 'vertical_tabs' => true,
307 + 'tabs' => apply_filters( 'gamipress_bulk_awards_tool_tabs', array(
308 + 'bulk_award_points' => array(
309 + 'icon' => 'dashicons-star-filled',
310 + 'title' => __( 'Points', 'gamipress' ),
311 + 'fields' => array(
312 + 'bulk_award_points',
313 + 'bulk_award_points_type',
314 + 'bulk_award_points_register_movement',
315 + 'bulk_award_points_earnings_text',
316 + 'bulk_award_points_all_users',
317 + 'bulk_award_points_users',
318 + 'bulk_award_points_roles',
319 + 'bulk_award_points_button',
320 + ),
321 + ),
322 + 'bulk_award_achievements' => array(
323 + 'icon' => 'dashicons-awards',
324 + 'title' => __( 'Achievements', 'gamipress' ),
325 + 'fields' => array(
326 + 'bulk_award_achievements',
327 + 'bulk_award_achievements_all_users',
328 + 'bulk_award_achievements_users',
329 + 'bulk_award_achievements_roles',
330 + 'bulk_award_achievements_button',
331 + ),
332 + ),
333 + 'bulk_award_rank' => array(
334 + 'icon' => 'dashicons-rank',
335 + 'title' => __( 'Ranks', 'gamipress' ),
336 + 'fields' => array(
337 + 'bulk_award_rank',
338 + 'bulk_award_rank_all_users',
339 + 'bulk_award_rank_users',
340 + 'bulk_award_rank_roles',
341 + 'bulk_award_rank_button',
342 + ),
343 + ),
344 + 'bulk_award_requirement' => array(
345 + 'icon' => 'dashicons-editor-ol',
346 + 'title' => __( 'Requirements', 'gamipress' ),
347 + 'fields' => array(
348 + 'bulk_award_requirements_type',
349 + 'bulk_award_requirements',
350 + 'bulk_award_requirements_all_users',
351 + 'bulk_award_requirements_users',
352 + 'bulk_award_requirements_roles',
353 + 'bulk_award_requirements_button',
354 + ),
355 + )
356 + ) )
357 + );
358 +
359 + return $meta_boxes;
360 +
361 +}
362 +add_filter( 'gamipress_tools_general_meta_boxes', 'gamipress_bulk_awards_tool_meta_boxes' );
363 +
364 +/**
365 + * AJAX handler for the recount activity tool
366 + *
367 + * @since 1.4.1
368 + */
369 +function gamipress_ajax_bulk_awards_tool() {
370 + // Security check, forces to die if not security passed
371 + check_ajax_referer( 'gamipress_admin', 'nonce' );
372 +
373 + global $wpdb;
374 +
375 + // Check user capabilities
376 + if( ! current_user_can( gamipress_get_manager_capability() ) ) {
377 + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
378 + }
379 +
380 + // Check parameters received
381 + if( ! isset( $_POST['bulk_award'] ) || empty( $_POST['bulk_award'] ) ) {
382 + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
383 + }
384 +
385 + $bulk_award = sanitize_text_field( $_POST['bulk_award'] );
386 + $loop = ( ! isset( $_POST['loop'] ) ? 0 : absint( $_POST['loop'] ) );
387 + $limit = 100;
388 + $offset = $limit * $loop;
389 + $run_again = false;
390 +
391 + ignore_user_abort( true );
392 +
393 + if ( ! gamipress_is_function_disabled( 'set_time_limit' ) )
394 + set_time_limit( 0 );
395 +
396 + // Initialize vars
397 + $points_types = gamipress_get_points_types();
398 + $rank_types = gamipress_get_rank_types();
399 + $to_all_users = false;
400 + $specific_users = array();
401 + $specific_roles = array();
402 + $join = array();
403 + $where = array();
404 +
405 + if( $bulk_award === 'points' ) {
406 + // Check points parameters
407 +
408 + $points_to_award = absint( $_POST['bulk_award_points'] );
409 +
410 + if( $points_to_award === 0 ) {
411 + wp_send_json_error( __( 'Choose a valid points amount to award.', 'gamipress' ) );
412 + }
413 +
414 + $points_type_to_award = sanitize_text_field( $_POST['bulk_award_points_type'] );
415 +
416 + if( $points_type_to_award === '' || ! isset( $points_types[$points_type_to_award] ) ) {
417 + wp_send_json_error( __( 'Choose a valid points type.', 'gamipress' ) );
418 + }
419 +
420 + $to_all_users = isset( $_POST['bulk_award_points_all_users'] );
421 +
422 + if( ! $to_all_users ) {
423 + $specific_users = isset( $_POST['bulk_award_points_users'] ) ? $_POST['bulk_award_points_users'] : array();
424 + $specific_roles = isset( $_POST['bulk_award_points_roles'] ) ? $_POST['bulk_award_points_roles'] : array();
425 + }
426 +
427 + } else if( $bulk_award === 'achievements' ) {
428 + // Check achievements parameters
429 +
430 + $achievements = isset( $_POST['bulk_award_achievements'] ) ? $_POST['bulk_award_achievements'] : array();
431 +
432 + if( empty( $achievements ) ) {
433 + wp_send_json_error( __( 'Choose at least one achievement to be awarded.', 'gamipress' ) );
434 + }
435 +
436 + $to_all_users = isset( $_POST['bulk_award_achievements_all_users'] );
437 +
438 + if( ! $to_all_users ) {
439 + $specific_users = isset( $_POST['bulk_award_achievements_users'] ) ? $_POST['bulk_award_achievements_users'] : array();
440 + $specific_roles = isset( $_POST['bulk_award_achievements_roles'] ) ? $_POST['bulk_award_achievements_roles'] : array();
441 + }
442 +
443 + } else if( $bulk_award === 'rank' ) {
444 + // Check rank parameters
445 +
446 + $rank_id = absint( $_POST['bulk_award_rank'] );
447 +
448 + $rank = get_post( $rank_id );
449 +
450 + if( ! $rank || ! isset( $rank_types[$rank->post_type] ) ) {
451 + wp_send_json_error( __( 'Choose a valid rank to be awarded.', 'gamipress' ) );
452 + }
453 +
454 + $to_all_users = isset( $_POST['bulk_award_rank_all_users'] );
455 +
456 + if( ! $to_all_users ) {
457 + $specific_users = isset( $_POST['bulk_award_rank_users'] ) ? $_POST['bulk_award_rank_users'] : array();
458 + $specific_roles = isset( $_POST['bulk_award_rank_roles'] ) ? $_POST['bulk_award_rank_roles'] : array();
459 + }
460 +
461 + } else if( $bulk_award === 'requirements' ) {
462 + // Check requirements parameters
463 +
464 + $requirements = isset( $_POST['bulk_award_requirements'] ) ? $_POST['bulk_award_requirements'] : array();
465 +
466 + if( empty( $requirements ) ) {
467 + wp_send_json_error( __( 'Choose at least one requirement to be awarded.', 'gamipress' ) );
468 + }
469 +
470 + $to_all_users = isset( $_POST['bulk_award_requirements_all_users'] );
471 +
472 + if( ! $to_all_users ) {
473 + $specific_users = isset( $_POST['bulk_award_requirements_users'] ) ? $_POST['bulk_award_requirements_users'] : array();
474 + $specific_roles = isset( $_POST['bulk_award_requirements_roles'] ) ? $_POST['bulk_award_requirements_roles'] : array();
475 + }
476 +
477 + }
478 +
479 + // Ensure all specific users are correct
480 + foreach( $specific_users as $i => $specific_user ) {
481 + if( absint( $specific_user ) === 0 )
482 + unset( $specific_users[$i] );
483 + }
484 +
485 + // Ensure all specific roles are correct
486 + $editable_roles = get_editable_roles();
487 +
488 + foreach( $specific_roles as $i => $specific_role ) {
489 + if( ! isset( $editable_roles[$specific_role] ) )
490 + unset( $specific_roles[$i] );
491 + }
492 +
493 + // Check users parameters
494 + if( ! $to_all_users && empty( $specific_users ) && empty( $specific_roles ) )
495 + wp_send_json_error( __( 'Choose at least one user to award.', 'gamipress' ) );
496 +
497 + if( $to_all_users ) {
498 +
499 + // Get stored users
500 + $users = gamipress_get_users( array(
501 + 'offset' => $offset,
502 + 'limit' => $limit,
503 + ) );
504 +
505 + // Return a success message if finished, else run again
506 + if( empty( $users ) && $loop !== 0 ) {
507 + wp_send_json_success( __( 'Bulk award process has been done successfully.', 'gamipress' ) );
508 + } else {
509 + $run_again = true;
510 + }
511 +
512 + } else {
513 + // Get specific stored users
514 +
515 + // Setup specific users where
516 + if( ! empty( $specific_users ) && count( $specific_users ) > 0 ) {
517 +
518 + // Sanitize user IDs
519 + foreach ( $specific_users as $i => $user_id ) {
520 + $specific_users[$i] = absint( $user_id );
521 + }
522 +
523 + $where[] = "u.ID IN( " . implode( ', ', $specific_users ) . " )";
524 + }
525 +
526 + // Setup specific users join
527 + if( ! empty( $specific_roles ) && count( $specific_roles ) > 0 ) {
528 + // Setup join to the users meta table
529 + $join[] = "LEFT JOIN {$wpdb->usermeta} AS umcap ON ( umcap.user_id = u.ID AND umcap.meta_key = '" . $wpdb->get_blog_prefix() . "capabilities' )";
530 +
531 + foreach( $specific_roles as $role ) {
532 + $role = esc_sql( $role );
533 +
534 + $where[] = "umcap.meta_value LIKE '%\\\"{$role}\\\"%'";
535 + }
536 +
537 + }
538 +
539 + // Setup each query part
540 + $where = ( ! empty( $where ) ? "WHERE (" . implode( ') OR (', $where ) . ")" : '' );
541 + $join = ( ! empty( $join ) ? implode( ' ', $join ) : '' );
542 +
543 + $sql = "SELECT u.ID
544 + FROM {$wpdb->users} AS u
545 + {$join}
546 + {$where}
547 + ORDER BY u.ID ASC LIMIT {$offset}, {$limit}";
548 +
549 + $users = $wpdb->get_results( $sql );
550 +
551 + // Return a success message if finished, else run again
552 + if( empty( $users ) && $loop !== 0 ) {
553 + wp_send_json_success( __( 'Bulk award process has been done successfully.', 'gamipress' ) );
554 + } else {
555 + $run_again = true;
556 + }
557 + }
558 +
559 + if( empty( $users ) )
560 + wp_send_json_error( __( 'Could not find users to award.', 'gamipress' ) );
561 +
562 + // Let's to bulk award
563 + foreach( $users as $user ) {
564 +
565 + if( $bulk_award === 'points' ) {
566 +
567 + // When an admin awards points to user is required to set the total points balance
568 + $user_points = gamipress_get_user_points( $user->ID, $points_type_to_award );
569 +
570 + // Award points
571 + gamipress_award_points_to_user( $user->ID, $user_points + $points_to_award, $points_type_to_award, array( 'admin_id' => get_current_user_id() ) );
572 +
573 + $register_movement = isset( $_POST['bulk_award_points_register_movement'] ) ? true : false;
574 + $earnings_text = isset( $_POST['bulk_award_points_earnings_text'] ) ? stripslashes( sanitize_text_field( $_POST['bulk_award_points_earnings_text'] ) ) : '';
575 +
576 + if( $register_movement ) {
577 +
578 + // Insert the custom user earning for the manual balance adjustment
579 + gamipress_insert_user_earning( $user->ID, array(
580 + 'title' => $earnings_text,
581 + 'user_id' => $user->ID,
582 + 'post_id' => gamipress_get_points_type_id( $points_type_to_award ),
583 + 'post_type' => 'points-type',
584 + 'points' => $points_to_award,
585 + 'points_type' => $points_type_to_award,
586 + 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
587 + ) );
588 +
589 + }
590 +
591 + } else if( $bulk_award === 'achievements' ) {
592 +
593 + // Award achievements
594 + foreach( $achievements as $achievement ) {
595 + gamipress_award_achievement_to_user( absint( $achievement ), $user->ID, get_current_user_id() );
596 + }
597 +
598 + } else if( $bulk_award === 'rank' ) {
599 +
600 + // Award rank
601 + gamipress_update_user_rank( $user->ID, $rank_id, get_current_user_id() );
602 +
603 + } else if( $bulk_award === 'requirements' ) {
604 +
605 + // Award requirements
606 + foreach( $requirements as $requirement ) {
607 + gamipress_award_achievement_to_user( absint( $requirement ), $user->ID, get_current_user_id() );
608 + }
609 +
610 + }
611 +
612 + }
613 +
614 + if( $run_again ) {
615 +
616 + $awarded_users = $limit * ( $loop + 1 );
617 +
618 + if( $to_all_users ) {
619 + // Get the users count
620 + $users_count = gamipress_get_users_count();
621 + } else {
622 + $users_count = absint( $wpdb->get_var(
623 + "SELECT COUNT(*)
624 + FROM {$wpdb->users} AS u
625 + {$join}
626 + {$where}
627 + ORDER BY u.ID ASC"
628 + ) );
629 + }
630 +
631 + $remaining_users = $users_count - $awarded_users;
632 +
633 + // Return a run again message (just when awarding to all users)
634 + wp_send_json_success( array(
635 + 'run_again' => $run_again,
636 + 'message' => sprintf( __( '%d remaining users', 'gamipress' ), ( $remaining_users > 0 ? $remaining_users : 0 ) ),
637 + ) );
638 +
639 + } else {
640 + // Return a success message
641 + wp_send_json_success( __( 'Bulk award process has been done successfully.', 'gamipress' ) );
642 + }
643 +
644 +
645 +}
646 +add_action( 'wp_ajax_gamipress_bulk_awards_tool', 'gamipress_ajax_bulk_awards_tool' );