rosell-dk / webp-express

Wordpress plugin for serving autogenerated WebP images instead of jpeg/png to browsers that supports WebP
GNU General Public License v3.0
225 stars 64 forks source link

Multisite – Alter HTML produces wrong webp urls when destination structure is set to "image roots" #510

Closed Hube2 closed 2 years ago

Hube2 commented 3 years ago

I posted this on the WP support forum but I thought I would add it here as well just in case someone here can help me.

I have plugin installed on a mutlisite installation. The multisite uses sub folders for sub sites. Everything is working fine on the main site but not working on sub site.

The issue seems to be the url for the webp image.

Example: The original image url is: https://www.website.com/wp-content/uploads/sites/2/2019/01/Export-Home-Products-v2.png

The webp url is: https://www.website.com/en-uk/wp-content/webp-express/webp-images/uploads/2019/01/Export-Home-Products-v2.png.webp

Note the addition of the sub site sub folder en-uk before wp-content

Any idea how I can correct this?

Hube2 commented 3 years ago

That is not the problem. For some reason it's missing the "/sites/2/" portion of the url

Hube2 commented 3 years ago

Since the project I'm working on could not wait I have come up with a solution.

Note that this is only when you are using multisite with sub directories.

It's probably not the best solution but it works and it should not interfere with the plugin should the error be corrected... I hope.

<?php 

    add_filter('the_content', 'correct_webp_multisite_url', 999999); // priority higher than webp-express
    add_filter('the_excerpt', 'correct_webp_multisite_url', 999999);
    add_filter('post_thumbnail_html', 'correct_webp_multisite_url', 999999);
    add_filter('woocommerce_product_get_image', 'correct_webp_multisite_url', 999999);
    add_filter('get_avatar', 'correct_webp_multisite_url', 999999);
    add_filter('acf_the_content', 'correct_webp_multisite_url', 999999);

    function correct_webp_multisite_url($content) {

        // correct multisite sub directory install urls

        // get the blog id
        $blog_id = get_current_blog_id();

        if ($blog_id == 1) {
            // not needed on main site
            return $content;
        }

        // get the current site path slug returns "/{$slug}/"
        $path = get_blog_details()->path;

        // insert missing "sites/{$blog_id}" for date folders
        // looks for year immediately after "uploads/"
        $content = preg_replace(
            '#'.$path.'wp-content/webp-express/webp-images/uploads/([0-9]{4}/)#',
            $path.'wp-content/webp-express/webp-images/uploads/sites/'.$blog_id.'/\1',
            $content);

        // insert missing "sites/{$blog_id}" for uploads root for site
        // looks for no /s after "uploads/"
        $content = preg_replace(
            '#'.$path.'wp-content/webp-express/webp-images/uploads/([^/]+)$#',
            $path.'wp-content/webp-express/webp-images/uploads/sites/'.$blog_id.'/\1',
            $content);

        return $content;
    }
rosell-dk commented 2 years ago

I looked into the issue.

I can inform that the problem occurs with Alter HTML for images that are in separate webp folder when "destination-structure" is set to "Image roots".

So it for example works in these scenarios:

Possible quick fix for you: Change "destination-structure" to "Document root".

I'm working on resolving the issue

Hube2 commented 2 years ago

Took me a while to figure out what you meant by using "Mingled". There are various issues with doing this.

For example when doing backups, I don't backup the WebP folders because everything in it can be easily regenerated. I don't want to waste time or space backing up things that can be easily fixed in other ways.

The other issue has to do with the number of files in a folder. The site in question is image heavy with a lot of custom image sizes. Many people don't know this, but when a folder on a server is extremely full it can impact server performance, but that's another story.

Thanks for the fix on this.

rosell-dk commented 2 years ago

Actually, it also produced wrong url path in subdomain. Same error (missing "site/x" in path) - easy fix, as I can simply remove the check for !is_subdomain_install()

rosell-dk commented 2 years ago

I just pre-released. You can download 0.23.0 here (look for “Please select a specific version to download”).

I plan to release 0.23.0 tomorrow. @Hube2