Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to replace URL #34

Open
ouun opened this issue Apr 22, 2021 · 5 comments
Open

Add function to replace URL #34

ouun opened this issue Apr 22, 2021 · 5 comments
Assignees
Milestone

Comments

@ouun
Copy link

ouun commented Apr 22, 2021

Dear KeyCDN-Team,
thank you very much for this amazing plugin. The latest updates are amazing!
However a simple helper function to replace a URL with the CDN URL would be awesome. Let's say for some reason I need to get the CDN URL of a path which does not contain one of the "CDN Inclusions". E.g. a hook for get_template_path() using the rewrite engine will not change the URL.

I have a workaround in place that adds and removes a fake JS file so that CDN Rewrite Engine processes it. However e.g. a simple get_cdn_url($url) function that uses the rewrite engine for whatever is passed. That would be awesome and replaces this:

add_filter('get_template_path', function ($url) {
            $workaround = '/workaround.js';

            // Workaround: Add & remove JS appendix to get recognized by Cache Enabler Rewrite Engine
            return str_replace($workaround, '', CDN_Enabler_Engine::rewriter($url . $workaround));
}
@coreykn
Copy link
Contributor

coreykn commented May 7, 2021

You're welcome! We're really happy to hear that. Hmm, currently the regular expression used in CDN_Enabler_Engine::rewriter() relies on an extension to ensure correct matches are made.

To get a better idea of what you're trying to accomplish, can you please share an example of the value(s) that you're wanting to rewrite? For example, the value of the $url variable.

@ouun
Copy link
Author

ouun commented May 11, 2021

Hi @coreykn,
thanks a lot for getting back to me. In my scenario I need to filter a base URL that my theme uses in JS to async load JS and CSS. There is one main.js with a loader script that refences various futher JS & CSS files. So only the base URL without filename is passed to main.js, therefore it is not recognized by CDN_Enabler_Engine::rewriter() and the theme assets are not loaded from CDN.

Example:
WP passes https://example.com/app/themes/mytheme/dist/ as base URL to main.js. In main.js the files js/1a2.js and style/2b3.css are referenced by combining the base URL and filename to https://example.com/app/themes/mytheme/dist/js/1a2.js and https://example.com/app/themes/mytheme/dist/style/1a2.css.

Both files will not get loaded from CDN as the files never go through the PHP filters as main.js is running client side.

Solution:
Lets say there is a filter to adjust the base URL https://example.com/app/themes/mytheme/dist/ to whatever is my CDN URL from Cache Enabler settings. So I just pass https://example.com/app/themes/mytheme/dist/ to CDN_Enabler_Engine::rewriter() and receive back https://myCDN.com/app/themes/mytheme/dist/.

I already had various other use cases where I just wanted to get a URL transformed into CDN URL without a file appendix. That is why I used the workaround above in which I temporary add a filename to the URL, pass it to CDN_Enabler_Engine::rewriter() to get processed and then remove the filename again from the returned value.

I hope that makes it more clear. :)

Kind regards,

Philipp

@coreykn
Copy link
Contributor

coreykn commented May 18, 2021

Thank you for the detailed reply, I really appreciate it. That definitely makes it clear. 🙂

I do believe having this capability would be beneficial and I would like to bring it to CDN Enabler. I have other tasks that are taking priority at the moment, which means I will need to come back to this at a later point of time. A separate method could easily be added to support this, or even updating CDN_Enabler_Engine::rewrite_url() itself to allow this. However, my goal is to keep all rewriting in CDN_Enabler_Engine::rewriter() due to all the handling built around this method. When I get back to it I'll have to see if that will be the best way.

In the meantime you can continue to use your workaround or come up with a custom function. For example, CDN_Enabler_Engine::$settings['cdn_hostname'] is available to replace a specific value.

@coreykn coreykn self-assigned this Jul 20, 2021
@coreykn coreykn added this to the v2.1.0 milestone Jul 20, 2021
@mattneal-stafflink
Copy link

Just wanted to bump this request, and say that I'd love to use it as well. I'm currently using the WordPress API to display posts, and I'm getting a meta field called 'property_images', and as you would expect, it returns the actual URL, and I pass that to the front end. Being able to feed that string/array to a function like @ouun has suggested would be perfect for a situation like this.

For the moment, I can simply create a function that changes the URL's before I pass them to the front end, but I would assume if we use the CDN Enabler engine, that would be a much more comprehensive way to do it. My way is quite hacky.

@mattneal-stafflink
Copy link

Actually, I'm not sure if this is 100% how it should be done, but the code I was working with can access the CDN Enabler Engine class, so I can just do this:

if( class_exists( 'CDN_Enabler_Engine' ) ) {
    $image = CDN_Enabler_Engine::rewriter( $image );
}

This works perfectly. If you can think of why this is a terrible idea, I'd love to hear it so I don't break my production sites lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants