Untitled
add_action('wp_head', function(){ ?> <script> jQuery(document).ready(function($) { // Only run on single product pages or pages with vendor store header if (document.body.classList.contains('single-product') || $('#marketking_vendor_store_page_header').length > 0) { // Function to mask email addresses function maskEmail(email) { var parts = email.split('@'); if (parts.length !== 2) return email; var username = parts[0]; var domain = parts[1]; var maskedUsername = username.charAt(0) + '*'.repeat(Math.max(2, username.length - 2)) + (username.length > 2 ? username.charAt(username.length - 1) : ''); var maskedDomain = domain.charAt(0) + '*'.repeat(domain.length - 2) + domain.charAt(domain.length - 1); return maskedUsername + '@' + maskedDomain; } // Function to mask phone numbers function maskPhone(phone) { // Remove all non-numeric characters var cleanPhone = phone.replace(/\D/g, ''); if (cleanPhone.length < 7) return phone; return '*'.repeat(Math.max(cleanPhone.length - 4, 0)) + cleanPhone.slice(-4); } // Function to process text nodes function processTextNodes(node) { if (node.nodeType === 3) { // Text node // Email regex var emailRegex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g; // Phone regex - matches various formats var phoneRegex = /(\+\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/g; var newText = node.textContent .replace(emailRegex, function(match) { return maskEmail(match); }) .replace(phoneRegex, function(match) { return maskPhone(match); }); if (newText !== node.textContent) { node.textContent = newText; } } else if (node.nodeType === 1 && // Element node !['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA'].includes(node.tagName)) { Array.from(node.childNodes).forEach(processTextNodes); } } // Initial processing processTextNodes(document.body); // Monitor DOM changes var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { mutation.addedNodes.forEach(function(node) { processTextNodes(node); }); }); }); // Start observing DOM changes observer.observe(document.body, { childList: true, subtree: true }); } }); </script> <?php });
Leave a Comment