PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 5.8.0
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v5.8.0
5.8.0 5.8.1 5.7.0 5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 All 41 releases
double-opt-in / assets / js / doi-submit-notice.js

doi-submit-notice.js in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 5.8.0, at assets/js/doi-submit-notice.js

129 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Double Opt-In: confirmation hint under the Contact Form 7 message (5.8.0).
3 *
4 * CF7 writes its message with innerText, so the hint is a separate block.
5 * The server puts everything, already translated, into `apiResponse.doi`
6 * (CF7Integration::filterFeedbackResponse). Built with textContent only.
7 */
8 (function () {
9 'use strict';
10
11 function removeNotice(form) {
12 var old = form.querySelector('.f12-doi-notice');
13 if (old) {
14 old.parentNode.removeChild(old);
15 }
16 }
17
18 /*
19 * Actions added by add-ons (filter f12_doi_submit_notice_data, 5.8.0).
20 * A link opens in a new tab; a button only announces the click
21 * (`f12-doi-notice-action`) — the add-on's own script does the work.
22 */
23 function renderAction(form, action) {
24 if (!action || typeof action.id !== 'string' || !action.label) {
25 return null;
26 }
27
28 if (action.type === 'link') {
29 if (typeof action.url !== 'string' || action.url.indexOf('https://') !== 0) {
30 return null;
31 }
32 var link = document.createElement('a');
33 link.className = 'f12-doi-action f12-doi-action-' + action.id;
34 link.href = action.url;
35 link.target = '_blank';
36 link.rel = 'noopener noreferrer';
37 link.textContent = String(action.label);
38 return link;
39 }
40
41 var button = document.createElement('button');
42 button.type = 'button';
43 button.className = 'f12-doi-action f12-doi-action-' + action.id;
44 button.textContent = String(action.label);
45
46 // A button that waits (the reminder's "send it again": the first mail
47 // gets a minute) counts down in its label. Greyed out with no reason
48 // given, it read as broken.
49 var wait = parseInt(action.wait, 10) || 0;
50 if (wait > 0) {
51 var label = String(action.label);
52 var left = wait;
53 button.disabled = true;
54 button.textContent = label + ' (' + left + ' s)';
55 var timer = setInterval(function () {
56 left -= 1;
57 if (left > 0) {
58 button.textContent = label + ' (' + left + ' s)';
59 return;
60 }
61 clearInterval(timer);
62 button.disabled = false;
63 button.textContent = label;
64 }, 1000);
65 }
66
67 button.addEventListener('click', function () {
68 form.dispatchEvent(new CustomEvent('f12-doi-notice-action', {
69 bubbles: true,
70 detail: { id: action.id, data: action.data || {}, button: button, form: form }
71 }));
72 });
73
74 return button;
75 }
76
77 document.addEventListener('wpcf7beforesubmit', function (event) {
78 removeNotice(event.target);
79 });
80
81 document.addEventListener('wpcf7mailsent', function (event) {
82 var form = event.target;
83 var detail = event.detail || {};
84 var doi = detail.apiResponse && detail.apiResponse.doi;
85 if (!doi || !form || !form.querySelector) {
86 return;
87 }
88
89 removeNotice(form);
90
91 var box = document.createElement('div');
92 box.className = 'f12-doi-notice';
93
94 (doi.lines || []).forEach(function (line) {
95 var p = document.createElement('p');
96 p.textContent = String(line);
97 box.appendChild(p);
98 });
99
100 if (doi.inbox && typeof doi.inbox.url === 'string' && doi.inbox.url.indexOf('https://') === 0) {
101 var link = document.createElement('a');
102 link.className = 'f12-doi-inbox';
103 link.href = doi.inbox.url;
104 link.target = '_blank';
105 link.rel = 'noopener noreferrer';
106 link.textContent = String(doi.inbox.label || doi.inbox.name || '');
107 var p = document.createElement('p');
108 p.appendChild(link);
109 box.appendChild(p);
110 }
111
112 (doi.actions || []).forEach(function (action) {
113 var el = renderAction(form, action);
114 if (el) {
115 var row = document.createElement('p');
116 row.appendChild(el);
117 box.appendChild(row);
118 }
119 });
120
121 var output = form.querySelector('.wpcf7-response-output');
122 if (output && output.parentNode) {
123 output.parentNode.insertBefore(box, output.nextSibling);
124 } else {
125 form.appendChild(box);
126 }
127 });
128 })();
129