PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / assets / js / notice-response.js

notice-response.js in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.6.1, at assets/js/notice-response.js

104 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global suredonationNoticeResponse */
2 ( function () {
3 const notices = {
4 'sd-review-donation': {
5 primary: 'rate_suredonation',
6 snooze: 'maybe_later',
7 dismiss: 'dismissed',
8 },
9 'sd-test-mode': {
10 primary: 'switch_to_live',
11 snooze: 'maybe_later',
12 dismiss: 'dismissed',
13 },
14 'sd-review-gateway': {
15 primary: 'rate_suredonation',
16 snooze: 'maybe_later',
17 dismiss: 'dismissed',
18 },
19 'sd-setup-gateway': {
20 primary: 'configure_gateway',
21 snooze: 'maybe_later',
22 dismiss: 'dismissed',
23 },
24 'sd-paypal-reconnect': {
25 primary: 'paypal_reconnect_notice_cta',
26 dismiss: 'paypal_reconnect_notice_dismiss',
27 },
28 'sd-webhook-not-configured': {
29 primary: 'configure_webhook',
30 dismiss: 'dismissed',
31 },
32 };
33
34 function getAction( el, noticeId ) {
35 const config = notices[ noticeId ];
36 if ( ! config ) {
37 return null;
38 }
39
40 // Plain (non-banner) notice CTA link, e.g. the webhook notice's
41 // "configure" link.
42 if ( el.classList.contains( 'sd-notice-cta' ) ) {
43 return config.primary;
44 }
45
46 if (
47 el.classList.contains( 'button-primary' ) ||
48 ( el.classList.contains( 'astra-notice-close' ) &&
49 el.getAttribute( 'target' ) === '_blank' )
50 ) {
51 return config.primary;
52 }
53 if ( el.hasAttribute( 'data-repeat-notice-after' ) ) {
54 return config.snooze;
55 }
56 if ( el.classList.contains( 'astra-notice-close' ) ) {
57 return config.dismiss;
58 }
59 return null;
60 }
61
62 function sendResponse( noticeId, button ) {
63 const body = new FormData();
64 body.append( 'action', 'suredonation_notice_response' );
65 body.append( 'nonce', suredonationNoticeResponse.nonce );
66 body.append( 'notice_id', noticeId );
67 body.append( 'button', button );
68
69 // keepalive lets the request outlive the page: CTA buttons navigate the
70 // same tab immediately, so without it the in-flight request is aborted on
71 // unload and the click is never recorded.
72 fetch( suredonationNoticeResponse.ajaxurl, {
73 method: 'POST',
74 body,
75 keepalive: true,
76 } ).catch( () => {} );
77 }
78
79 Object.keys( notices ).forEach( function ( noticeId ) {
80 const container = document.getElementById( noticeId );
81 if ( ! container ) {
82 return;
83 }
84
85 container.addEventListener( 'click', function ( e ) {
86 // WordPress core dismiss button (the top-right "x").
87 if ( e.target.closest( 'button.notice-dismiss' ) ) {
88 sendResponse( noticeId, notices[ noticeId ].dismiss );
89 return;
90 }
91
92 const link = e.target.closest( 'a' );
93 if ( ! link ) {
94 return;
95 }
96
97 const action = getAction( link, noticeId );
98 if ( action ) {
99 sendResponse( noticeId, action );
100 }
101 } );
102 } );
103 }() );
104