PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.12
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.12
2.0.14 2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / contact-form / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.12, at blocks/contact-form/frontend.js

105 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
4 function initContactForm(wrapper) {
5 var form = wrapper.querySelector('.bkbg-cf-form');
6 var btn = wrapper.querySelector('.bkbg-cf-submit');
7 var status = wrapper.querySelector('.bkbg-cf-status');
8 if (!form || !btn || !status) return;
9
10 var recipient = wrapper.dataset.recipient || '';
11 var recipientSig = wrapper.dataset.recipientSig || '';
12 var subject = wrapper.dataset.subject || 'New message from your website';
13 var successMsg = wrapper.dataset.success || 'Thank you! Your message has been sent.';
14 var errorMsg = wrapper.dataset.error || 'Something went wrong. Please try again.';
15
16 var endpoint = (window.wpApiSettings && window.wpApiSettings.root)
17 ? window.wpApiSettings.root.replace(/\/?$/, '/') + 'blockenberg/v1/contact'
18 : '/wp-json/blockenberg/v1/contact';
19
20 function showStatus(msg, type) {
21 status.textContent = msg;
22 status.className = 'bkbg-cf-status bkbg-cf-' + type;
23 }
24
25 function clearStatus() {
26 status.className = 'bkbg-cf-status';
27 status.textContent = '';
28 }
29
30 function setLoading(active) {
31 btn.disabled = active;
32 if (active) {
33 btn.classList.add('is-loading');
34 } else {
35 btn.classList.remove('is-loading');
36 }
37 }
38
39 form.addEventListener('submit', function (e) {
40 e.preventDefault();
41 clearStatus();
42
43 // Native HTML5 validation
44 if (!form.checkValidity()) {
45 form.reportValidity();
46 return;
47 }
48
49 var data = {
50 name : (form.querySelector('[name="name"]') || {}).value || '',
51 email : (form.querySelector('[name="email"]') || {}).value || '',
52 phone : (form.querySelector('[name="phone"]') || {}).value || '',
53 message : (form.querySelector('[name="message"]') || {}).value || '',
54 website : (form.querySelector('[name="website"]') || {}).value || '', // honeypot
55 recipient: recipient,
56 recipientSig: recipientSig,
57 subject : subject
58 };
59
60 // Basic client-side email check
61 if (!data.email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) {
62 showStatus('Please enter a valid email address.', 'error');
63 return;
64 }
65
66 setLoading(true);
67
68 fetch(endpoint, {
69 method : 'POST',
70 headers: { 'Content-Type': 'application/json' },
71 body : JSON.stringify(data)
72 })
73 .then(function (res) {
74 return res.json().then(function (body) {
75 return { ok: res.ok, body: body };
76 });
77 })
78 .then(function (result) {
79 setLoading(false);
80 if (result.ok && result.body && result.body.ok) {
81 showStatus(successMsg, 'success');
82 form.reset();
83 } else {
84 var msg = (result.body && result.body.message) ? result.body.message : errorMsg;
85 showStatus(msg, 'error');
86 }
87 })
88 .catch(function () {
89 setLoading(false);
90 showStatus(errorMsg, 'error');
91 });
92 });
93 }
94
95 function init() {
96 document.querySelectorAll('.bkbg-cf-wrapper').forEach(initContactForm);
97 }
98
99 if (document.readyState === 'loading') {
100 document.addEventListener('DOMContentLoaded', init);
101 } else {
102 init();
103 }
104 })();
105