| 1 |
(function ( $, window, document ) { |
| 2 |
|
| 3 |
// Selector Control |
| 4 |
$('.gamipress-selector select').each(function() { gamipress_selector( $(this) ); }); |
| 5 |
|
| 6 |
// Post Selector Control |
| 7 |
$('.gamipress-post-selector select').each(function() { gamipress_post_selector( $(this) ); }); |
| 8 |
|
| 9 |
// User Selector Control |
| 10 |
$('.gamipress-user-selector select').each(function() { gamipress_user_selector( $(this) ); }); |
| 11 |
|
| 12 |
// Initialize on widgets area |
| 13 |
$(document).on('widget-updated widget-added', function(e, widget) { |
| 14 |
|
| 15 |
// Selector Control |
| 16 |
widget.find( '.gamipress-selector select:not(.select2-hidden-accessible)' ).each(function() { |
| 17 |
gamipress_selector( $(this) ); |
| 18 |
}); |
| 19 |
|
| 20 |
// Post Selector Control |
| 21 |
widget.find( '.gamipress-post-selector select:not(.select2-hidden-accessible)' ).each(function() { |
| 22 |
gamipress_post_selector( $(this) ); |
| 23 |
}); |
| 24 |
|
| 25 |
// User Selector Control |
| 26 |
widget.find( '.gamipress-user-selector select:not(.select2-hidden-accessible)' ).each(function() { |
| 27 |
gamipress_user_selector( $(this) ); |
| 28 |
}); |
| 29 |
|
| 30 |
}); |
| 31 |
|
| 32 |
// Dynamically show/hide achievement meta inputs based on "Earned By" selection |
| 33 |
$("#_gamipress_earned_by").on('change', function() { |
| 34 |
|
| 35 |
// Define our potentially unnecessary inputs |
| 36 |
var gamipress_points_required = $('#_gamipress_points_required').closest('.cmb-row'); |
| 37 |
var gamipress_rank_type_required = $('#_gamipress_rank_type_required').parent().parent(); |
| 38 |
var gamipress_rank_required = $('#_gamipress_rank_required').parent().parent(); |
| 39 |
|
| 40 |
// Hide our potentially unnecessary inputs |
| 41 |
gamipress_points_required.hide(); |
| 42 |
gamipress_rank_type_required.hide(); |
| 43 |
gamipress_rank_required.hide(); |
| 44 |
|
| 45 |
// Determine which inputs we should show |
| 46 |
if ( $(this).val() === 'points' ) { |
| 47 |
gamipress_points_required.show(); |
| 48 |
} else if ( $(this).val() === 'rank' ) { |
| 49 |
gamipress_rank_type_required.show(); |
| 50 |
$('#_gamipress_rank_type_required').trigger('change'); |
| 51 |
//gamipress_rank_required.show(); |
| 52 |
} |
| 53 |
}).trigger('change'); |
| 54 |
|
| 55 |
$('#_gamipress_rank_type_required').on('change', function() { |
| 56 |
|
| 57 |
var $this = $(this); |
| 58 |
|
| 59 |
var rank_type = $(this).val(); |
| 60 |
|
| 61 |
if( rank_type === '' ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$('<span class="spinner is-active" style="float: none;"></span>').insertAfter( $this ); |
| 66 |
|
| 67 |
var gamipress_rank_required = $('#_gamipress_rank_required').parent().parent(); |
| 68 |
var gamipress_rank_required_select = $('#_gamipress_rank_required'); |
| 69 |
|
| 70 |
gamipress_rank_required.hide(); |
| 71 |
|
| 72 |
if( rank_type !== '' && $("#_gamipress_earned_by").val() === 'rank' ) { |
| 73 |
|
| 74 |
$.post( |
| 75 |
ajaxurl, |
| 76 |
{ |
| 77 |
action: 'gamipress_get_ranks_options_html', |
| 78 |
nonce: gamipress_admin.nonce, |
| 79 |
post_type: rank_type, |
| 80 |
selected: gamipress_rank_required_select.val(), |
| 81 |
}, |
| 82 |
function( response ) { |
| 83 |
|
| 84 |
$this.next('.spinner').remove(); |
| 85 |
|
| 86 |
gamipress_rank_required_select.html( response ); |
| 87 |
|
| 88 |
// During request user can change the earned by value, so prevent to show if it was changed |
| 89 |
if( $("#_gamipress_earned_by").val() === 'rank' ) { |
| 90 |
gamipress_rank_required.show(); |
| 91 |
} |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
} else { |
| 96 |
gamipress_rank_required.hide(); |
| 97 |
} |
| 98 |
}); |
| 99 |
|
| 100 |
// Dynamically show/hide achievement meta inputs based on "Unlock with Points" checkbox |
| 101 |
$('#_gamipress_unlock_with_points').on('change', function() { |
| 102 |
var target = $('.cmb2-id--gamipress-points-to-unlock'); |
| 103 |
|
| 104 |
if( $(this).prop('checked') ) { |
| 105 |
target.slideDown(250); |
| 106 |
} else { |
| 107 |
target.slideUp(250); |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
if( ! $('#_gamipress_unlock_with_points').prop('checked') ) { |
| 112 |
$('.cmb2-id--gamipress-points-to-unlock').hide(); |
| 113 |
} |
| 114 |
|
| 115 |
// Toggle visibility of maximum earners based on show earners checked status |
| 116 |
$('#_gamipress_show_earners').on('change', function() { |
| 117 |
var target = $('.cmb2-id--gamipress-maximum-earners'); |
| 118 |
|
| 119 |
if( $(this).prop('checked') ) { |
| 120 |
target.slideDown(250); |
| 121 |
} else { |
| 122 |
target.slideUp(250); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
if( ! $('#_gamipress_show_earners').prop('checked') ) { |
| 127 |
$('.cmb2-id--gamipress-maximum-earners').hide(); |
| 128 |
} |
| 129 |
|
| 130 |
// Get the current slug for slugs checks |
| 131 |
var current_slug = $('input#post_name').val(); |
| 132 |
|
| 133 |
$('.gamipress-form').on( 'keyup', 'input#post_name', function() { |
| 134 |
var field = $(this); |
| 135 |
var slug = field.val(); |
| 136 |
var preview = field.next('.cmb2-metabox-description').find('.gamipress-post-name'); |
| 137 |
var error = gamipress_get_slug_error( slug, current_slug ); |
| 138 |
|
| 139 |
// Update preview element |
| 140 |
if( preview.length ) |
| 141 |
preview.text(slug); |
| 142 |
|
| 143 |
// Delete any existing version of this warning |
| 144 |
$('#slug-warning').remove(); |
| 145 |
|
| 146 |
if( error.length ) { |
| 147 |
// Set input to look like danger |
| 148 |
field.css({'background':'#faa', 'color':'#a00', 'border-color':'#a55' }); |
| 149 |
|
| 150 |
// Output a custom warning |
| 151 |
field.parent().append('<span id="slug-warning" class="cmb2-metabox-description" style="color: #a00;">' + error + '</span>'); |
| 152 |
|
| 153 |
// Disable the save button |
| 154 |
$('input#publish').prop( 'disabled', true ); |
| 155 |
|
| 156 |
return false; |
| 157 |
} else { |
| 158 |
// Restore the input style if there is no error |
| 159 |
field.css({'background':'', 'color':'', 'border-color':''}); |
| 160 |
|
| 161 |
// Re-enable the save button |
| 162 |
if( $('input#publish').prop( 'disabled' ) ) { |
| 163 |
$('input#publish').prop( 'disabled', false ); |
| 164 |
} |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
$('.gamipress-form input#post_name').trigger( 'keyup' ); |
| 169 |
|
| 170 |
// On change singular name, update plural and slug |
| 171 |
$('.gamipress-form').on( 'keyup', 'input#post_title', function() { |
| 172 |
var field = $(this); |
| 173 |
var singular = field.val(); |
| 174 |
var form = field.closest('.gamipress-form'); |
| 175 |
var plural_field = form.find('input#_gamipress_plural_name'); |
| 176 |
var slug_field = form.find('input#post_name'); |
| 177 |
|
| 178 |
// Bail if not plural field found |
| 179 |
if( plural_field.length === 0 ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
// Bail if user does not input letters |
| 184 |
if( ! /[a-zA-Z]/g.test(singular) ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
var plural = singular + 'S'; |
| 189 |
|
| 190 |
// Add the "s" as lowercase if text has any lowercase letter |
| 191 |
if( /[a-z]/g.test(singular) ) { |
| 192 |
plural = singular + 's'; |
| 193 |
} |
| 194 |
|
| 195 |
var slug = plural.toLowerCase().replaceAll(' ', '-'); |
| 196 |
|
| 197 |
// Update plural and slug field |
| 198 |
plural_field.val( plural ); |
| 199 |
slug_field.val( slug ); |
| 200 |
|
| 201 |
}); |
| 202 |
|
| 203 |
// Edit user rank |
| 204 |
$('body').on('click', '.profile-rank .profile-rank-toggle', function(e) { |
| 205 |
e.preventDefault(); |
| 206 |
|
| 207 |
$(this).slideUp(); |
| 208 |
$(this).next('.profile-rank-form-wrapper').slideDown(); |
| 209 |
|
| 210 |
var select = $(this).next('.profile-rank-form-wrapper').find('select'); |
| 211 |
|
| 212 |
// Save current value in a element's data |
| 213 |
select.data( 'current', select.val() ); |
| 214 |
|
| 215 |
}); |
| 216 |
|
| 217 |
// Cancel user rank edit |
| 218 |
$('body').on('click', '.profile-rank .profile-rank-cancel', function(e) { |
| 219 |
e.preventDefault(); |
| 220 |
|
| 221 |
var parent = $(this).parent().parent(); |
| 222 |
var select = parent.find('select'); |
| 223 |
|
| 224 |
parent.slideUp(); |
| 225 |
parent.prev('.profile-rank-toggle').slideDown(); |
| 226 |
|
| 227 |
// Restore current value (if user changed the input but won't save it) |
| 228 |
select.val( select.data( 'current' ) ); |
| 229 |
|
| 230 |
}); |
| 231 |
|
| 232 |
// Save user rank edit |
| 233 |
$('body').on('click', '.profile-rank .profile-rank-save', function(e) { |
| 234 |
e.preventDefault(); |
| 235 |
|
| 236 |
var $this = $(this); |
| 237 |
var parent = $this.closest('.profile-rank-form-wrapper'); |
| 238 |
var select = parent.find('select'); |
| 239 |
var rank_id = select.val(); |
| 240 |
var current_rank_id = select.data('current'); |
| 241 |
var user_id = $('#wpbody-content input[name="user_id"]').val(); |
| 242 |
|
| 243 |
// If no changes made, then toggle form visibility |
| 244 |
if( current_rank_id === rank_id ) { |
| 245 |
parent.slideUp(); |
| 246 |
parent.prev('.profile-rank-toggle').slideDown(); |
| 247 |
|
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
// Show loader |
| 252 |
parent.find('.spinner').addClass('is-active'); |
| 253 |
|
| 254 |
$.ajax({ |
| 255 |
url: ajaxurl, |
| 256 |
method: 'POST', |
| 257 |
data: { |
| 258 |
action: 'gamipress_profile_update_user_rank', |
| 259 |
nonce: gamipress_admin.nonce, |
| 260 |
rank_id: rank_id, |
| 261 |
user_id: user_id |
| 262 |
}, |
| 263 |
success: function( response ) { |
| 264 |
// Hide loader |
| 265 |
parent.find('.spinner').removeClass('is-active'); |
| 266 |
|
| 267 |
// Toggle form visibility |
| 268 |
parent.slideUp(); |
| 269 |
parent.prev('.profile-rank-toggle').slideDown(); |
| 270 |
|
| 271 |
// Update the rank preview |
| 272 |
if( response.data !== undefined && response.data.rank !== undefined ) { |
| 273 |
var rank_wrapper = $this.closest('.profile-rank'); |
| 274 |
|
| 275 |
rank_wrapper.find('.profile-rank-thumbnail').html( response.data.rank.thumbnail ); |
| 276 |
rank_wrapper.find('.profile-rank-title').html( response.data.rank.post_title ); |
| 277 |
} |
| 278 |
|
| 279 |
// Save current value in a element's data |
| 280 |
select.data( 'current', rank_id ); |
| 281 |
|
| 282 |
// Force user earnings table to refresh |
| 283 |
gamipress_refresh_user_earnings_table(); |
| 284 |
} |
| 285 |
}); |
| 286 |
}); |
| 287 |
|
| 288 |
// Edit user points |
| 289 |
$('body').on('click', '.profile-points .profile-points-toggle', function(e) { |
| 290 |
e.preventDefault(); |
| 291 |
|
| 292 |
var form = $(this).next('.profile-points-form-wrapper'); |
| 293 |
var points_input = form.find('.profile-points-new-balance-input input'); |
| 294 |
|
| 295 |
$(this).slideUp(); |
| 296 |
form.slideDown(); |
| 297 |
|
| 298 |
// Save current value in a element's data |
| 299 |
points_input.data( 'current', points_input.val() ); |
| 300 |
}); |
| 301 |
|
| 302 |
// On check register points balance movement |
| 303 |
$('body').on('change', '.profile-points .profile-points-register-movement-input input', function(e) { |
| 304 |
|
| 305 |
var target = $(this).parent().next(); |
| 306 |
|
| 307 |
if( $(this).prop('checked') ) { |
| 308 |
target.slideDown('fast'); |
| 309 |
} else { |
| 310 |
target.slideUp('fast'); |
| 311 |
} |
| 312 |
}); |
| 313 |
|
| 314 |
// Cancel user points edit |
| 315 |
$('body').on('click', '.profile-points .profile-points-cancel', function(e) { |
| 316 |
e.preventDefault(); |
| 317 |
|
| 318 |
var form = $(this).parent().parent(); |
| 319 |
var points_input = form.find('.profile-points-new-balance-input input'); |
| 320 |
var register_movement_input = form.find('.profile-points-register-movement-input input'); |
| 321 |
|
| 322 |
form.slideUp(); |
| 323 |
form.prev('.profile-points-toggle').slideDown(); |
| 324 |
|
| 325 |
// Restore current value (if user changed the input but won't save it) |
| 326 |
points_input.val( points_input.data('current') ); |
| 327 |
register_movement_input.prop( 'checked', false ).trigger('change'); |
| 328 |
|
| 329 |
}); |
| 330 |
|
| 331 |
// Save user points edit |
| 332 |
$('body').on('click', '.profile-points .profile-points-save', function(e) { |
| 333 |
e.preventDefault(); |
| 334 |
|
| 335 |
var $this = $(this); |
| 336 |
var parent = $this.closest('.profile-points-form-wrapper'); |
| 337 |
var points_input = parent.find('.profile-points-new-balance-input input'); |
| 338 |
var register_movement_input = parent.find('.profile-points-register-movement-input input'); |
| 339 |
var earnings_text_input = parent.find('.profile-points-earning-text-input input'); |
| 340 |
var points = points_input.val(); |
| 341 |
var register_movement = register_movement_input.prop('checked'); |
| 342 |
var earnings_text = earnings_text_input.val(); |
| 343 |
var current_points = points_input.data('current'); |
| 344 |
var points_type = points_input.data('points-type'); |
| 345 |
var user_id = $('#wpbody-content input[name="user_id"]').val(); |
| 346 |
|
| 347 |
// If no changes made, then toggle form visibility |
| 348 |
if( current_points === points ) { |
| 349 |
parent.slideUp(); |
| 350 |
parent.prev('.profile-points-toggle').slideDown(); |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
// Show loader |
| 355 |
parent.find('.spinner').addClass('is-active'); |
| 356 |
|
| 357 |
$.ajax({ |
| 358 |
url: ajaxurl, |
| 359 |
method: 'POST', |
| 360 |
data: { |
| 361 |
action: 'gamipress_profile_update_user_points', |
| 362 |
nonce: gamipress_admin.nonce, |
| 363 |
points: points, |
| 364 |
points_type: points_type, |
| 365 |
register_movement: ( register_movement ? 1 : 0 ), |
| 366 |
earnings_text: earnings_text, |
| 367 |
user_id: user_id |
| 368 |
}, |
| 369 |
success: function( response ) { |
| 370 |
// Hide loader |
| 371 |
parent.find('.spinner').removeClass('is-active'); |
| 372 |
|
| 373 |
// Toggle form visibility |
| 374 |
parent.slideUp(); |
| 375 |
parent.prev('.profile-points-toggle').slideDown(); |
| 376 |
register_movement_input.prop( 'checked', false ); |
| 377 |
|
| 378 |
// Update the points preview |
| 379 |
if( response.data !== undefined && response.data.points !== undefined ) { |
| 380 |
|
| 381 |
$this.closest('.profile-points').find('.profile-points-amount').html( response.data.points ); |
| 382 |
|
| 383 |
// Update the ranks preview |
| 384 |
if( response.data.ranks !== undefined && response.data.ranks.length ) { |
| 385 |
|
| 386 |
// Loop each rank to update their respective preview |
| 387 |
response.data.ranks.forEach(function(rank) { |
| 388 |
var rank_wrapper = $('.profile-rank-' + rank.post_type ); |
| 389 |
|
| 390 |
if( rank_wrapper.length ) { |
| 391 |
|
| 392 |
var select = rank_wrapper.find('select'); |
| 393 |
|
| 394 |
// Update the preview elements |
| 395 |
rank_wrapper.find('.profile-rank-thumbnail').html( rank.thumbnail ); |
| 396 |
rank_wrapper.find('.profile-rank-title').html( rank.post_title ); |
| 397 |
|
| 398 |
// Update the input element value |
| 399 |
select.val( rank.ID ); |
| 400 |
select.data( 'current', rank.ID ); |
| 401 |
|
| 402 |
} |
| 403 |
}); |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
// Force user earnings table to refresh |
| 408 |
gamipress_refresh_user_earnings_table(); |
| 409 |
} |
| 410 |
|
| 411 |
// Save current value in a element's data |
| 412 |
points_input.data( 'current', points ); |
| 413 |
|
| 414 |
// Force user earnings table to refresh |
| 415 |
gamipress_refresh_user_earnings_table(); |
| 416 |
} |
| 417 |
}); |
| 418 |
}); |
| 419 |
|
| 420 |
$('body').on('keypress', '.profile-points input', function(e) { |
| 421 |
var keycode = ( e.keyCode ? e.keyCode : e.which ); |
| 422 |
|
| 423 |
// check if key pressed is enter/intro |
| 424 |
if ( keycode === 13 ) { |
| 425 |
e.preventDefault(); |
| 426 |
|
| 427 |
// Trigger click on save button |
| 428 |
$(this).closest('.profile-points').find('.profile-points-save').trigger('click'); |
| 429 |
} |
| 430 |
}); |
| 431 |
|
| 432 |
// Award type select |
| 433 |
if( $("#gamipress-award-achievement-type-select, #gamipress-award-requirement-type-select").length ) { |
| 434 |
|
| 435 |
$("#gamipress-award-achievement-type-select, #gamipress-award-requirement-type-select").on('change', function() { |
| 436 |
var $this = $(this); |
| 437 |
var award_options_wrapper = $this.closest('.form-table').next(); |
| 438 |
var post_type = $this.val(); |
| 439 |
|
| 440 |
// Hide all tables |
| 441 |
award_options_wrapper.children().hide(); |
| 442 |
|
| 443 |
if( ! post_type.length ) { |
| 444 |
return; |
| 445 |
} |
| 446 |
|
| 447 |
// Show the option selected |
| 448 |
$("#" + post_type).show(); |
| 449 |
|
| 450 |
if( ! $("#" + post_type).data('loaded') ) { |
| 451 |
|
| 452 |
var action = ( $this.attr('id') === 'gamipress-award-achievement-type-select' ? 'achievement' : 'requirement' ); |
| 453 |
|
| 454 |
$("#" + post_type).data('loaded', true); |
| 455 |
|
| 456 |
$.ajax({ |
| 457 |
url: ajaxurl, |
| 458 |
method: 'POST', |
| 459 |
data: { |
| 460 |
action: 'gamipress_profile_load_award_' + action + '_table', |
| 461 |
nonce: gamipress_admin.nonce, |
| 462 |
user_id: $("input#user_id").val(), |
| 463 |
post_type: post_type, |
| 464 |
}, |
| 465 |
success: function(response) { |
| 466 |
$("#" + post_type).html( $(response.data).html() ); |
| 467 |
} |
| 468 |
}); |
| 469 |
|
| 470 |
} |
| 471 |
}).trigger('change'); |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
// User award/revoke ajax from awards table |
| 476 |
$('body').on('click', '#gamipress-awards-options .gamipress-award-achievement, #gamipress-awards-options .gamipress-revoke-achievement', function(e) { |
| 477 |
e.preventDefault(); |
| 478 |
|
| 479 |
var $this = $(this); |
| 480 |
|
| 481 |
// Bail if already running an ajax request |
| 482 |
if( $this.hasClass('disabled') ) return; |
| 483 |
|
| 484 |
var sibling_selector = ( $this.hasClass('gamipress-award-achievement') ? '.gamipress-revoke-achievement' : '.gamipress-award-achievement' ); |
| 485 |
|
| 486 |
// Add a custom class to avoid multiples clicks |
| 487 |
$this.addClass('disabled'); |
| 488 |
|
| 489 |
// Also, disable the other link |
| 490 |
$this.parent().find(sibling_selector).addClass('disabled'); |
| 491 |
|
| 492 |
// Add a loader |
| 493 |
$( '<span class="spinner is-active"></span>' ).insertAfter( $this ); |
| 494 |
|
| 495 |
// Make the request |
| 496 |
$.get( $this.attr('href'), function( response ) { |
| 497 |
|
| 498 |
var $response = $(response); |
| 499 |
|
| 500 |
// Update user ranks and points |
| 501 |
$('.profile-ranks').html( $response.find('.profile-ranks').html() ); |
| 502 |
$('.profile-points').html( $response.find('.profile-points').html() ); |
| 503 |
|
| 504 |
// Reload the awards table |
| 505 |
var post_type = $this.closest('.gamipress-table').attr('id').replace('-table', ''); |
| 506 |
var requirements = [ 'points-award', 'points-deduct', 'step', 'rank-requirement' ]; |
| 507 |
|
| 508 |
var action = ( requirements.indexOf( post_type ) === -1 ? 'achievement' : 'requirement' ); |
| 509 |
|
| 510 |
$.ajax({ |
| 511 |
url: ajaxurl, |
| 512 |
method: 'POST', |
| 513 |
data: { |
| 514 |
action: 'gamipress_profile_load_award_' + action + '_table', |
| 515 |
nonce: gamipress_admin.nonce, |
| 516 |
user_id: $("input#user_id").val(), |
| 517 |
post_type: post_type, |
| 518 |
}, |
| 519 |
success: function(response) { |
| 520 |
$("#" + post_type).html( $(response.data).html() ); |
| 521 |
} |
| 522 |
}); |
| 523 |
|
| 524 |
// Force user earnings table to refresh |
| 525 |
gamipress_refresh_user_earnings_table(); |
| 526 |
} ); |
| 527 |
}); |
| 528 |
|
| 529 |
// User award/revoke ajax from user earnings table |
| 530 |
$('body').on('click', '.gamipress_user_earnings .gamipress-revoke-user-earning', function(e) { |
| 531 |
|
| 532 |
e.preventDefault(); |
| 533 |
|
| 534 |
var $this = $(this); |
| 535 |
|
| 536 |
// Bail if already running an ajax request |
| 537 |
if( $this.hasClass('disabled') ) return; |
| 538 |
|
| 539 |
// Add a custom class to avoid multiples clicks |
| 540 |
$this.addClass('disabled'); |
| 541 |
|
| 542 |
// Add a loader |
| 543 |
$( '<span class="spinner is-active"></span>' ).insertAfter( $this ); |
| 544 |
|
| 545 |
// Make the request |
| 546 |
$.get( $this.attr('href'), function( response ) { |
| 547 |
|
| 548 |
// If currently on user earnings screen, refresh current page and return |
| 549 |
if( $('body').hasClass('gamipress_page_gamipress_user_earnings') ) { |
| 550 |
|
| 551 |
// Remove the loader |
| 552 |
$this.next('.spinner').remove(); |
| 553 |
|
| 554 |
// Remove class disabled and add a confirmation text |
| 555 |
$this.removeClass('disabled').text('Revoked successfully!'); |
| 556 |
|
| 557 |
// Refresh current screen |
| 558 |
location.reload(); |
| 559 |
|
| 560 |
// Bail to not refresh component that aren't on user earnings screen |
| 561 |
return; |
| 562 |
} |
| 563 |
|
| 564 |
var $response = $(response); |
| 565 |
|
| 566 |
// Update user ranks and points |
| 567 |
$('.profile-ranks').html( $response.find('.profile-ranks').html() ); |
| 568 |
$('.profile-points').html( $response.find('.profile-points').html() ); |
| 569 |
|
| 570 |
// Force user earnings table to refresh |
| 571 |
gamipress_refresh_user_earnings_table(); |
| 572 |
|
| 573 |
} ); |
| 574 |
|
| 575 |
}); |
| 576 |
|
| 577 |
$('body').on('cmb2_add_row', '.cmb-add-group-row', function(e, button) { |
| 578 |
|
| 579 |
}); |
| 580 |
|
| 581 |
// CMB2 Add Row |
| 582 |
$(document).on('cmb2_add_row', function(e, added_row) { |
| 583 |
// Selector Control |
| 584 |
added_row.find( '.gamipress-selector select' ).each(function() { |
| 585 |
gamipress_select2_destroy( $(this), false ); |
| 586 |
gamipress_selector( $(this) ); |
| 587 |
}); |
| 588 |
|
| 589 |
// Post Selector Control |
| 590 |
added_row.find( '.gamipress-post-selector select' ).each(function() { |
| 591 |
gamipress_select2_destroy( $(this), true ); |
| 592 |
gamipress_post_selector( $(this) ); |
| 593 |
}); |
| 594 |
|
| 595 |
// User Selector Control |
| 596 |
added_row.find( '.gamipress-user-selector select' ).each(function() { |
| 597 |
gamipress_select2_destroy( $(this), true ); |
| 598 |
gamipress_user_selector( $(this) ); |
| 599 |
}); |
| 600 |
}); |
| 601 |
|
| 602 |
// Pattern tags toggle |
| 603 |
$('body').on('click', '.gamipress-pattern-tags-list-toggle', function(e) { |
| 604 |
e.preventDefault(); |
| 605 |
|
| 606 |
var $this = $(this); |
| 607 |
var list = $this.closest('.cmb-td').find('.gamipress-pattern-tags-list'); |
| 608 |
|
| 609 |
if( ! list.hasClass('gamipress-pattern-tags-list-open') ) { |
| 610 |
list.addClass('gamipress-pattern-tags-list-open').slideDown('fast'); |
| 611 |
$this.text( $this.data('show-text') ); |
| 612 |
} else { |
| 613 |
list.removeClass('gamipress-pattern-tags-list-open').slideUp('fast'); |
| 614 |
$this.text( $this.data('hide-text') ); |
| 615 |
} |
| 616 |
}); |
| 617 |
|
| 618 |
// Add-ons page |
| 619 |
if( $('.gamipress_page_gamipress_add_ons').length ) { |
| 620 |
|
| 621 |
// Add-ons tabs |
| 622 |
$('.gamipress_page_gamipress_add_ons .wp-filter a').on('click', function(e) { |
| 623 |
e.preventDefault(); |
| 624 |
|
| 625 |
if( $(this).hasClass('current') ) { |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
var current = $(this).closest('.wp-filter').find('a.current'); |
| 630 |
|
| 631 |
// Toggle plugin cards visibility |
| 632 |
$('.gamipress-plugin-card.' + current.data('target')).hide(); |
| 633 |
|
| 634 |
$('.gamipress-plugin-card.' + $(this).data('target')).fadeIn(250); |
| 635 |
|
| 636 |
// Toggle current class |
| 637 |
current.removeClass('current'); |
| 638 |
$(this).addClass('current'); |
| 639 |
}); |
| 640 |
|
| 641 |
// Hide all plugins cards |
| 642 |
$('.gamipress-add-ons .gamipress-plugin-card:not(.' + $('.gamipress_page_gamipress_add_ons .wp-filter a.current').data('target') + ')').hide(); |
| 643 |
|
| 644 |
// Trigger click on first tab |
| 645 |
$('.gamipress_page_gamipress_add_ons .wp-filter a.current').trigger('click'); |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
// Default badge |
| 650 |
$('body').on('click', '.gamipress-badge-builder-default-badge-thumb', function (e) { |
| 651 |
e.preventDefault(); |
| 652 |
e.stopPropagation(); |
| 653 |
|
| 654 |
var $this = $(this); |
| 655 |
var image = $this.data('image'); |
| 656 |
var post = $this.data('post'); |
| 657 |
|
| 658 |
$('.gamipress-badge-builder-default-badge-thumb').removeClass('active'); |
| 659 |
$this.addClass('active'); |
| 660 |
|
| 661 |
$.post( |
| 662 |
ajaxurl, |
| 663 |
{ |
| 664 |
action: 'gamipress_badge_builder_set_featured_image', |
| 665 |
nonce: gamipress_admin.nonce, |
| 666 |
image: image, |
| 667 |
post_id: post |
| 668 |
}).done(function (response) { |
| 669 |
|
| 670 |
if ( ! response || ! response.success ) |
| 671 |
return; |
| 672 |
|
| 673 |
if (wp && wp.media && wp.media.featuredImage) { |
| 674 |
wp.media.featuredImage.set( response.data ); |
| 675 |
} |
| 676 |
}); |
| 677 |
}); |
| 678 |
|
| 679 |
// Target blank on featured menu links |
| 680 |
$('#adminmenu .gamipress-admin-menu-badge, ' |
| 681 |
+ '#wp-admin-bar-gamipress .gamipress-admin-menu-badge').each( function() { |
| 682 |
var parent = $(this).closest('a'); |
| 683 |
|
| 684 |
parent.attr('target', '_blank'); |
| 685 |
} ); |
| 686 |
|
| 687 |
// Auto initialize upgrade if user reloads the page during an upgrade |
| 688 |
if( $('#gamipress-upgrade-notice').find('.gamipress-upgrade-progress[data-running-upgrade]').length ) { |
| 689 |
gamipress_start_upgrade( $('#gamipress-upgrade-notice').find('.gamipress-upgrade-progress[data-running-upgrade]').data('running-upgrade') ); |
| 690 |
} |
| 691 |
|
| 692 |
})( jQuery, window, document ); |
| 693 |
|
| 694 |
var gamipress_current_upgrade_info; |
| 695 |
var gamipress_current_upgrade_progress; |
| 696 |
var gamipress_current_upgrade_cancelled = false; |
| 697 |
|
| 698 |
// Start upgrade |
| 699 |
function gamipress_start_upgrade( version ) { |
| 700 |
|
| 701 |
var $ = $ || jQuery; |
| 702 |
version = version.replace('.', '').replace('.', '').replace('.', ''); |
| 703 |
|
| 704 |
$('#gamipress-upgrade-notice').html( |
| 705 |
'<p>Upgrading GamiPress database... <span></span></p>' |
| 706 |
+ '<div class="gamipress-upgrade-progress"><div class="gamipress-upgrade-progress-bar" style="width: 0%;"></div></div>' |
| 707 |
+ '<p style="display: none;">' |
| 708 |
+ '<a id="gamipress-cancel-upgrade" href="#" class="button">Cancel Upgrade</a>' |
| 709 |
+ '</p>' |
| 710 |
); |
| 711 |
|
| 712 |
$.ajax({ |
| 713 |
url: ajaxurl, |
| 714 |
data: { |
| 715 |
action: 'gamipress_' + version + '_upgrade_info', |
| 716 |
nonce: gamipress_admin.nonce, |
| 717 |
}, |
| 718 |
success: function( response ) { |
| 719 |
|
| 720 |
// Upgrade done! |
| 721 |
if( response.data.upgraded !== undefined && response.data.upgraded ) { |
| 722 |
$('#gamipress-upgrade-notice').html('<p>Upgrade has been already completed.</p><div class="gamipress-upgrade-progress"><div class="gamipress-upgrade-progress-bar" style="width: 100%;"></div></div>'); |
| 723 |
return; |
| 724 |
} |
| 725 |
|
| 726 |
// Update progress vars |
| 727 |
gamipress_current_upgrade_info = response.data.total; |
| 728 |
gamipress_current_upgrade_progress = 0; |
| 729 |
|
| 730 |
// Show a visual text with remaining entries |
| 731 |
$('#gamipress-upgrade-notice p:first span').html( gamipress_current_upgrade_info - gamipress_current_upgrade_progress + ' remaining entries' ); |
| 732 |
|
| 733 |
// Add a click event on the upgrade cancel button |
| 734 |
// Note: Function is placed here because clicking fast on start upgrade could provoke cancelling it at start |
| 735 |
$('body').on('click', '#gamipress-cancel-upgrade', function(e) { |
| 736 |
e.preventDefault(); |
| 737 |
|
| 738 |
$('#gamipress-upgrade-notice').html('<p>Cancelling upgrade...</p>'); |
| 739 |
|
| 740 |
gamipress_current_upgrade_cancelled = true; |
| 741 |
|
| 742 |
gamipress_stop_upgrade( version, true ); |
| 743 |
}); |
| 744 |
|
| 745 |
// Show the cancel upgrade button |
| 746 |
$('#gamipress-cancel-upgrade').parent().show(); |
| 747 |
|
| 748 |
gamipress_run_upgrade( version ); |
| 749 |
|
| 750 |
}, |
| 751 |
error: function( response ) { |
| 752 |
gamipress_stop_upgrade( version ); |
| 753 |
$('#gamipress-upgrade-notice').html('<p class="error">Upgrading process failed.</p>'); |
| 754 |
} |
| 755 |
}); |
| 756 |
|
| 757 |
} |
| 758 |
|
| 759 |
// Refresh the user earnings table (located in user profile screen) |
| 760 |
function gamipress_refresh_user_earnings_table() { |
| 761 |
|
| 762 |
var $ = $ || jQuery; |
| 763 |
|
| 764 |
// Check if table exists |
| 765 |
var table = $('.ct-ajax-list-table[data-object="gamipress_user_earnings"]'); |
| 766 |
|
| 767 |
if( table.length ) |
| 768 |
ct_ajax_list_table_paginate_table( table, table.find('input#current-page-selector').val() ); |
| 769 |
|
| 770 |
} |
| 771 |
|
| 772 |
// Run upgrade |
| 773 |
function gamipress_run_upgrade( version ) { |
| 774 |
|
| 775 |
var $ = $ || jQuery; |
| 776 |
|
| 777 |
if( gamipress_current_upgrade_cancelled ) { |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
version = version.replace('.', '').replace('.', '').replace('.', ''); |
| 782 |
|
| 783 |
$.ajax({ |
| 784 |
url: ajaxurl, |
| 785 |
data: { |
| 786 |
action: 'gamipress_process_' + version + '_upgrade', |
| 787 |
nonce: gamipress_admin.nonce, |
| 788 |
current: gamipress_current_upgrade_progress, |
| 789 |
}, |
| 790 |
success: function( response ) { |
| 791 |
|
| 792 |
// Upgrade done! |
| 793 |
if( response.data.upgraded !== undefined && response.data.upgraded ) { |
| 794 |
$('#gamipress-upgrade-notice').html('<p>Upgrading process finished successfully.</p><div class="gamipress-upgrade-progress"><div class="gamipress-upgrade-progress-bar" style="width: 100%;"></div></div>'); |
| 795 |
return; |
| 796 |
} |
| 797 |
|
| 798 |
gamipress_current_upgrade_progress = response.data.current; |
| 799 |
|
| 800 |
// Upgraded successfully |
| 801 |
if( gamipress_current_upgrade_progress >= gamipress_current_upgrade_info ) { |
| 802 |
$('#gamipress-upgrade-notice').html('<p>Upgrading process finished successfully.</p><div class="gamipress-upgrade-progress"><div class="gamipress-upgrade-progress-bar" style="width: 100%;"></div></div>'); |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
// Show a visual text with remaining entries |
| 807 |
$('#gamipress-upgrade-notice p:first span').html( gamipress_current_upgrade_info - gamipress_current_upgrade_progress + ' remaining entries' ); |
| 808 |
|
| 809 |
// Update progress bar width |
| 810 |
$('#gamipress-upgrade-notice .gamipress-upgrade-progress .gamipress-upgrade-progress-bar').attr('style', 'width: ' + ( ( gamipress_current_upgrade_progress / gamipress_current_upgrade_info ) * 100 ) + '%') |
| 811 |
|
| 812 |
// Give to the server 3 seconds until the next upgrade run |
| 813 |
setTimeout( function() { |
| 814 |
gamipress_run_upgrade( version ); |
| 815 |
}, 3000 ); |
| 816 |
|
| 817 |
}, |
| 818 |
error: function( response ) { |
| 819 |
gamipress_stop_upgrade( version ); |
| 820 |
$('#gamipress-upgrade-notice').html('<p class="error">Upgrading process failed.</p>'); |
| 821 |
} |
| 822 |
}); |
| 823 |
|
| 824 |
} |
| 825 |
|
| 826 |
// Stop upgrade |
| 827 |
function gamipress_stop_upgrade( version, show_result ) { |
| 828 |
|
| 829 |
if( show_result === undefined ) { |
| 830 |
show_result = false; |
| 831 |
} |
| 832 |
|
| 833 |
var $ = $ || jQuery; |
| 834 |
version = version.replace('.', '').replace('.', '').replace('.', ''); |
| 835 |
|
| 836 |
$.ajax({ |
| 837 |
url: ajaxurl, |
| 838 |
data: { |
| 839 |
action: 'gamipress_stop_process_' + version + '_upgrade', |
| 840 |
nonce: gamipress_admin.nonce, |
| 841 |
}, |
| 842 |
success: function( response ) { |
| 843 |
if( show_result === true ) { |
| 844 |
$('#gamipress-upgrade-notice').html('<p class="error">Upgrade cancelled.</p>'); |
| 845 |
} |
| 846 |
}, |
| 847 |
error: function( response ) { |
| 848 |
if( show_result === true ) { |
| 849 |
$('#gamipress-upgrade-notice').html('<p class="error">Upgrade cancellation failed.</p>'); |
| 850 |
} |
| 851 |
} |
| 852 |
}); |
| 853 |
|
| 854 |
} |
| 855 |
|