Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Post content showing links and media assets from the original URL #22

Closed
straube opened this issue Jun 29, 2018 · 9 comments
Closed

Post content showing links and media assets from the original URL #22

straube opened this issue Jun 29, 2018 · 9 comments
Assignees
Milestone

Comments

@straube
Copy link
Owner

straube commented Jun 29, 2018

Let's say one has set up the plugin with two different domains: foobar.net and foobar.com, where foobar.net is the original domain. When pointing the browser to foobar.com, all links and media assets added to post content will have the original domain.

This happens because the plugin doesn't filter the post content to replace the original domain with the current one. I don't think this should be enforced, though. An option to enable this filter is the best way to handle cases like this, letting the site admin decide whether they want to keep the original URL.

@straube straube added this to the 0.8 milestone Jun 29, 2018
@straube
Copy link
Owner Author

straube commented Jun 29, 2018

A temporary solution is adding a filter to the theme's functions.php file:

add_filter('the_content', 'fix_content_for_multiple_domain');
 
function fix_content_for_multiple_domain($content)
{
    if (defined('MULTPLE_DOMAIN_ORIGINAL_DOMAIN') && MULTPLE_DOMAIN_ORIGINAL_DOMAIN !== MULTPLE_DOMAIN_DOMAIN) {
        $regex = '/(https?:\/\/)' . preg_quote(MULTPLE_DOMAIN_ORIGINAL_DOMAIN) . '/i';
        $content = preg_replace($regex, '$1' . MULTPLE_DOMAIN_DOMAIN, $content);
    }
    return $content;
}

WARNING: before you go ahead and change your functions.php file, I strongly advise you to make a copy/back up of it. This way you can easily roll back the changes in case anything goes wrong.

You may also refer to the WP reference on the_content filter: https://developer.wordpress.org/reference/hooks/the_content/

@tbrandysky
Copy link

tbrandysky commented Sep 3, 2018

I can confirm there is such issue. I also can see the media links pointing back to other domain. This is an issue for our company as we access the wp-admin from internal subnets and these internal subnet domain is then listed publicly in <img src="internal-domain.company.com">. The result is the address of our internal domain leaks and the media image resource is not visible for our clients. Can this be fixed any soon please?

@tbrandysky
Copy link

btw: the proposed temporary solution doesn't fix the issue for me. I still can see <img src="https://internal.company.com\> in the post when I try to access it on https://company.com site

@straube
Copy link
Owner Author

straube commented Sep 7, 2018

@tbrandysky Could you check if the new release (0.8.0) fixes the issue?

@tbrandysky
Copy link

Unfortunately we still see the same behaviour as I described in my previous post.

@straube straube added the bug label Sep 7, 2018
@straube straube self-assigned this Sep 7, 2018
@straube
Copy link
Owner Author

straube commented Sep 7, 2018

As noticed by @tbrandysky, assets can still be loaded from a URL different than the current one.

Let's say a site's current multiple domain setup is:

  • example.com (original domain)
  • example.net
  • example.org

If an asset is added to a post content from example.net and one accesses the site from example.com, the asset will still be loaded from example.net. That happens because the current version of the plugin only replaces the original domain with the current domain. example.net not being the original domain won't be replaced.

To fix this issue, the plugin must walk the entire list of domains and replace any occurrence with the current domain.

This will be fixed on 0.8.1. The following snippet is a temporary solution. Add it to your theme's functions.php file.

add_filter('the_content', 'fix_content_for_multiple_domain', 20);
 
function fix_content_for_multiple_domain($content)
{
    if (!defined('MULTPLE_DOMAIN_DOMAIN')) {
        return $content;
    }
    $domains = get_option('multiple-domain-domains');
    foreach (array_keys($domains) as $domain) {
        if (MULTPLE_DOMAIN_DOMAIN === $domain) {
            continue;
        }
        $regex = '/(https?:\/\/)' . preg_quote($domain) . '/i';
        $content = preg_replace($regex, '$1' . MULTPLE_DOMAIN_DOMAIN, $content);
    }
    return $content;
}

@tbrandysky
Copy link

I can confirm that this solution works.
Thank you.

@tbrandysky
Copy link

I've found out there still is a request to get that data from example.com even when accessing site on example.net:

accessing WP at https://example.net

that part of source HTML with wrong URL reference:

<style type="text/css"> #site-header { background-image: url(https://example.com/wp-content/uploads/2018/09/architecture-blur-bright-440313_2.jpg); background-size: cover; }

@tbrandysky
Copy link

another thing is that the plugin always redirects users to https even when site is HTTP only

straube added a commit that referenced this issue Oct 27, 2018
Fix #22. Improved verification that checks whether SSL is in use.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants