Advertisement
palsushobhan

remove_email_external_url_product_excerpt_description

May 12th, 2025
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. add_action('after_wcfm_products_manage', function() {
  2.     ?>
  3.     <script>
  4.     jQuery(function($) {
  5.         function removeExternalLinks(htmlContent) {
  6.             if (!htmlContent) return htmlContent;
  7.  
  8.             const tempDiv = document.createElement('div');
  9.             tempDiv.innerHTML = htmlContent;
  10.  
  11.             const links = tempDiv.getElementsByTagName('a');
  12.            
  13.             for (let i = links.length - 1; i >= 0; i--) {
  14.                 const link = links[i];
  15.                 const href = link.getAttribute('href');
  16.  
  17.                 if (isExternalLink(href)) {
  18.                     const textNode = document.createTextNode(link.textContent);
  19.                     link.parentNode.replaceChild(textNode, link);
  20.                 }
  21.             }
  22.  
  23.             return tempDiv.innerHTML;
  24.         }
  25.  
  26.         function isExternalLink(url) {
  27.             if (!url) return false;
  28.  
  29.             const internalPrefixes = [
  30.                 '#', '/', './', '../',
  31.                 'mailto:', 'tel:',
  32.                 'javascript:',
  33.                 window.location.origin
  34.             ];
  35.  
  36.             if (internalPrefixes.some(prefix =>
  37.                 typeof prefix === 'string' && url.startsWith(prefix)
  38.             )) {
  39.                 return false;
  40.             }
  41.  
  42.             try {
  43.                 const linkURL = new URL(url, window.location.origin);
  44.                 return linkURL.hostname !== window.location.hostname;
  45.             } catch (e) {
  46.                 return true;
  47.             }
  48.         }
  49.  
  50.         function removeEmailAddresses(htmlContent) {
  51.             if (!htmlContent) return htmlContent;
  52.  
  53.             const emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
  54.             return htmlContent.replace(emailRegex, '');
  55.         }
  56.  
  57.         $('#wcfm_products_simple_submit_button').on('mousedown', function(e) {
  58.             const excerptContent = getWCFMEditorContent('excerpt');
  59.             if (excerptContent) {
  60.                 const modifiedExcerpt = removeEmailAddresses(removeExternalLinks(excerptContent));
  61.                 if (typeof tinymce != 'undefined' && tinymce.get('excerpt') != null) {
  62.                     tinymce.get('excerpt').setContent(modifiedExcerpt);
  63.                 } else {
  64.                     $('#excerpt').val(modifiedExcerpt);
  65.                 }
  66.             }
  67.  
  68.             const descriptionContent = getWCFMEditorContent('description');
  69.             if (descriptionContent) {
  70.                 const modifiedDescription = removeEmailAddresses(removeExternalLinks(descriptionContent));
  71.                 if (typeof tinymce != 'undefined' && tinymce.get('description') != null) {
  72.                     tinymce.get('description').setContent(modifiedDescription);
  73.                 } else {
  74.                     $('#description').val(modifiedDescription);
  75.                 }
  76.             }
  77.         });
  78.     });
  79.     </script>
  80.     <?php
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement