PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.3
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.3
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 7.8.7 7.8.6 All 45 releases
← All changes | includes/admin/tools/import-export-points.php +398 -0 7.9.28.0.3 View file →
@@ -1,0 +1,398 @@
1 +<?php
2 +/**
3 + * Import/Export Points Tool
4 + *
5 + * @package GamiPress\Admin\Tools\Import_Export_Points
6 + * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
7 + * @since 1.6.4
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Register Import/Export Points Tool meta boxes
14 + *
15 + * @since 1.6.4
16 + *
17 + * @param array $meta_boxes
18 + *
19 + * @return array
20 + */
21 +function gamipress_import_export_points_tool_meta_boxes( $meta_boxes ) {
22 +
23 + $meta_boxes['import-export-points'] = array(
24 + 'title' => gamipress_dashicon( 'star-filled' ) . __( 'User Points', 'gamipress' ),
25 + 'fields' => apply_filters( 'gamipress_import_export_points_tool_fields', array(
26 +
27 + // Export
28 +
29 + 'export_points_points_types' => array(
30 + 'name' => __( 'Points Types To Export', 'gamipress' ),
31 + 'tooltip' => __( 'Choose the points types to export.', 'gamipress' ),
32 + 'label_cb' => 'cmb_tooltip_label_cb',
33 + 'type' => 'multicheck',
34 + 'classes' => 'gamipress-switch',
35 + 'options_cb' => 'gamipress_options_cb_points_types',
36 + 'option_all' => false,
37 + ),
38 + 'export_points_user_field' => array(
39 + 'name' => __( 'User Field', 'gamipress' ),
40 + 'tooltip' => __( 'Choose the field to display on user column.', 'gamipress' ),
41 + 'label_cb' => 'cmb_tooltip_label_cb',
42 + 'type' => 'select',
43 + 'options' => array(
44 + 'id' => __( 'ID', 'gamipress' ),
45 + 'username' => __( 'Username', 'gamipress' ),
46 + 'email' => __( 'Email', 'gamipress' ),
47 + ),
48 + 'default' => 'email'
49 + ),
50 + 'export_points' => array(
51 + 'label' => __( 'Export User Points', 'gamipress' ),
52 + 'type' => 'button',
53 + 'icon' => 'dashicons-download',
54 + 'button' => 'primary',
55 + 'action' => 'export_points'
56 +
57 + ),
58 +
59 + // Import
60 +
61 + 'import_points_file' => array(
62 + 'name' => __( 'CSV File', 'gamipress' ),
63 + 'type' => 'text',
64 + 'attributes' => array(
65 + 'type' => 'file',
66 + 'accept' => '.csv'
67 + )
68 + ),
69 + 'import_actions' => array(
70 + 'type' => 'multi_buttons',
71 + 'buttons' => array(
72 + 'import_points' => array(
73 + 'label' => __( 'Import User Points', 'gamipress' ),
74 + 'type' => 'button',
75 + 'button' => 'primary',
76 + 'icon' => 'dashicons-upload',
77 + 'action' => 'import_points'
78 + ),
79 + 'download_points_csv_template' => array(
80 + 'label' => __( 'Download CSV Template', 'gamipress' ),
81 + 'type' => 'button',
82 + 'icon' => 'dashicons-media-spreadsheet',
83 + )
84 + ),
85 + ),
86 +
87 + ) ),
88 + 'vertical_tabs' => true,
89 + 'tabs' => apply_filters( 'gamipress_import_export_points_tool_tabs', array(
90 + 'export_points' => array(
91 + 'icon' => 'dashicons-download',
92 + 'title' => __( 'Export', 'gamipress' ),
93 + 'fields' => array(
94 + 'export_points_points_types',
95 + 'export_points_user_field',
96 + 'export_points',
97 + ),
98 + ),
99 + 'import_points' => array(
100 + 'icon' => 'dashicons-upload',
101 + 'title' => __( 'Import', 'gamipress' ),
102 + 'fields' => array(
103 + 'import_points_file',
104 + 'import_actions',
105 + ),
106 + ),
107 + ) )
108 + );
109 +
110 + return $meta_boxes;
111 +
112 +}
113 +add_filter( 'gamipress_tools_import_export_meta_boxes', 'gamipress_import_export_points_tool_meta_boxes' );
114 +
115 +/**
116 + * AJAX handler for the export points tool
117 + *
118 + * @since 1.6.4
119 + */
120 +function gamipress_import_export_points_tool_ajax_export() {
121 + // Security check, forces to die if not security passed
122 + check_ajax_referer( 'gamipress_admin', 'nonce' );
123 +
124 + global $wpdb;
125 +
126 + // Check user capabilities
127 + if( ! current_user_can( gamipress_get_manager_capability() ) ) {
128 + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
129 + }
130 +
131 + // Setup vars
132 + $points_types = gamipress_get_points_types();
133 + $desired_points_types = ( isset( $_REQUEST['points_types'] ) ? $_REQUEST['points_types'] : array() );
134 + $user_field = ( isset( $_REQUEST['user_field'] ) ? sanitize_text_field( $_REQUEST['user_field'] ) : 'email' );
135 + $loop = ( isset( $_REQUEST['loop'] ) ? absint( $_REQUEST['loop'] ) : 0 );
136 + $limit = 200;
137 + $offset = $limit * $loop;
138 + $items_to_export = array();
139 +
140 + if( $loop === 0 ) {
141 + // Set the CSV headers
142 + $items_to_export[] = array(
143 + 'user' => __( 'User', 'gamipress' ),
144 + 'points' => __( 'Points', 'gamipress' ),
145 + 'points_type' => __( 'Points Type (slug)', 'gamipress' )
146 + );
147 + }
148 +
149 + // Check the points types received
150 + if( empty( $desired_points_types ) ) {
151 + wp_send_json_error( __( 'You need to choose at least 1 points type to export.', 'gamipress' ) );
152 + }
153 +
154 + if( ! is_array( $desired_points_types ) ) {
155 + wp_send_json_error( __( 'You need to choose at least 1 points type to export.', 'gamipress' ) );
156 + }
157 +
158 + ignore_user_abort( true );
159 +
160 + if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) {
161 + set_time_limit( 0 );
162 + }
163 +
164 + // Get stored users
165 + $users = gamipress_get_users( array(
166 + 'offset' => $offset,
167 + 'limit' => $limit,
168 + ) );
169 +
170 + if( empty( $users ) ) {
171 + // Return a success message
172 + wp_send_json_success( __( 'User\'s points balances export process has been done successfully.', 'gamipress' ) );
173 + }
174 +
175 + // Let's to get the data from our users
176 + foreach( $users as $user ) {
177 +
178 + $user_column = '';
179 +
180 + switch( $user_field ) {
181 + case 'id':
182 + $user_column = $user->ID;
183 + break;
184 + case 'username':
185 + $user_column = $user->user_login;
186 + break;
187 + case 'email':
188 + $user_column = $user->user_email;
189 + break;
190 + }
191 +
192 + foreach( $desired_points_types as $points_type ) {
193 +
194 + // Skip not registered points types
195 + if( ! isset( $points_types[$points_type] ) )
196 + continue;
197 +
198 + // Export a row per points type
199 + $items_to_export[] = array(
200 + 'user' => $user_column,
201 + 'points' => gamipress_get_user_points( $user->ID, $points_type ),
202 + 'points_type' => $points_type
203 + );
204 +
205 + }
206 +
207 + }
208 +
209 + $exported_users = $limit * ( $loop + 1 );
210 +
211 + // Get the users count
212 + $users_count = gamipress_get_users_count();
213 +
214 + $total = $users_count - $exported_users;
215 +
216 + if( $total > 0 ) {
217 + // Return a run again message
218 + wp_send_json_success( array(
219 + 'run_again' => true,
220 + 'items' => $items_to_export,
221 + 'message' => sprintf( __( '%d remaining users', 'gamipress' ), ( $users_count - $exported_users ) ),
222 + ) );
223 + } else {
224 + // Return a run again message
225 + wp_send_json_success( array(
226 + 'run_again' => false,
227 + 'items' => $items_to_export,
228 + 'message' => __( 'User\'s points balances export process has been done successfully.', 'gamipress' ),
229 + ) );
230 + }
231 +
232 +}
233 +add_action( 'wp_ajax_gamipress_import_export_points_tool_export', 'gamipress_import_export_points_tool_ajax_export' );
234 +
235 +
236 +/**
237 + * AJAX handler for the import points tool
238 + *
239 + * @since 1.6.4
240 + */
241 +function gamipress_import_export_points_tool_ajax_import() {
242 + // Security check, forces to die if not security passed
243 + check_ajax_referer( 'gamipress_admin', 'nonce' );
244 +
245 + // Check user capabilities
246 + if( ! current_user_can( gamipress_get_manager_capability() ) ) {
247 + wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
248 + }
249 +
250 + // Check parameters received
251 + if( ! isset( $_FILES['file'] ) ) {
252 + wp_send_json_error( __( 'No file to import.', 'gamipress' ) );
253 + }
254 +
255 + $import_file = $_FILES['file']['tmp_name'];
256 +
257 + if( empty( $import_file ) ) {
258 + wp_send_json_error( __( 'Can\'t retrieve the file to import, check server file permissions.', 'gamipress' ) );
259 + }
260 +
261 + ignore_user_abort( true );
262 +
263 + if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) {
264 + set_time_limit( 0 );
265 + }
266 +
267 + // Retrieve the content from the file
268 + $file_contents = file_get_contents( $import_file );
269 +
270 + if( empty( $file_contents ) ) {
271 + wp_send_json_error( __( 'Empty file, so nothing to import.', 'gamipress' ) );
272 + }
273 +
274 + // Setup vars
275 + $points_types = gamipress_get_points_types();
276 +
277 + // Explode by line breaks
278 + $lines = explode( "\n", $file_contents );
279 +
280 + foreach( $lines as $number => $line ) {
281 +
282 + $columns = str_getcsv( $line );
283 +
284 + if( count( $columns ) >= 3 ) {
285 +
286 + $user = false;
287 + $points = 0;
288 + $points_type = '';
289 + $log_description = '';
290 + $deduct = false;
291 +
292 + // User
293 + if( isset( $columns[0] ) && ! empty( $columns[0] ) ) {
294 +
295 + $user_field = 'login';
296 +
297 + if( filter_var( $columns[0], FILTER_VALIDATE_EMAIL ) ) {
298 + $user_field = 'email';
299 + } else if( is_numeric( $columns[0] ) ) {
300 + $user_field = 'id';
301 + }
302 +
303 + $user = get_user_by( $user_field, $columns[0] );
304 +
305 + }
306 +
307 + // Points
308 + if( isset( $columns[1] ) && is_numeric( $columns[1] ) ) {
309 +
310 + // If points amount has a negative sign, then user is looking for deduct
311 + if ( substr( $columns[1], 0, 1 ) === '-') {
312 + $deduct = true;
313 + $columns[1] = substr( $columns[1], 1); // Remove the negative sign
314 + }
315 +
316 + $points = absint( $columns[1] );
317 +
318 + }
319 +
320 + // Points Type
321 + if( isset( $columns[2] ) && ! empty( $columns[2] ) && isset( $points_types[$columns[2]] ) ) {
322 +
323 + $points_type = $columns[2];
324 +
325 + }
326 +
327 + // Log Description
328 + if( isset( $columns[3] ) && ! empty( $columns[3] ) ) {
329 +
330 + $log_description = $columns[3];
331 +
332 + }
333 +
334 + // Check if everything is done
335 + if( $user && $points > 0 && ! empty( $points_type ) ) {
336 +
337 + // When award points passing an admin ID, we need to pass the full new amount
338 + $current_points = gamipress_get_user_points( $user->ID, $points_type );
339 +
340 + $args = array(
341 + 'admin_id' => get_current_user_id(),
342 + 'reason' => $log_description,
343 + 'log_type' => 'points_award'
344 + );
345 +
346 + // If log description is empty, let GamiPress to setup it from log settings
347 + if( empty( $log_description ) ) {
348 + $args = array( 'admin_id' => get_current_user_id() );
349 + }
350 +
351 + if( $deduct ) {
352 +
353 + $args['log_type'] = 'points_revoke';
354 +
355 + // Deduct points to the user
356 + gamipress_deduct_points_to_user( $user->ID, $current_points - $points, $points_type, $args );
357 +
358 + $points_type_data = gamipress_get_points_type( $points_type );
359 +
360 + // Insert the custom user earning
361 + gamipress_insert_user_earning( $user->ID, array(
362 + 'title' => $args['reason'],
363 + 'user_id' => $user->ID,
364 + 'post_id' => $points_type_data['ID'],
365 + 'post_type' => 'points-type',
366 + 'points' => $points * -1, // Add a negative amount
367 + 'points_type' => $points_type,
368 + 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
369 + ) );
370 +
371 + } else {
372 + // Award points to the user
373 + gamipress_award_points_to_user( $user->ID, $points + $current_points, $points_type, $args );
374 +
375 + $points_type_data = gamipress_get_points_type( $points_type );
376 +
377 + // Insert the custom user earning
378 + gamipress_insert_user_earning( $user->ID, array(
379 + 'title' => $args['reason'],
380 + 'user_id' => $user->ID,
381 + 'post_id' => $points_type_data['ID'],
382 + 'post_type' => 'points-type',
383 + 'points' => $points,
384 + 'points_type' => $points_type,
385 + 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
386 + ) );
387 + }
388 +
389 + }
390 +
391 + }
392 +
393 + }
394 +
395 + // Return a success message
396 + wp_send_json_success( __( 'User\'s points balances has been updated successfully.', 'gamipress' ) );
397 +}
398 +add_action( 'wp_ajax_gamipress_import_export_points_tool_import', 'gamipress_import_export_points_tool_ajax_import' );