| @@ -1,0 +1,1102 @@ | ||
| 1 | +(function( $ ) { | |
| 2 | + | |
| 3 | + // ---------------------------------- | |
| 4 | + // Bulk Awards/Revokes Tool | |
| 5 | + // ---------------------------------- | |
| 6 | + | |
| 7 | + // On change requirement type | |
| 8 | + $('#bulk-awards, #bulk-revokes').on('change', '#bulk_award_requirements_type, #bulk_revoke_requirements_type', function() { | |
| 9 | + | |
| 10 | + var target = $(this).closest('.cmb-row').next().find('select[data-post-type]'); | |
| 11 | + var post_type = $(this).val(); | |
| 12 | + | |
| 13 | + | |
| 14 | + if( post_type !== '' ) { | |
| 15 | + target.val('').change(); | |
| 16 | + target.data('post-type', post_type) | |
| 17 | + } | |
| 18 | + }); | |
| 19 | + | |
| 20 | + function gamipress_run_bulk_tool( button, loop ) { | |
| 21 | + | |
| 22 | + // Initialize loop | |
| 23 | + if( loop === undefined ) loop = 0; | |
| 24 | + | |
| 25 | + var response_id = button.attr('id').replace('_button', '_response'); | |
| 26 | + var active_tab = button.closest('.cmb-tabs-wrap').find('.cmb-tab.active'); | |
| 27 | + var action = ( button.attr('id').indexOf('bulk_award_') !== -1 ? 'bulk_award' : 'bulk_revoke' ); | |
| 28 | + var data; | |
| 29 | + | |
| 30 | + if( action === 'bulk_award' ) { | |
| 31 | + data = { | |
| 32 | + action: 'gamipress_bulk_awards_tool', | |
| 33 | + nonce: gamipress_admin_tools.nonce, | |
| 34 | + bulk_award: button.attr('id').replace('bulk_award_', '').replace('_button', ''), | |
| 35 | + loop: loop | |
| 36 | + }; | |
| 37 | + } else if( action === 'bulk_revoke' ) { | |
| 38 | + data = { | |
| 39 | + action: 'gamipress_bulk_revokes_tool', | |
| 40 | + nonce: gamipress_admin_tools.nonce, | |
| 41 | + bulk_revoke: button.attr('id').replace('bulk_revoke_', '').replace('_button', ''), | |
| 42 | + loop: loop | |
| 43 | + }; | |
| 44 | + } | |
| 45 | + | |
| 46 | + // Loop all fields to build the request data | |
| 47 | + $(active_tab.data('fields')).find('input, select, textarea').each(function() { | |
| 48 | + | |
| 49 | + if( $(this).attr('type') === 'checkbox' ) { | |
| 50 | + // Checkboxes are sent just when checked | |
| 51 | + if( $(this).prop('checked') ) | |
| 52 | + data[$(this).attr('name')] = $(this).val(); | |
| 53 | + } else { | |
| 54 | + data[$(this).attr('name')] = $(this).val(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + }); | |
| 58 | + | |
| 59 | + // Disable the button | |
| 60 | + button.prop('disabled', true); | |
| 61 | + | |
| 62 | + if( ! $('#' + response_id).length ) | |
| 63 | + button.parent().append('<span id="' + response_id + '" style="display: inline-block; padding: 5px 0 0 8px;"></span>'); | |
| 64 | + | |
| 65 | + // Show the spinner | |
| 66 | + if( loop === 0 ) | |
| 67 | + $('#' + response_id).html('<span class="spinner is-active" style="float: none; margin: 0;"></span>'); | |
| 68 | + | |
| 69 | + $.post( | |
| 70 | + ajaxurl, | |
| 71 | + data, | |
| 72 | + function( response ) { | |
| 73 | + | |
| 74 | + // Run again utility | |
| 75 | + if( response.data.run_again !== undefined && response.data.run_again && response.success === true ) { | |
| 76 | + | |
| 77 | + // Keep the spinner and add the server response | |
| 78 | + $('#' + response_id).html( | |
| 79 | + '<span class="spinner is-active" style="float: none; margin: 0;"></span>' | |
| 80 | + + '<span style="display: inline-block; padding-left: 5px;">' + ( response.data.message !== undefined ? response.data.message : response.data ) + '</span>' | |
| 81 | + ); | |
| 82 | + | |
| 83 | + loop++; | |
| 84 | + | |
| 85 | + // Run again passing the next loop index | |
| 86 | + gamipress_run_bulk_tool( button, loop ); | |
| 87 | + | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Update response color text | |
| 92 | + if( response.success === false ) | |
| 93 | + $('#' + response_id).css({color:'#a00'}); | |
| 94 | + else | |
| 95 | + $('#' + response_id).css({color:''}); | |
| 96 | + | |
| 97 | + // Add the server response | |
| 98 | + $('#' + response_id).html(response.data); | |
| 99 | + | |
| 100 | + // Increase loop count | |
| 101 | + if( response.success !== false ) | |
| 102 | + loop++; | |
| 103 | + | |
| 104 | + // Enable the button | |
| 105 | + button.prop('disabled', false); | |
| 106 | + } | |
| 107 | + ).fail(function() { | |
| 108 | + | |
| 109 | + $('#' + response_id).html('The server has returned an internal error.'); | |
| 110 | + | |
| 111 | + // Enable the button | |
| 112 | + button.prop('disabled', false); | |
| 113 | + }); | |
| 114 | + | |
| 115 | + } | |
| 116 | + | |
| 117 | + $('#bulk_award_points_button, #bulk_award_achievements_button, #bulk_award_rank_button, #bulk_award_requirements_button, ' | |
| 118 | + + '#bulk_revoke_points_button, #bulk_revoke_achievements_button, #bulk_revoke_rank_button, #bulk_revoke_requirements_button').on('click', function(e) { | |
| 119 | + e.preventDefault(); | |
| 120 | + | |
| 121 | + gamipress_run_bulk_tool( $(this) ); | |
| 122 | + }); | |
| 123 | + | |
| 124 | + // ---------------------------------- | |
| 125 | + // Recount Activity Tool | |
| 126 | + // ---------------------------------- | |
| 127 | + | |
| 128 | + function gamipress_run_recount_activity_tool( loop ) { | |
| 129 | + | |
| 130 | + if( loop === undefined ) | |
| 131 | + loop = 0; | |
| 132 | + | |
| 133 | + var button = $("#recount_activity"); | |
| 134 | + var activity = $('#activity_to_recount').val(); | |
| 135 | + var limit = $('#entries_per_loop').val(); | |
| 136 | + | |
| 137 | + $.post( | |
| 138 | + ajaxurl, | |
| 139 | + { | |
| 140 | + action: 'gamipress_recount_activity_tool', | |
| 141 | + nonce: gamipress_admin_tools.nonce, | |
| 142 | + activity: activity, | |
| 143 | + limit: limit, | |
| 144 | + loop: loop // Used on run again utility to let know to the tool in which loop we are now | |
| 145 | + }, | |
| 146 | + function( response ) { | |
| 147 | + | |
| 148 | + // Run again utility | |
| 149 | + if( response.data.run_again !== undefined && response.data.run_again && response.success === true ) { | |
| 150 | + | |
| 151 | + var running_selector = '#recount-activity-response #running-' + activity; | |
| 152 | + | |
| 153 | + if( ! $(running_selector).length ) | |
| 154 | + $('#recount-activity-response').append( '<span id="running-' + activity + '"></span>' ); | |
| 155 | + | |
| 156 | + $(running_selector).html( ( response.data.message !== undefined ? response.data.message : response.data ) ); | |
| 157 | + | |
| 158 | + if( response.data.log !== undefined && response.data.log.length ) { | |
| 159 | + if( ! $('#recount-activity-log').length ) | |
| 160 | + $( | |
| 161 | + '<div id="recount-activity-log">' | |
| 162 | + + '<p>' | |
| 163 | + + '<a href="#" id="recount-activity-log-toggle">Show log</a>' | |
| 164 | + + ' | ' | |
| 165 | + + '<a href="#" id="recount-activity-log-download">Download log</a>' | |
| 166 | + + '</p>' | |
| 167 | + + '<div id="recount-activity-log-content" style="display: none;"></div>' | |
| 168 | + + '</div>' | |
| 169 | + ).insertAfter( $('#recount-activity-response') ); | |
| 170 | + | |
| 171 | + $('#recount-activity-log-content').append( response.data.log ); | |
| 172 | + } | |
| 173 | + | |
| 174 | + loop++; | |
| 175 | + | |
| 176 | + // Run again passing the next loop index | |
| 177 | + gamipress_run_recount_activity_tool( loop ); | |
| 178 | + | |
| 179 | + return; | |
| 180 | + } | |
| 181 | + | |
| 182 | + $('#recount-activity-notice').remove(); | |
| 183 | + | |
| 184 | + if( response.success === false ) | |
| 185 | + $('#recount-activity-response').css({color:'#a00'}); | |
| 186 | + else | |
| 187 | + $('#recount-activity-response').css({color:''}); | |
| 188 | + | |
| 189 | + $('#recount-activity-response').html(response.data); | |
| 190 | + | |
| 191 | + // Enable the button and the activity select | |
| 192 | + button.prop('disabled', false); | |
| 193 | + $('#activity_to_recount').prop('disabled', false); | |
| 194 | + } | |
| 195 | + ).fail(function() { | |
| 196 | + | |
| 197 | + $('#recount-activity-notice').remove(); | |
| 198 | + | |
| 199 | + $('#recount-activity-response').html('The server has returned an internal error.'); | |
| 200 | + | |
| 201 | + if( $('#recount-activity-log').length ) | |
| 202 | + $('#recount-activity-log-content').append('The server has returned an internal error.'); | |
| 203 | + | |
| 204 | + // Enable the button and the activity select | |
| 205 | + button.prop('disabled', false); | |
| 206 | + $('#activity_to_recount').prop('disabled', false); | |
| 207 | + }); | |
| 208 | + } | |
| 209 | + | |
| 210 | + $("#recount_activity").on('click', function(e) { | |
| 211 | + e.preventDefault(); | |
| 212 | + | |
| 213 | + $('#recount-activity-warning').remove(); | |
| 214 | + | |
| 215 | + if( $('#activity_to_recount').val() === '' ) { | |
| 216 | + $(this).parent().prepend('<p id="recount-activity-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose an activity to recount.</p>'); | |
| 217 | + return false; | |
| 218 | + } | |
| 219 | + | |
| 220 | + var $this = $(this); | |
| 221 | + | |
| 222 | + // Disable the button and the activity select | |
| 223 | + $this.prop('disabled', true); | |
| 224 | + $('#activity_to_recount').prop('disabled', true); | |
| 225 | + | |
| 226 | + // Reset the activity log | |
| 227 | + if( $('#recount-activity-log').length ) | |
| 228 | + $('#recount-activity-log').remove(); | |
| 229 | + | |
| 230 | + // Show a notice to let know to the user that process could take a while | |
| 231 | + $this.parent().prepend('<p id="recount-activity-notice" class="cmb2-metabox-description">' + gamipress_admin_tools.recount_activity_notice + '</p>'); | |
| 232 | + | |
| 233 | + if( ! $('#recount-activity-response').length ) | |
| 234 | + $this.parent().append('<span id="recount-activity-response"></span>'); | |
| 235 | + | |
| 236 | + // Show the spinner | |
| 237 | + $('#recount-activity-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 238 | + | |
| 239 | + // Make the ajax request | |
| 240 | + gamipress_run_recount_activity_tool(); | |
| 241 | + }); | |
| 242 | + | |
| 243 | + // Activity log toggle | |
| 244 | + $('body').on('click', '#recount-activity-log-toggle', function(e) { | |
| 245 | + e.preventDefault(); | |
| 246 | + | |
| 247 | + $('#recount-activity-log-content').toggle(); | |
| 248 | + | |
| 249 | + if( $(this).text() === 'Show log' ) { | |
| 250 | + $(this).text( 'Hide log' ); | |
| 251 | + } else { | |
| 252 | + $(this).text( 'Show log' ); | |
| 253 | + } | |
| 254 | + }); | |
| 255 | + | |
| 256 | + // Download activity log | |
| 257 | + $('body').on('click', '#recount-activity-log-download', function(e) { | |
| 258 | + e.preventDefault(); | |
| 259 | + | |
| 260 | + var activity = $('#activity_to_recount').val(); | |
| 261 | + | |
| 262 | + gamipress_download_file( $('#recount-activity-log-content').text(), 'gamipress-activity-recount-' + activity, 'log' ); | |
| 263 | + }); | |
| 264 | + | |
| 265 | + // ---------------------------------- | |
| 266 | + // Logs Clean Up Tool | |
| 267 | + // ---------------------------------- | |
| 268 | + | |
| 269 | + $('#logs_clean_up_count, #logs_clean_up').on('click', function(e) { | |
| 270 | + e.preventDefault(); | |
| 271 | + | |
| 272 | + var $this = $(this); | |
| 273 | + var form = $this.closest('#cmb2-metabox-logs-clean-up'); | |
| 274 | + var id = $this.attr('id'); | |
| 275 | + var response_id = 'logs-clean-up-response'; | |
| 276 | + var log_types = []; | |
| 277 | + var from = form.find('input[name="from"]').val(); | |
| 278 | + var to = form.find('input[name="to"]').val(); | |
| 279 | + var error = ''; | |
| 280 | + | |
| 281 | + form.find('input[name="logs_type[]"]:checked').each(function() { | |
| 282 | + log_types.push( $(this).val() ) | |
| 283 | + }); | |
| 284 | + | |
| 285 | + if( log_types.length === 0 ) | |
| 286 | + error = 'Please, choose at least 1 log type.'; | |
| 287 | + | |
| 288 | + if( ! $('#' + response_id).length ) | |
| 289 | + $this.parent().append('<span id="' + response_id + '" style="display: inline-block; padding: 5px 0 0 8px;"></span>'); | |
| 290 | + | |
| 291 | + // Remove error messages | |
| 292 | + $('#' + response_id).html(''); | |
| 293 | + | |
| 294 | + // If there is any error, show it to the user | |
| 295 | + if( error !== '' ) { | |
| 296 | + $('#' + response_id).css({color:'#a00'}); | |
| 297 | + $('#' + response_id).html(error); | |
| 298 | + return false; | |
| 299 | + } | |
| 300 | + | |
| 301 | + // Disable both buttons | |
| 302 | + $('#logs_clean_up_count, #logs_clean_up').prop('disabled', true); | |
| 303 | + | |
| 304 | + // Show the spinner | |
| 305 | + $('#' + response_id).html('<span class="spinner is-active" style="float: none; margin: 0;"></span>'); | |
| 306 | + | |
| 307 | + $.post( | |
| 308 | + ajaxurl, | |
| 309 | + { | |
| 310 | + action: 'gamipress_' + id + '_tool', | |
| 311 | + nonce: gamipress_admin_tools.nonce, | |
| 312 | + log_types: log_types, | |
| 313 | + from: from, | |
| 314 | + to: to | |
| 315 | + }, | |
| 316 | + function( response ) { | |
| 317 | + | |
| 318 | + if( ! $('#' + response_id).length ) | |
| 319 | + $this.parent().append('<span id="' + response_id + '" style="display: inline-block; padding: 5px 0 0 8px;"></span>'); | |
| 320 | + | |
| 321 | + // Update response color text | |
| 322 | + if( response.success === false ) | |
| 323 | + $('#' + response_id).css({color:'#a00'}); | |
| 324 | + else | |
| 325 | + $('#' + response_id).css({color:''}); | |
| 326 | + | |
| 327 | + $('#' + response_id).html( ( response.data.message !== undefined ? response.data.message : response.data ) ); | |
| 328 | + | |
| 329 | + // Enable the buttons | |
| 330 | + $('#logs_clean_up_count, #logs_clean_up').prop('disabled', false); | |
| 331 | + } | |
| 332 | + ).fail(function() { | |
| 333 | + | |
| 334 | + $('#' + response_id).html('The server has returned an internal error.'); | |
| 335 | + | |
| 336 | + // Enable the buttons | |
| 337 | + $('#logs_clean_up_count, #logs_clean_up').prop('disabled', false); | |
| 338 | + }); | |
| 339 | + | |
| 340 | + }); | |
| 341 | + | |
| 342 | + // ---------------------------------- | |
| 343 | + // Reset Data Tool | |
| 344 | + // ---------------------------------- | |
| 345 | + | |
| 346 | + var reset_data_dialog = $("#reset-data-dialog"); | |
| 347 | + | |
| 348 | + reset_data_dialog.dialog({ | |
| 349 | + dialogClass : 'wp-dialog', | |
| 350 | + modal : true, | |
| 351 | + autoOpen : false, | |
| 352 | + closeOnEscape : true, | |
| 353 | + draggable : false, | |
| 354 | + width : 500, | |
| 355 | + buttons : [ | |
| 356 | + { | |
| 357 | + text: "Yes, delete it permanently", | |
| 358 | + class: "button-primary reset-data-button", | |
| 359 | + click: function() { | |
| 360 | + $('.reset-data-button').prop('disabled', true); | |
| 361 | + | |
| 362 | + if( ! $('#reset-data-response').length ) | |
| 363 | + $('.reset-data-button').parent().parent().prepend('<span id="reset-data-response"></span>'); | |
| 364 | + | |
| 365 | + // Show the spinner | |
| 366 | + $('#reset-data-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 367 | + | |
| 368 | + var items = []; | |
| 369 | + | |
| 370 | + $('.cmb2-id-data-to-reset input:checked').each(function() { | |
| 371 | + items.push($(this).val()); | |
| 372 | + }); | |
| 373 | + | |
| 374 | + $.post( | |
| 375 | + ajaxurl, | |
| 376 | + { | |
| 377 | + action: 'gamipress_reset_data_tool', | |
| 378 | + nonce: gamipress_admin_tools.nonce, | |
| 379 | + items: items | |
| 380 | + }, | |
| 381 | + function( response ) { | |
| 382 | + | |
| 383 | + if( response.success === false ) | |
| 384 | + $('#reset-data-response').css({color:'#a00'}); | |
| 385 | + else | |
| 386 | + $('#reset-data-response').css({color:''}); | |
| 387 | + | |
| 388 | + $('#reset-data-response').html(response.data); | |
| 389 | + | |
| 390 | + if( response.success === true ) { | |
| 391 | + | |
| 392 | + setTimeout(function() { | |
| 393 | + $('.cmb2-id-data-to-reset input:checked').each(function() { | |
| 394 | + $(this).prop( 'checked', false ); | |
| 395 | + }); | |
| 396 | + | |
| 397 | + reset_data_dialog.dialog( "close" ); | |
| 398 | + }, 5000); | |
| 399 | + } | |
| 400 | + | |
| 401 | + $('.reset-data-button').prop('disabled', false); | |
| 402 | + } | |
| 403 | + ); | |
| 404 | + } | |
| 405 | + }, | |
| 406 | + { | |
| 407 | + text: "Cancel", | |
| 408 | + class: "cancel-reset-data-button", | |
| 409 | + click: function() { | |
| 410 | + $( this ).dialog( "close" ); | |
| 411 | + } | |
| 412 | + } | |
| 413 | + | |
| 414 | + ] | |
| 415 | + }); | |
| 416 | + | |
| 417 | + $("#reset_data").on('click', function(e) { | |
| 418 | + e.preventDefault(); | |
| 419 | + | |
| 420 | + $('#reset-data-warning').remove(); | |
| 421 | + | |
| 422 | + var checked_options = $('.cmb2-id-data-to-reset input:checked'); | |
| 423 | + | |
| 424 | + if( checked_options.length ) { | |
| 425 | + | |
| 426 | + var reminder_html = ''; | |
| 427 | + | |
| 428 | + checked_options.each(function() { | |
| 429 | + reminder_html += '<li>' + $(this).next().text() + '</li>' | |
| 430 | + }); | |
| 431 | + | |
| 432 | + // Add a reminder with data to be removed | |
| 433 | + $('#reset-data-reminder').html('<ul>' + reminder_html + '</ul>'); | |
| 434 | + | |
| 435 | + // Open our dialog | |
| 436 | + reset_data_dialog.dialog('open'); | |
| 437 | + | |
| 438 | + // Remove the initial jQuery UI Dialog auto focus | |
| 439 | + $('.ui-dialog :button').blur(); | |
| 440 | + } else { | |
| 441 | + $(this).parent().prepend('<p id="reset-data-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose at least one option.</p>'); | |
| 442 | + } | |
| 443 | + }); | |
| 444 | + | |
| 445 | + $('.cmb2-id-data-to-reset').on('change', 'input', function() { | |
| 446 | + | |
| 447 | + $('#reset-data-warning').remove(); | |
| 448 | + | |
| 449 | + var checked_option = $(this).val(); | |
| 450 | + | |
| 451 | + if( checked_option === 'achievement_types' ) { | |
| 452 | + $('.cmb2-id-data-to-reset input[value="achievements"], .cmb2-id-data-to-reset input[value="steps"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 453 | + } else if( checked_option === 'achievements' ) { | |
| 454 | + $('.cmb2-id-data-to-reset input[value="steps"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 455 | + } else if( checked_option === 'points_types' ) { | |
| 456 | + $('.cmb2-id-data-to-reset input[value="points_awards"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 457 | + $('.cmb2-id-data-to-reset input[value="points_deducts"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 458 | + } else if( checked_option === 'rank_types' ) { | |
| 459 | + $('.cmb2-id-data-to-reset input[value="ranks"], .cmb2-id-data-to-reset input[value="rank_requirements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 460 | + } else if( checked_option === 'ranks' ) { | |
| 461 | + $('.cmb2-id-data-to-reset input[value="rank_requirements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 462 | + } else if( checked_option === 'earnings' ) { | |
| 463 | + $('.cmb2-id-data-to-reset input[value="earned_points"], .cmb2-id-data-to-reset input[value="earned_achievements"], .cmb2-id-data-to-reset input[value="earned_ranks"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 464 | + } | |
| 465 | + | |
| 466 | + }); | |
| 467 | + | |
| 468 | + // ---------------------------------- | |
| 469 | + // Export Achievements, Points and Ranks Tool | |
| 470 | + // ---------------------------------- | |
| 471 | + | |
| 472 | + $('#export_achievements, #export_points, #export_ranks, #export_earnings').on('click', function(e) { | |
| 473 | + e.preventDefault(); | |
| 474 | + | |
| 475 | + var $this = $(this); | |
| 476 | + var type; | |
| 477 | + var error = ''; | |
| 478 | + | |
| 479 | + switch( $this.attr('id') ) { | |
| 480 | + case 'export_achievements': | |
| 481 | + // Achievements export | |
| 482 | + type = 'achievements'; | |
| 483 | + | |
| 484 | + // Check achievement types | |
| 485 | + if( ! $('input[name="export_achievements_achievement_types[]"]:checked').length ) { | |
| 486 | + error = 'You need to choose at least 1 achievement type to export.'; | |
| 487 | + } | |
| 488 | + break; | |
| 489 | + case 'export_points': | |
| 490 | + // Points export | |
| 491 | + type = 'points'; | |
| 492 | + | |
| 493 | + // Check points types | |
| 494 | + if( ! $('input[name="export_points_points_types[]"]:checked').length ) { | |
| 495 | + error = 'You need to choose at least 1 points type to export.'; | |
| 496 | + } | |
| 497 | + break; | |
| 498 | + case 'export_ranks': | |
| 499 | + // Ranks export | |
| 500 | + type = 'ranks'; | |
| 501 | + | |
| 502 | + // Check rank types | |
| 503 | + if( ! $('input[name="export_ranks_rank_types[]"]:checked').length ) { | |
| 504 | + error = 'You need to choose at least 1 rank type to export.'; | |
| 505 | + } | |
| 506 | + break; | |
| 507 | + case 'export_earnings': | |
| 508 | + // Ranks export | |
| 509 | + type = 'earnings'; | |
| 510 | + | |
| 511 | + // Check rank types | |
| 512 | + if( ! $('input[name="export_earnings_types[]"]:checked').length ) { | |
| 513 | + error = 'You need to choose at least 1 type to export.'; | |
| 514 | + } | |
| 515 | + break; | |
| 516 | + } | |
| 517 | + | |
| 518 | + // Remove error messages | |
| 519 | + $('#export-' + type + '-warning').remove(); | |
| 520 | + | |
| 521 | + // If there is any error, show it to the user | |
| 522 | + if( error !== '' ) { | |
| 523 | + $this.parent().prepend('<p id="export-' + type + '-warning" class="cmb2-metabox-description" style="color: #a00;">' + error + '</p>'); | |
| 524 | + return false; | |
| 525 | + } | |
| 526 | + | |
| 527 | + // Reset the data to export | |
| 528 | + to_export = []; | |
| 529 | + | |
| 530 | + gamipress_run_export_tool( type ); | |
| 531 | + | |
| 532 | + }); | |
| 533 | + | |
| 534 | + var to_export = []; | |
| 535 | + | |
| 536 | + // Function to handle the export process | |
| 537 | + function gamipress_run_export_tool( type, loop ) { | |
| 538 | + | |
| 539 | + var button_element = $('#export_' + type ); | |
| 540 | + var response_element = $('#export-' + type + '-response'); | |
| 541 | + var data; | |
| 542 | + | |
| 543 | + if( loop === undefined ) | |
| 544 | + loop = 0; | |
| 545 | + | |
| 546 | + // Disable the export button | |
| 547 | + button_element.prop('disabled', true); | |
| 548 | + | |
| 549 | + // Check if response element exists | |
| 550 | + if( ! response_element.length ) { | |
| 551 | + button_element.parent().append('<span id="export-' + type + '-response" style="display: inline-block; padding: 5px 0 0 8px;"></span>'); | |
| 552 | + | |
| 553 | + response_element = $('#export-' + type + '-response'); | |
| 554 | + } | |
| 555 | + | |
| 556 | + // Show the spinner | |
| 557 | + if( loop === 0 ) | |
| 558 | + response_element.html('<span class="spinner is-active" style="float: none; margin: 0;"></span>'); | |
| 559 | + | |
| 560 | + // Setup request data per type | |
| 561 | + switch( type ) { | |
| 562 | + case 'achievements': | |
| 563 | + // Achievements data | |
| 564 | + var achievement_types = []; | |
| 565 | + | |
| 566 | + $('input[name="export_achievements_achievement_types[]"]:checked').each(function() { | |
| 567 | + achievement_types.push( $(this).val() ); | |
| 568 | + }); | |
| 569 | + | |
| 570 | + data = { | |
| 571 | + action: 'gamipress_import_export_achievements_tool_export', | |
| 572 | + nonce: gamipress_admin_tools.nonce, | |
| 573 | + achievement_types: achievement_types, | |
| 574 | + user_field: $('#export_achievements_user_field').val(), | |
| 575 | + achievement_field: $('#export_achievements_achievement_field').val(), | |
| 576 | + loop: loop | |
| 577 | + }; | |
| 578 | + break; | |
| 579 | + case 'points': | |
| 580 | + // Points data | |
| 581 | + var points_types = []; | |
| 582 | + | |
| 583 | + $('input[name="export_points_points_types[]"]:checked').each(function() { | |
| 584 | + points_types.push( $(this).val() ); | |
| 585 | + }); | |
| 586 | + | |
| 587 | + data = { | |
| 588 | + action: 'gamipress_import_export_points_tool_export', | |
| 589 | + nonce: gamipress_admin_tools.nonce, | |
| 590 | + points_types: points_types, | |
| 591 | + user_field: $('#export_points_user_field').val(), | |
| 592 | + loop: loop | |
| 593 | + }; | |
| 594 | + break; | |
| 595 | + case 'ranks': | |
| 596 | + // Ranks data | |
| 597 | + var rank_types = []; | |
| 598 | + | |
| 599 | + $('input[name="export_ranks_rank_types[]"]:checked').each(function() { | |
| 600 | + rank_types.push( $(this).val() ); | |
| 601 | + }); | |
| 602 | + | |
| 603 | + data = { | |
| 604 | + action: 'gamipress_import_export_ranks_tool_export', | |
| 605 | + nonce: gamipress_admin_tools.nonce, | |
| 606 | + rank_types: rank_types, | |
| 607 | + user_field: $('#export_ranks_user_field').val(), | |
| 608 | + rank_field: $('#export_ranks_rank_field').val(), | |
| 609 | + loop: loop | |
| 610 | + }; | |
| 611 | + break; | |
| 612 | + case 'earnings': | |
| 613 | + // Ranks data | |
| 614 | + var types = []; | |
| 615 | + | |
| 616 | + $('input[name="export_earnings_types[]"]:checked').each(function() { | |
| 617 | + types.push( $(this).val() ); | |
| 618 | + }); | |
| 619 | + | |
| 620 | + data = { | |
| 621 | + action: 'gamipress_import_export_earnings_tool_export', | |
| 622 | + nonce: gamipress_admin_tools.nonce, | |
| 623 | + types: types, | |
| 624 | + user_field: $('#export_earnings_user_field').val(), | |
| 625 | + post_field: $('#export_earnings_post_field').val(), | |
| 626 | + from: $('#export_earnings_from').val(), | |
| 627 | + to: $('#export_earnings_to').val(), | |
| 628 | + loop: loop | |
| 629 | + }; | |
| 630 | + break; | |
| 631 | + } | |
| 632 | + | |
| 633 | + $.post( | |
| 634 | + ajaxurl, | |
| 635 | + data, | |
| 636 | + function( response ) { | |
| 637 | + | |
| 638 | + if( response.data.items !== undefined ) { | |
| 639 | + // Concat received items | |
| 640 | + to_export = to_export.concat( response.data.items ); | |
| 641 | + } | |
| 642 | + | |
| 643 | + // Run again utility | |
| 644 | + if( response.data.run_again !== undefined && response.data.run_again && response.success === true ) { | |
| 645 | + | |
| 646 | + response_element.html( ( response.data.message !== undefined ? response.data.message : response.data ) ); | |
| 647 | + | |
| 648 | + loop++; | |
| 649 | + | |
| 650 | + // Run again passing the next loop index | |
| 651 | + gamipress_run_export_tool( type, loop ); | |
| 652 | + | |
| 653 | + return; | |
| 654 | + } | |
| 655 | + | |
| 656 | + if( response.success === false ) | |
| 657 | + response_element.css({color:'#a00'}); | |
| 658 | + else | |
| 659 | + response_element.css({color:''}); | |
| 660 | + | |
| 661 | + response_element.html( ( response.data.message !== undefined ? response.data.message : response.data ) ); | |
| 662 | + | |
| 663 | + // Enable the export button | |
| 664 | + button_element.prop('disabled', false); | |
| 665 | + | |
| 666 | + if( to_export.length ) { | |
| 667 | + // Download the CSV with the data | |
| 668 | + gamipress_download_csv( to_export, 'gamipress-user-' + type + '-export' ); | |
| 669 | + } | |
| 670 | + } | |
| 671 | + ).fail(function() { | |
| 672 | + | |
| 673 | + response_element.html('The server has returned an internal error.'); | |
| 674 | + | |
| 675 | + // Enable the export button | |
| 676 | + button_element.prop('disabled', false); | |
| 677 | + }); | |
| 678 | + | |
| 679 | + } | |
| 680 | + | |
| 681 | + // ---------------------------------- | |
| 682 | + // Import Achievements, Points and Ranks Tool | |
| 683 | + // ---------------------------------- | |
| 684 | + | |
| 685 | + $('#import_achievements, #import_points, #import_ranks').on('click', function(e) { | |
| 686 | + e.preventDefault(); | |
| 687 | + | |
| 688 | + var $this = $(this); | |
| 689 | + var type; | |
| 690 | + | |
| 691 | + switch( $this.attr('id') ) { | |
| 692 | + case 'import_achievements': | |
| 693 | + // Achievements import | |
| 694 | + type = 'achievements'; | |
| 695 | + break; | |
| 696 | + case 'import_points': | |
| 697 | + // Points import | |
| 698 | + type = 'points'; | |
| 699 | + break; | |
| 700 | + case 'import_ranks': | |
| 701 | + // Ranks import | |
| 702 | + type = 'ranks'; | |
| 703 | + break; | |
| 704 | + } | |
| 705 | + | |
| 706 | + // Remove error messages | |
| 707 | + $('#import-' + type + '-warning').remove(); | |
| 708 | + | |
| 709 | + // Remove old responses | |
| 710 | + $('#import-' + type + '-response').remove(); | |
| 711 | + | |
| 712 | + // Check if CSV file has been chosen | |
| 713 | + if( $('#import_' + type + '_file')[0].files[0] === undefined ) { | |
| 714 | + $this.parent().prepend('<p id="import-' + type + '-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose a CSV file to import.</p>'); | |
| 715 | + return false; | |
| 716 | + } | |
| 717 | + | |
| 718 | + // Setup the form data to send | |
| 719 | + var form_data = new FormData(); | |
| 720 | + form_data.append( 'action', 'gamipress_import_export_' + type + '_tool_import' ); | |
| 721 | + form_data.append( 'nonce', gamipress_admin_tools.nonce ); | |
| 722 | + form_data.append( 'file', $('#import_' + type + '_file')[0].files[0] ); | |
| 723 | + | |
| 724 | + // Disable the button | |
| 725 | + $this.prop('disabled', true); | |
| 726 | + | |
| 727 | + if( ! $('#import-' + type + '-response').length ) | |
| 728 | + $this.parent().prepend('<p id="import-' + type + '-response" class="cmb2-metabox-description"></p>'); | |
| 729 | + | |
| 730 | + // Show the spinner | |
| 731 | + $('#import-' + type + '-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 732 | + | |
| 733 | + $.ajax({ | |
| 734 | + url: ajaxurl, | |
| 735 | + method: 'post', | |
| 736 | + cache: false, | |
| 737 | + contentType: false, | |
| 738 | + processData: false, | |
| 739 | + data: form_data, | |
| 740 | + success: function(response) { | |
| 741 | + | |
| 742 | + // Set a red color to the response to let user known that something is going wrong | |
| 743 | + if( response.success === false ) | |
| 744 | + $('#import-' + type + '-response').css({color:'#a00'}); | |
| 745 | + else | |
| 746 | + $('#import-' + type + '-response').css({color:''}); | |
| 747 | + | |
| 748 | + // Update the response content | |
| 749 | + $('#import-' + type + '-response').html( response.data ); | |
| 750 | + | |
| 751 | + // Re-enable the button | |
| 752 | + $this.prop('disabled', false); | |
| 753 | + | |
| 754 | + } | |
| 755 | + }); | |
| 756 | + | |
| 757 | + }); | |
| 758 | + | |
| 759 | + // ---------------------------------- | |
| 760 | + // Download Achievements, Points and Ranks CSV Template | |
| 761 | + // ---------------------------------- | |
| 762 | + | |
| 763 | + $('#download_achievements_csv_template, #download_points_csv_template, #download_ranks_csv_template').on('click', function(e) { | |
| 764 | + e.preventDefault(); | |
| 765 | + | |
| 766 | + var type, sample_data; | |
| 767 | + | |
| 768 | + switch( $(this).attr('id') ) { | |
| 769 | + case 'download_achievements_csv_template': | |
| 770 | + // Achievements sample data | |
| 771 | + type = 'achievements'; | |
| 772 | + sample_data = [ | |
| 773 | + { | |
| 774 | + user: 'User (ID, username or email)', | |
| 775 | + achievements: 'Achievements (Comma-separated list of IDs, titles and/or slugs)', | |
| 776 | + notes: 'Notes (This column won\'t be processed by the tool)', | |
| 777 | + }, | |
| 778 | + { | |
| 779 | + user: gamipress_admin_tools.user_id, | |
| 780 | + achievements: '1,2,3', | |
| 781 | + notes: 'Awarding by user ID and passing the achievements IDs', | |
| 782 | + }, | |
| 783 | + { | |
| 784 | + user: gamipress_admin_tools.user_name, | |
| 785 | + achievements: 'Test Badge,Custom Quest,Super Achievement', | |
| 786 | + notes: 'Awarding by username and passing the achievements titles', | |
| 787 | + }, | |
| 788 | + { | |
| 789 | + user: gamipress_admin_tools.user_email, | |
| 790 | + achievements: 'test-badge,custom-quest,super-achievement', | |
| 791 | + notes: 'Awarding by email and passing the achievements slugs', | |
| 792 | + }, | |
| 793 | + { | |
| 794 | + user: gamipress_admin_tools.user_id, | |
| 795 | + achievements: '-1,-Test Badge,-test-badge', | |
| 796 | + notes: 'Adding a negative sign will revoke the achievements', | |
| 797 | + }, | |
| 798 | + ]; | |
| 799 | + break; | |
| 800 | + case 'download_points_csv_template': | |
| 801 | + // Points sample data | |
| 802 | + type = 'points'; | |
| 803 | + sample_data = [ | |
| 804 | + { | |
| 805 | + user: 'User (ID, username or email)', | |
| 806 | + points: 'Points', | |
| 807 | + points_type: 'Points Type (slug)', | |
| 808 | + log: 'Log Description (Optional)', | |
| 809 | + notes: 'Notes (This column won\'t be processed by the tool)', | |
| 810 | + }, | |
| 811 | + { | |
| 812 | + user: gamipress_admin_tools.user_id, | |
| 813 | + points: '100', | |
| 814 | + points_type: 'credits', | |
| 815 | + log: '100 credits awarded through the user\'s ID', | |
| 816 | + notes: 'Awarding points by user ID', | |
| 817 | + }, | |
| 818 | + { | |
| 819 | + user: gamipress_admin_tools.user_name, | |
| 820 | + points: '1000', | |
| 821 | + points_type: 'coins', | |
| 822 | + log: '1,000 coins awarded through the user\'s username', | |
| 823 | + notes: 'Awarding points by username', | |
| 824 | + }, | |
| 825 | + { | |
| 826 | + user: gamipress_admin_tools.user_email, | |
| 827 | + points: '50', | |
| 828 | + points_type: 'gems', | |
| 829 | + log: '50 gems awarded through the user\'s email', | |
| 830 | + notes: 'Awarding points by email', | |
| 831 | + }, | |
| 832 | + { | |
| 833 | + user: gamipress_admin_tools.user_email, | |
| 834 | + points: '-50', | |
| 835 | + points_type: 'gems', | |
| 836 | + log: '50 gems deducted through the user\'s email', | |
| 837 | + notes: 'Adding a negative sign will deduct the points', | |
| 838 | + }, | |
| 839 | + ]; | |
| 840 | + break; | |
| 841 | + case 'download_ranks_csv_template': | |
| 842 | + // Ranks sample data | |
| 843 | + type = 'ranks'; | |
| 844 | + sample_data = [ | |
| 845 | + { | |
| 846 | + user: 'User (ID, username or email)', | |
| 847 | + rank: 'Rank (ID, title or slug of rank to assign to the user)', | |
| 848 | + notes: 'Notes (This column won\'t be processed by the tool)', | |
| 849 | + }, | |
| 850 | + { | |
| 851 | + user: gamipress_admin_tools.user_id, | |
| 852 | + rank: '1', | |
| 853 | + notes: 'Setting rank by user ID and passing the rank ID', | |
| 854 | + }, | |
| 855 | + { | |
| 856 | + user: gamipress_admin_tools.user_name, | |
| 857 | + rank: 'Test Rank', | |
| 858 | + notes: 'Setting rank by username and passing the rank title', | |
| 859 | + }, | |
| 860 | + { | |
| 861 | + user: gamipress_admin_tools.user_email, | |
| 862 | + rank: 'test-rank', | |
| 863 | + notes: 'Setting rank by email and passing the rank slug', | |
| 864 | + }, | |
| 865 | + { | |
| 866 | + user: gamipress_admin_tools.user_id, | |
| 867 | + rank: '-1', | |
| 868 | + notes: 'Adding a negative sign will revoke the rank to the user and will try to assign the previous one (following the priority order)', | |
| 869 | + }, | |
| 870 | + ]; | |
| 871 | + break; | |
| 872 | + } | |
| 873 | + | |
| 874 | + if( Array.isArray( sample_data ) ) { | |
| 875 | + gamipress_download_csv( sample_data, 'gamipress-' + type + '-csv-template' ); | |
| 876 | + } | |
| 877 | + | |
| 878 | + }); | |
| 879 | + | |
| 880 | + // ---------------------------------- | |
| 881 | + // Export Setup Tool | |
| 882 | + // ---------------------------------- | |
| 883 | + | |
| 884 | + $('.gamipress-all-types-multicheck').on('change', 'input', function() { | |
| 885 | + | |
| 886 | + var box = $(this).closest('.cmb2-metabox').attr('id'); | |
| 887 | + | |
| 888 | + if( box === 'cmb2-metabox-import-export-setup' ) { | |
| 889 | + $('#export-setup-warning').remove(); | |
| 890 | + } else if( box === 'cmb2-metabox-import-export-earnings' ) { | |
| 891 | + $('#export-earnings-warning').remove(); | |
| 892 | + } | |
| 893 | + | |
| 894 | + | |
| 895 | + var checked_option = $(this).val(); | |
| 896 | + var type = ''; | |
| 897 | + | |
| 898 | + if( checked_option === 'all-points-types' ) { | |
| 899 | + | |
| 900 | + $('.gamipress-all-types-multicheck input[value$="-points-type"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 901 | + $('.gamipress-all-types-multicheck input[value$="-points-awards"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 902 | + $('.gamipress-all-types-multicheck input[value$="-points-deducts"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 903 | + | |
| 904 | + } else if( checked_option.endsWith( '-points-type' ) ) { | |
| 905 | + | |
| 906 | + type = checked_option.replace( '-points-type', '' ); | |
| 907 | + $('.gamipress-all-types-multicheck input[value="' + type + '-points-awards"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 908 | + $('.gamipress-all-types-multicheck input[value="' + type + '-points-deducts"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 909 | + | |
| 910 | + } else if( checked_option === 'all-achievement-types' ) { | |
| 911 | + | |
| 912 | + $('.gamipress-all-types-multicheck input[value$="-achievement-type"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 913 | + $('.gamipress-all-types-multicheck input[value$="-achievements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 914 | + $('.gamipress-all-types-multicheck input[value$="-steps"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 915 | + | |
| 916 | + } else if( checked_option.endsWith( '-achievement-type' ) ) { | |
| 917 | + | |
| 918 | + type = checked_option.replace( '-achievement-type', '' ); | |
| 919 | + $('.gamipress-all-types-multicheck input[value="' + type + '-achievements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 920 | + $('.gamipress-all-types-multicheck input[value="' + type + '-steps"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 921 | + | |
| 922 | + } else if( checked_option.endsWith( '-achievements' ) ) { | |
| 923 | + | |
| 924 | + type = checked_option.replace( '-achievements', '' ); | |
| 925 | + $('.gamipress-all-types-multicheck input[value="' + type + '-steps"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 926 | + | |
| 927 | + } else if( checked_option === 'all-rank-types' ) { | |
| 928 | + | |
| 929 | + $('.gamipress-all-types-multicheck input[value$="-rank-type"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 930 | + $('.gamipress-all-types-multicheck input[value$="-ranks"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 931 | + $('.gamipress-all-types-multicheck input[value$="-rank-requirements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 932 | + | |
| 933 | + } else if( checked_option.endsWith( '-rank-type' ) ) { | |
| 934 | + | |
| 935 | + type = checked_option.replace( '-rank-type', '' ); | |
| 936 | + $('.gamipress-all-types-multicheck input[value="' + type + '-ranks"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 937 | + $('.gamipress-all-types-multicheck input[value="' + type + '-rank-requirements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 938 | + | |
| 939 | + } else if( checked_option.endsWith( '-ranks' ) ) { | |
| 940 | + | |
| 941 | + type = checked_option.replace( '-ranks', '' ); | |
| 942 | + $('.gamipress-all-types-multicheck input[value="' + type + '-rank-requirements"]').prop( 'checked', $(this).prop( 'checked' ) ); | |
| 943 | + } | |
| 944 | + | |
| 945 | + }); | |
| 946 | + | |
| 947 | + $("#export_setup").on('click', function(e) { | |
| 948 | + e.preventDefault(); | |
| 949 | + | |
| 950 | + $('#export-setup-warning').remove(); | |
| 951 | + | |
| 952 | + var checked_options = $('.cmb2-id-export-setup-options input:checked'); | |
| 953 | + | |
| 954 | + if( checked_options.length === 0 ) { | |
| 955 | + $(this).parent().prepend('<p id="export-setup-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose at least one option to export.</p>'); | |
| 956 | + return; | |
| 957 | + } | |
| 958 | + | |
| 959 | + $('.export-setup-button').prop('disabled', true); | |
| 960 | + | |
| 961 | + if( ! $('#export-setup-response').length ) | |
| 962 | + $('.export-setup-button').parent().parent().prepend('<span id="export-setup-response"></span>'); | |
| 963 | + | |
| 964 | + // Show the spinner | |
| 965 | + $('#export-setup-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 966 | + | |
| 967 | + var items = []; | |
| 968 | + | |
| 969 | + $('.cmb2-id-export-setup-options input:checked').each(function() { | |
| 970 | + items.push($(this).val()); | |
| 971 | + }); | |
| 972 | + | |
| 973 | + $.post( | |
| 974 | + ajaxurl, | |
| 975 | + { | |
| 976 | + action: 'gamipress_export_setup_tool', | |
| 977 | + nonce: gamipress_admin_tools.nonce, | |
| 978 | + items: items | |
| 979 | + }, | |
| 980 | + function( response ) { | |
| 981 | + | |
| 982 | + if( response.success === false ) | |
| 983 | + $('#export-setup-response').css({color:'#a00'}); | |
| 984 | + else | |
| 985 | + $('#export-setup-response').css({color:''}); | |
| 986 | + | |
| 987 | + $('#export-setup-response').html( ( response.data.message !== undefined ? response.data.message : response.data ) ); | |
| 988 | + | |
| 989 | + if( response.success === true ) { | |
| 990 | + gamipress_download_file( response.data.setup, 'setup-export', 'txt', 'text/plain' ); | |
| 991 | + } | |
| 992 | + | |
| 993 | + $('.export-setup-button').prop('disabled', false); | |
| 994 | + } | |
| 995 | + ); | |
| 996 | + | |
| 997 | + }); | |
| 998 | + | |
| 999 | + // ---------------------------------- | |
| 1000 | + // Import Setup Tool | |
| 1001 | + // ---------------------------------- | |
| 1002 | + | |
| 1003 | + $('#import_setup').on('click', function(e) { | |
| 1004 | + e.preventDefault(); | |
| 1005 | + | |
| 1006 | + $('#import-setup-warning').remove(); | |
| 1007 | + | |
| 1008 | + if( $('#import_setup_file')[0].files[0] === undefined ) { | |
| 1009 | + $(this).parent().prepend('<p id="import-setup-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose a configuration file to import.</p>'); | |
| 1010 | + return false; | |
| 1011 | + } | |
| 1012 | + | |
| 1013 | + var $this = $(this); | |
| 1014 | + var form_data = new FormData(); | |
| 1015 | + form_data.append( 'action', 'gamipress_import_setup_tool' ); | |
| 1016 | + form_data.append( 'nonce', gamipress_admin_tools.nonce ); | |
| 1017 | + form_data.append( 'file', $('#import_setup_file')[0].files[0] ); | |
| 1018 | + | |
| 1019 | + // Disable the button | |
| 1020 | + $this.prop('disabled', true); | |
| 1021 | + | |
| 1022 | + if( ! $('#import-setup-response').length ) | |
| 1023 | + $this.parent().append('<span id="import-setup-response"></span>'); | |
| 1024 | + | |
| 1025 | + // Show the spinner | |
| 1026 | + $('#import-setup-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 1027 | + | |
| 1028 | + $.ajax({ | |
| 1029 | + url: ajaxurl, | |
| 1030 | + method: 'post', | |
| 1031 | + cache: false, | |
| 1032 | + contentType: false, | |
| 1033 | + processData: false, | |
| 1034 | + data: form_data, | |
| 1035 | + success: function(response) { | |
| 1036 | + | |
| 1037 | + if( response.success === false ) | |
| 1038 | + $('#import-setup-response').css({color:'#a00'}); | |
| 1039 | + else | |
| 1040 | + $('#import-setup-response').css({color:''}); | |
| 1041 | + | |
| 1042 | + $('#import-setup-response').html(response.data); | |
| 1043 | + | |
| 1044 | + $this.prop('disabled', false); | |
| 1045 | + | |
| 1046 | + } | |
| 1047 | + }); | |
| 1048 | + }); | |
| 1049 | + | |
| 1050 | + // ---------------------------------- | |
| 1051 | + // Import Settings Tool | |
| 1052 | + // ---------------------------------- | |
| 1053 | + | |
| 1054 | + $('#import_settings').on('click', function(e) { | |
| 1055 | + e.preventDefault(); | |
| 1056 | + | |
| 1057 | + $('#import-settings-warning').remove(); | |
| 1058 | + | |
| 1059 | + if( $('#import_settings_file')[0].files[0] === undefined ) { | |
| 1060 | + $(this).parent().prepend('<p id="import-settings-warning" class="cmb2-metabox-description" style="color: #a00;">You need to choose a configuration file to import.</p>'); | |
| 1061 | + return false; | |
| 1062 | + } | |
| 1063 | + | |
| 1064 | + var $this = $(this); | |
| 1065 | + var form_data = new FormData(); | |
| 1066 | + form_data.append( 'action', 'gamipress_import_settings_tool' ); | |
| 1067 | + form_data.append( 'nonce', gamipress_admin_tools.nonce ); | |
| 1068 | + form_data.append( 'file', $('#import_settings_file')[0].files[0] ); | |
| 1069 | + | |
| 1070 | + // Disable the button | |
| 1071 | + $this.prop('disabled', true); | |
| 1072 | + | |
| 1073 | + if( ! $('#import-settings-response').length ) | |
| 1074 | + $this.parent().append('<span id="import-settings-response"></span>'); | |
| 1075 | + | |
| 1076 | + // Show the spinner | |
| 1077 | + $('#import-settings-response').html('<span class="spinner is-active" style="float: none;"></span>'); | |
| 1078 | + | |
| 1079 | + $.ajax({ | |
| 1080 | + url: ajaxurl, | |
| 1081 | + method: 'post', | |
| 1082 | + cache: false, | |
| 1083 | + contentType: false, | |
| 1084 | + processData: false, | |
| 1085 | + data: form_data, | |
| 1086 | + success: function(response) { | |
| 1087 | + | |
| 1088 | + if( response.success === false ) | |
| 1089 | + $('#import-settings-response').css({color:'#a00'}); | |
| 1090 | + else | |
| 1091 | + $('#import-settings-response').css({color:''}); | |
| 1092 | + | |
| 1093 | + $('#import-settings-response').html(response.data); | |
| 1094 | + | |
| 1095 | + $this.prop('disabled', false); | |
| 1096 | + | |
| 1097 | + } | |
| 1098 | + }); | |
| 1099 | + | |
| 1100 | + }); | |
| 1101 | + | |
| 1102 | +})( jQuery ); | |