PluginProbe
CryptX / trunk
CryptX vtrunk
4.2.0 4.1.1 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.9 2.0 2.1 2.2 2.3 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 92 releases
cryptx / js / admin-notice.js

admin-notice.js in CryptX trunk, at js/admin-notice.js

83 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Makes dismissing the review notice stick.
3 *
4 * WordPress draws the cross on any notice carrying "is-dismissible", but all
5 * it does is remove the element -- the next page load brings it straight back.
6 * Everything here is therefore an enhancement of something that already works
7 * without it: "No thanks" is a real link to a real handler, and following it
8 * records the refusal and returns to the page. This just spares the reload,
9 * and gives the cross the same meaning as the link next to it.
10 *
11 * No build step and no dependencies. The URL and its nonce arrive in a data
12 * attribute rather than in an inline script, which is the same reason the
13 * front end carries data-cx: a page with a strict Content-Security-Policy
14 * should not need an exception on this plugin's account.
15 */
16 ( function () {
17 var notice = document.getElementById( 'cryptx-review-notice' );
18
19 if ( ! notice ) {
20 return;
21 }
22
23 var url = notice.getAttribute( 'data-cryptx-review' );
24
25 if ( ! url ) {
26 return;
27 }
28
29 var recorded = false;
30
31 /**
32 * Tells the server not to ask again.
33 *
34 * Fire and forget: the answer is a redirect to the page we are already on,
35 * and there is nothing useful to do with it. A failure is not worth
36 * reporting either -- the worst case is being asked once more.
37 */
38 function record() {
39 if ( recorded ) {
40 return;
41 }
42
43 recorded = true;
44
45 window.fetch( url, {
46 credentials: 'same-origin',
47 redirect: 'follow',
48 } ).catch( function () {} );
49 }
50
51 // Delegated, because WordPress appends the cross after this file runs.
52 notice.addEventListener( 'click', function ( event ) {
53 var target = event.target;
54
55 if ( ! target || ! target.closest ) {
56 return;
57 }
58
59 if ( target.closest( '.notice-dismiss' ) ) {
60 record();
61
62 return;
63 }
64
65 var link = target.closest( '[data-cryptx-review-action]' );
66
67 if ( ! link ) {
68 return;
69 }
70
71 record();
72
73 // The review page opens in its own tab, so this one stays put and the
74 // notice has to be taken away by hand. Declining navigates nowhere at
75 // all -- the request above is the whole of it.
76 if ( link.getAttribute( 'data-cryptx-review-action' ) === 'dismiss' ) {
77 event.preventDefault();
78 }
79
80 notice.remove();
81 } );
82 } )();
83