PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / assets / js / admin / charitable-admin-connect.js

charitable-admin-connect.js in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at assets/js/admin/charitable-admin-connect.js

658 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global charitable_admin */
2 /**
3 * Connect functionality.
4 *
5 * @since 1.5.4
6 */
7
8 'use strict';
9
10 var CharitableConnect = window.CharitableConnect || ( function( document, window, $ ) {
11
12 /**
13 * Elements reference.
14 *
15 * @since 1.5.5
16 *
17 * @type {object}
18 */
19 var el = {
20 $connectBtn: $( '#charitable-settings-connect-btn' ),
21 $connectKey: $( '#charitable-settings-upgrade-license-key' ),
22 };
23
24 /**
25 * Public functions and properties.
26 *
27 * @since 1.5.5
28 *
29 * @type {object}
30 */
31 var app = {
32
33 /**
34 * Start the engine.
35 *
36 * @since 1.5.5
37 */
38 init: function() {
39
40 $( app.ready );
41
42 },
43
44 /**
45 * Document ready.
46 *
47 * @since 1.5.5
48 */
49 ready: function() {
50
51 app.events();
52 },
53
54 /**
55 * Register JS events.
56 *
57 * @since 1.5.5
58 */
59 events: function() {
60
61 app.connectBtnClick();
62 },
63
64 /**
65 * Register connect button event.
66 *
67 * @since 1.5.5
68 */
69 connectBtnClick: function() {
70
71 // If the button contains the 'data-pro-connect' attribute and it's false, don't show the button.
72 // If there is no data-pro-connect attribute, don't show the button.
73 if ( ! el.$connectBtn.data( 'pro-connect' ) || el.$connectBtn.data( 'pro-connect' ) === false ) {
74 return;
75 }
76
77 el.$connectBtn.on( 'click', function( event ) {
78 // Stop the form from submitting.
79 event.preventDefault();
80 app.gotoUpgradeUrl();
81 } );
82 },
83
84 /**
85 * Get the alert arguments in case of Pro already installed.
86 *
87 * @since 1.5.5
88 *
89 * @param {object} res Ajax query result object.
90 *
91 * @returns {object} Alert arguments.
92 */
93 proAlreadyInstalled: function( res ) {
94
95 var buttons = {
96 confirm: {
97 text: charitable_admin.plugin_activate_btn,
98 btnClass: 'btn-confirm',
99 keys: [ 'enter' ],
100 action: function() {
101 window.location.reload();
102 },
103 },
104 };
105
106 return {
107 title: charitable_admin.almost_done,
108 content: res.data.message,
109 icon: 'fa fa-check-circle',
110 type: 'green',
111 buttons: buttons,
112 };
113 },
114
115 /**
116 * Pro installed but inactive: show popup "Activate now?" then call AJAX to activate; redirect to dashboard on success.
117 *
118 * @since 1.8.9.6
119 * @param {object} data Response data with message, redirect_url, plugin_basename.
120 */
121 showActivateProPopup: function( data ) {
122 var self = this;
123 $.alert( {
124 title: charitable_admin.license_validated || charitable_admin.success,
125 content: data.message,
126 icon: 'fa fa-check-circle',
127 type: 'green',
128 boxWidth: '800px',
129 buttons: {
130 activate: {
131 text: charitable_admin.activate_now,
132 btnClass: 'btn-confirm',
133 keys: [ 'enter' ],
134 action: function() {
135 self.activateProThenRedirect( data );
136 },
137 },
138 cancel: {
139 text: charitable_admin.cancel,
140 keys: [ 'esc' ],
141 action: function() {
142 window.location.reload();
143 },
144 },
145 },
146 } );
147 },
148
149 /**
150 * Call AJAX to activate Charitable Pro, then redirect to dashboard on success; show error and keep Lite on failure.
151 *
152 * @since 1.8.9.6
153 * @param {object} data Data with redirect_url, plugin_basename.
154 */
155 activateProThenRedirect: function( data ) {
156 $.post( charitable_admin.ajax_url, {
157 action: 'charitable_activate_pro',
158 nonce: charitable_admin.nonce,
159 plugin_basename: data.plugin_basename || 'charitable-pro/charitable.php',
160 } )
161 .done( function( res ) {
162 if ( res.success && res.data.redirect_url ) {
163 window.location.href = res.data.redirect_url;
164 } else if ( res.data && res.data.message ) {
165 $.alert( {
166 title: charitable_admin.oops,
167 content: res.data.message,
168 icon: 'fa fa-exclamation-circle',
169 type: 'orange',
170 buttons: { confirm: { text: charitable_admin.ok, btnClass: 'btn-confirm' } },
171 } );
172 }
173 } )
174 .fail( function( xhr ) {
175 app.failAlert( xhr );
176 } );
177 },
178
179 /**
180 * Pro already installed: ask to activate. Yes = redirect to activate, No = back to general settings.
181 *
182 * @since 1.8.9.6
183 * @param {object} data Response data with activate_url, general_url, message.
184 */
185 showProAlreadyInstalledConfirm: function( data ) {
186
187 $.alert( {
188 title: charitable_admin.success,
189 content: data.message,
190 icon: 'fa fa-check-circle',
191 type: 'green',
192 boxWidth: '800px',
193 buttons: {
194 activate: {
195 text: charitable_admin.plugin_activate_btn,
196 btnClass: 'btn-confirm',
197 keys: [ 'enter' ],
198 action: function() {
199 window.location.href = data.activate_url;
200 },
201 },
202 no: {
203 text: charitable_admin.cancel,
204 keys: [ 'esc' ],
205 action: function() {
206 window.location.href = data.general_url;
207 },
208 },
209 },
210 } );
211 },
212
213 /**
214 * Go to upgrade url.
215 *
216 * @since 1.5.5
217 * @version 1.8.9.6
218 */
219 gotoUpgradeUrl: function( event ) {
220
221 var data = {
222 action: 'charitable_connect_url',
223 key: el.$connectKey.val(),
224 nonce: charitable_admin.nonce,
225 };
226
227 // Test arg: ?charitable_connect_test=1 on settings URL simulates non-localhost (install/activate modals).
228 if ( window.location.search.indexOf( 'charitable_connect_test=1' ) !== -1 ) {
229 data.charitable_connect_test = '1';
230 }
231
232 // if there is key empty, then alert.
233 if ( ! el.$connectKey.val() ) {
234 $.alert( {
235 title: charitable_admin.oops,
236 icon: 'fa fa-exclamation-circle',
237 type: 'orange',
238 boxWidth: '800px',
239 content: charitable_admin.please_enter_key,
240 buttons: {
241 confirm: {
242 text: charitable_admin.ok,
243 btnClass: 'btn-confirm',
244 keys: [ 'enter' ],
245 },
246 },
247 } );
248 // prevent the form from submitting.
249 event.preventDefault();
250 return false;
251 }
252
253 var $btn = el.$connectBtn;
254 var originalHtml = $btn.html();
255 var spinnerHtml = '<i class="fa fa-spinner fa-spin" aria-hidden="true"></i> ';
256 $btn.prop( 'disabled', true ).html( spinnerHtml + charitable_admin.verifying );
257
258 $.post( charitable_admin.ajax_url, data )
259 .done( function( res ) {
260
261 if ( res.success ) {
262 // Pro installed but inactive: show popup to activate (no redirect to upgrade.wpcharitable.com).
263 if ( res.data.show_activate_pro_popup ) {
264 app.showActivateProPopup( res.data );
265 return;
266 }
267 // Redirect to upgrade.wpcharitable.com (Pro not installed — need to download).
268 if ( res.data.upgrade_url ) {
269 el.$connectBtn.data( 'charitable-skip-restore', true );
270 window.location.href = res.data.upgrade_url;
271 return;
272 }
273 if ( res.data.reload ) {
274 $.alert( app.proAlreadyInstalled( res ) );
275 return;
276 }
277 if ( res.data.action === 'show_pro_download_confirmation' ) {
278 app.showProDownloadConfirmation( res.data );
279 return;
280 }
281 // Pro already installed: ask to activate. Yes = go to activate, No = back to general settings.
282 if ( res.data.pro_already_installed_confirm ) {
283 app.showProAlreadyInstalledConfirm( res.data );
284 return;
285 }
286 // Localhost: show message and Manual Install button (opens docs in new tab only).
287 // Refresh the page on either button so the settings UI shows the saved license.
288 if ( res.data.show_manual_upgrade && res.data.url ) {
289 $.alert( {
290 title: charitable_admin.license_validated || charitable_admin.success,
291 content: res.data.message,
292 icon: 'fa fa-check-circle',
293 type: 'green',
294 boxWidth: '800px',
295 buttons: {
296 confirm: {
297 text: charitable_admin.ok,
298 btnClass: 'btn-confirm',
299 keys: [ 'enter' ],
300 action: function() {
301 window.location.reload();
302 },
303 },
304 manual: {
305 text: charitable_admin.manual_install,
306 btnClass: 'btn-confirm',
307 action: function() {
308 window.open( res.data.url, '_blank' );
309 window.location.reload();
310 },
311 },
312 },
313 } );
314 return;
315 }
316 }
317
318 // Error or legacy response: show message (with optional manual upgrade for errors).
319 if ( res.data && res.data.show_manual_upgrade && res.data.url ) {
320 $.alert( {
321 title: charitable_admin.oops,
322 content: res.data.message,
323 icon: 'fa fa-exclamation-circle',
324 type: 'orange',
325 boxWidth: '800px',
326 buttons: {
327 confirm: {
328 text: charitable_admin.ok,
329 btnClass: 'btn-confirm',
330 keys: [ 'enter' ],
331 },
332 url: {
333 text: charitable_admin.manual_install,
334 btnClass: 'btn-confirm',
335 action: function() {
336 window.open( res.data.url, '_blank' );
337 },
338 },
339 },
340 } );
341 } else if ( res.data && res.data.message ) {
342 $.alert( {
343 title: charitable_admin.oops,
344 content: res.data.message,
345 icon: 'fa fa-exclamation-circle',
346 type: 'orange',
347 boxWidth: '800px',
348 buttons: {
349 confirm: {
350 text: charitable_admin.ok,
351 btnClass: 'btn-confirm',
352 keys: [ 'enter' ],
353 },
354 },
355 } );
356 }
357 } )
358 .always( function() {
359 var $b = el.$connectBtn;
360 // Only restore if we're not redirecting (e.g. user saw popup and stayed on page).
361 if ( $b.data( 'charitable-skip-restore' ) ) {
362 $b.removeData( 'charitable-skip-restore' );
363 return;
364 }
365 if ( originalHtml ) {
366 $b.prop( 'disabled', false ).html( originalHtml );
367 } else {
368 $b.prop( 'disabled', false );
369 }
370 } )
371 .fail( function( xhr ) {
372
373 app.failAlert( xhr );
374 } );
375 },
376
377 /**
378 * Show Pro download confirmation modal.
379 *
380 * @since 1.8.9.6
381 * @version 1.8.9.6
382 *
383 * @param {object} data Response data from server.
384 */
385 showProDownloadConfirmation: function( data ) {
386
387 $.alert( {
388 title: data.confirmation_title || charitable_admin.license_validated,
389 content: data.confirmation_text,
390 icon: 'fa fa-check-circle',
391 type: 'green',
392 boxWidth: '800px',
393 buttons: {
394 download: {
395 text: charitable_admin.download_pro,
396 btnClass: 'btn-confirm',
397 keys: [ 'enter' ],
398 action: function() {
399 app.downloadProPlugin( data.pro_download_url );
400 },
401 },
402 cancel: {
403 text: charitable_admin.cancel,
404 keys: [ 'esc' ],
405 action: function() {
406 window.location.reload();
407 },
408 },
409 },
410 } );
411 },
412
413 /**
414 * Download Charitable Pro plugin.
415 *
416 * @since 1.8.9.6
417 * @version 1.8.9.6
418 *
419 * @param {string} downloadUrl The download URL for Pro plugin.
420 */
421 downloadProPlugin: function( downloadUrl ) {
422
423 var data = {
424 action: 'charitable_download_pro',
425 download_url: downloadUrl,
426 nonce: charitable_admin.nonce,
427 };
428
429 // Show loading message.
430 $.alert( {
431 title: charitable_admin.downloading,
432 content: charitable_admin.downloading_pro_message,
433 icon: 'fa fa-spinner fa-spin',
434 type: 'blue',
435 boxWidth: '600px',
436 buttons: false,
437 closeIcon: false,
438 } );
439
440 $.post( charitable_admin.ajax_url, data )
441 .done( function( res ) {
442 // Close the loading dialog.
443 if ( typeof jconfirm !== 'undefined' && jconfirm.instances && jconfirm.instances.length > 0 ) {
444 jconfirm.instances[ jconfirm.instances.length - 1 ].close();
445 }
446
447 if ( res.success ) {
448 if ( res.data.action === 'show_activation_confirmation' ) {
449 app.showActivationConfirmation( res.data );
450 return;
451 }
452 }
453
454 // Handle download errors.
455 var errorMessage = res.data && res.data.message ? res.data.message : charitable_admin.download_failed;
456 var addonsUrl = res.data && res.data.addons_url ? res.data.addons_url : null;
457
458 var buttons = {
459 confirm: {
460 text: charitable_admin.ok,
461 btnClass: 'btn-confirm',
462 keys: [ 'enter' ],
463 },
464 };
465
466 if ( addonsUrl ) {
467 buttons.addons = {
468 text: charitable_admin.visit_addons_page,
469 btnClass: 'btn-confirm',
470 action: function() {
471 window.location.href = addonsUrl;
472 },
473 };
474 }
475
476 $.alert( {
477 title: charitable_admin.oops,
478 content: errorMessage,
479 icon: 'fa fa-exclamation-circle',
480 type: 'orange',
481 boxWidth: '800px',
482 buttons: buttons,
483 } );
484 } )
485 .fail( function( xhr ) {
486 // Close the loading dialog.
487 if ( typeof jconfirm !== 'undefined' && jconfirm.instances && jconfirm.instances.length > 0 ) {
488 jconfirm.instances[ jconfirm.instances.length - 1 ].close();
489 }
490 app.failAlert( xhr );
491 } );
492 },
493
494 /**
495 * Show Pro activation confirmation modal.
496 *
497 * @since 1.8.9.6
498 * @version 1.8.9.6
499 *
500 * @param {object} data Response data from server.
501 */
502 showActivationConfirmation: function( data ) {
503
504 $.alert( {
505 title: data.activation_title || charitable_admin.success,
506 content: data.activation_text,
507 icon: 'fa fa-check-circle',
508 type: 'green',
509 boxWidth: '800px',
510 buttons: {
511 activate: {
512 text: charitable_admin.activate_now,
513 btnClass: 'btn-confirm',
514 keys: [ 'enter' ],
515 action: function() {
516 app.activateProPlugin( data.plugin_basename );
517 },
518 },
519 later: {
520 text: charitable_admin.activate_later,
521 keys: [ 'esc' ],
522 action: function() {
523 // Just close the modal - Pro is downloaded and ready for manual activation.
524 $.alert( {
525 title: charitable_admin.ready_to_use,
526 content: charitable_admin.pro_ready_to_activate,
527 icon: 'fa fa-info-circle',
528 type: 'blue',
529 boxWidth: '600px',
530 buttons: {
531 confirm: {
532 text: charitable_admin.ok,
533 btnClass: 'btn-confirm',
534 keys: [ 'enter' ],
535 },
536 },
537 } );
538 },
539 },
540 },
541 } );
542 },
543
544 /**
545 * Activate Charitable Pro plugin.
546 *
547 * @since 1.8.9.6
548 * @version 1.8.9.6
549 *
550 * @param {string} pluginBasename The plugin basename to activate.
551 */
552 activateProPlugin: function( pluginBasename ) {
553
554 var data = {
555 action: 'charitable_activate_pro',
556 plugin_basename: pluginBasename,
557 nonce: charitable_admin.nonce,
558 };
559
560 // Show loading message.
561 $.alert( {
562 title: charitable_admin.activating,
563 content: charitable_admin.activating_pro_message,
564 icon: 'fa fa-spinner fa-spin',
565 type: 'blue',
566 boxWidth: '600px',
567 buttons: false,
568 closeIcon: false,
569 } );
570
571 $.post( charitable_admin.ajax_url, data )
572 .done( function( res ) {
573 // Close the loading dialog.
574 if ( typeof jconfirm !== 'undefined' && jconfirm.instances && jconfirm.instances.length > 0 ) {
575 jconfirm.instances[ jconfirm.instances.length - 1 ].close();
576 }
577
578 if ( res.success && res.data.reload ) {
579 $.alert( {
580 title: charitable_admin.success,
581 content: res.data.message,
582 icon: 'fa fa-check-circle',
583 type: 'green',
584 boxWidth: '600px',
585 buttons: {
586 confirm: {
587 text: charitable_admin.ok,
588 btnClass: 'btn-confirm',
589 keys: [ 'enter' ],
590 action: function() {
591 window.location.reload();
592 },
593 },
594 },
595 } );
596 return;
597 }
598
599 // Handle activation errors.
600 var errorMessage = res.data && res.data.message ? res.data.message : charitable_admin.activation_failed;
601
602 $.alert( {
603 title: charitable_admin.oops,
604 content: errorMessage,
605 icon: 'fa fa-exclamation-circle',
606 type: 'orange',
607 boxWidth: '800px',
608 buttons: {
609 confirm: {
610 text: charitable_admin.ok,
611 btnClass: 'btn-confirm',
612 keys: [ 'enter' ],
613 },
614 },
615 } );
616 } )
617 .fail( function( xhr ) {
618 // Close the loading dialog.
619 if ( typeof jconfirm !== 'undefined' && jconfirm.instances && jconfirm.instances.length > 0 ) {
620 jconfirm.instances[ jconfirm.instances.length - 1 ].close();
621 }
622 app.failAlert( xhr );
623 } );
624 },
625
626 /**
627 * Alert in case of server error.
628 *
629 * @since 1.5.5
630 *
631 * @param {object} xhr XHR object.
632 */
633 failAlert: function( xhr ) {
634
635 $.alert( {
636 title: charitable_admin.oops,
637 content: charitable_admin.server_error + '<br>' + xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText,
638 icon: 'fa fa-exclamation-circle',
639 type: 'orange',
640 buttons: {
641 confirm: {
642 text: charitable_admin.ok,
643 btnClass: 'btn-confirm',
644 keys: [ 'enter' ],
645 },
646 },
647 } );
648 },
649 };
650
651 // Provide access to public functions/properties.
652 return app;
653
654 }( document, window, jQuery ) );
655
656 // Initialize.
657 CharitableConnect.init();
658