Code Monkey home page Code Monkey logo

Comments (8)

futtta avatar futtta commented on May 23, 2024

Considering http://your.site.com/wp-content/cache/autoptimize/js/autoptimize_123.js and a base cdn url of http://cdn.site.com, we need to replace your.site.com with cdn.site.com.

get_template_directory_uri would give http://your.site.com/wp-content/themes/yourtheme/
get_stylesheet_uri would give http://your.site.com/wp-content/themes/yourtheme/style.css
get_theme_root_uri would give http://your.site.com/wp-content/themes/
wp_content_url would give http://your.site.com/wp-content/
site_url would give http://your.site.com/

so it seems to me site_url is exactly what I should be using?

frank

from autoptimize.

redconfetti avatar redconfetti commented on May 23, 2024

It looks to me that the site_url is not always the base URL as you expect, but might include a subdirectory. Wordpress allows for the content directory to be redefined, as well as many other directory types

The project that I'm currently working on is using the roots/bedrock configuration for Wordpress sites, which redefines WP_CONTENT_URL as under 'app' instead of 'wp-content', and the WP_SITEURL is defined as in a 'wp' subfolder, because it installs the Wordpress core files via Composer.

With this alternative configuration, your plugin looks for http://your.site.com/wp/ and doesn't find a match because our theme is linking to assets under http://your.site.com/app/.

In the Wordpress Codex article on Changing The Site URL it states:

The "Site Address (URL)" setting is the address you want people to type in their browser to reach your WordPress blog.
The "WordPress Address (URL)" setting is the address where your WordPress core files reside.

In the context of the Wordpress Settings -> General page, the field labelled "Site Address (URL)" corresponds to the 'home' in wp_options, while the "WordPress Address (URL)" corresponds to the 'siteurl' value.

Here is the output I get from WP-CLI when running these functions:

wp> content_url();
string(37) "http://example.com/app"
wp> home_url();
string(34) "https://example.com"
wp> site_url();
string(36) "http://example.com/wp"

To make your plugin compatible with all these different alternative URLs, especially since they can exist separately for themes, plugins, and uploads, perhaps the best solution is to abstract the base URL from the the site_url() or content_url() string (this seems more preferable).

So lets say that the JS, CSS, or HTML links to http://example.com/app/uploads/09/22/image1.jpg, the content_url() would return http://example.com/app/, and you could simply extract http://example.com and use that for the search and replace. No matter what sub-directory path is used in the URL, it will remain after the replace.

function get_base_url($url) {
  $parsed_url = parse_url($url);
  return $parsed_url['scheme'] . '://' . $parsed_url['host'];
}
$url = 'http://www.example.com/app';
echo get_base_url($url); // "http://www.example.com"

A CDN network that provides generic mirroring of assets should still find the assets if you're only replacing the domain name, and leaving whatever custom sub-directory path in URL, whether that be /wp-content/uploads/09/22/image1.jpg or something special like /app/uploads/09/22/image1.jpg or /files/uploads/09/22/image1.jpg.

from autoptimize.

futtta avatar futtta commented on May 23, 2024

I consider this somewhat of an edge case, but you are right, with a setup where your renamed wp-content is not inside the directory where the WP core files reside (app vs wp) autoptimize's CDN functionality does not work currently.

I'll see if I can get this fixed with the (parse_url'ed) content_url() :-)

from autoptimize.

futtta avatar futtta commented on May 23, 2024

OK, commited a possible fix (limiting the changes to the url_replace_cdn function, as the constant is also used elsewhere), cfr. 2c1e94a

Would be great if you could test & confirm (or deny) this fixes your problem.

from autoptimize.

redconfetti avatar redconfetti commented on May 23, 2024

Thank you @futtta. I very much appreciate the energy you put into that commit. I was really just wanting to contribute some information about the issue I had identified for the benefit of everyone that uses it.

Unfortunately I'm trying to implement a method for using a mirroring CDN with the project I'm on by a deadline (this Saturday), and right now the Autoptimize plugin is intermittently not working at all with our application. I believe it has something to do with the use of ob_start(), which might be used by other plugins at the same time to modify the buffer output, though I'm not entirely sure.

I'm instead testing out a solution in my own plugin currently that registers an origin host and cd host string in two WP options. It then uses a str_replace on the URLs that I'm wanting to target.

  public function replace_host($url, $path='') {
    if ( is_admin() ) return $url;
    return str_replace($this->origin_host, $this->mirror_host, $url);
  }

I'm then applying this function as a filter for the script and stylesheet loaders, as well as the content_url() response.

add_filter('script_loader_src', array($this, 'replace_host'));
add_filter('style_loader_src', array($this, 'replace_host'));
add_filter('content_url', array($this,'replace_host'), 10, 2);

Unfortunately the content_url filter doesn't apply to asset links embedded in the post content, unless I define WP_CONTENT_URL in a file like wp-config.php.

I'm thinking that perhaps I'll instead have to do a find and replace on the expected origin paths for uploads (i.e. /uploads/) using a filter on the_content.

from autoptimize.

futtta avatar futtta commented on May 23, 2024

Good luck with your deadline @redconfetti, hope you'll have time to test the commit out later! :-)

from autoptimize.

futtta avatar futtta commented on May 23, 2024

Have you been able to check that change out @redconfetti ?

from autoptimize.

futtta avatar futtta commented on May 23, 2024

own testing revealed no issues, considered fixed unless told otherwise ;-)

from autoptimize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.