| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import domReady from '@wordpress/dom-ready'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Window dependencies. |
| 9 |
*/ |
| 10 |
const { epAdmin, ajaxurl } = window; |
| 11 |
|
| 12 |
/** |
| 13 |
* Initialize. |
| 14 |
* |
| 15 |
* @returns {void} |
| 16 |
*/ |
| 17 |
const init = () => { |
| 18 |
const notices = document.querySelectorAll('.notice[data-ep-notice]'); |
| 19 |
|
| 20 |
/** |
| 21 |
* Handle clicking in an ElasticPress notice. |
| 22 |
* |
| 23 |
* If the click target is the dismiss button send an AJAX request to remember |
| 24 |
* the dismissal. |
| 25 |
* |
| 26 |
* @param {Event} event Click event. |
| 27 |
* @returns {void} |
| 28 |
*/ |
| 29 |
const onClick = (event) => { |
| 30 |
/** |
| 31 |
* Only proceed if we're clicking dismiss. |
| 32 |
*/ |
| 33 |
if (!event.target.classList.contains('notice-dismiss')) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Handler is admin-ajax.php, so the body needs to be form data. |
| 39 |
*/ |
| 40 |
const formData = new FormData(); |
| 41 |
|
| 42 |
formData.append('action', 'ep_notice_dismiss'); |
| 43 |
formData.append('notice', event.currentTarget.dataset.epNotice); |
| 44 |
formData.append('nonce', epAdmin.nonce); |
| 45 |
|
| 46 |
apiFetch({ |
| 47 |
method: 'POST', |
| 48 |
url: ajaxurl, |
| 49 |
body: formData, |
| 50 |
}); |
| 51 |
}; |
| 52 |
|
| 53 |
/** |
| 54 |
* Bind click events to notices. |
| 55 |
*/ |
| 56 |
for (const notice of notices) { |
| 57 |
notice.addEventListener('click', onClick); |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Initialize. |
| 63 |
*/ |
| 64 |
domReady(init); |
| 65 |
|