| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import/Export Ranks Tool |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Tools\Import_Export_Ranks |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.6.4 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register Import/Export Ranks 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_ranks_tool_meta_boxes( $meta_boxes ) { |
| 22 |
|
| 23 |
$meta_boxes['import-export-ranks'] = array( |
| 24 |
'title' => gamipress_dashicon( 'rank' ) . __( 'User Ranks', 'gamipress' ), |
| 25 |
'fields' => apply_filters( 'gamipress_import_export_ranks_tool_fields', array( |
| 26 |
|
| 27 |
// Export |
| 28 |
|
| 29 |
'export_ranks_rank_types' => array( |
| 30 |
'name' => __( 'Rank Types To Export', 'gamipress' ), |
| 31 |
'tooltip' => __( 'Choose the rank types to export.', 'gamipress' ), |
| 32 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 33 |
'type' => 'multicheck', |
| 34 |
'classes' => 'gamipress-switch', |
| 35 |
'options_cb' => 'gamipress_options_cb_rank_types', |
| 36 |
'option_all' => false, |
| 37 |
), |
| 38 |
'export_ranks_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_ranks_rank_field' => array( |
| 51 |
'name' => __( 'Rank Field', 'gamipress' ), |
| 52 |
'tooltip' => __( 'Choose the field to display on rank column.', 'gamipress' ), |
| 53 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 54 |
'type' => 'select', |
| 55 |
'options' => array( |
| 56 |
'id' => __( 'ID', 'gamipress' ), |
| 57 |
'title' => __( 'Title', 'gamipress' ), |
| 58 |
'slug' => __( 'Slug', 'gamipress' ), |
| 59 |
), |
| 60 |
'default' => 'slug' |
| 61 |
), |
| 62 |
'export_ranks' => array( |
| 63 |
'label' => __( 'Export User Ranks', 'gamipress' ), |
| 64 |
'type' => 'button', |
| 65 |
'icon' => 'dashicons-download', |
| 66 |
'button' => 'primary', |
| 67 |
'action' => 'export_ranks' |
| 68 |
|
| 69 |
), |
| 70 |
|
| 71 |
// Import |
| 72 |
|
| 73 |
'import_ranks_file' => array( |
| 74 |
'name' => __( 'CSV File', 'gamipress' ), |
| 75 |
'type' => 'text', |
| 76 |
'attributes' => array( |
| 77 |
'type' => 'file', |
| 78 |
'accept' => '.csv' |
| 79 |
) |
| 80 |
), |
| 81 |
'import_actions' => array( |
| 82 |
'type' => 'multi_buttons', |
| 83 |
'buttons' => array( |
| 84 |
'import_ranks' => array( |
| 85 |
'label' => __( 'Import User Ranks', 'gamipress' ), |
| 86 |
'type' => 'button', |
| 87 |
'button' => 'primary', |
| 88 |
'icon' => 'dashicons-upload', |
| 89 |
'action' => 'import_ranks' |
| 90 |
), |
| 91 |
'download_ranks_csv_template' => array( |
| 92 |
'label' => __( 'Download CSV Template', 'gamipress' ), |
| 93 |
'type' => 'button', |
| 94 |
'icon' => 'dashicons-media-spreadsheet', |
| 95 |
) |
| 96 |
), |
| 97 |
), |
| 98 |
|
| 99 |
) ), |
| 100 |
'vertical_tabs' => true, |
| 101 |
'tabs' => apply_filters( 'gamipress_import_export_ranks_tool_tabs', array( |
| 102 |
'export_ranks' => array( |
| 103 |
'icon' => 'dashicons-download', |
| 104 |
'title' => __( 'Export', 'gamipress' ), |
| 105 |
'fields' => array( |
| 106 |
'export_ranks_rank_types', |
| 107 |
'export_ranks_user_field', |
| 108 |
'export_ranks_rank_field', |
| 109 |
'export_ranks', |
| 110 |
), |
| 111 |
), |
| 112 |
'import_ranks' => array( |
| 113 |
'icon' => 'dashicons-upload', |
| 114 |
'title' => __( 'Import', 'gamipress' ), |
| 115 |
'fields' => array( |
| 116 |
'import_ranks_file', |
| 117 |
'import_actions', |
| 118 |
), |
| 119 |
), |
| 120 |
) ) |
| 121 |
); |
| 122 |
|
| 123 |
return $meta_boxes; |
| 124 |
|
| 125 |
} |
| 126 |
add_filter( 'gamipress_tools_import_export_meta_boxes', 'gamipress_import_export_ranks_tool_meta_boxes' ); |
| 127 |
|
| 128 |
/** |
| 129 |
* AJAX handler for the export ranks tool |
| 130 |
* |
| 131 |
* @since 1.6.4 |
| 132 |
*/ |
| 133 |
function gamipress_import_export_ranks_tool_ajax_export() { |
| 134 |
// Security check, forces to die if not security passed |
| 135 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 136 |
|
| 137 |
global $wpdb; |
| 138 |
|
| 139 |
// Check user capabilities |
| 140 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 141 |
wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
// Setup vars |
| 145 |
$rank_types = gamipress_get_rank_types(); |
| 146 |
$desired_rank_types = ( isset( $_REQUEST['rank_types'] ) ? $_REQUEST['rank_types'] : array() ); |
| 147 |
$user_field = ( isset( $_REQUEST['user_field'] ) ? sanitize_text_field( $_REQUEST['user_field'] ) : 'email' ); |
| 148 |
$rank_field = ( isset( $_REQUEST['rank_field'] ) ? sanitize_text_field( $_REQUEST['rank_field'] ) : 'slug' ); |
| 149 |
$loop = ( isset( $_REQUEST['loop'] ) ? absint( $_REQUEST['loop'] ) : 0 ); |
| 150 |
$limit = 200; |
| 151 |
$offset = $limit * $loop; |
| 152 |
$items_to_export = array(); |
| 153 |
|
| 154 |
if( $loop === 0 ) { |
| 155 |
// Set the CSV headers |
| 156 |
$items_to_export[] = array( |
| 157 |
'user' => __( 'User', 'gamipress' ), |
| 158 |
'rank' => __( 'Rank', 'gamipress' ), |
| 159 |
'rank_type' => __( 'Rank Type (slug)', 'gamipress' ), |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
// Check the rank types received |
| 164 |
if( empty( $desired_rank_types ) ) { |
| 165 |
wp_send_json_error( __( 'You need to choose at least 1 rank type to export.', 'gamipress' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
ignore_user_abort( true ); |
| 169 |
|
| 170 |
if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) { |
| 171 |
set_time_limit( 0 ); |
| 172 |
} |
| 173 |
|
| 174 |
// Get stored users |
| 175 |
$users = gamipress_get_users( array( |
| 176 |
'offset' => $offset, |
| 177 |
'limit' => $limit, |
| 178 |
) ); |
| 179 |
|
| 180 |
if( empty( $users ) ) { |
| 181 |
// Return a success message |
| 182 |
wp_send_json_success( __( 'User\'s ranks export process has been done successfully.', 'gamipress' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
// Let's to get the data from our users |
| 186 |
foreach( $users as $user ) { |
| 187 |
|
| 188 |
$user_column = ''; |
| 189 |
|
| 190 |
switch( $user_field ) { |
| 191 |
case 'id': |
| 192 |
$user_column = $user->ID; |
| 193 |
break; |
| 194 |
case 'username': |
| 195 |
$user_column = $user->user_login; |
| 196 |
break; |
| 197 |
case 'email': |
| 198 |
$user_column = $user->user_email; |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
foreach( $desired_rank_types as $rank_type ) { |
| 203 |
|
| 204 |
// Skip not registered rank types |
| 205 |
if( ! isset( $rank_types[$rank_type] ) ) |
| 206 |
continue; |
| 207 |
|
| 208 |
$rank_column = ''; |
| 209 |
|
| 210 |
// get the user rank |
| 211 |
switch( $rank_field ) { |
| 212 |
case 'id': |
| 213 |
$rank_column = gamipress_get_user_rank_id( $user->ID, $rank_type ); |
| 214 |
break; |
| 215 |
case 'title': |
| 216 |
$rank = gamipress_get_user_rank( $user->ID, $rank_type ); |
| 217 |
$rank_column = $rank->post_title; |
| 218 |
break; |
| 219 |
case 'slug': |
| 220 |
$rank = gamipress_get_user_rank( $user->ID, $rank_type ); |
| 221 |
$rank_column = $rank->post_name; |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
// Export a row per rank type |
| 226 |
$items_to_export[] = array( |
| 227 |
'user' => $user_column, |
| 228 |
'rank' => $rank_column, |
| 229 |
'rank_type' => $rank_type, |
| 230 |
); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
$exported_users = $limit * ( $loop + 1 ); |
| 237 |
|
| 238 |
// Get the users count |
| 239 |
$users_count = gamipress_get_users_count(); |
| 240 |
|
| 241 |
$total = $users_count - $exported_users; |
| 242 |
|
| 243 |
if( $total > 0 ) { |
| 244 |
// Return a run again message |
| 245 |
wp_send_json_success( array( |
| 246 |
'run_again' => true, |
| 247 |
'items' => $items_to_export, |
| 248 |
'message' => sprintf( __( '%d remaining users', 'gamipress' ), ( $users_count - $exported_users ) ), |
| 249 |
) ); |
| 250 |
} else { |
| 251 |
// Return a run again message |
| 252 |
wp_send_json_success( array( |
| 253 |
'run_again' => false, |
| 254 |
'items' => $items_to_export, |
| 255 |
'message' => __( 'User\'s ranks export process has been done successfully.', 'gamipress' ), |
| 256 |
) ); |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
add_action( 'wp_ajax_gamipress_import_export_ranks_tool_export', 'gamipress_import_export_ranks_tool_ajax_export' ); |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* AJAX handler for the import ranks tool |
| 265 |
* |
| 266 |
* @since 1.6.4 |
| 267 |
*/ |
| 268 |
function gamipress_import_export_ranks_tool_ajax_import() { |
| 269 |
// Security check, forces to die if not security passed |
| 270 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 271 |
|
| 272 |
global $wpdb; |
| 273 |
|
| 274 |
// Check user capabilities |
| 275 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 276 |
wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) ); |
| 277 |
} |
| 278 |
|
| 279 |
// Check parameters received |
| 280 |
if( ! isset( $_FILES['file'] ) ) { |
| 281 |
wp_send_json_error( __( 'No file to import.', 'gamipress' ) ); |
| 282 |
} |
| 283 |
|
| 284 |
$import_file = $_FILES['file']['tmp_name']; |
| 285 |
|
| 286 |
if( empty( $import_file ) ) { |
| 287 |
wp_send_json_error( __( 'Can\'t retrieve the file to import, check server file permissions.', 'gamipress' ) ); |
| 288 |
} |
| 289 |
|
| 290 |
ignore_user_abort( true ); |
| 291 |
|
| 292 |
if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) { |
| 293 |
set_time_limit( 0 ); |
| 294 |
} |
| 295 |
|
| 296 |
// Retrieve the content from the file |
| 297 |
$file_contents = file_get_contents( $import_file ); |
| 298 |
|
| 299 |
if( empty( $file_contents ) ) { |
| 300 |
wp_send_json_error( __( 'Empty file, so nothing to import.', 'gamipress' ) ); |
| 301 |
} |
| 302 |
|
| 303 |
// Setup vars |
| 304 |
$posts = GamiPress()->db->posts; |
| 305 |
$rank_types = gamipress_get_rank_types_slugs(); |
| 306 |
$post_type_where = "AND post_type IN('" . implode( "', '", $rank_types ) . "')"; |
| 307 |
|
| 308 |
// Explode by line breaks |
| 309 |
$lines = explode( "\n", $file_contents ); |
| 310 |
|
| 311 |
foreach( $lines as $number => $line ) { |
| 312 |
|
| 313 |
$columns = str_getcsv( $line ); |
| 314 |
|
| 315 |
if( count( $columns ) >= 2 ) { |
| 316 |
|
| 317 |
$user = false; |
| 318 |
$rank = ''; |
| 319 |
|
| 320 |
// User |
| 321 |
if( isset( $columns[0] ) && ! empty( $columns[0] ) ) { |
| 322 |
|
| 323 |
$user_field = 'login'; |
| 324 |
|
| 325 |
if( filter_var( $columns[0], FILTER_VALIDATE_EMAIL ) ) { |
| 326 |
$user_field = 'email'; |
| 327 |
} else if( is_numeric( $columns[0] ) ) { |
| 328 |
$user_field = 'id'; |
| 329 |
} |
| 330 |
|
| 331 |
$user = get_user_by( $user_field, $columns[0] ); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
// Rank |
| 336 |
if( isset( $columns[1] ) && ! empty( $columns[1] ) ) { |
| 337 |
|
| 338 |
$rank = $columns[1]; |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
// Check if everything is done |
| 343 |
if( $user && ! empty( $rank ) ) { |
| 344 |
|
| 345 |
$revoke = false; |
| 346 |
|
| 347 |
// If rank has a negative sign, then user is looking to revoke |
| 348 |
if ( substr( $rank, 0, 1 ) === '-') { |
| 349 |
$revoke = true; |
| 350 |
$rank = substr( $rank, 1); // Remove the negative sign |
| 351 |
} |
| 352 |
|
| 353 |
if( is_numeric( $rank ) ) { |
| 354 |
|
| 355 |
// Search by ID |
| 356 |
$rank_post = gamipress_get_post( $rank ); |
| 357 |
|
| 358 |
} else { |
| 359 |
|
| 360 |
// Search by title |
| 361 |
$rank_post = $wpdb->get_row( $wpdb->prepare( |
| 362 |
"SELECT * FROM {$posts} WHERE post_title = %s {$post_type_where}", |
| 363 |
$rank |
| 364 |
) ); |
| 365 |
|
| 366 |
if( ! $rank_post ) { |
| 367 |
// Search by slug |
| 368 |
$rank_post = $wpdb->get_row( $wpdb->prepare( |
| 369 |
"SELECT * FROM {$posts} WHERE post_name = %s {$post_type_where}", |
| 370 |
$rank |
| 371 |
) ); |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
// Check if post object is a rank |
| 378 |
if( $rank_post && gamipress_is_rank( $rank_post ) ) { |
| 379 |
|
| 380 |
if( $revoke ) { |
| 381 |
$new_rank_id = gamipress_get_prev_user_rank_id( $user->ID, $rank_post->post_type ); |
| 382 |
|
| 383 |
// Revoke the rank to the user |
| 384 |
gamipress_revoke_rank_to_user( $user->ID, $rank_post->ID, $new_rank_id, array( 'admin_id' => get_current_user_id() ) ); |
| 385 |
} else { |
| 386 |
// Award the rank to the user |
| 387 |
gamipress_award_rank_to_user( $rank_post->ID, $user->ID, array( 'admin_id' => get_current_user_id() ) ); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
// Return a success message |
| 398 |
wp_send_json_success( __( 'Ranks has been awarded successfully to all users.', 'gamipress' ) ); |
| 399 |
} |
| 400 |
add_action( 'wp_ajax_gamipress_import_export_ranks_tool_import', 'gamipress_import_export_ranks_tool_ajax_import' ); |