PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.4
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.4
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / assets / js / gamipress-admin.js

gamipress-admin.js in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.4, at assets/js/gamipress-admin.js

853 lines 27.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 slug_field.trigger('keyup');
202
203 });
204
205 // Edit user rank
206 $('body').on('click', '.profile-rank .profile-rank-toggle', function(e) {
207 e.preventDefault();
208
209 $(this).slideUp();
210 $(this).next('.profile-rank-form-wrapper').slideDown();
211
212 var select = $(this).next('.profile-rank-form-wrapper').find('select');
213
214 // Save current value in a element's data
215 select.data( 'current', select.val() );
216
217 });
218
219 // Cancel user rank edit
220 $('body').on('click', '.profile-rank .profile-rank-cancel', function(e) {
221 e.preventDefault();
222
223 var parent = $(this).parent().parent();
224 var select = parent.find('select');
225
226 parent.slideUp();
227 parent.prev('.profile-rank-toggle').slideDown();
228
229 // Restore current value (if user changed the input but won't save it)
230 select.val( select.data( 'current' ) );
231
232 });
233
234 // Save user rank edit
235 $('body').on('click', '.profile-rank .profile-rank-save', function(e) {
236 e.preventDefault();
237
238 var $this = $(this);
239 var parent = $this.closest('.profile-rank-form-wrapper');
240 var select = parent.find('select');
241 var rank_id = select.val();
242 var current_rank_id = select.data('current');
243 var user_id = $('#wpbody-content input[name="user_id"]').val();
244
245 // If no changes made, then toggle form visibility
246 if( current_rank_id === rank_id ) {
247 parent.slideUp();
248 parent.prev('.profile-rank-toggle').slideDown();
249
250 return false;
251 }
252
253 // Show loader
254 parent.find('.spinner').addClass('is-active');
255
256 $.ajax({
257 url: ajaxurl,
258 method: 'POST',
259 data: {
260 action: 'gamipress_profile_update_user_rank',
261 nonce: gamipress_admin.nonce,
262 rank_id: rank_id,
263 user_id: user_id
264 },
265 success: function( response ) {
266 // Hide loader
267 parent.find('.spinner').removeClass('is-active');
268
269 // Toggle form visibility
270 parent.slideUp();
271 parent.prev('.profile-rank-toggle').slideDown();
272
273 // Update the rank preview
274 if( response.data !== undefined && response.data.rank !== undefined ) {
275 var rank_wrapper = $this.closest('.profile-rank');
276
277 rank_wrapper.find('.profile-rank-thumbnail').html( response.data.rank.thumbnail );
278 rank_wrapper.find('.profile-rank-title').html( response.data.rank.post_title );
279 }
280
281 // Save current value in a element's data
282 select.data( 'current', rank_id );
283
284 // Force user earnings table to refresh
285 gamipress_refresh_user_earnings_table();
286 }
287 });
288 });
289
290 // Edit user points
291 $('body').on('click', '.profile-points .profile-points-toggle', function(e) {
292 e.preventDefault();
293
294 var form = $(this).next('.profile-points-form-wrapper');
295 var points_input = form.find('.profile-points-new-balance-input input');
296
297 $(this).slideUp();
298 form.slideDown();
299
300 // Save current value in a element's data
301 points_input.data( 'current', points_input.val() );
302 });
303
304 // On check register points balance movement
305 $('body').on('change', '.profile-points .profile-points-register-movement-input input', function(e) {
306
307 var target = $(this).parent().next();
308
309 if( $(this).prop('checked') ) {
310 target.slideDown('fast');
311 } else {
312 target.slideUp('fast');
313 }
314 });
315
316 // Cancel user points edit
317 $('body').on('click', '.profile-points .profile-points-cancel', function(e) {
318 e.preventDefault();
319
320 var form = $(this).parent().parent();
321 var points_input = form.find('.profile-points-new-balance-input input');
322 var register_movement_input = form.find('.profile-points-register-movement-input input');
323
324 form.slideUp();
325 form.prev('.profile-points-toggle').slideDown();
326
327 // Restore current value (if user changed the input but won't save it)
328 points_input.val( points_input.data('current') );
329 register_movement_input.prop( 'checked', false ).trigger('change');
330
331 });
332
333 // Save user points edit
334 $('body').on('click', '.profile-points .profile-points-save', function(e) {
335 e.preventDefault();
336
337 var $this = $(this);
338 var parent = $this.closest('.profile-points-form-wrapper');
339 var points_input = parent.find('.profile-points-new-balance-input input');
340 var register_movement_input = parent.find('.profile-points-register-movement-input input');
341 var earnings_text_input = parent.find('.profile-points-earning-text-input input');
342 var points = points_input.val();
343 var register_movement = register_movement_input.prop('checked');
344 var earnings_text = earnings_text_input.val();
345 var current_points = points_input.data('current');
346 var points_type = points_input.data('points-type');
347 var user_id = $('#wpbody-content input[name="user_id"]').val();
348
349 // If no changes made, then toggle form visibility
350 if( current_points === points ) {
351 parent.slideUp();
352 parent.prev('.profile-points-toggle').slideDown();
353 return false;
354 }
355
356 // Show loader
357 parent.find('.spinner').addClass('is-active');
358
359 $.ajax({
360 url: ajaxurl,
361 method: 'POST',
362 data: {
363 action: 'gamipress_profile_update_user_points',
364 nonce: gamipress_admin.nonce,
365 points: points,
366 points_type: points_type,
367 register_movement: ( register_movement ? 1 : 0 ),
368 earnings_text: earnings_text,
369 user_id: user_id
370 },
371 success: function( response ) {
372 // Hide loader
373 parent.find('.spinner').removeClass('is-active');
374
375 // Toggle form visibility
376 parent.slideUp();
377 parent.prev('.profile-points-toggle').slideDown();
378 register_movement_input.prop( 'checked', false );
379
380 // Update the points preview
381 if( response.data !== undefined && response.data.points !== undefined ) {
382
383 $this.closest('.profile-points').find('.profile-points-amount').html( response.data.points );
384
385 // Update the ranks preview
386 if( response.data.ranks !== undefined && response.data.ranks.length ) {
387
388 // Loop each rank to update their respective preview
389 response.data.ranks.forEach(function(rank) {
390 var rank_wrapper = $('.profile-rank-' + rank.post_type );
391
392 if( rank_wrapper.length ) {
393
394 var select = rank_wrapper.find('select');
395
396 // Update the preview elements
397 rank_wrapper.find('.profile-rank-thumbnail').html( rank.thumbnail );
398 rank_wrapper.find('.profile-rank-title').html( rank.post_title );
399
400 // Update the input element value
401 select.val( rank.ID );
402 select.data( 'current', rank.ID );
403
404 }
405 });
406
407 }
408
409 // Force user earnings table to refresh
410 gamipress_refresh_user_earnings_table();
411 }
412
413 // Save current value in a element's data
414 points_input.data( 'current', points );
415
416 // Force user earnings table to refresh
417 gamipress_refresh_user_earnings_table();
418 }
419 });
420 });
421
422 $('body').on('keypress', '.profile-points input', function(e) {
423 var keycode = ( e.keyCode ? e.keyCode : e.which );
424
425 // check if key pressed is enter/intro
426 if ( keycode === 13 ) {
427 e.preventDefault();
428
429 // Trigger click on save button
430 $(this).closest('.profile-points').find('.profile-points-save').trigger('click');
431 }
432 });
433
434 // Award type select
435 if( $("#gamipress-award-achievement-type-select, #gamipress-award-requirement-type-select").length ) {
436
437 $("#gamipress-award-achievement-type-select, #gamipress-award-requirement-type-select").on('change', function() {
438 var $this = $(this);
439 var award_options_wrapper = $this.closest('.form-table').next();
440 var post_type = $this.val();
441
442 // Hide all tables
443 award_options_wrapper.children().hide();
444
445 if( ! post_type.length ) {
446 return;
447 }
448
449 // Show the option selected
450 $("#" + post_type).show();
451
452 if( ! $("#" + post_type).data('loaded') ) {
453
454 var action = ( $this.attr('id') === 'gamipress-award-achievement-type-select' ? 'achievement' : 'requirement' );
455
456 $("#" + post_type).data('loaded', true);
457
458 $.ajax({
459 url: ajaxurl,
460 method: 'POST',
461 data: {
462 action: 'gamipress_profile_load_award_' + action + '_table',
463 nonce: gamipress_admin.nonce,
464 user_id: $("input#user_id").val(),
465 post_type: post_type,
466 },
467 success: function(response) {
468 $("#" + post_type).html( $(response.data).html() );
469 }
470 });
471
472 }
473 }).trigger('change');
474
475 }
476
477 // User award/revoke ajax from awards table
478 $('body').on('click', '#gamipress-awards-options .gamipress-award-achievement, #gamipress-awards-options .gamipress-revoke-achievement', function(e) {
479 e.preventDefault();
480
481 var $this = $(this);
482
483 // Bail if already running an ajax request
484 if( $this.hasClass('disabled') ) return;
485
486 var sibling_selector = ( $this.hasClass('gamipress-award-achievement') ? '.gamipress-revoke-achievement' : '.gamipress-award-achievement' );
487
488 // Add a custom class to avoid multiples clicks
489 $this.addClass('disabled');
490
491 // Also, disable the other link
492 $this.parent().find(sibling_selector).addClass('disabled');
493
494 // Add a loader
495 $( '<span class="spinner is-active"></span>' ).insertAfter( $this );
496
497 // Make the request
498 $.get( $this.attr('href'), function( response ) {
499
500 var $response = $(response);
501
502 // Update user ranks and points
503 $('.profile-ranks').html( $response.find('.profile-ranks').html() );
504 $('.profile-points').html( $response.find('.profile-points').html() );
505
506 // Reload the awards table
507 var post_type = $this.closest('.gamipress-table').attr('id').replace('-table', '');
508 var requirements = [ 'points-award', 'points-deduct', 'step', 'rank-requirement' ];
509
510 var action = ( requirements.indexOf( post_type ) === -1 ? 'achievement' : 'requirement' );
511
512 $.ajax({
513 url: ajaxurl,
514 method: 'POST',
515 data: {
516 action: 'gamipress_profile_load_award_' + action + '_table',
517 nonce: gamipress_admin.nonce,
518 user_id: $("input#user_id").val(),
519 post_type: post_type,
520 },
521 success: function(response) {
522 $("#" + post_type).html( $(response.data).html() );
523 }
524 });
525
526 // Force user earnings table to refresh
527 gamipress_refresh_user_earnings_table();
528 } );
529 });
530
531 // User award/revoke ajax from user earnings table
532 $('body').on('click', '.gamipress_user_earnings .gamipress-revoke-user-earning', function(e) {
533
534 e.preventDefault();
535
536 var $this = $(this);
537
538 // Bail if already running an ajax request
539 if( $this.hasClass('disabled') ) return;
540
541 // Add a custom class to avoid multiples clicks
542 $this.addClass('disabled');
543
544 // Add a loader
545 $( '<span class="spinner is-active"></span>' ).insertAfter( $this );
546
547 // Make the request
548 $.get( $this.attr('href'), function( response ) {
549
550 // If currently on user earnings screen, refresh current page and return
551 if( $('body').hasClass('gamipress_page_gamipress_user_earnings') ) {
552
553 // Remove the loader
554 $this.next('.spinner').remove();
555
556 // Remove class disabled and add a confirmation text
557 $this.removeClass('disabled').text('Revoked successfully!');
558
559 // Refresh current screen
560 location.reload();
561
562 // Bail to not refresh component that aren't on user earnings screen
563 return;
564 }
565
566 var $response = $(response);
567
568 // Update user ranks and points
569 $('.profile-ranks').html( $response.find('.profile-ranks').html() );
570 $('.profile-points').html( $response.find('.profile-points').html() );
571
572 // Force user earnings table to refresh
573 gamipress_refresh_user_earnings_table();
574
575 } );
576
577 });
578
579 // CMB2 Add Row
580 $(document).on('cmb2_add_row', function(e, added_row) {
581 // Selector Control
582 added_row.find( '.gamipress-selector select' ).each(function() {
583 gamipress_select2_destroy( $(this), false );
584 gamipress_selector( $(this) );
585 });
586
587 // Post Selector Control
588 added_row.find( '.gamipress-post-selector select' ).each(function() {
589 gamipress_select2_destroy( $(this), true );
590 gamipress_post_selector( $(this) );
591 });
592
593 // User Selector Control
594 added_row.find( '.gamipress-user-selector select' ).each(function() {
595 gamipress_select2_destroy( $(this), true );
596 gamipress_user_selector( $(this) );
597 });
598 });
599
600 // Pattern tags toggle
601 $('body').on('click', '.gamipress-pattern-tags-list-toggle', function(e) {
602 e.preventDefault();
603
604 var $this = $(this);
605 var list = $this.closest('.cmb-td').find('.gamipress-pattern-tags-list');
606
607 if( ! list.hasClass('gamipress-pattern-tags-list-open') ) {
608 list.addClass('gamipress-pattern-tags-list-open').slideDown('fast');
609 $this.text( $this.data('show-text') );
610 } else {
611 list.removeClass('gamipress-pattern-tags-list-open').slideUp('fast');
612 $this.text( $this.data('hide-text') );
613 }
614 });
615
616 // Add-ons page
617 if( $('.gamipress_page_gamipress_add_ons').length ) {
618
619 // Add-ons tabs
620 $('.gamipress_page_gamipress_add_ons .wp-filter a').on('click', function(e) {
621 e.preventDefault();
622
623 if( $(this).hasClass('current') ) {
624 return;
625 }
626
627 var current = $(this).closest('.wp-filter').find('a.current');
628
629 // Toggle plugin cards visibility
630 $('.gamipress-plugin-card.' + current.data('target')).hide();
631
632 $('.gamipress-plugin-card.' + $(this).data('target')).fadeIn(250);
633
634 // Toggle current class
635 current.removeClass('current');
636 $(this).addClass('current');
637 });
638
639 // Hide all plugins cards
640 $('.gamipress-add-ons .gamipress-plugin-card:not(.' + $('.gamipress_page_gamipress_add_ons .wp-filter a.current').data('target') + ')').hide();
641
642 // Trigger click on first tab
643 $('.gamipress_page_gamipress_add_ons .wp-filter a.current').trigger('click');
644
645 }
646
647 // Default badge
648 $('body').on('click', '.gamipress-badge-builder-default-badge-thumb', function (e) {
649 e.preventDefault();
650 e.stopPropagation();
651
652 var $this = $(this);
653 var image = $this.data('image');
654 var post = $this.data('post');
655
656 $('.gamipress-badge-builder-default-badge-thumb').removeClass('active');
657 $this.addClass('active');
658
659 $.post(
660 ajaxurl,
661 {
662 action: 'gamipress_badge_builder_set_featured_image',
663 nonce: gamipress_admin.nonce,
664 image: image,
665 post_id: post
666 }).done(function (response) {
667
668 if ( ! response || ! response.success )
669 return;
670
671 if (wp && wp.media && wp.media.featuredImage) {
672 wp.media.featuredImage.set( response.data );
673 }
674 });
675 });
676
677 // Target blank on featured menu links
678 $('#adminmenu .gamipress-admin-menu-badge, '
679 + '#wp-admin-bar-gamipress .gamipress-admin-menu-badge').each( function() {
680 var parent = $(this).closest('a');
681
682 parent.attr('target', '_blank');
683 } );
684
685 // Auto initialize upgrade if user reloads the page during an upgrade
686 if( $('#gamipress-upgrade-notice').find('.gamipress-upgrade-progress[data-running-upgrade]').length ) {
687 gamipress_start_upgrade( $('#gamipress-upgrade-notice').find('.gamipress-upgrade-progress[data-running-upgrade]').data('running-upgrade') );
688 }
689
690 })( jQuery, window, document );
691
692 var gamipress_current_upgrade_info;
693 var gamipress_current_upgrade_progress;
694 var gamipress_current_upgrade_cancelled = false;
695
696 // Start upgrade
697 function gamipress_start_upgrade( version ) {
698
699 var $ = $ || jQuery;
700 version = version.replace('.', '').replace('.', '').replace('.', '');
701
702 $('#gamipress-upgrade-notice').html(
703 '<p>Upgrading GamiPress database... <span></span></p>'
704 + '<div class="gamipress-upgrade-progress"><div class="gamipress-upgrade-progress-bar" style="width: 0%;"></div></div>'
705 + '<p style="display: none;">'
706 + '<a id="gamipress-cancel-upgrade" href="#" class="button">Cancel Upgrade</a>'
707 + '</p>'
708 );
709
710 $.ajax({
711 url: ajaxurl,
712 data: {
713 action: 'gamipress_' + version + '_upgrade_info',
714 nonce: gamipress_admin.nonce,
715 },
716 success: function( response ) {
717
718 // Upgrade done!
719 if( response.data.upgraded !== undefined && response.data.upgraded ) {
720 $('#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>');
721 return;
722 }
723
724 // Update progress vars
725 gamipress_current_upgrade_info = response.data.total;
726 gamipress_current_upgrade_progress = 0;
727
728 // Show a visual text with remaining entries
729 $('#gamipress-upgrade-notice p:first span').html( gamipress_current_upgrade_info - gamipress_current_upgrade_progress + ' remaining entries' );
730
731 // Add a click event on the upgrade cancel button
732 // Note: Function is placed here because clicking fast on start upgrade could provoke cancelling it at start
733 $('body').on('click', '#gamipress-cancel-upgrade', function(e) {
734 e.preventDefault();
735
736 $('#gamipress-upgrade-notice').html('<p>Cancelling upgrade...</p>');
737
738 gamipress_current_upgrade_cancelled = true;
739
740 gamipress_stop_upgrade( version, true );
741 });
742
743 // Show the cancel upgrade button
744 $('#gamipress-cancel-upgrade').parent().show();
745
746 gamipress_run_upgrade( version );
747
748 },
749 error: function( response ) {
750 gamipress_stop_upgrade( version );
751 $('#gamipress-upgrade-notice').html('<p class="error">Upgrading process failed.</p>');
752 }
753 });
754
755 }
756
757 // Refresh the user earnings table (located in user profile screen)
758 function gamipress_refresh_user_earnings_table() {
759
760 var $ = $ || jQuery;
761
762 // Check if table exists
763 var table = $('.ct-ajax-list-table[data-object="gamipress_user_earnings"]');
764
765 if( table.length )
766 ct_ajax_list_table_paginate_table( table, table.find('input#current-page-selector').val() );
767
768 }
769
770 // Run upgrade
771 function gamipress_run_upgrade( version ) {
772
773 var $ = $ || jQuery;
774
775 if( gamipress_current_upgrade_cancelled ) {
776 return;
777 }
778
779 version = version.replace('.', '').replace('.', '').replace('.', '');
780
781 $.ajax({
782 url: ajaxurl,
783 data: {
784 action: 'gamipress_process_' + version + '_upgrade',
785 nonce: gamipress_admin.nonce,
786 current: gamipress_current_upgrade_progress,
787 },
788 success: function( response ) {
789
790 // Upgrade done!
791 if( response.data.upgraded !== undefined && response.data.upgraded ) {
792 $('#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>');
793 return;
794 }
795
796 gamipress_current_upgrade_progress = response.data.current;
797
798 // Upgraded successfully
799 if( gamipress_current_upgrade_progress >= gamipress_current_upgrade_info ) {
800 $('#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>');
801 return;
802 }
803
804 // Show a visual text with remaining entries
805 $('#gamipress-upgrade-notice p:first span').html( gamipress_current_upgrade_info - gamipress_current_upgrade_progress + ' remaining entries' );
806
807 // Update progress bar width
808 $('#gamipress-upgrade-notice .gamipress-upgrade-progress .gamipress-upgrade-progress-bar').attr('style', 'width: ' + ( ( gamipress_current_upgrade_progress / gamipress_current_upgrade_info ) * 100 ) + '%')
809
810 // Give to the server 3 seconds until the next upgrade run
811 setTimeout( function() {
812 gamipress_run_upgrade( version );
813 }, 3000 );
814
815 },
816 error: function( response ) {
817 gamipress_stop_upgrade( version );
818 $('#gamipress-upgrade-notice').html('<p class="error">Upgrading process failed.</p>');
819 }
820 });
821
822 }
823
824 // Stop upgrade
825 function gamipress_stop_upgrade( version, show_result ) {
826
827 if( show_result === undefined ) {
828 show_result = false;
829 }
830
831 var $ = $ || jQuery;
832 version = version.replace('.', '').replace('.', '').replace('.', '');
833
834 $.ajax({
835 url: ajaxurl,
836 data: {
837 action: 'gamipress_stop_process_' + version + '_upgrade',
838 nonce: gamipress_admin.nonce,
839 },
840 success: function( response ) {
841 if( show_result === true ) {
842 $('#gamipress-upgrade-notice').html('<p class="error">Upgrade cancelled.</p>');
843 }
844 },
845 error: function( response ) {
846 if( show_result === true ) {
847 $('#gamipress-upgrade-notice').html('<p class="error">Upgrade cancellation failed.</p>');
848 }
849 }
850 });
851
852 }
853