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

How to bypass the URL rewriting? #69

Open
straube opened this issue Jan 16, 2020 · 5 comments
Open

How to bypass the URL rewriting? #69

straube opened this issue Jan 16, 2020 · 5 comments

Comments

@straube
Copy link
Owner

straube commented Jan 16, 2020

Sometimes it may be useful to keep a URL as it is, not applying the rewriting. Here is a case the exemplifies a situation: https://wordpress.org/support/topic/linking-to-original-domain/.

I'm not sure if prepending something to the URL/domain is the best way to achieve that but it's one way.

Any other suggestions are welcome.

@QuietNoise
Copy link

QuietNoise commented Jan 29, 2020

The biggest problem is that replaceDomain/replaceDomainUsingLessMemory are very intrusive. They replace URLs regardless of where they are (anchors, images, JS, comments, pure text, legitimate links to another domain from the pool).
If someone will try to quote a serialised code (with a domain in it) in a post content then plugin will break the code.

Got some suggestions with progressive complexity.

I guess the simplest workaround would be to add filter hook in fixContentUrls i.e.

public function fixContentUrls($content)
{
    foreach (array_keys($this->domains) as $domain) {
        $content = $this->replaceDomain($domain, $content);
    }
    $content = apply_filters('after_fixContentUrls', $content);
    return $content;
}

This way everyone can apply their own filters after your plugin did the job.

Another way would be to allow special keyword in URL (i.e. httpmd:// as a protocol or urls starting with [FORCE]) which you then replace/remove in your filter. Disadvantage of this is that people will endup with bunch of broken URLs if they disable the plugin.

Similarly you can add a shortcode that folks can use to add URL which your plugin will treat differently. Same disadvantage as above.

The heaviest duty approach would be to search content for the actual tags with HTML href or src attributes (a, img, link) and replace the URLs unless tags contain a speciffic attribute i.e. data-nomd.

Hope that this gives you some ideas.

@straube
Copy link
Owner Author

straube commented Feb 3, 2020

@QuietNoise thanks for your suggestions. I'll put some thought on it to see what option fits better.

@straube straube added this to the 1.1 milestone Feb 11, 2020
@jffaria
Copy link
Contributor

jffaria commented Oct 31, 2020

Hello folks!

I just needed this functionality now and what I thought of is changing fixContentUrls so that it uses an URL pattern matching only the configured domains (except current domain) for preg_split with PREG_SPLIT_DELIM_CAPTURE flag, then perform fixUrl on each URL found (every other array entry) and imploding the array to return the content.

The regex for splitting would be similar to the one in replaceDomain function ($regex = '/(https?):\/\/' . preg_quote($domain, '/') . '(?![a-z0-9.\-:])/i';) but matching any of the domains instead of only one.

In that way, fixUrl would be the only place where actual rewriting takes place, and instead of testing only for wp-admin we could test for any URLs/paths set in a filter or in the settings view on the admin GUI if you wanna expose that there. And as it would work on shorter and simpler strings, replaceDomain could use str_replace instead of preg_replace, or something like that.

Not sure what performance impact those changes would have in general case, but if nothing needs to be replaced then only a preg_split will be done, instead of potentially many preg_replace (one per domain) that don't replace anything. Otoh when there are URLs to be fixed, there would be as many str_replace as URLs to be fixed, and the preg_split on top, instead of as many preg_replace's as domains configured (on a potentially large string).

For now I will just rewrite back to the original domain with mod_rewrite anyway, but leaving this thought here 👍

Cheers

@straube
Copy link
Owner Author

straube commented Oct 31, 2020

@jffaria I've been running low on free time to update the plugin. I liked your idea and I'd love if you could submit a PR. Maybe at least a starting point to review and work together. Feel free to do that if you have a chance.

In any case, I really appreciate your contribution to a possible solution!

@jffaria
Copy link
Contributor

jffaria commented Oct 31, 2020

@straube okay, since you liked the idea I will try my hand at putting this into a PR in the next few days. Thanks for the feedback!

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

3 participants