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;
- }
- }
- $('#wcfm_products_simple_submit_button').on('mousedown', function(e) {
- const excerptContent = getWCFMEditorContent('excerpt');
- if (excerptContent) {
- const modifiedExcerpt = 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 = removeExternalLinks(descriptionContent);
- if (typeof tinymce != 'undefined' && tinymce.get('description') != null) {
- tinymce.get('description').setContent(modifiedDescription);
- } else {
- $('#description').val(modifiedDescription);
- }
- }
- });
- });
- </script>
- <?php
- });
Advertisement
Comments
-
- Good morning,
- I want to thank you for your help in creating and guiding me through the PHP solution. It worked perfectly, and I appreciate the time and effort you put into it. Just to clarify, only the admin (platform owner) can add external links, while vendors or sellers won't have access to that option. Thanks to your guidance, vendors can no longer add external links, improving the platform's security. I truly appreciate your support and expertise!
- Again, thank you.
Add Comment
Please, Sign In to add comment
Advertisement