1( function () { 2 'use strict'; 3 4 /** 5 * Apply honeypot refill data to a CF7 form. 6 * 7 * @param {HTMLFormElement} form 8 * @param {Object} honeypotData Keys are CF7 field names. 9 */ 10 function applyHoneypotRefill( form, honeypotData ) { 11 if ( ! form || ! honeypotData || typeof honeypotData !== 'object' ) { 12 return; 13 } 14 15 Object.keys( honeypotData ).forEach( function ( fieldName ) { 16 const token = honeypotData[ fieldName ]; 17 18 if ( ! token || ! token.random_hash || ! token.field_name ) { 19 return; 20 } 21 22 const wrapper = 23 form.querySelector( '[data-cf7apps-honeypot="' + fieldName + '"]' ) || 24 form.querySelector( '.wpcf7-form-control-wrap.' + fieldName + '-wrap' ); 25 26 if ( ! wrapper ) { 27 return; 28 } 29 30 const hashInput = form.querySelector( 31 'input[name="' + fieldName + '-random-hash"]' 32 ); 33 34 if ( hashInput ) { 35 hashInput.value = token.random_hash; 36 } 37 38 const honeypotInput = wrapper.querySelector( 39 'input.wpcf7-form-control[type="text"]' 40 ); 41 42 if ( honeypotInput ) { 43 honeypotInput.setAttribute( 'name', token.field_name ); 44 honeypotInput.value = ''; 45 } 46 } ); 47 } 48 49 /** 50 * Sync honeypot input values before CF7 builds FormData. 51 * 52 * @param {HTMLFormElement} form 53 */ 54 function syncHoneypotInputValues( form ) { 55 if ( ! form ) { 56 return; 57 } 58 59 form.querySelectorAll( '[data-cf7apps-honeypot]' ).forEach( function ( wrapper ) { 60 const honeypotInput = wrapper.querySelector( 61 'input.wpcf7-form-control[type="text"]' 62 ); 63 64 if ( ! honeypotInput ) { 65 return; 66 } 67 68 const attributeValue = honeypotInput.getAttribute( 'value' ); 69 70 if ( attributeValue && ! honeypotInput.value ) { 71 honeypotInput.value = attributeValue; 72 } 73 } ); 74 } 75 76 /** 77 * Bind refill listeners to a single CF7 form. 78 * 79 * @param {HTMLFormElement} form 80 */ 81 function bindHoneypotRefillEvents( form ) { 82 if ( ! form || form.dataset.cf7appsHoneypotBound ) { 83 return; 84 } 85 86 if ( ! form.querySelector( '[data-cf7apps-honeypot]' ) ) { 87 return; 88 } 89 90 form.dataset.cf7appsHoneypotBound = '1'; 91 92 form.addEventListener( 'submit', function () { 93 syncHoneypotInputValues( form ); 94 }, true ); 95 96 form.addEventListener( 'wpcf7reset', function ( event ) { 97 if ( event.detail && event.detail.apiResponse && event.detail.apiResponse.honeypot ) { 98 applyHoneypotRefill( form, event.detail.apiResponse.honeypot ); 99 } 100 } ); 101 102 form.addEventListener( 'wpcf7submit', function ( event ) { 103 if ( event.detail && event.detail.apiResponse && event.detail.apiResponse.honeypot ) { 104 applyHoneypotRefill( form, event.detail.apiResponse.honeypot ); 105 } 106 } ); 107 } 108 109 /** 110 * Initialize honeypot refill for all CF7 forms on the page. 111 */ 112 function initHoneypotRefill() { 113 if ( typeof wpcf7 === 'undefined' ) { 114 return; 115 } 116 117 if ( typeof wpcf7.submit === 'function' && ! wpcf7.__cf7appsHoneypotSubmitWrapped ) { 118 const originalSubmit = wpcf7.submit; 119 120 wpcf7.submit = function ( form, options ) { 121 if ( form instanceof HTMLFormElement ) { 122 syncHoneypotInputValues( form ); 123 } 124 125 return originalSubmit.call( this, form, options ); 126 }; 127 128 wpcf7.__cf7appsHoneypotSubmitWrapped = true; 129 } 130 131 const forms = document.querySelectorAll( '.wpcf7 > form' ); 132 133 forms.forEach( function ( form ) { 134 bindHoneypotRefillEvents( form ); 135 136 if ( ! form.querySelector( '[data-cf7apps-honeypot]' ) ) { 137 return; 138 } 139 140 const forceRefill = 141 ( typeof wpcf7 !== 'undefined' && wpcf7.cached ) || 142 ( typeof cf7appsHoneypotRefill !== 'undefined' && 143 cf7appsHoneypotRefill.forceRefillOnInit ); 144 145 if ( forceRefill && typeof wpcf7.reset === 'function' ) { 146 wpcf7.reset( form ); 147 } 148 } ); 149 } 150 151 if ( document.readyState === 'loading' ) { 152 document.addEventListener( 'DOMContentLoaded', initHoneypotRefill ); 153 } else { 154 initHoneypotRefill(); 155 } 156} )();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.