Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('after_wcfm_products_manage', function() {
- ?>
- <script>
- jQuery(function($) {
- function removeExternalLinks(htmlContent) {
- if (!htmlContent) return htmlContent;
- const tempDiv = document.createElement('div');
- tempDiv.innerHTML = htmlContent;
- const links = tempDiv.getElementsByTagName('a');
- for (let i = links.length - 1; i >= 0; i--) {
- const link = links[i];
- const href = link.getAttribute('href');
- if (isExternalLink(href)) {
- const textNode = document.createTextNode(link.textContent);
- link.parentNode.replaceChild(textNode, link);
- }
- }
- return tempDiv.innerHTML;
- }
- function isExternalLink(url) {
- if (!url) return false;
- const internalPrefixes = [
- '#', '/', './', '../',
- 'mailto:', 'tel:',
- 'javascript:',
- window.location.origin
- ];
- if (internalPrefixes.some(prefix =>
- typeof prefix === 'string' && url.startsWith(prefix)
- )) {
- return false;
- }
- try {
- const linkURL = new URL(url, window.location.origin);
- return linkURL.hostname !== window.location.hostname;
- } catch (e) {
- return true;
- }
- }
- function removeEmailAddresses(htmlContent) {
- if (!htmlContent) return htmlContent;
- const emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
- return htmlContent.replace(emailRegex, '');
- }
- $('#wcfm_products_simple_submit_button').on('mousedown', function(e) {
- const excerptContent = getWCFMEditorContent('excerpt');
- if (excerptContent) {
- const modifiedExcerpt = removeEmailAddresses(removeExternalLinks(excerptContent));
- if (typeof tinymce != 'undefined' && tinymce.get('excerpt') != null) {
- tinymce.get('excerpt').setContent(modifiedExcerpt);
- } else {
- $('#excerpt').val(modifiedExcerpt);
- }
- }
- const descriptionContent = getWCFMEditorContent('description');
- if (descriptionContent) {
- const modifiedDescription = removeEmailAddresses(removeExternalLinks(descriptionContent));
- if (typeof tinymce != 'undefined' && tinymce.get('description') != null) {
- tinymce.get('description').setContent(modifiedDescription);
- } else {
- $('#description').val(modifiedDescription);
- }
- }
- });
- });
- </script>
- <?php
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement