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
223 stars 63 forks source link

[Feature Request] Convert images in background. #308

Open lwxbr opened 5 years ago

lwxbr commented 5 years ago

Well, the idea is run the conversion while you are not using Wordpress, you schedule the conversions and then, when it's over, the plugin will warn you when the conversion is finished (by email or in own admin panel.

PS.: The plugin is running quite well in a modal. Consider that issue just as an improvement.

rosell-dk commented 5 years ago

Background updates is in principle a good idea. However, the question is when to run it. Using wp-cron has its weaknesses and the minimal recurrence time is hourly

Perhaps something that runs from admin. Or perhaps even pushing javascript to the clients that keeps triggering a script that converts the next image in the queue every 30 second or so. Nah, probably too problematic.

rosell-dk commented 5 years ago

It is actually possible to have wp-cron run every 5 minutes. Alternatively, it could be implemented with set_transient as shown here. However, both of these methods activates the conversion at the time of a page request. I need to confirm this, but I'm guessing that this means that the page is not delivered until after the task has run. This is unacceptable as a conversion could take 20 seconds and we don't want to add 20 seconds to the page load!

rosell-dk commented 5 years ago

Stumbled upon spawn_cron. Interesting...

rosell-dk commented 5 years ago

Ok, actually wp_cron calls spawn_cron. wp_cron does not block the page load, but can delay it with 1 second, according to this answer. UPDATE: It turns out that the 1 second delay is no longer an issue (since 2006)

rosell-dk commented 5 years ago

A remaining question is whether to use wp_cron, do the async call ourself, or find a library for doing it. This library calls the Wordpress Request class to do the async call, which again is based on cURL - or fsockopen when cURL is unavailable. Using wp_cron and spawn_cron also ultimately ends up using the Request class (unless an alternative cron method has been set up). (spawn_cron calls wp_remote_post, which instantiates WP_Http and calls the request method, which calls Requests::request)

Stuff to read up upon: https://stackoverflow.com/questions/36171222/async-curl-request-in-php

lwxbr commented 5 years ago

Thanks, @rosell-dk, I lighted the powder and you did the rest. I'm happy that is making progress.

bbceg commented 3 weeks ago

I'm having trouble getting WP-CLI to work for me due to wp residing in a subdirectory i.e. ...web_root/wp/ , the content (and therefore uploads folder) residing outside of wp i.e. ...web_root/content/ and, in a production environment, the use of symlinked release folders with shared content (e.g. uploads) symlinked from the release folder. I believe this the bedrock approach.

Currently when running wp webp-express I get "Conversion failed. Path is outside resolved document root"

This is a long-winded way of saying that this would be a really helpful feature for me! Something that ran constantly in the background checking for all unconverted images, or even only for newly uploaded images (which would be dropped into a queue) would be great.

I wonder if Woocommerce might provide any useful pointers? It's constantly dropping things into the cron queue.

It might also be worth remembering that wp-cron can be disabled and replaced with a server-based cron job to be run with any level of frequency. Not ideal for those that don't have access to the server but perhaps another option for those that do.

I hope theses suggestions are in someway helpful but please do regard if not.

Thanks for the great plugin.