| 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-webhook-not-configured': { |
| 25 |
primary: 'configure_webhook', |
| 26 |
dismiss: 'dismissed', |
| 27 |
}, |
| 28 |
}; |
| 29 |
|
| 30 |
function getAction( el, noticeId ) { |
| 31 |
const config = notices[ noticeId ]; |
| 32 |
if ( ! config ) { |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
// Plain (non-banner) notice CTA link, e.g. the webhook notice's |
| 37 |
// "configure" link. |
| 38 |
if ( el.classList.contains( 'sd-notice-cta' ) ) { |
| 39 |
return config.primary; |
| 40 |
} |
| 41 |
|
| 42 |
if ( |
| 43 |
el.classList.contains( 'button-primary' ) || |
| 44 |
( el.classList.contains( 'astra-notice-close' ) && |
| 45 |
el.getAttribute( 'target' ) === '_blank' ) |
| 46 |
) { |
| 47 |
return config.primary; |
| 48 |
} |
| 49 |
if ( el.hasAttribute( 'data-repeat-notice-after' ) ) { |
| 50 |
return config.snooze; |
| 51 |
} |
| 52 |
if ( el.classList.contains( 'astra-notice-close' ) ) { |
| 53 |
return config.dismiss; |
| 54 |
} |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
function sendResponse( noticeId, button ) { |
| 59 |
const body = new FormData(); |
| 60 |
body.append( 'action', 'suredonation_notice_response' ); |
| 61 |
body.append( 'nonce', suredonationNoticeResponse.nonce ); |
| 62 |
body.append( 'notice_id', noticeId ); |
| 63 |
body.append( 'button', button ); |
| 64 |
|
| 65 |
// keepalive lets the request outlive the page: CTA buttons navigate the |
| 66 |
// same tab immediately, so without it the in-flight request is aborted on |
| 67 |
// unload and the click is never recorded. |
| 68 |
fetch( suredonationNoticeResponse.ajaxurl, { |
| 69 |
method: 'POST', |
| 70 |
body, |
| 71 |
keepalive: true, |
| 72 |
} ).catch( () => {} ); |
| 73 |
} |
| 74 |
|
| 75 |
Object.keys( notices ).forEach( function ( noticeId ) { |
| 76 |
const container = document.getElementById( noticeId ); |
| 77 |
if ( ! container ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
container.addEventListener( 'click', function ( e ) { |
| 82 |
// WordPress core dismiss button (the top-right "x"). |
| 83 |
if ( e.target.closest( 'button.notice-dismiss' ) ) { |
| 84 |
sendResponse( noticeId, notices[ noticeId ].dismiss ); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
const link = e.target.closest( 'a' ); |
| 89 |
if ( ! link ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
const action = getAction( link, noticeId ); |
| 94 |
if ( action ) { |
| 95 |
sendResponse( noticeId, action ); |
| 96 |
} |
| 97 |
} ); |
| 98 |
} ); |
| 99 |
}() ); |
| 100 |
|