PluginProbe
Booking Calendar / 11.2
Booking Calendar v11.2
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / page-form-builder / field-packs / captcha / _src / field-captcha-wptpl.js

field-captcha-wptpl.js in Booking Calendar 11.2, at includes/page-form-builder/field-packs/captcha/_src/field-captcha-wptpl.js

282 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // ---------------------------------------------------------------------------------------------------------------------
2 // == File /includes/page-form-builder/field-packs/captcha/_out/field-captcha-wptpl.js
3 // == Pack Text Captcha (WP-template–driven) — Builder-focused renderer
4 // == Exports to shortcode: [captcha]
5 // ---------------------------------------------------------------------------------------------------------------------
6 (function (w) {
7 'use strict';
8
9 var Core = w.WPBC_BFB_Core || {};
10 var Registry = Core.WPBC_BFB_Field_Renderer_Registry;
11 var Base = Core.WPBC_BFB_Field_Base;
12
13 if ( ! Registry || typeof Registry.register !== 'function' || ! Base ) {
14 w._wpbc?.dev?.error?.( 'wpbc_bfb_field_captcha', 'Core registry/base missing' );
15 return;
16 }
17
18 /**
19 * Renderer for "captcha" field (template-driven).
20 * Methods/names are in snake_case to follow project conventions.
21 */
22 class wpbc_bfb_field_captcha extends Base {
23
24 /** Template id without "tmpl-" prefix. */
25 static template_id = 'wpbc-bfb-field-captcha';
26
27 /**
28 * Defaults must stay in sync with the PHP schema.
29 * @returns {Object}
30 */
31 static get_defaults() {
32 return {
33 type : 'captcha',
34 label : 'CAPTCHA',
35 html_id : '',
36 cssclass : '',
37 help : ''
38 };
39 }
40
41 /**
42 * Render via wp.template into the Builder canvas.
43 *
44 * @param {HTMLElement} el
45 * @param {Object} data
46 * @param {Object} ctx
47 * @return {void}
48 */
49 static render( el, data, ctx ) {
50 if ( ! el ) {
51 return;
52 }
53
54 // Normalize first (mirrors Base.render).
55 var d = this.normalize_data( data );
56
57 // Sanitize helpers available in core.
58 var S = Core.WPBC_BFB_Sanitize || {};
59 var html_id = d.html_id ? ( S.sanitize_html_id ? S.sanitize_html_id( String( d.html_id ) ) : String( d.html_id ) ) : '';
60 var css_next = S.sanitize_css_classlist ? S.sanitize_css_classlist( String( d.cssclass || '' ) ) : String( d.cssclass || '' );
61
62 // Keep wrapper dataset in sync.
63 if ( el.dataset.html_id !== html_id ) { el.dataset.html_id = html_id; }
64
65 // Compile template via Base (handles "tmpl-" + caching).
66 var tpl = this.get_template( this.template_id );
67 if ( typeof tpl !== 'function' ) {
68 w._wpbc?.dev?.error?.( 'captcha_wptpl.tpl.missing', 'Template not found: ' + this.template_id );
69 el.innerHTML = '<div class="wpbc_bfb__error" role="alert">Template not found: wpbc-bfb-field-captcha.</div>';
70 return;
71 }
72
73 // Stable id suffix (prefer normalized id if present).
74 var id_suffix = ( d.id != null ) ? String( d.id ) : ( Math.random().toString( 36 ).slice( 2 ) );
75
76 // Lightweight inline placeholder (SVG) to avoid generating real captcha in Builder.
77 var placeholder_svg = '<svg xmlns="http://www.w3.org/2000/svg" width="160" height="40"><rect width="100%" height="100%" fill="#f2f2f2"/><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="monospace" font-size="16">CAPTCHA CODE</text></svg>';
78 var placeholder_src = 'data:image/svg+xml;utf8,' + encodeURIComponent( placeholder_svg );
79 var alt_text = 'TEXT CAPTCHA';
80
81 // Render via template.
82 try {
83 var tpl_data = Object.assign( {}, d, {
84 html_id : html_id,
85 cssclass : css_next,
86 id : id_suffix,
87 placeholder_src: placeholder_src,
88 alt_text : alt_text
89 } );
90 el.innerHTML = tpl( tpl_data );
91 } catch ( e ) {
92 w._wpbc?.dev?.error?.( 'captcha_wptpl.tpl.render', e );
93 el.innerHTML = '<div class="wpbc_bfb__error" role="alert">Error rendering CAPTCHA preview.</div>';
94 return;
95 }
96
97 // Keep wrapper meta consistent.
98 el.dataset.type = d.type || 'captcha';
99 el.setAttribute( 'data-label', ( d.label != null ? String( d.label ) : '' ) );
100
101 // Overlay (handles/toolbars).
102 Core.UI?.WPBC_BFB_Overlay?.ensure?.( ctx?.builder, el );
103 }
104
105 /**
106 * Optional hook after field drop. Keep Base logic for auto-names/ids.
107 *
108 * @param {Object} data
109 * @param {HTMLElement} el
110 * @param {Object} ctx
111 * @return {void}
112 */
113 static on_field_drop( data, el, ctx ) {
114 super.on_field_drop?.( data, el, ctx );
115 }
116 }
117
118 try {
119 Registry.register( 'captcha', wpbc_bfb_field_captcha );
120 } catch ( e ) {
121 w._wpbc?.dev?.error?.( 'wpbc_bfb_field_captcha.register', e );
122 }
123
124 // -----------------------------------------------------------------------------------------------------------------
125 // Export for "Booking Form" (Advanced Form shortcode)
126 // -----------------------------------------------------------------------------------------------------------------
127 // -----------------------------------------------------------------------------------------------------------------
128 // Export for "Booking Form" (Advanced Form shortcode)
129 // -----------------------------------------------------------------------------------------------------------------
130 /**
131 * Booking Form exporter callback (Advanced Form shortcode) for "captcha".
132 *
133 * This callback is registered per field type via:
134 * WPBC_BFB_Exporter.register( 'captcha', callback )
135 *
136 * Core call site (builder-exporter.js):
137 * WPBC_BFB_Exporter.run_registered_exporter( field, io, cfg, once, ctx )
138 * -> callback( field, emit, { io, cfg, once, ctx, core } );
139 *
140 * @callback WPBC_BFB_ExporterCallback
141 * @param {Object} field
142 * Normalized field data coming from the Builder structure.
143 * - field.type {string} Field type, here always "captcha".
144 * - field.label {string} Visible label in the form (may be empty).
145 * - field.html_id {string} Optional HTML id / user-visible id (ignored here).
146 * - field.cssclass {string} Extra CSS classes entered in Inspector (ignored here).
147 * - ... (Any other pack-specific props are also present.)
148 *
149 * @param {function(string):void} emit
150 * Emits one line/fragment into the export buffer.
151 * - Each call corresponds to one `push()` in the core exporter.
152 * - For multi-line output (e.g. label + shortcode), call `emit()` multiple times:
153 * emit('<l>Label</l>');
154 * emit('<br>[captcha]');
155 *
156 * @param {Object} [extras]
157 * Extra context passed by the core exporter.
158 *
159 * @param {Object} [extras.io]
160 * Low-level writer used internally by the core.
161 * Normally you do NOT need it in packs — prefer `emit()`.
162 * - extras.io.open(str) -> open a nested block (increments indentation).
163 * - extras.io.close(str) -> close a block (decrements indentation).
164 * - extras.io.push(str) -> push raw line (used by `emit()`).
165 * - extras.io.blank() -> push an empty line.
166 *
167 * @param {Object} [extras.cfg]
168 * Export configuration (same object passed to WPBC_BFB_Exporter.export_form()).
169 * Useful flags for field packs:
170 * - extras.cfg.addLabels {boolean} Default: true.
171 * If false, packs should NOT emit <l>Label</l> lines.
172 * - extras.cfg.newline {string} Newline separator (usually "\n").
173 * - extras.cfg.gapPercent{number} Layout gap (used only by section/column logic).
174 *
175 * @param {Object} [extras.once]
176 * Shared "once-per-form" guards across all fields.
177 * Captcha is a typical once-per-form field:
178 * - extras.once.captcha {number} Counter of exported captchas.
179 *
180 * @param {Object} [extras.ctx]
181 * Shared export context for the entire form (not used here).
182 *
183 * @param {Object} [extras.core]
184 * Reference to WPBC_BFB_Core passed from builder-exporter.js.
185 * Primarily used to access sanitizers:
186 * - extras.core.WPBC_BFB_Sanitize.escape_html(...)
187 * - etc.
188 */
189 function register_captcha_booking_form_exporter() {
190
191 const Exp = w.WPBC_BFB_Exporter;
192 if ( ! Exp || typeof Exp.register !== 'function' ) { return; }
193 if ( typeof Exp.has_exporter === 'function' && Exp.has_exporter( 'captcha' ) ) { return; }
194
195 const S = Core.WPBC_BFB_Sanitize || (w.WPBC_BFB_Core && w.WPBC_BFB_Core.WPBC_BFB_Sanitize) || {};
196 const esc = S.escape_html || (v => String( v ));
197
198 /**
199 * @type {WPBC_BFB_ExporterCallback}
200 */
201 Exp.register( 'captcha', (field, emit, extras = {}) => {
202
203 const cfg = extras.cfg || {};
204 const addLabels = cfg.addLabels !== false;
205 const once = (extras.once && typeof extras.once === 'object') ? extras.once : null;
206
207 // Once-per-form guard: only the first [captcha] is exported.
208 if ( once ) {
209 once.captcha = (once.captcha || 0) + 1;
210 if ( once.captcha > 1 ) {
211 return;
212 }
213 }
214
215 // Body is fixed for this pack.
216 const body = '[captcha]';
217
218 // Label behavior identical to legacy emit_label_then('[captcha]').
219 const raw_label = (field && typeof field.label === 'string') ? field.label : '';
220 const label = raw_label.trim();
221
222 if ( label && addLabels ) {
223 emit( `<l>${esc( label )}</l>` );
224 emit( `<br>${body}` );
225 } else {
226 emit( body );
227 }
228 // Help text is appended centrally by WPBC_BFB_Exporter.render_field_node().
229 } );
230 }
231
232 // Try immediate registration; if core isn’t ready, wait for the event.
233 if ( w.WPBC_BFB_Exporter && typeof w.WPBC_BFB_Exporter.register === 'function' ) {
234 register_captcha_booking_form_exporter();
235 } else {
236 document.addEventListener( 'wpbc:bfb:exporter-ready', register_captcha_booking_form_exporter, { once: true } );
237 }
238
239
240 // -----------------------------------------------------------------------------------------------------------------
241 // Export for "Booking Data" (Content of booking fields data)
242 // -----------------------------------------------------------------------------------------------------------------
243 /**
244 * Booking Data exporter callback ("Content of booking fields data") for "captcha".
245 *
246 * This field does NOT contribute any content to the "Content of booking fields data"
247 * template, but we still register an explicit exporter that emits an empty string
248 * for consistency and introspection.
249 *
250 * Registered per field type via:
251 * WPBC_BFB_ContentExporter.register( 'captcha', callback )
252 *
253 * Core call site (builder-exporter.js):
254 * WPBC_BFB_ContentExporter.run_registered_exporter( field, emit, { cfg, core } );
255 *
256 * @callback WPBC_BFB_ContentExporterCallback
257 * @param {Object} field Normalized field data (not used here).
258 * @param {function(string):void} emit
259 * Emits a raw HTML fragment into the "Content of booking fields data" template.
260 * Here we explicitly emit an empty string.
261 * @param {Object} [extras] Additional context (unused).
262 */
263 function register_captcha_booking_data_exporter() {
264
265 const C = w.WPBC_BFB_ContentExporter;
266 if ( ! C || typeof C.register !== 'function' ) { return; }
267 if ( typeof C.has_exporter === 'function' && C.has_exporter( 'captcha' ) ) { return; }
268
269 C.register( 'captcha', function (field, emit, extras) { // eslint-disable-line no-unused-vars
270 // Explicitly export an empty string for captcha in "Content of booking fields data".
271 //emit( '' );
272 } );
273 }
274
275 if ( w.WPBC_BFB_ContentExporter && typeof w.WPBC_BFB_ContentExporter.register === 'function' ) {
276 register_captcha_booking_data_exporter();
277 } else {
278 document.addEventListener( 'wpbc:bfb:content-exporter-ready', register_captcha_booking_data_exporter, { once: true } );
279 }
280
281 })( window );
282