commit 37cf714058971510f010d4808b944d53b42292f9
Author: Malin Rewrite rules were saved to the following files: Rewrite rules were removed from the following files: Failed writing rewrite rules to the following files: Failed deleting unused rewrite rules in the following files:
`
+
+becomes:
+`
`
+
+Jetpack automatically serves webp files to browsers that supports it using same mechanism as the standard WebP Express configuration: If the "Accept" header contains "image/webp", a webp is served (keeping original file extension, but setting the "content-type" header to "image/webp"), otherwise a jpg is served.
+
+As images are no longer pointed to your original server, the .htaccess rules created by WebP Express will not have any effect.
+
+So if you are using Jetpack you don't really need WebP Express?
+Well, there is no point in having the "Speed up image load times" enabled together with WebP Express.
+
+But there is a case for using WebP Express rather than Jetpacks "Speed up image load times" feature:
+
+Jetpack has the same drawback as the *Varied image responses* operation mode: If a user downloads the file, there will be a mismatch between the file extension and the image type (the file is ie called "logo.jpg", but it is really a webp image). I don't think that is a big issue, but for those who do, WebP Express might still be for you, even though you have Jetpack. And that is because WebP Express can be set up just to generate webp's, without doing the internal redirection to webp (will be possible from version 0.10.0). You can then for example use the [Cache Enabler](https://wordpress.org/plugins/cache-enabler/) plugin, which is able to generate and cache two versions of each page. One for browsers that accepts webp and one for those that don't. In the HTML for webp-enabled browsers, the images points directly to the webp files.
+
+Pro Jetpack:
+- It is a free CDN which serves webp out of the box.
+- It optimizes jpegs and pngs as well (but note that only about 1 out of 5 users gets these, as webp is widely supported now)
+
+Con Jetpack:
+- It is a big plugin to install if you are only after the CDN
+- It requires that you create an account on Wordpress.com
+
+Pro WebP Express:
+- You have control over quality and metadata
+- It is a small plugin and care has been taken to add only very little overhead
+- Plays well together with Cache Enabler. By not redirecting jpg to webp, there is no need to do any special configuration on the CDN and no issue with misleading file extension, if user downloads a file.
+
+Con WebP Express:
+- If you are using a CDN and you are redirecting jpg to webp, you must configure the CDN to forward the Accept header. It is not possible on all CDNs.
+
+### Why do I not see the option to set WebP quality to auto?
+The option will only display, if your system is able to detect jpeg qualities. To make your server capable to do that, install *Imagick extension* (PECL >= 2.2.2) or enable exec() calls and install either *Imagick* or *Gmagick*.
+
+If you have the *Imagick*, the *Imagick binary* or the *Remote WebP Express* conversion method working, but don't have the global "auto" option, you will have the auto option available in options of the individual converter.
+
+Note: If you experience that the general auto option doesn't show, even though the above-mentioned requirements should be in order, check out [this support-thread](https://wordpress.org/support/topic/still-no-auto-option/).
+
+### How do I configure my CDN ("Varied image responses" mode)?
+In *Varied image responses* operation mode, the image responses *varies* depending on whether the browser supports webp or not (which browsers signals in the *Accept* header). Some CDN's support this out of the box, others requires some configuration and others doesn't support it at all.
+
+For a CDN to cooperate, it needs to
+1) forward the *Accept* header and
+2) Honour the Vary:Accept response header.
+
+You can also make it "work" on some CDN's by bypassing cache for images. But I rather suggest that you try out the *CDN friendly* mode (see next FAQ item)
+
+#### Status of some CDN's
+
+- *KeyCDN*: Does not support varied image responses. I have added a feature request [here](https://community.keycdn.com/t/support-vary-accept-header-for-conditional-webp/1864). You can give it a +1 if you like!
+- *Cloudflare*: See the "I am on Cloudflare" item
+- *Cloudfront*: Works, but needs to be configured to forward the accept header. Go to *Distribution settings*, find the *Behavior tab*, select the Behavior and click the Edit button. Choose *Whitelist* from *Forward Headers* and then add the "Accept" header to the whitelist.
+
+I shall add more to the list. You are welcome to help out [here](https://wordpress.org/support/topic/which-cdns-works-in-standard-mode/).
+
+### How do I make it work with CDN? ("CDN friendly" mode)
+In *CDN friendly* mode, there is no trickery with varied image responses, so no special attention is required *on the CDN*.
+
+However, there are other pitfalls.
+
+The thing is that, unless you have the whole site on a CDN, you are probably using a plugin that *alters the HTML* in order to point your static assets to the CDN. If you have enabled the "Alter HTML" in WebP Express, it means that you now have *two alterations* on the image URLs!
+
+How will that play out?
+
+Well, if *WebP Express* gets to alter the HTML *after* the image URLs have been altered to point to a CDN, we have trouble. WebP Express does not alter external images but the URLs are now external.
+
+However, if *WebP Express* gets to alter the HTML *before* the other plugin, things will work fine.
+
+So it is important that *WebP Express* gets there first.
+
+*The good news is that WebP Express does get there first on all the plugins I have tested.*
+
+But what can you do if it doesn't?
+
+Firstly, you have an option in WebP Express to select between:
+1. Use content filtering hooks (the_content, the_excerpt, etc)
+2. The complete page (using output buffering)
+
+The content filtering hooks gets to process the content before output buffering does. So in case output buffering isn't early enough for you, choose the content filtering hooks.
+
+There is a risk that you CDN plugin also uses content filtering hooks. I haven't encountered any though. But if there is any out there that does, chances are that they get to process the content before WebP Express, because I have set the priority of these hooks quite high (10000). The reasoning behind this is to that we want to replace images that might be inserted using the same hook (for example, a theme might use *the_content* filter to insert the featured image). If you do encounter a plugin for changing URLs for CDN which uses the content filtering hooks, you are currently out of luck. Let me know, so I can fix that (ie. by making the priority configurable)
+
+Here are a list of some plugins for CDN and when they process the HTML:
+
+| Plugin | Method | Hook(s) | Priority
+| ----------------- | ------------------ | ------------------------------------------------ | ---------------
+| BunnyCDN | Output buffering | template_redirect | default (10)
+| CDN enabler | Output buffering | template_redirect | default (10)
+| Jetpack | content filtering | the_content, etc | the_content: 10
+| W3 Total Cache | Output buffering | no hooks. Buffering is started before hooks |
+| WP Fastest Cache | Output buffering | no hooks. Buffering is started before hooks |
+| WP Super Cache | Output buffering | init | default (10)
+
+
+With output buffering the plugin that starts the output buffering first gets to process the output last. So WebP Express starts as late as possible, which is on the `template_redirect` hook, with priority 10000 (higher means later). This is later than the `init` hook, which is again later than the `no hooks`.
+
+
+### I am on Cloudflare
+Without configuration, Cloudflare will not maintain separate caches for jpegs and webp; all browsers will get jpeg. To make Cloudflare cache not only by URL, but also by header, you need to use the [Custom Cache Key](https://support.cloudflare.com/hc/en-us/articles/115004290387) page rule, and add *Header content* to make separate caches depending on the *Accept* request header.
+
+However, the *Custom Cache Key* rule currently requires an *Enterprise* account. And if you already have that, you may as well go with the *Polish* feature, which starts at the “Pro” level plan. With the *Polish* feature, you will not need WebP Express.
+
+To make *WebP Express* work on a free Cloudflare account, you have the following choices:
+
+1. You can configure the CDN not to cache jpeg images by adding the following page rule: If rule matches: `example.com/*.jpg`, set: *Cache level* to: *Bypass*
+
+2. You can set up another CDN (on another provider), which you just use for handling the images. You need to configure that CDN to forward the *Accept header*. You also need to install a Wordpress plugin that points images to that CDN.
+
+3. You can switch operation mode to "CDN friendly" and use HTML altering.
+
+### I am on WP Engine
+From version 0.17.1 on WebP Express works with WP engine and this combination will be tested before each release.
+
+You can use the plugin both in "Varied image responses" mode and in "CDN friendly mode".
+
+To make the redirection work, you must:
+1) Grab the nginx configuration found in the "I am on Nginx/OpenResty" section in this FAQ. Use the "try_files" variant.
+2) Contact help center and ask them to insert that configuration.
+3) Make sure the settings match this configuration. Follow the "beware" statements in the "I am on Nginx/OpenResty" section.
+
+WebP Express tweaks the workings of "Redirect to converter" a bit for WP engine. That PHP script usually serves the webp directly, along with a Vary:Accept header. This header is however overwritten by the caching machinery on WP engine. As a work-around, I modified the response of the script for WP engine. Instead of serving the image, it serves a redirect to itself. As there now IS a corresponding webp, this repeated request will not be redirected to the PHP script, but directly to the webp. And headers are OK for those redirects. You can hit the "Live test" button next to "Enable redirection to converter?" to verify that this works as just described.
+
+If you (contrary to this headline!) are in fact not on WP Engine, but might want to be, I have an affiliate link for you. It will give you 3 months free and it will give me a reward too, if you should decide to stay there. Here you go: [Get 3 months free when you sign up for WP Engine.](https://shareasale.com/r.cfm?b=1343154&u=2194916&m=41388&urllink=&afftrack=)
+
+### WebP Express / ShortPixel setup
+Here is a recipe for using WebP Express together with ShortPixel, such that WebP Express generates the webp's, and ShortPixel only is used to create `
`, while in a non-webp enabled browser, it looks like this: `
`
+
+
+*6. Optionally add Cache Enabler rewrite rules in your .htaccess*
+*Cache Enabler* provides some rewrite rules that redirects to the cached file directly in the `.htaccess`, bypassing PHP entirely. Their plugin doesn't do that for you, so you will have to do it manually in order to get the best performance. The rules are in the "Advanced configuration" section on [this page](https://www.keycdn.com/support/wordpress-cache-enabler-plugin).
+
+
+### Does it work with lazy loaded images?
+No plugins/frameworks has yet been discovered, which does not work with *WebP Express*.
+
+The most common way of lazy-loading is by setting a *data-src* attribute on the image and let javascript use that value for setting the *src* attribute. That method works, as the image request, seen from the server side, is indistinguishable from any other image request. It could however be that some obscure lazy load implementation would load the image with an XHR request. In that case, the *Accept* header will not contain 'image/webp', but '*/*', and a jpeg will be served, even though the browser supports webp.
+
+The following lazy load plugins/frameworks has been tested and works with *WebP Express*:
+- [BJ Lazy Load](https://da.wordpress.org/plugins/bj-lazy-load/)
+- [Owl Carousel 2](https://owlcarousel2.github.io/OwlCarousel2/)
+- [Lazy Load by WP Rocket](https://wordpress.org/plugins/rocket-lazy-load/)
+
+I have only tested the above in *Varied image responses* mode, but it should also work in *CDN friendly* mode. Both *Alter HTML* options have been designed to work with standard lazy load attributes.
+
+### Can I make an exceptions for some images?
+There can be instances where you actually need to serve a jpeg or png. For example if you are demonstrating how a jpeg looks using some compression settings.
+
+If you want an image to be served in the original format (jpeg og png), do one of the following things:
+- Add "?original" to the image url.
+- Place an empty file in the same folder as the jpeg/png. The file name must be the same as the jpeg/png with ".do-not-convert" appended
+
+Doing this will bypass redirection to webp and also prevent Alter HTML to use the webp instead of the original.
+
+*Bypassing for an entire folder*
+To bypass redirection for an entire folder, you can put something like this into your index `.htaccess`:
+```
+RewriteRule ^wp-content/uploads/2021/06/ - [L]
+```
+PS: If *WebP Express* has placed rules in that .htaccess, you need to place the rule *above* the rules inserted by *WebP Express*
+
+If you got any further questions, look at, or comment on [this topic](https://wordpress.org/support/topic/can-i-make-an-exception-for-specific-post-image/)
+
+### Alter HTML only replaces some of the images
+
+If you are wondering why Alter HTML are missing some images, it can be due to one of the following reasons:
+
+- WebP Express doesn't convert external images, only images on your server. Alter HTML will therefore not alter URLS that unless they point to your server or a CDN that you have added in the *CDN hostnames* section
+- WebP Express has a "Scope" option, which for example can be set to "Uploads and themes". Only images that resides within the selected scope are replaced with webp.
+- If you have selected ``-tags will be replaced (CSS images will not be replaced). Additionally, the `
`-tag must have a "src" attribute or a commonly used data attribute for lazyloading (such as “data-src” or “data-lazy-src”)
+- If you have set the "How to replace" option to "Use content filtering hooks", images inserted with some third party plugins/themes might not be replaced. To overcome that, change that setting to "The complete page".
+- The image might have been inserted with javascript. WebP Express doesn't changSome plugins might insert the
+
+### Update failed and cannot reinstall
+The 0.17.0 release contained binaries with dots in their filenames, which caused the unpacking during update to fail on a few systems. This failure could leave an incomplete installation. With important files missing - such as the main plugin file - Wordpress no longer registers that the plugin is there (it is missing from the list). However, the folder is there in the file system and trying to install WebP Express again fails because Wordpress complains about just that. The solution is to remove the "webp-express" folder in "plugins" manually (via ftp or a plugin, such as File Manager) and then install WebP Express anew. The setting will be intact. The filenames that caused the trouble where fixed in 0.17.2.
+
+### When is feature X coming? / Roadmap
+No schedule. I move forward as time allows. I currently spend a lot of time answering questions in the support forum. If someone would be nice and help out answering questions here, it would allow me to spend that time developing. Also, donations would allow me to turn down some of the more boring requests from my customers, and speed things up here.
+
+Right now I am focusing on the File Manager. I would like to add possibility for converting, bulk converting, viewing conversion logs, viewing stats, etc.
+
+Here are other things in pipeline:
+- Excluding certain files and folders.
+- Supporting Save-Data header in Varied Image Responses mode (send extra compressed images to clients who wants to use as little bandwidth as possible).
+- Displaying rules for NGINX.
+- Allow webp for all browsers using [this javascript library](http://libwebpjs.hohenlimburg.org/v0.6.0/). Unfortunately, the javascript library does not (currently) support srcset attributes, which is why I moved this item down the priority list. We need srcset to be supported for the feature to be useful.
+
+The current milestones, their subtasks and their progress can be viewed here: https://github.com/rosell-dk/webp-express/milestones
+
+If you wish to affect priorities, it is certainly possible. You can try to argue your case in the forum or you can simply let the money do the talking. By donating as little as a cup of coffee on [ko-fi.com/rosell](https://ko-fi.com/rosell), you can leave a wish. I shall take these wishes into account when prioritizing between new features.
+
+### Beta testing
+I generally create a pre-release before publishing. If you [follow me on ko-fi](https://ko-fi.com/rosell), you will get notified when a pre-release is available. I generally create a pre-release on fridays and mark it as stable on mondays. In order to download a pre-release, go to [the advanced page](https://wordpress.org/plugins/webp-express/advanced/) and scroll down to "Please select a specific version to download". I don't name the pre-releases different. You will just see the next version here before it is available the usual way.
+
+## Changes in 0.21.1
+*(released: 27 Oct 2021)*
+* Bugfix: File manager could not handle many images. It now loads tree branches on need basis instead of the complete tree in one go
+* Bugfix: For mulisite, the redirect after settings save was not working (bug introduced in 0.21.0)
+
+For more info, see the closed issues on the [webp-express 0.21.1 milestone](https://github.com/rosell-dk/webp-express/milestone/42?closed=1)
+
+## Changes in 0.21.0
+*(released: 25 Oct 2021)*
+* Added image browser (in Media tab)
+* Updated webp convert library to 2.7.0.
+* ImageMagick now supports the "near-lossless" option (provided Imagick >= 7.0.10-54)
+* Added "try-common-system-paths" option for ImageMagick (default: true). Thanks to Henrik Alves for adding this option.
+* Bugfix: Handling Uncaught Fatal Exception during .htaccess read failure. Thanks to Manuel D'Orso from Italy for the fix.
+* Bugfix: File names which were not UTF8 caused trouble in the Bulk Convert. Thank to "mills4078" for the fix
+* Bugfix: Redirection back to settings after saving settings failed on some systems. Thanks to Martin Rehberger (@kingkero) from Germany for the fix.
+* Bugfix: Webp urls did not contain port number (only relevant when the website was not on default port number). Thanks to Nicolas LIENART (@nicolnt) from France for providing the fix.
+
+For more info, see the closed issues on the [webp-express 0.21.0 milestone](https://github.com/rosell-dk/webp-express/milestone/41?closed=1) and the
+[webp-convert 2.7.0 milestone](https://github.com/rosell-dk/webp-convert/milestone/24?closed=1)
+
+
+## Changes in 0.20.1
+*(released: 20 Jun 2021)*
+* Bugfix: Removed composer.lock. It was locked on PHP 7.2, which caused server error on some sites.
+
+## Changes in 0.20.0
+*(released: 17 Jun 2021)*
+* Added WP CLI support. Add "wp webp-express convert" to crontab for nightly conversions of new images! Thanks to Isuru Sampath Ratnayake from Sri Lanka for initializing this.
+* Added "sharp-yuv" (not as option, but as always on). Better YUV->RGB color conversion at almost no price. [Read more here](https://www.ctrl.blog/entry/webp-sharp-yuv.html). Supported by cwebp, vips, gmagick, graphicsmagick, imagick and imagemagick
+* Bumped cwebp binaries to 1.2.0
+* cwebp now only validates hash of supplied precompiled binaries when necessary. This cuts down conversion time.
+* Convert on upload now defaults to false, as it may impact upload experience in themes with many formats.
+* bugfix: Alpha quality was saved incorrectly for PNG. Thanks to Chriss Gibbs from the UK for finding and fixing this.
+* bugfix: wp-debug log could be flooded with "Undefined index: HTTP_ACCEPT". Thanks to @markusreis for finding and fixing this.
+
+## Changes in 0.19.0
+*(released: 13 Nov 2020)*
+* New convertion method: ffmpeg
+* Fixed problem in Bulk Convert when files had special characters in their filename
+* Prevented problems if the plugin gets included twice (can anybody enlighten me on how this might happen?)
+
+For more info, see the closed issues on the [0.19.0 milestone on the github repository](https://github.com/rosell-dk/webp-express/milestone/36?closed=1)
+
+## Changes in 0.18.3
+*(released: 5 Nov 2020)*
+* Bugfix: WebP Express uses live tests to determine the capabilities of the server in respect to .htaccess files (using the htaccess-capability-tester library). The results are used for warnings and also for optimizing the rules in the .htaccess files. However, HTTP Requests can fail due to other reasons than the feature not working (ie timeout). Such failures should lead to an indeterminate result, but it was interpreted as if the feature was not working.
+* The Live test now displays a bit more information if the HTTP request failed.
+* Changed default value for "destination structure" to "Image roots", as "Document root" doesn't work on hosts that have defined DOCUMENT_ROOT in an unusual way.
+* Added possibility to change "%{DOCUMENT_ROOT}" part of RewriteCond by adding a line to wp-config.php. THIS IS A BETA FEATURE AND MIGHT BE REVOKED IF NOBODY ACTUALLY NEEDS IT.
+* Got rid of PHP notice Constant WEBPEXPRESS_MIGRATION_VERSION already defined
+* Fixed donation link. It now points to https://ko-fi.com/rosell again
+
+For more info, see the closed issues on the 0.18.3 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/34?closed=1
+
+## Supporting WebP Express
+Bread on the table don't come for free, even though this plugin does, and always will. I enjoy developing this, and supporting you guys, but I kind of need the bread too. Please make it possible for me to continue putting effort into this plugin:
+
+- [Buy me a coffee](https://ko-fi.com/rosell)
+- [Buy me coffee on a regular basis](https://github.com/sponsors/rosell-dk) and help ensuring my coffee supplies doesn't run dry.
+
+
+**Persons / Companies currently backing the project via patreon - Thanks!**
+
+- Max Kreminsky
+- Nodeflame
+- [Mathieu Gollain-Dupont](https://www.linkedin.com/in/mathieu-gollain-dupont-9938a4a/)
+- Ruben Solvang
+
+**Persons who contributed with coffee within the last 30 days:**
+
+| Name | Date | Message |
+| ---------------------- | -------------- | ------------ |
+| Anon | 2020-08-18 | - |
+| Eder Ribeiro | 2020-08-08 | Hello Bjørn I believe that it is a fantastic solution and that it deserves maximum support to keep getting better! If you can, check out my configuration tip, https://github.com/rosell-dk/webp-express/issues/433 |
+| Christian | 2020-08-05 | Merci pour votre plugin. Exceptionnel et gratuit. |
+
+
+**Persons who contributed with extra generously amounts of coffee / lifetime backing (>50$) - thanks!:**
+
+| Name | Amount | Message |
+| ---------------------- | -----------| --------- |
+| Justin - BigScoots | $105 | Such an amazing plugin for so many reasons, thank you! |
+| Sebastian | $99 | WebP for Wordpress – what a great plugin! Thank you! |
+| Tammy Lee | $90 | |
+| Max Kreminsky | $65 | |
+| Steven Sullivan | $51 | Thank you for such a wonderful plugin. |
diff --git a/README.txt b/README.txt
new file mode 100755
index 0000000..c0b2652
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,1081 @@
+=== WebP Express ===
+Contributors: rosell.dk
+Donate link: https://ko-fi.com/rosell
+Tags: webp, images, performance
+Requires at least: 4.0
+Tested up to: 6.5
+Stable tag: 0.25.9
+Requires PHP: 5.6
+License: GPLv3
+License URI: https://www.gnu.org/licenses/gpl-3.0.html
+
+Serve autogenerated WebP images instead of jpeg/png to browsers that supports WebP.
+
+== Description ==
+
+More than 9 out of 10 users are using a browser that is able to display webp images. Yet, on most websites, they are served jpeg images, which are typically double the size of webp images for a given quality. What a waste of bandwidth! This plugin was created to help remedy that situation. With little effort, Wordpress admins can have their site serving autogenerated webp images to browsers that supports it, while still serving jpeg and png files to browsers that does not support webp.
+
+### The image converter
+The plugin uses the [WebP Convert](https://github.com/rosell-dk/webp-convert) library to convert images to webp. *WebP Convert* is able to convert images using multiple methods. There are the "local" conversion methods: `imagick`, `cwebp`, `vips`, `gd`. If none of these works on your host, there are the cloud alternatives: `ewww` (paid) or connecting to a Wordpress site where you got WebP Express installed and you enabled the "web service" functionality.
+
+### The "Serving webp to browsers that supports it" part.
+
+The plugin supports different ways of delivering webps to browsers that supports it:
+
+1. By routing jpeg/png images to the corresponding webp - or to the image converter if the image hasn't been converted yet.
+2. By altering the HTML, replacing image tags with *picture* tags. Missing webps are auto generated upon visit.
+3. By altering the HTML, replacing image URLs so all points to webp. The replacements only being made for browsers that supports webp. Again, missing webps are auto generated upon visit.
+4. In combination with *Cache Enabler*, the same as above can be achieved, but with page caching.
+5. You can also deliver webp to *all* browsers and add the [webpjs](http://webpjs.appspot.com) javascript, which provides webp support for browsers that doesn't support webp natively. However, beware that the javascript doesn't support srcset attributes, which is why I haven't added that method to the plugin (yet).
+
+The plugin implements the "WebP On Demand" solution described [here](https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/webp-on-demand/webp-on-demand.md) and builds on a bunch of open source libraries (all maintained by me):
+- [WebP Convert](https://github.com/rosell-dk/webp-convert): For converting images to webp
+- [WebP Convert Cloud Service](https://github.com/rosell-dk/webp-convert-cloud-service): For the Web Service functionality
+- [DOM Util for WebP](https://github.com/rosell-dk/dom-util-for-webp): For the Alter HTML functionality
+- [Image MimeType Guesser](https://github.com/rosell-dk/image-mime-type-guesser): For detecting mime types of images.
+- [HTAccess Capability Tester](https://github.com/rosell-dk/htaccess-capability-tester): For testing .htaccess capabilities in a given directory, using live tests
+- [WebP Convert File Manager](https://github.com/rosell-dk/webp-convert-filemanager): For browsing conversions and triggering conversions.
+- [Exec With Fallback](https://github.com/rosell-dk/exec-with-fallback): For emulating exec() on systems where it is disabled (using proc_open(), passthru() or similar alternatives).
+
+### Benefits
+- Much faster load time for images in browsers that supports webp. The converted images are typically *less than half the size* (for jpeg), while maintaining the same quality. Bear in mind that for most web sites, images are responsible for the largest part of the waiting time.
+- Better user experience (whether performance goes from terrible to bad, or from good to impressive, it is a benefit).
+- Better ranking in Google searches (performance is taken into account by Google).
+- Less bandwidth consumption - makes a huge difference in the parts of the world where the internet is slow and costly (you know, ~80% of the world population lives under these circumstances).
+- Currently ~97% of all traffic are done with browsers supporting webp.
+- It's great for the environment too! Reducing network traffic reduces electricity consumption which reduces CO2 emissions.
+
+== Installation ==
+
+1. Upload the plugin files to the `/wp-content/plugins/webp-express` directory, or install the plugin through the WordPress plugins screen directly.
+2. Activate the plugin through the 'Plugins' screen in WordPress
+3. Configure it (the plugin doesn't do anything until configured)
+4. Verify that it works
+5. (Optional) Bulk convert all images, either in the admin ui or using WP CLI (command: "webp-express")
+
+### Configuring
+You configure the plugin in *Settings > WebP Express*.
+
+#### Operation modes
+As sort of a main switch, you can choose between the following modes of operation:
+
+*Varied image responses*:
+WebP Express creates redirection rules for images, such that a request for a jpeg will result in a webp – but only if the request comes from a webp-enabled browser. If a webp already exists, it is served immediately. Otherwise it is converted and then served. Note that not all CDN's handles varied responses well.
+
+*CDN friendly*:
+In "CDN friendly" mode, a jpeg is always served as a jpeg. Instead of varying the image response, WebP Express alters the HTML for webp usage.
+
+*Just redirect*:
+In "just redirect" mode, WebP Express is used just for redirecting jpeg and pngs to existing webp images in the same folder. So in this mode, WebP express will not do any converting. It may be that you use another plugin for that, or that you converted the images off-line and uploaded them manually.
+
+*Tweaked*:
+Here you have all options available.
+
+#### Conversion methods
+WebP Express has a bunch of methods available for converting images: Executing cwebp binary, Gd extension, Imagick extension, ewww cloud converter and remote WebP express. Each requires *something*. In many cases, one of the conversion methods will be available. You can quickly identify which converters are working - there is a green icon next to them. Hovering conversion methods that are not working will show you what is wrong.
+
+In case no conversion methods are working out of the box, you have several options:
+- You can install this plugin on another website, which supports a local conversion method and connect to that using the "Remote WebP Express" conversion method
+- You can [purchase a key](https://ewww.io/plans/) for the ewww cloud converter. They do not charge credits for webp conversions, so all you ever have to pay is the one dollar start-up fee :)
+- You can set up [webp-convert-cloud-service](https://github.com/rosell-dk/webp-convert-cloud-service) on another server and connect to that. Its open source.
+- You can try to meet the server requirements of cwebp, gd, imagick or gmagick. Check out [this wiki page](https://github.com/rosell-dk/webp-convert/wiki/Meeting-the-requirements-of-the-converters) on how to do that
+
+### Quality detection of jpegs
+If your server has Imagick extension or is able to execute imagemagick binary, the plugin will be able to detect the quality of a jpeg, and use that quality for the converted webp. You can tell if the quality detection is available by hovering the help icon in Conversion > Jpeg options > Quality for lossy. The last line in that help text tells you.
+
+This auto quality has benefits over fixed quality as it ensures that each conversion are converted with an appropriate quality. Encoding low quality jpegs to high quality webps does not magically increase the visual quality so that your webp looks better than the original. But it does result in a much larger filesize than if the jpeg where converting to a webp with the same quality setting as the original.
+
+If you do not have quality detection working, you can try one of the following:
+- Install Imagick on the server (for this purpose, it is not required that it is compiled with WebP support)
+- Install imagemagick on the server and grant permission for PHP to use the "exec" function.
+- Use "Remote WebP Express" converter to connect to a site, that *does* have quality detection working
+- If you have cwebp converter available, you can configure it to aim for a certain reduction, rather than using the quality parameter. Set this to for example 50%, or even 45%.
+
+### Verifying that it works (in "Varied image responses" mode)
+1. Make sure at least one of the conversion methods are working. It should have a green checkmark next to it.
+2. If you haven't saved yet, click "Save settings". This will put redirection rules into .htaccess files in the relevant directories (typically in uploads, themes and wp-content/webp-express/webp-images, depending on the "Scope" setting)
+3. I assume that you checked at least one of the two first checkboxes in the .htaccess rules section. Otherwise you aren't using "varied responses", and then the "CDN friendly" mode will be more appropriate.
+4. Click the "Live test" buttons to see that the enabled rules actually are working. If they are not, it *could* be that the server needs a little time to recognize the changed rules.
+
+The live tests are quite thorough and I recommend them over a manual test. However, it doesn't hurt to do a manual inspection too.
+
+*Doing a manual inspection*
+
+Note that when WebP Express is serving varied image responses, the image URLs *still points to the jpg/png*. If the URL is visited using a browser that supports webp, however, the response will be a webp image. So there is a mismatch between the file extension (the filename ends with "jpg" or "png") and the file type. But luckily, the browser does not rely on the extension to determine the file type, it only looks at the Content-Type response header.
+
+To verify that the plugin is working (without clicking the test button), do the following:
+
+- Open the page in a browser that supports webp, ie Google Chrome
+- Right-click the page and choose "Inspect"
+- Click the "Network" tab
+- Reload the page
+- Find a jpeg or png image in the list. In the "type" column, it should say "webp"
+
+You can also look at the headers. When WebP Express has redirected to an existing webp, there will be a "X-WebP-Express" header with the following value: "Redirected directly to existing webp". If there isn't (and you have checked "Enable redirection to converter"), you should see a "WebP-Convert-Log" header (WebP-Express uses the [WebP Convert](https://github.com/rosell-dk/webp-convert) for conversions).
+
+### Notes
+
+*Note:*
+The redirect rules created in *.htaccess* are pointing to a PHP script. If you happen to change the url path of your plugins, the rules will have to be updated. The *.htaccess* also passes the path to wp-content (relative to document root) to the script, so the script knows where to find its configuration and where to store converted images. So again, if you move the wp-content folder, or perhaps moves Wordpress to a subfolder, the rules will have to be updated. As moving these things around is a rare situation, WebP Express are not using any resources monitoring this. However, it will do the check when you visit the settings page.
+
+*Note:*
+Do not simply remove the plugin without deactivating it first. Deactivation takes care of removing the rules in the *.htaccess* file. With the rules there, but converter gone, your Google Chrome visitors will not see any jpeg images.
+
+### Bulk convert
+You can start a bulk conversion two ways:
+1. In the admin UI. On the settings screen, there is a "Bulk Convert" button
+2. By using WP CLI (command: "webp-express").
+
+I'm currently working on a file manager interface, which will become a third way.
+
+### Making sure new images becomes converted
+There are several ways:
+1. Enable redirection to converter in the *.htaccess rules* section.
+2. Enable "Convert on upload". Note that this may impact upload experience in themes which defines many formats.
+3. Set up a cron job, which executes `wp webp-express convert` regularily
+
+### WP CLI command
+WebP Express currently supports commands for converting and flushing webp images throug the CLI. You can use the --help option to learn about the options:
+*wp webp-express --help*. Displays the available commands
+*wp webp-express convert --help*. Displays the available options for the "convert" command.
+
+A few examples:
+*wp webp-express convert*: Creates webp images for all unconverted images
+*wp webp-express convert --reconvert*: Also convert images that are already converted
+*wp webp-express convert themes*: Only images in the themes folder
+*wp webp-express convert uploads/2021*: Only images in the "2021" folder inside the uploads folder
+*wp webp-express convert --only-png*: Only the PNG images
+*wp webp-express convert --quality=50*: Use quality 50 (instead of what was entered in settings screen)
+*wp webp-express convert --converter=cwebp*: Specifically use cwebp converter.
+
+*wp webp-express flushwebp*: Remove all webp images
+*wp webp-express flushwebp --only-png*: Remove all webp images that are conversions of PNG images
+
+Synopsises:
+`
+wp webp-express convert [
`
+
+becomes:
+`
`
+
+Jetpack automatically serves webp files to browsers that supports it using same mechanism as the standard WebP Express configuration: If the "Accept" header contains "image/webp", a webp is served (keeping original file extension, but setting the "content-type" header to "image/webp"), otherwise a jpg is served.
+
+As images are no longer pointed to your original server, the .htaccess rules created by WebP Express will not have any effect.
+
+So if you are using Jetpack you don't really need WebP Express?
+Well, there is no point in having the "Speed up image load times" enabled together with WebP Express.
+
+But there is a case for using WebP Express rather than Jetpacks "Speed up image load times" feature:
+
+Jetpack has the same drawback as the *standard* WebP Express configuration: If a user downloads the file, there will be a mismatch between the file extension and the image type (the file is ie called "logo.jpg", but it is really a webp image). I don't think that is a big issue, but for those who do, WebP Express might still be for you, even though you have Jetpack. And that is because WebP Express can be set up just to generate webp's, without doing the internal redirection to webp (will be possible from version 0.10.0). You can then for example use the [Cache Enabler](https://wordpress.org/plugins/cache-enabler/) plugin, which is able to generate and cache two versions of each page. One for browsers that accepts webp and one for those that don't. In the HTML for webp-enabled browsers, the images points directly to the webp files.
+
+Pro Jetpack:
+- It is a free CDN which serves webp out of the box.
+- It optimizes jpegs and pngs as well (but note that only about 1 out of 5 users gets these, as webp is widely supported now)
+
+Con Jetpack:
+- It is a big plugin to install if you are only after the CDN
+- It requires that you create an account on Wordpress.com
+
+Pro WebP Express:
+- You have control over quality and metadata
+- It is a small plugin and care has been taken to add only very little overhead
+- Plays well together with Cache Enabler. By not redirecting jpg to webp, there is no need to do any special configuration on the CDN and no issue with misleading file extension, if user downloads a file.
+
+Con WebP Express:
+- If you are using a CDN and you are redirecting jpg to webp, you must configure the CDN to forward the Accept header. It is not possible on all CDNs.
+
+= Why do I not see the option to set WebP quality to auto? =
+The option will only display, if your system is able to detect jpeg qualities. To make your server capable to do that, install *Imagick extension* (PECL >= 2.2.2) or enable exec() calls and install either *Imagick* or *Gmagick*.
+
+If you have the *Imagick*, the *Imagick binary* or the *Remote WebP Express* conversion method working, but don't have the global "auto" option, you will have the auto option available in options of the individual converter.
+
+Note: If you experience that the general auto option doesn't show, even though the above-mentioned requirements should be in order, check out [this support-thread](https://wordpress.org/support/topic/still-no-auto-option/).
+
+= How do I configure my CDN in "Varied image responses" operation mode? =
+In *Varied image responses* operation mode, the image responses *varies* depending on whether the browser supports webp or not (which browsers signals in the *Accept* header). Some CDN's support this out of the box, others requires some configuration and others doesn't support it at all.
+
+For a CDN to cooperate, it needs to
+1) forward the *Accept* header and
+2) Honour the Vary:Accept response header.
+
+You can also make it "work" on some CDN's by bypassing cache for images. But I rather suggest that you try out the *CDN friendly* mode (see next FAQ item)
+
+*Status of some CDN's*:
+
+- *KeyCDN*: Does not support varied image responses. I have added a feature request [here](https://community.keycdn.com/t/support-vary-accept-header-for-conditional-webp/1864). You can give it a +1 if you like!
+- *Cloudflare*: See the "I am on Cloudflare" section
+- *Cloudfront*: Works, but needs to be configured to forward the accept header. Go to *Distribution settings*, find the *Behavior tab*, select the Behavior and click the Edit button. Choose *Whitelist* from *Forward Headers* and then add the "Accept" header to the whitelist.
+
+I shall add more to the list. You are welcome to help out [here](https://wordpress.org/support/topic/which-cdns-works-in-standard-mode/).
+
+### How do I make it work with CDN? ("CDN friendly" mode)
+In *CDN friendly* mode, there is no trickery with varied image responses, so no special attention is required *on the CDN*.
+
+However, there are other pitfalls.
+
+The thing is that, unless you have the whole site on a CDN, you are probably using a plugin that *alters the HTML* in order to point your static assets to the CDN. If you have enabled the "Alter HTML" in WebP Express, it means that you now have *two alterations* on the image URLs!
+
+How will that play out?
+
+Well, if *WebP Express* gets to alter the HTML *after* the image URLs have been altered to point to a CDN, we have trouble. WebP Express does not alter external images but the URLs are now external.
+
+However, if *WebP Express* gets to alter the HTML *before* the other plugin, things will work fine.
+
+So it is important that *WebP Express* gets there first.
+
+*The good news is that WebP Express does get there first on all the plugins I have tested.*
+
+But what can you do if it doesn't?
+
+Firstly, you have an option in WebP Express to select between:
+1. Use content filtering hooks (the_content, the_excerpt, etc)
+2. The complete page (using output buffering)
+
+The content filtering hooks gets to process the content before output buffering does. So in case output buffering isn't early enough for you, choose the content filtering hooks.
+
+There is a risk that you CDN plugin also uses content filtering hooks. I haven't encountered any though. But if there is any out there that does, chances are that they get to process the content before WebP Express, because I have set the priority of these hooks quite high (10000). The reasoning behind this is to that we want to replace images that might be inserted using the same hook (for example, a theme might use *the_content* filter to insert the featured image). If you do encounter a plugin for changing URLs for CDN which uses the content filtering hooks, you are currently out of luck. Let me know, so I can fix that (ie. by making the priority configurable)
+
+Here are a list of some plugins for CDN and when they process the HTML:
+
+| Plugin | Method | Hook(s) | Priority
+| ----------------- | ------------------ | ------------------------------------------------ | ---------------
+| BunnyCDN | Output buffering | template_redirect | default (10)
+| CDN enabler | Output buffering | template_redirect | default (10)
+| Jetpack | content filtering | the_content, etc | the_content: 10
+| W3 Total Cache | Output buffering | no hooks. Buffering is started before hooks |
+| WP Fastest Cache | Output buffering | no hooks. Buffering is started before hooks |
+| WP Super Cache | Output buffering | init | default (10)
+
+With output buffering the plugin that starts the output buffering first gets to process the output last. So WebP Express starts as late as possible, which is on the `template_redirect` hook, with priority 10000 (higher means later). This is later than the `init` hook, which is again later than the `no hooks`.
+
+= I am on Cloudflare =
+Without configuration, Cloudflare will not maintain separate caches for jpegs and webp; all browsers will get jpeg. To make Cloudflare cache not only by URL, but also by header, you need to use the [Custom Cache Key](https://support.cloudflare.com/hc/en-us/articles/115004290387) page rule, and add *Header content* to make separate caches depending on the *Accept* request header.
+
+However, the *Custom Cache Key* rule currently requires an *Enterprise* account. And if you already have that, you may as well go with the *Polish* feature, which starts at the “Pro” level plan. With the *Polish* feature, you will not need WebP Express.
+
+To make *WebP Express* work on a free Cloudflare account, you have the following choices:
+
+1. You can configure the CDN not to cache jpeg images by adding the following page rule: If rule matches: `example.com/*.jpg`, set: *Cache level* to: *Bypass*
+
+2. You can set up another CDN (on another provider), which you just use for handling the images. You need to configure that CDN to forward the *Accept header*. You also need to install a Wordpress plugin that points images to that CDN.
+
+3. You can switch operation mode to "CDN friendly" and use HTML altering.
+
+= I am on WP Engine =
+From version 0.17.1 on WebP Express works with WP engine and this combination will be tested before each release.
+
+You can use the plugin both in "Varied image responses" mode and in "CDN friendly mode".
+
+To make the redirection work, you must:
+1) Grab the nginx configuration found in the "I am on Nginx/OpenResty" section in this FAQ. Use the "try_files" variant.
+2) Contact help center and ask them to insert that configuration.
+3) Make sure the settings match this configuration. Follow the "beware" statements in the "I am on Nginx/OpenResty" section.
+
+WebP Express tweaks the workings of "Redirect to converter" a bit for WP engine. That PHP script usually serves the webp directly, along with a Vary:Accept header. This header is however overwritten by the caching machinery on WP engine. As a work-around, I modified the response of the script for WP engine. Instead of serving the image, it serves a redirect to itself. As there now IS a corresponding webp, this repeated request will not be redirected to the PHP script, but directly to the webp. And headers are OK for those redirects. You can hit the "Live test" button next to "Enable redirection to converter?" to verify that this works as just described.
+
+If you (contrary to this headline!) are in fact not on WP Engine, but might want to be, I have an affiliate link for you. It will give you 3 months free and it will give me a reward too, if you should decide to stay there. Here you go: [Get 3 months free when you sign up for WP Engine.](https://shareasale.com/r.cfm?b=1343154&u=2194916&m=41388&urllink=&afftrack=)
+
+= WebP Express / ShortPixel setup =
+Here is a recipe for using WebP Express together with ShortPixel, such that WebP Express generates the webp's, and ShortPixel only is used to create `
`, while in a non-webp enabled browser, it looks like this: `
`
+
+
+*6. Optionally add Cache Enabler rewrite rules in your .htaccess*
+*Cache Enabler* provides some rewrite rules that redirects to the cached file directly in the `.htaccess`, bypassing PHP entirely. Their plugin doesn't do that for you, so you will have to do it manually in order to get the best performance. The rules are in the "Advanced configuration" section on [this page](https://www.keycdn.com/support/wordpress-cache-enabler-plugin).
+
+= Does it work with lazy loaded images? =
+No plugins/frameworks has yet been discovered, which does not work with *WebP Express*.
+
+The most common way of lazy-loading is by setting a *data-src* attribute on the image and let javascript use that value for setting the *src* attribute. That method works, as the image request, seen from the server side, is indistinguishable from any other image request. It could however be that some obscure lazy load implementation would load the image with an XHR request. In that case, the *Accept* header will not contain 'image/webp', but '*/*', and a jpeg will be served, even though the browser supports webp.
+
+The following lazy load plugins/frameworks has been tested and works with *WebP Express*:
+- [BJ Lazy Load](https://da.wordpress.org/plugins/bj-lazy-load/)
+- [Owl Carousel 2](https://owlcarousel2.github.io/OwlCarousel2/)
+- [Lazy Load by WP Rocket](https://wordpress.org/plugins/rocket-lazy-load/)
+
+I have only tested the above in *Varied image responses* mode, but it should also work in *CDN friendly* mode. Both *Alter HTML* options have been designed to work with standard lazy load attributes.
+
+= Can I make an exceptions for some images? =
+There can be instances where you actually need to serve a jpeg or png. For example if you are demonstrating how a jpeg looks using some compression settings.
+
+If you want an image to be served in the original format (jpeg og png), do one of the following things:
+- Add "?dontreplace" to the image url.
+- Place an empty file in the same folder as the jpeg/png. The file name must be the same as the jpeg/png with ".dontreplace" appended
+
+Doing this will bypass redirection to webp and also prevent Alter HTML to use the webp instead of the original.
+
+NOTE: You may have to regenerate .htaccess rules (by clicking the button) in order for this to work. The feature was added in 0.23.0 and if you started using WebP Express before that, the necessary rules will not be there, unless you regenerate, that is.
+
+*Bypassing for an entire folder*
+To bypass redirection for an entire folder, you can put something like this into your root .htaccess:
+`
+RewriteRule ^uploads/2021/06/ - [L]
+`
+PS: If *WebP Express* has placed rules in that .htaccess, you need to place the rule *above* the rules inserted by *WebP Express*
+
+If you got any further questions, look at, or comment on [this topic](https://wordpress.org/support/topic/can-i-make-an-exception-for-specific-post-image/)
+
+= Update failed and cannot reinstall =
+The 0.17.0 release contained binaries with dots in their filenames, which caused the unpacking during update to fail on a few systems. This failure could leave an incomplete installation. With important files missing - such as the main plugin file - Wordpress no longer registers that the plugin is there (it is missing from the list). However, the folder is there in the file system and trying to install WebP Express again fails because Wordpress complains about just that. The solution is to remove the "webp-express" folder in "plugins" manually (via ftp or a plugin, such as File Manager) and then install WebP Express anew. The setting will be intact. The filenames that caused the trouble where fixed in 0.17.2.
+
+= Alter HTML only replaces some of the images =
+
+If you are wondering why Alter HTML are missing some images, it can be due to one of the following reasons:
+
+- WebP Express doesn't convert external images, only images on your server. Alter HTML will therefore not alter URLS that unless they point to your server or a CDN that you have added in the *CDN hostnames* section
+- WebP Express has a "Scope" option, which for example can be set to "Uploads and themes". Only images that resides within the selected scope are replaced with webp.
+- If you have selected ``-tags will be replaced (CSS images will not be replaced). Additionally, the `
`-tag must have a "src" attribute or a commonly used data attribute for lazyloading (such as “data-src” or “data-lazy-src”)
+- If you have set the "How to replace" option to "Use content filtering hooks", images inserted with some third party plugins/themes might not be replaced. To overcome that, change that setting to "The complete page".
+- The image might have been inserted with javascript. WebP Express doesn't changSome plugins might insert the
+
+= When is feature X coming? / Roadmap =
+No schedule. I move forward as time allows. I currently spend a lot of time answering questions in the support forum. If someone would be nice and help out answering questions here, it would allow me to spend that time developing. Also, donations would allow me to turn down some of the more boring requests from my customers, and speed things up here.
+
+Right now I am focusing on the File Manager. I would like to add possibility for converting, bulk converting, viewing conversion logs, viewing stats, etc.
+
+Here are other things in pipeline:
+- Excluding certain files and folders.
+- Supporting Save-Data header in Varied Image Responses mode (send extra compressed images to clients who wants to use as little bandwidth as possible).
+- Displaying rules for NGINX.
+- Allow webp for all browsers using [this javascript library](http://libwebpjs.hohenlimburg.org/v0.6.0/). Unfortunately, the javascript library does not (currently) support srcset attributes, which is why I moved this item down the priority list. We need srcset to be supported for the feature to be useful.
+
+The current milestones, their subtasks and their progress can be viewed here: https://github.com/rosell-dk/webp-express/milestones
+
+If you wish to affect priorities, it is certainly possible. You can try to argue your case in the forum or you can simply let the money do the talking. By donating as little as a cup of coffee on [ko-fi.com/rosell](https://ko-fi.com/rosell), you can leave a wish. I shall take these wishes into account when prioritizing between new features.
+
+= Beta testing =
+I generally create a pre-release before publishing. If you [follow me on ko-fi](https://ko-fi.com/rosell), you will get notified when a pre-release is available. I generally create a pre-release on fridays and mark it as stable on mondays. In order to download a pre-release, go to [the advanced page](https://wordpress.org/plugins/webp-express/advanced/) and scroll down to "Please select a specific version to download". I don't name the pre-releases different. You will just see the next version here before it is available the usual way.
+
+= Can I buy you a cup of coffee? =
+You sure can! To do so, [go here!](https://ko-fi.com/rosell). If payment doesn't work for your country, [try here instead](https://buymeacoff.ee/rosell).
+
+If you want to make sure that my coffee supplies don't run dry, you can even buy me a cup of coffee on a regular basis [by going here](https://github.com/sponsors/rosell-dk)
+
+
+== Screenshots ==
+
+1. WebP Express settings
+
+== Changelog ==
+
+= 0.25.9 =
+(released 7 April 2024)
+* Bugfix: Fixed ewww conversion method after ewww API change
+
+= 0.25.8 =
+(released 20 October 2023)
+* Bugfix: Depreciation warning on PHP 8.2 with Alter HTML. Thanks to @igamingsolustions for reporting the bug
+
+= 0.25.7 =
+(released 19 October 2023)
+* Bugfix: Removed depreciation warning on settings screen, which was showing in PHP 8.2. Thanks to Rob Meijerink from the Netherlands for providing the fix in a pull request
+* Bugfix: Removed depreciation warning when converting, happening in PHP 8.2. Thanks to Sophie B for reporting and Rob Meijerink from the Netherlands for providing the fix in a pull request
+* Bugfix: One of the Mime fallback methods did not work. Thanks to gerasart from Ukraine for providing the fix in a pull request
+
+= 0.25.6 =
+(released 15 April 2023)
+* Bugfix: A bug in another plugin can cause delete file hook to be called with an empty string, which WebP Express was not prepared for (fatal exception). Thanks to Colin Frick from Liechtenstein for providing the fix in a pull request on [github](https://github.com/rosell-dk/webp-express/)
+* Bugfix: Bug when testing imagick converter from settings page (only PHP 8). Thanks to Sisir from Bangladesh for reporting and providing the fix in a pull request
+
+= 0.25.5 =
+(released 23 May 2022)
+* When using the "Prevent using webps larger than original" with Alter HTML (picture tags) on images with srcset, those webps that where larger than the originals was picked out from the srcset for the webp. This could lead to bad quality images. In the fix, the image will only have a webp source alternative when ALL the webps are smaller than their originals (when the "prevent..." option is set).
+* Bugfix: In rare cases, WebP Express could fail detecting mime type
+
+= 0.25.4 =
+(released 6 May 2022)
+* AlterHTML (when using picture tags): Fixed charset problems for special characters in alt and title attributes. The bug was introduced in 0.25.2. Thanks to Cruglk for first pointing this out.
+
+= 0.25.3 =
+(released 4 May 2022)
+* AlterHTML: Fixed BIG BUG introduced in 0.25.2 (the webp urls was wrong when using picture tags). So sorry! Thankfully, I was quickly made aware of this and quickly patched it.
+
+= 0.25.2 =
+(released 4 May 2022)
+* AlterHTML did not skip existing picture tags when they contained newlines, resulting in picture tags inside picture tags, which is invalid markup. Thanks to Jonas for being very helpful in solving this.
+
+= 0.25.1 =
+(released 7 dec 2021)
+* An innocent text file triggered Windows Defender. It has been removed. Thanks to Javad Naroogheh from Iran for notifying
+
+= 0.25.0 =
+(released 7 dec 2021, on my daughters 10 year birthday!)
+* No exec()? - We don't give up easily, but now emulates it if possible, using proc_open(), passthru() or other alternatives. The result is that the cwebp converter now is available on more systems. Quality detection of jpegs also works on more systems now. The fallback feature with the emulations is btw implemented in a new library, [exec-with-fallback](https://github.com/rosell-dk/exec-with-fallback)
+* Bugfix: Our WP CLI command "webp-express" has a quality option, but it was not working
+
+For older releases, check out changelog.txt
+
+== Upgrade Notice ==
+
+= 0.25.9 =
+* Fixed ewww conversion method after ewww API change
+
+= 0.25.8 =
+* PHP 8.2 bugfix
+
+= 0.25.7 =
+* PHP 8.2 bugfixes
+
+= 0.25.6 =
+* Two bugfixes - thanks for the pull requests on github :)
+
+= 0.25.5 =
+* Two bugfixes, one of them in Alter HTML. If you are using Alter HTML with picture tags and have enabled "Prevent using webps larger than original" and are using page caching, you should flush your page cache.
+
+= 0.25.4 =
+* AlterHTML (when using picture tags): Fixed charset problems for special characters in alt and title attributes.
+
+= 0.25.3 =
+* AlterHTML: Fixed BIG BUG introduced in 0.25.2
+
+= 0.25.2 =
+* Fixed bug in Alter HTML functionality. It did not skip existing picture tags when they contained newlines, resulting in picture tags inside picture tags
+
+= 0.25.1 =
+* An innocent text file was triggering Windows Defender.
+
+= 0.25.0 =
+* No exec()? - We don't give up so easily anymore.
+
+= 0.24.2 =
+* Minor bugfix
+
+= 0.24.1 =
+* Improved file manager and fixed rare PHP error displayed on settings page
+
+= 0.23.0 =
+* Various improvements and bug fixes
+
+= 0.22.1 =
+* Two bug fixes related to .htaccess files and redirecting to converter
+
+= 0.22.0 =
+* Various improvements and bug fixes
+
+= 0.21.1 =
+* Two bug fixes
+
+= 0.21.0 =
+* Image browser and various bug fixes
+
+= 0.20.1 =
+* Bugfix for PHP 7.1 and below
+
+= 0.20.0 =
+* Added WP CLI support. Add "wp webp-express convert" to crontab for nightly conversions of new images.
+
+= 0.19.1 =
+* Bugfix for PHP 8
+
+= 0.19.0 =
+* Added new conversion method (ffmpeg)
+
+= 0.18.3 =
+* Minor fixes (see changelog)
+
+= 0.18.2 =
+* Fixed bug on settings page
+
+= 0.18.1 =
+* Minor bug fix for bulk convert
+
+= 0.18.0 =
+* General maintenance and improvements
+
+= 0.17.5 =
+* Various fixes, mainly by community.
+
+= 0.17.4 =
+* Various fixes by community. Maintainer is partly back. Will be fully back mid august.
+
+= 0.17.3 =
+* Fixed two critical bugs. One occurred on PHP 7.4, the other when updating old WebP Express on Wordpress 5.3
+
+= 0.17.2 =
+* Fixed bug: Updating plugin failed on a few hosts (in the unzip phase). The problem was introduced in 0.17.0 where the binaries contains dots in their filename.
+
+= 0.17.1 =
+* Miscellaneous bug fixes. Including the NGINX rules in the FAQ (added "xdestination" argument to webp-realizer.php). Now works with WP Engine.
+
+= 0.17.0 =
+* Improved Cwebp availability and Ewww performance. And updated cwebp binaries to version 1.0.3
+
+= 0.16.0 =
+* Various improvements and fixes
+
+= 0.15.3 =
+* Fixed fatal error upon activation for systems which cannot use document root for structuring (rare)
+
+= 0.15.2 =
+* Fixed the bug when File extension was set to "Set to .webp". It was buggy when file extension contained uppercase letters.
+
+= 0.15.1 =
+* Multiple bug fixes
+
+= 0.15.0 =
+* New "Scope" and "destination structure" options, nice test buttons and a lot of work behind the surface
+
+= 0.14.22 =
+* A bundle of bug fixes and a security fix for Windows
+
+= 0.14.21 =
+* Hopefully fixed WebP Express Error: "png" option is Object
+
+= 0.14.20 =
+* Ewww api-key was forgotten upon saving options
+
+= 0.14.19 =
+* Bug fix
+
+= 0.14.18 =
+* Multiple bug fixes
+
+= 0.14.17 =
+* Relaxed abspath sanity check on Windows and fixed updating password for Remote WebP Express
+
+= 0.14.16 =
+* Fixed more errors on systems with symlinked folders
+
+= 0.14.15 =
+* Fixed errors with "redirect to conversion script"
+
+= 0.14.14 =
+Fixed errors on systems with symlinked folders
+
+= 0.14.13 =
+Fixed errors in conversion scripts
+
+= 0.14.12 =
+Fixed critical bug
+
+= 0.14.11 =
+Important security fixes. Upgrade immediately!
+
+= 0.14.10 =
+Security related
+
+= 0.14.9 =
+Security related
+
+= 0.14.8 =
+Security related
+
+= 0.14.7 =
+Security related: Removed unneccesary files from webp-convert library
+
+= 0.14.6 =
+Security related
+
+= 0.14.5 =
+Security related
+
+= 0.14.4 =
+Now bundles with multiple cwebp binaries for linux for systems where 1.0.2 fails.
+
+= 0.14.3 =
+Fixed supplied binary for cwebp (linux)
+
+= 0.14.2 =
+A couple of bugfixes
+
+= 0.14.1 =
+Security related
+
+= 0.14.0 =
+New awesome conversion options that gets you even smaller webp files without compromising quality.
+
+= 0.13.2 =
+Fixed critical error on upload (can occur in combination with some plugins), and it seems we finally nailed the blank settings page bug.
+
+= 0.13.1 =
+Fixed several bugs. You should update...
+
+= 0.13.0 =
+Bulk Conversion, fixed problem with Gd converter and PNGs and more...
+
+= 0.12.2 =
+Fixed bug: On some nginx configurations, the newly added protection against directly calling the converter scripts were triggering also when it should not.
+
+= 0.12.1 =
+Fixed bug: Alter HTML crashed when HTML was larger than 600kb and "image urls" where selected
+
+= 0.12.0 =
+Multisite support and a new operation mode
+
+= 0.11.3 =
+Fixed several bugs. You should update :)
+
+= 0.11.0 =
+WebP Express can now alter HTML to either point to webp images directly or by using the picture tag syntax. Also, non-existing webp-files can be converted upon request (means you can reference the converted webp files before they are actually converted!)
+
+= 0.10.0 =
+WebP Express can now be used in conjunction with Cache Enabler and ShortPixel. Also introduced "Operation modes" in order to keep setting screens simple but still allow tweaking.
+
+= 0.9.1 =
+Fixed critical bug causing options page to go blank
+
+= 0.9.0 =
+Option to redirect to existing webp images directly in .htaccess (improves performance)
+
+= 0.8.0 =
+New converter and miscellaneous improvements
diff --git a/assets/banner-772x250.jpg b/assets/banner-772x250.jpg
new file mode 100644
index 0000000..af8b74c
Binary files /dev/null and b/assets/banner-772x250.jpg differ
diff --git a/assets/icon-128x128.png b/assets/icon-128x128.png
new file mode 100644
index 0000000..e905cbe
Binary files /dev/null and b/assets/icon-128x128.png differ
diff --git a/assets/icon-256x256.png b/assets/icon-256x256.png
new file mode 100644
index 0000000..c2bbb97
Binary files /dev/null and b/assets/icon-256x256.png differ
diff --git a/assets/icon.svg b/assets/icon.svg
new file mode 100644
index 0000000..e7ffa5a
--- /dev/null
+++ b/assets/icon.svg
@@ -0,0 +1,27 @@
+
+
+
+
\ No newline at end of file
diff --git a/assets/screenshot-1.png b/assets/screenshot-1.png
new file mode 100644
index 0000000..7ca21c1
Binary files /dev/null and b/assets/screenshot-1.png differ
diff --git a/changelog.txt b/changelog.txt
new file mode 100644
index 0000000..54fac63
--- /dev/null
+++ b/changelog.txt
@@ -0,0 +1,572 @@
+= 0.25.9 =
+(released 7 April 2024)
+* Bugfix: Fixed ewww conversion method after ewww API change
+
+= 0.25.8 =
+(released 20 October 2023)
+* Bugfix: Depreciation warning on PHP 8.2 with Alter HTML. Thanks to @igamingsolustions for reporting the bug
+
+= 0.25.7 =
+(released 19 October 2023)
+* Bugfix: Removed depreciation warning on settings screen, which was showing in PHP 8.2. Thanks to Rob Meijerink from the Netherlands for providing the fix in a pull request
+* Bugfix: Removed depreciation warning when converting, happening in PHP 8.2. Thanks to Sophie B for reporting and Rob Meijerink from the Netherlands for providing the fix in a pull request
+* Bugfix: One of the Mime fallback methods did not work. Thanks to gerasart from Ukraine for providing the fix in a pull request
+
+= 0.25.6 =
+(released 15 April 2023)
+* Bugfix: A bug in another plugin can cause delete file hook to be called with an empty string, which WebP Express was not prepared for (fatal exception). Thanks to Colin Frick from Liechtenstein for providing the fix in a pull request on [github](https://github.com/rosell-dk/webp-express/)
+* Bugfix: Bug when testing imagick converter from settings page (only PHP 8). Thanks to Sisir from Bangladesh for reporting and providing the fix in a pull request
+
+= 0.25.5 =
+(released 23 May 2022)
+* When using the "Prevent using webps larger than original" with Alter HTML (picture tags) on images with srcset, those webps that where larger than the originals was picked out from the srcset for the webp. This could lead to bad quality images. In the fix, the image will only have a webp source alternative when ALL the webps are smaller than their originals (when the "prevent..." option is set). Note: If you were affected by this and use a page caching plugin, you should flush your page cache!
+* Bugfix: In rare cases, WebP Express could fail detecting mime type
+
+= 0.25.4 =
+(released 6 May 2022)
+* AlterHTML (when using picture tags): Fixed charset problems for special characters in alt and title attributes. The bug was introduced in 0.25.2. Thanks to Cruglk for first pointing this out.
+
+= 0.25.3 =
+(released 4 May 2022)
+* AlterHTML: Fixed BIG BUG introduced in 0.25.2 (the webp urls was wrong when using picture tags). So sorry! Thankfully, I was quickly made aware of this and quickly patched it.
+
+= 0.25.2 =
+(released 4 May 2022)
+* AlterHTML did not skip existing picture tags when they contained newlines, resulting in picture tags inside picture tags, which is invalid markup. Thanks to Jonas for being very helpful in solving this.
+
+= 0.25.1 =
+(released 7 Dec 2021)
+* An innocent text file triggered Windows Defender. It has been removed. Thanks to Javad Naroogheh from Iran for notifying
+
+= 0.25.0 =
+(released 7 Dec 2021, on my daughters 10 year birthday!)
+* No exec()? - We don't give up easily, but now emulates it if possible, using proc_open(), passthru() or other alternatives. The result is that the cwebp converter now is available on more systems. Quality detection of jpegs also works on more systems now. The fallback feature with the emulations is btw implemented in a new library, [exec-with-fallback](https://github.com/rosell-dk/exec-with-fallback)
+* Bugfix: Our WP CLI command "webp-express" has a quality option, but it was not working
+
+= 0.24.2 =
+*(released: 25 Nov 2021)*
+* Bugfix: In FileManager, the original image wasn't showing on all systems
+
+= 0.24.1 =
+*(released: 24 Nov 2021)*
+* Bugfix: On some systems, the converter test run that is done when showing the settings page could result in an uncatched error.
+
+= 0.24.0 =
+*(pre-released: 20 Nov 2021, not "released", in the sense that stable tag never pointed to this release)*
+* Improved file manager: You can now tweak conversion settings, delete conversion and see mime type of images.
+* Conversions logs are made more readable
+* Bumped webp-convert to 0.24.0
+
+For more info, see the closed issues on the [webp-express 0.24.0 milestone](https://github.com/rosell-dk/webp-express/milestone/45?closed=1)
+
+= 0.23.0 =
+*(pre-released: 15 Nov 2021, scheduled for release 16 Nov 2021)*
+* Changed names for preventing replacing image with webp. Use "?dontreplace" / ".dontreplace" instead of "?original" and ".do-not-convert". The old ones are deprecated, but will still work (for a while)
+* You can now convert images in the file manager and view conversion log
+* The new file manager UI is now available in multisite too
+* Changed names for the escape hatches that was introduced in 0.22. Use "?dontreplace" and ".dontreplace" rather than "?original" and ".do-not-convert". The old names still works
+* Added message to users that have the Elementor plugin installed on how to configure Elementor to inline CSS in order for Alter HTML to be able to replace image URLs (only displayed when relevant)
+* Added UI for "skip-these-precompiled-binaries" cwebp option. Thanks to @madmax4ever for posting code for this.
+* Bumped dom-util-for-webp library to 0.5
+* Bugfix: In multisite, Alter HTML produced wrong webp urls when destination structure was set to "image roots". Thanks to John A. Huebner II (@hube2) who lives on a big hill in central NY, USA for reporting the issue.
+* Bugfix: One of the newly introduced escape hatches didn't work in Alter HTML.
+
+For more info, see the closed issues on the [webp-express 0.23 milestone](https://github.com/rosell-dk/webp-express/milestone/43?closed=1)
+
+= 0.22.1 =
+*(released: 09 Nov 2021)*
+* Bugfix: Old unupdated rewrite rules for redirecting to converter caused error (bug was introduced in 0.22.0). Thanks, @utrenkner for reacting quickly!
+* Bugfix: Backslashes in file paths in .htaccess on Windows caused redirecting not to work. Thanks to Herb Miller (@bobbingwide) from UK for discovering and debugging this.
+
+= 0.22.0 =
+*(released: 09 Nov 2021)*
+* Added option to disable creating log files and button to do delete them. Thanks to Amit Sonkhiya and many others for suggesting this.
+* WebP Express now prevents serving webps when they are bigger than the source. Added option for it (default is to prevent)
+* You can now prevent that a certain image is redirected to webp by appending "?original" to the URL. Also works for Alter HTML.
+* You can now prevent that a certain image is redirected to webp by placing a dummy ".do-not-convert" file next to the original.
+* The conversion manager now uses the new "?original" escape hatch to avoid that the original image is being redirected. Beware that if you are on Nginx and have created rewrite rules, you will need to implement the same escape hatch to avoid the "Original" image to be redirected to webp. However, in a future update, I will probably implement a PHP script for serving the original so you can also just wait.
+* Bugfix: Alter HTML could in some [rare cases](https://github.com/rosell-dk/webp-express/issues/528) produce invalid HTML.
+For more info, see the closed issues on the [webp-express 0.22 milestone](https://github.com/rosell-dk/webp-express/milestone/13?closed=1)
+* Bugfix: The "Enable redirection to converter" functionality was too greedy. It did not only redirect jpegs and pngs, but any file request. The bug occurred when Destination folder was set to "Image roots". The fix updates the .htaccess files if neccessary.
+* Minor bugfix: Symlinking the webp-express plugin folder would break "Redirection to converter" functionality
+
+For more info, see the closed issues on the [webp-express 0.22 milestone](https://github.com/rosell-dk/webp-express/milestone/13?closed=1)
+
+= 0.21.1 =
+*(released: 27 Oct 2021)*
+* Bugfix: File manager could not handle many images. It now loads tree branches on need basis instead of the complete tree in one go
+* Bugfix: For mulisite, the redirect after settings save was not working (bug introduced in 0.21.0)
+For more info, see the closed issues on the [webp-express 0.21.1 milestone](https://github.com/rosell-dk/webp-express/milestone/42?closed=1)
+
+= 0.21.0 =
+*(released: 25 Oct 2021)*
+* Added image browser (in Media tab)
+* Updated webp convert library to 2.7.0.
+* ImageMagick now supports the "near-lossless" option (provided Imagick >= 7.0.10-54)
+* Added "try-common-system-paths" option for ImageMagick (default: true). Thanks to Henrik Alves for adding this option.
+* Bugfix: Handling Uncaught Fatal Exception during .htaccess read failure. Thanks to Manuel D'Orso from Italy for the fix.
+* Bugfix: File names which were not UTF8 caused trouble in the Bulk Convert. Thank to "mills4078" for the fix
+* Bugfix: Redirection back to settings after saving settings failed on some systems. Thanks to Martin Rehberger (@kingkero) from Germany for the fix.
+* Bugfix: Webp urls did not contain port number (only relevant when the website was not on default port number). Thanks to Nicolas LIENART (@nicolnt) from France for providing the fix.
+
+For more info, see the closed issues on the [webp-express 0.21.0 milestone](https://github.com/rosell-dk/webp-express/milestone/41?closed=1) and the
+[webp-convert 2.7.0 milestone](https://github.com/rosell-dk/webp-convert/milestone/24?closed=1)
+
+= 0.20.1 =
+*(released: 20 Jun 2021)*
+* Bugfix: Removed composer.lock. It was locked on PHP 7.2, which caused server error on some sites (only some sites with old php were affected). Also deleted the library which required PHP 7.2 (onnov/detect-encoding/). The functionality is not really needed.
+
+= 0.20.0 =
+*(released: 17 Jun 2021)*
+* Added WP CLI support. Add "wp webp-express convert" to crontab for nightly conversions of new images! Thanks to Isuru Sampath Ratnayake from Sri Lanka for initializing this.
+* Added "sharp-yuv" (not as option, but as always on). Better YUV->RGB color conversion at almost no price. [Read more here](https://www.ctrl.blog/entry/webp-sharp-yuv.html). Supported by cwebp, vips, gmagick, graphicsmagick, imagick and imagemagick
+* Bumped cwebp binaries to 1.2.0
+* cwebp now only validates hash of supplied precompiled binaries when necessary. This cuts down conversion time.
+* Convert on upload now defaults to false, as it may impact upload experience in themes with many formats.
+* bugfix: Alpha quality was saved incorrectly for PNG. Thanks to Chriss Gibbs from the UK for finding and fixing this.
+* bugfix: wp-debug log could be flooded with "Undefined index: HTTP_ACCEPT". Thanks to @markusreis for finding and fixing this.
+
+= 0.19.1 =
+*(released: 03 May 2021)*
+* Bugfix for PHP 8.0 - fread() does not permit second argument to be 0. Thanks to @trition for reporting and fixing this bug.
+
+= 0.19.0 =
+*(released: 13 Nov 2020)*
+* New convertion method: ffmpeg
+* Fixed problem in Bulk Convert when files had special characters in their filename
+* Prevented problems if the plugin gets included twice (can anybody enlighten me on how this might happen?)
+
+For more info, see the closed issues on the [0.19.0 milestone on the github repository](https://github.com/rosell-dk/webp-express/milestone/36?closed=1)
+
+
+= 0.18.3 =
+*(released: 5 Nov 2020)*
+* Bugfix: WebP Express uses live tests to determine the capabilities of the server in respect to .htaccess files (using the htaccess-capability-tester library). The results are used for warnings and also for optimizing the rules in the .htaccess files. However, HTTP Requests can fail due to other reasons than the feature not working (ie timeout). Such failures should lead to an indeterminate result, but it was interpreted as if the feature was not working.
+* The Live test now displays a bit more information if the HTTP request failed.
+* Changed default value for "destination structure" to "Image roots", as "Document root" doesn't work on hosts that have defined DOCUMENT_ROOT in an unusual way.
+* Added possibility to change "%{DOCUMENT_ROOT}" part of RewriteCond by adding a line to wp-config.php. THIS IS A BETA FEATURE AND MIGHT BE REVOKED IF NOBODY ACTUALLY NEEDS IT.
+* Got rid of PHP notice Constant WEBPEXPRESS_MIGRATION_VERSION already defined
+* Fixed donation link. It now points to https://ko-fi.com/rosell again
+
+For more info, see the closed issues on the [0.18.3 milestone on the github repository](https://github.com/rosell-dk/webp-express/milestone/34?closed=1)
+
+= 0.18.2 =
+*(released: 28 Sep 2020)*
+* Bugfix: Fixed error on the settings page on a handful of setups.
+
+= 0.18.1 =
+*(released: 24 Sep 2020)*
+* Bugfix: Bulk Convert failed to show list on systems that did not have the [utf8-encode()](https://www.php.net/manual/en/function.utf8-encode.php) function.
+
+= 0.18.0 =
+*(released: 24 Sep 2020)*
+* You can now set cache control header in CDN friendly mode too
+* The code for testing what actually works in .htaccess files on the server setup has been moved to a new library: [htaccess-capability-tester](https://github.com/rosell-dk/htaccess-capability-tester). It has been strengthened in the process.
+* Improved diagnosing in the "Live test" buttons
+* Simplified the logic for adding "Vary header" in the .htaccess residing in the cache dir. The logic no longer depends on the Apache module "mod_envif" being installed. mod_envif has Apache "Base" status, which means it is very rarely missing, so I decided not to trigger automatically updating of the .htaccess rules. To apply the change, you must click the button that forces .htaccess regeneration
+* The plugin has a folder called "wod" which contains php scripts for converting an image to webp. This is used for the function that rely on redirect magic to trigger conversion ("Enable redirection to converter?" and "Create webp files upon request?"). The .htaccess file in the "wod" folder in the plugin dir contains directives for modifying access (in order to counterfight rules placed by security plugins for disallows scripts to be run directly). However if these directives has been disallowed in the server setup, any request to a file in the folder will result in a 500 internal server error. To circumvent this, a "wod2" folder has been added, which contains the same scripts, but without the .htaccess. Upon saving, WebP Express now automatically checks which one works, and points to that in the .htaccess rules.
+* Bugfix: webp mime type was not registred in .htaccess in "CDN friendly" mode. This is a minor fix so I decided not to update the .htaccess automatically. To apply it, you must click the button that forces .htaccess regeneration.
+* Bugfix: Bulk convert failed to load the list when there were filenames containing non-unicode characters
+* Added a new way to support me. I'm on [GitHub Sponsors](https://github.com/sponsors/rosell-dk)!
+
+For more info, see the closed issues on the 0.18.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/33?closed=1
+
+= 0.17.5 =
+*(released: 11 Aug 2020)*
+* Fixed "Path is outside resolved document root" in a certain symlinked configuration. Thanks to @spiderPan on github for providing the fix.
+* Added content filtering hooks for several third party plugins including ACF and WooCommerce Product Images. With this change, the "Use content filtering hooks" in Alter HTML works in more scenarios, which means there are fewer scenarios where you have to resort to the slower "The complete page" option. Thanks to alextuan for providing the contribution
+* Fixed problems with Alter HTML when migrating: Absolute paths were cached in the database and the cache was only updated upon saving settings. The paths are not cached anymore (recalculating these on each page load is not a performance problem)
+
+For more info, see the closed issues on the 0.17.5 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/30?closed=1
+
+= 0.17.4 =
+*(released: 26 Jun 2020)*
+* Hopefully fixed that configuration was resetting for some users
+* Fixed "Path is outside resolved document root" on file conversion attempts in Windows. Thanks to @Ruzgfpegk from Japan for providing the fix.
+* Fix errors not caught in the selftest. Thanks to Benji Bilheimer from Germany providing the fix.
+* Fix errors not caught in the selftest with unverified certificates. Thanks to Rikesh Ramlochund from Mauritius for providing the fix.
+* Fixed errors with filenames containing encoded symbols. Thanks to Eddie Zhou from Canada for the fix.
+
+
+= 0.17.3 =
+*(released: 3 Feb 2020)*
+
+* Fixed critical bug: Fatal error after updating plugin (if one had been postponing updating WebP Express for a while and then updated Wordpress and THEN updated WebP Express)
+* A critical bug was fixed in the webp-convert library (PHP 7.4 related)
+* A critical bug was fixed in dom-util-for-webp library (PHP 7.4 related)
+* Alter HTML now processes the "poster" attribute in Video tags. Thanks to @MikhailRoot from Russia for the PR on github.
+* On some Litespeed hosts, WebP Express reported that mod_headers was not available even though it was. Thanks to @lubieowoce from Poland for the PR on github)
+
+
+= 0.17.2 =
+*(released: 5 Oct 2019)*
+
+* Fixed bug: Updating plugin failed on a few hosts (in the unzip phase). Problem was introduced in 0.17.0 with the updated binaries.
+* Fixed bug: Alter HTML used the protocol (http/https) for the site for generated links (rather than keeping the protocol for the link). Thanks to Jacob Gullberg from Sweden for discovering this bug.
+
+If you experienced update problems due to the update bug, you will probably be left with an incomplete installation. Some of the plugin files are there, but not all. Especially, the main plugin file (webp-express.php) is missing, which means that Wordpress don't "see" the plugin (it is missing from the list). Trying to install WebP Express again will probably not work, because the "webp-express" folder is already there. You will then have to remove the "webp-express" folder in "plugins" manually (via ftp or a plugin, such as File Manager).
+
+For more info, see the closed issues on the 0.17.2 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/29?closed=1
+
+= 0.17.1 =
+*(released: 3 Oct 2019)*
+
+* Fixed NGINX rules in FAQ (added xdestination for the create webp upon request functionality)
+* Fixed issue with Alter HTML. Thanks to @jonathanernst for discovering issue and supplying the patch.
+* WebP Express now works on WP Engine. Check out the new "I am on WP Engine" section in the FAQ
+* Miscellaneous bug fixes
+
+For more info, see the closed issues on the 0.17.1 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/27?closed=1
+
+= 0.17.0 =
+*(released: 27 sep 2019)*
+
+* Cwebp conversion method runs on more systems (not subject to open_basedir restrictions and also tries "pure" cwebp command). Thanks to cng11 for reaching out so I spotted this.
+* Ewww conversion method no longer does a remote api-key check for each conversion - so it is faster. If an ewww conversions fails due to a non-functional key, the key will not be tried again (until next time the options are saved)
+* Updated cwebp binaries to version 1.0.3
+
+= 0.16.0 =
+*(released: 24 sep 2019)*
+
+* Added option to specify CDN urls in Alter HTML. Thanks to Gunnar Peipman from Estonia for suggesting this.
+* Direct Nginx users to Nginx FAQ section on welcome page
+* Fixed Bulk Conversion halting due to nonce expiry
+* Fixed unexpected output upon reactivation
+* Added affiliate link to [Optimole](https://optimole.pxf.io/20b0M) in the "Don't despair - You have options!" message
+
+= 0.15.3 =
+*(released: 19 sep 2019)*
+
+* Fixed fatal error upon activation for systems which cannot use document root for structuring (rare)
+
+= 0.15.2 =
+
+* Fixed the bug when File extension was set to "Set to .webp". It was buggy when file extension contained uppercase letters.
+
+= 0.15.1 =
+*(released: 17 sep 2019)*
+
+* Bug alert: Added alert about a bug when destination folder is set to "mingled" and File extension is set to "Set to .webp"
+* Bugfix: Plugin URL pointed to webpexpress - it should point to parent. This gave trouble with images located in plugins. Thanks to Guillaume Meyer from Switzerland for discovering and reporting.
+* Bugfix: Images with uppercase chars in extension did not get Vary:Accept
+* Bugfix: There were issues with "All content" and destination:document-root when webp-realizer is activated
+
+= 0.15.0 =
+*(released: 17 sep 2019)*
+
+* Provided test-buttons for checking if the redirects works.
+* You can now choose which folders WebP Express is active in. Ie "Uploads and Themes".
+* You can now choose an alternative file structure for the webps which does not rely on DOCUMENT_ROOT being available.
+* WebP Express can now handle when wp-content is symlinked.
+* The .htaccess rules are now divided across folders. Some rules are needed where the source files are located, some where the webp files are located.
+* Added option to convert only PNG files
+* And a couple of bugfixes.
+
+For more info, see the closed issues on the 0.15.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/22?closed=1
+
+= 0.14.22 =
+*(released: 4 aug 2019)*
+
+* Fixed bug in Nginx rules in the FAQ (they did not take into account that the webp images outside upload folder are never stored "mingled")
+* Fixed bug: The extension setting was not respected - it was always appending ".webp", never setting. Thanks to Florian from Germany and Derrick Hammer from USA for reporting.
+* Fixed bug: It turns out that Imagick gives up quality detection for some images and returns 0. This resulted in very poor quality webps when the quality was set to same as jpeg. The 0 is now treated as a failure and the max-quality will be used for such images. Thanks to @sanjayojha303 from India for reporting that there were quality problems on some images.
+* Fixed bug-like behavior: The conversion scripts no longer requires that the respective setting is on for Nginx. Thanks to Mike from Russia for reporting this.
+* Fixed bug: The error handler in webp-convert for handling warnings could in some cases result in endless recursion. For some the result was that they could no longer upload images. Thanks to Tobias Keller from Germany for reporting this bug.
+* Fixed minor bug: Attempt to call private method in a rare scenario (when accessing one of the php scripts in the "wod" folder directly - which is not allowed). Thanks to Giacomo Lawrance from the U.K. for providing input that led to this discovery.
+* Fixed minor bug: It was not tested whether a corresponding webp existed before trying to deleting it when an image was deleted. This produced warnings in debug.log.
+* Security related: Added sanitizing of paths to avoid false positives on coderisk.com (there where no risk because already test the paths for sanity - but this is not detected by coderisk, as the variable is not modified). This was simply done in order get rid of the warnings at coderisk.
+* Security fix: Paths were not sanitized on Windows.
+
+= 0.14.21 =
+*(released: 30 jun 2019)*
+
+* Hopefully fixed WebP Express Error: "png" option is Object
+
+= 0.14.20 =
+*(released: 29 jun 2019)*
+
+* Fixed bug: Ewww api-key was forgot upon saving options
+
+= 0.14.19 =
+*(released: 28 jun 2019)*
+
+* Removed a line that might course Sanity Check to fail ("path not within document root")
+
+= 0.14.18 =
+*(released: 28 jun 2019)*
+
+* Fixed Sanity Error: Path is outside allowed path on systems using symlinked folders
+* Updated cache breaking token for javascript in order for the last fix for changing password with Remote WebP Express to take effect
+* Fixed undefined variable error in image_make_intermediate_size hook, which prevented webps thumbnails to be generated upon upload
+* Minor bug fix in cwebp converter (updated to webp-convert v.2.1.4)
+
+= 0.14.17 =
+*(released: 28 jun 2019)*
+
+* Relaxed abspath sanity check on Windows
+* Fixed updating password for Remote WebP Express
+
+= 0.14.16 =
+*(released: 26 jun 2019)*
+
+* Fixed conversion errors using Bulk convert or Test convert on systems with symlinked folders
+
+= 0.14.15 =
+*(released: 26 jun 2019)*
+
+* Fixed errors with "redirect to conversion script" on systems with symlinked folders
+* Fixed errors with "redirect to conversion script" on systems where the filename cannot be passed through an environment variable
+
+= 0.14.14 =
+*(released: 26 jun 2019)*
+
+* Fixed errors on systems with symlinked folders
+
+= 0.14.13 =
+*(released: 26 jun 2019)*
+
+* Fixed errors in conversion scripts
+
+= 0.14.12 =
+*(released: 26 jun 2019)*
+
+* Fixed critical bug
+
+= 0.14.11 =
+*(released: 24 jun 2019)*
+
+The following security fixes has been applied in 0.14.0 - 0.14.11:
+It is urged that you upgrade all of you WebP Express installations!
+
+– Security fix: Closed a security hole that could be used to view the content of any file on the server (provided that the full path is known or guessed). This is a very serious flaw, which unfortunately has been around for quite a while.
+– Security fix: Added capability checks to options page.
+– Security fix: Sanitized user input.
+– Security fix: Added checks for file paths and directories.
+– Security fix: Nonces and capability checks for AJAX calls.
+
+= 0.14.10 =
+*(released: 24 jun 2019)*
+
+* Security related
+
+= 0.14.9 =
+*(released: 22 jun 2019)*
+
+* Security related
+
+= 0.14.8 =
+*(released: 21 jun 2019)*
+
+* Security related
+
+= 0.14.7 =
+*(released: 20 jun 2019)*
+
+* Security related: Removed unneccesary files from webp-convert library
+
+= 0.14.6 =
+*(released: 20 jun 2019)*
+
+* Security related
+
+= 0.14.5 =
+*(released: 20 jun 2019)*
+
+* Security related
+
+= 0.14.4 =
+*(released: 18 jun 2019)*
+
+* Now bundles with multiple cwebp binaries for linux for systems where 1.0.2 fails.
+
+= 0.14.3 =
+*(released: 18 jun 2019)*
+
+* Fixed filename of supplied cwebp for linux (bug was introduced in 0.14.2)
+
+= 0.14.2 =
+*(released: 17 jun 2019)*
+
+* Fixed problem with older versions of cwebp
+* Fixed that images was not deleted
+* Fixed cache problem on options page on systems that disables cache busting (it resulted in "SyntaxError: JSON.parse")
+
+= 0.14.1 =
+*(released: 15 jun 2019)*
+
+* Security related
+
+= 0.14.0 =
+*(released: 15 jun 2019)*
+
+* Security fix: Closed a security hole that could be used to view the content of any file on the server (provided that the full path is known or guessed). This is a very serious flaw, which has been around for quite a while. I urge you to upgrade to 0.14.0.
+* Added new "encoding" option, which can be set to auto. This can in some cases dramatically reduce the size of the webp. It is supported by all converters except ewww and gd.
+* Added new "near-lossless" option (only for cwebp and vips). Using this is a good idea for reducing size of lossless webps with an acceptable loss of quality
+* Added new "alpha-quality" option (all converters, except ewww and gd). Using this is a good idea when images with transparency are converted to lossy webp - it has the potential to reduce the size up to 50% (depending on the source material) while keeping an acceptable level of quality
+* Added new conversion methods: Vips and GraphicsMagick
+* Imagick conversion method now supports webp options (finally cracked it!)
+* Using MimeType detection instead of relying on file extensions
+* In "test" converter you now change options and also test PNG files
+* Added conversion logs
+* PNGs are now enabled by default (with the new conversion features especially PNGs are compressed much better)
+
+For more info, see the closed issues on the 0.14.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/9?closed=1
+
+= 0.13.0 =
+*(released: 21 mar 2019)*
+* Bulk Conversion
+* Fixed problems with Gd converter and PNG
+* Optinally auto convert upon media upload
+* Windows fix (thanks, lwxbr!)
+
+= 0.12.2 =
+*(released 8 mar 2019)*
+* Fixed bug: On some nginx configurations, the newly added protection against directly calling the converter scripts were triggering also when it should not.
+
+= 0.12.1 =
+*(released 7 mar 2019)*
+* Fixed bug: Alter HTML crashed when HTML was larger than 600kb and "image urls" where selected
+
+= 0.12.0 =
+*(released 5 mar 2019)*
+* Multisite support (!)
+* A new operation mode: "No conversion", if you do not want to use WebP Express for converting. Replaces the old "Just redirect" mode
+* Added capability testing of .htaccess. The .htaccess rules are now tailored to the capabilities on the system. For example, on some platforms the filename of a requested image is passed to the converter script through the query string, but on platforms that supports passing it through an environment variable, that method is used instead
+* Picturefill.js is now optional (alter html, picture tag)
+* A great bunch more!
+
+For more info, see the closed issues on the 0.12.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/12?closed=1
+
+= 0.11.3 =
+*(released 18 feb 2019)*
+* Fixed bug: Alter HTML caused media library not to display images on some systems. Alter HTML is now disabled in admin mode.
+* Alter HTML (picture tags) could produce the source tags with "src" attribute. But source tags inside picture tags must use "srcset" attribute. Fixed.
+* Alter HTML (image urls): srcsets containing "x" descriptors wasn't handled (ie, srcset="image.jpg 1x")
+* Fixed rewrite rules when placed in root so they are confined to wp-content and uploads. In particular, they no longer apply in wp-admin area, which might have caused problems, ie with media library.
+* Added warning when rules are placed in root and "Convert non-existing webp-files upon request" feature is enabled and WebP Express rules are to be placed below Wordpress rules
+* Fixed bug: The code that determined if WebP Express had placed rules in a .htaccess failed in "CDN friendly" mode. The effect was that these rules was not cleaned up upon plugin deactivation
+
+= 0.11.2 =
+*(released 14 feb 2019)*
+* Fixed bug which caused Alter HTML to fail miserably on some setups
+* AlterHTML now also looks for lazy load attributes in DIV and LI tags.
+
+= 0.11.1 =
+*(released 6 feb 2019)*
+* Fixed bug which caused the new "Convert non-existing webp-files upon request" not to work on all setups
+
+= 0.11.0 =
+*(released 6 feb 2019)*
+* Alter HTML to point to webp files (choose between picture tags or simply altering all image urls)
+* Convert non-existing webp-files upon request (means you can reference the converted webp files before they are actually converted!)
+
+For more info, see the closed issues on the 0.11.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/14?closed=1
+
+= 0.10.0 =
+*(released 7 jan 2019)*
+* Introduced "Operation modes" in order to keep setting screens simple but still allow tweaking
+* WebP Express can now be used in conjunction with Cache Enabler and ShortPixel
+* Cache-Control header is now added in *.htaccess*, when redirecting directly to existing webp
+
+For more info, see the closed issues on the 0.10.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/milestone/7?closed=1
+
+= 0.9.1 =
+*(released 28 dec 2018)*
+* Fixed critical bug causing blank page on options page
+
+= 0.9.0 =
+*(released 27 dec 2018)*
+* Optionally make .htaccess redirect directly to existing webp (improves performance)
+* Optionally do not send filename from *.htaccess* to the PHP in Querystring, but use other means (improves security and reduces risks of problems due to firewall rules)
+* Fixed some bugs
+
+For more info, see the closed issues on the 0.9.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/issues?q=is%3Aclosed+milestone%3A0.9.0
+
+= 0.8.1 =
+*(released 11 dec 2018)*
+* Fixed javascript bug
+
+= 0.8.0 =
+*(released 11 dec 2018)*
+* New conversion method, which calls imagick binary directly. This will make WebP express work out of the box on more systems
+* Made sure not to trigger LFI warning i Wordfence (to activate, click the force .htaccess button)
+* Imagick can now be configured to set quality to auto on systems where the auto option isn't generally available
+* Added Last-Modified header to images. This makes image caching work better
+* Added condition in .htaccess that checks that source file exists before handing over to converter
+* On some systems, converted files where stored in ie *..doc-rootwp-content..* rather than *..doc-root/wp-content..*. This is fixed, a clean-up script corrects the file structure upon upgrade.
+
+For more info, see the closed issues on the 0.8.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/issues?q=is%3Aclosed+milestone%3A0.8.0
+
+= 0.7.2 =
+*(released 21 nov 2018)*
+Fixed a critical bug which generated an error message which caused corrupt images. It was not the bug itself, but the error message it generated, that caused the images to be corrupted. It only happened when debugging was enabled in php.ini
+
+= 0.7.1 =
+*(released 9 nov 2018)*
+Fixed minor "bug". The Api version combobox in Remote WebP Express converter was showing on new sites, but I only want it to show when old api is being used.
+
+= 0.7.0 =
+*(released 9 nov 2018)*
+This version added option to provide conversion service to other sites!
+
+For more info, see the closed issues on the 0.7.0 milestone on the github repository: https://github.com/rosell-dk/webp-express/issues?q=is%3Aclosed+milestone%3A0.7.0
+
+= 0.6.0 =
+*(released 4 okt 2018)*
+This version added option for setting caching header, fixed a serious issue with *Imagick*, added a new converter, *Gmagick*, added a great deal of options to *Cwebp* and generally improved the interface.
+
+* Added option for caching
+* Fixed long standing and serious issue with Imagick converter. It no longer generates webp images in poor quality
+* Added gmagick as a new conversion method
+* WebPExpress now runs on newly released WebP-Convert 1.2.0
+* Added many new options for *cwebp*
+* You can now quickly see converter status by hovering over a converter
+* You can now choose between having quality auto-detected or not (if the server supports detecting quality).
+* If the server does not support detecting quality, the WPC converter will display a quality "auto" option
+* Added special intro message for those who has no working conversion methods
+* Added help texts for options
+* Settings are now saved, when changing converter options. Too many times, I found myself forgetting to save...
+
+For more info, see the closed issues on the 0.6.0 milestone on our github repository: https://github.com/rosell-dk/webp-express/issues?q=is%3Aclosed+milestone%3A0.6.0
+
+= 0.5.0 =
+This version works on many more setups than the previous. Also uses less resources and handles when images are changed.
+
+* Configuration is now stored in a separate configuration file instead of storing directly in the *.htaccess* file and passing it on via query string. When updating, these settings are migrated automatically.
+* Handles setups where Wordpress has been given its own directory (both methods mentioned [here](https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory))
+* Handles setups where *wp-content* has been moved, even out of Wordpress root.
+* Handles setups where Uploads folder has been moved, even out of *wp-content*.
+* Handles setups where Plugins folder has been moved, even out of *wp-content* or out of Wordpress root
+* Is not as likely to be subject to firewalls blocking requests (in 0.4.0, we passed all options in a querystring, and that could trigger firewalls under some circumstances)
+* Is not as likely to be subject to rewrite rules from other plugins interfering. WebP Express now stores the .htaccess in the wp-content folder (if you allow it). As this is deeper than the root folder, the rules in here takes precedence over rules in the main *.htaccess*
+* The *.htaccess* now passes the complete absulute path to the source file instead of a relative path. This is a less error-prone method.
+* Reconverts the webp, if source image has changed
+* Now runs on version 1.0.0 of [WebP On Demand](https://github.com/rosell-dk/webp-on-demand). Previously ran on 0.3.0
+* Now takes care of only loading the PHP classes when needed in order not to slow down your Wordpress. The frontend will only need to process four lines of code. The backend footprint is also quite small now (80 lines of code of hooks)
+* Now works in Wordpress 4.0 - 4.6.
+* Added cache-breaking tokens to image test links
+* Denies deactivation if rewrite rules could not be removed
+* Refactored thoroughly
+* More helpful texts.
+* Extensive testing. Tested on Wordpress 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8 and 4.9. Tested with PHP 5.6, PHP 7.0 and PHP 7.1. Tested on Apache and LiteSpeed. Tested when missing various write permissions. Tested migration. Tested when installed in root, in subfolder, when Wordpress has its own directory (both methods), when wp-content is moved out of Wordpress directory, when plugins is moved out of Wordpress directory, when both of them are moved and when uploads have been moved.
+
+For more info, see the closed issues on the 0.5.0 milestone on our github repository: https://github.com/rosell-dk/webp-express/milestone/2?closed=1
+
+= 0.4.0 =
+This version fixes some misbehaviours and provides new http headers with info about the conversion process.
+
+* Fixed bug: .htaccess was not updated every time the settings was saved.
+* Fixed bug: The plugin generated error upon activation.
+* Now produces X-WebP-Convert-And-Serve headers with info about the conversion - useful for validating that converter receives the expected arguments and executes correctly.
+* WebPExpress options are now removed when plugin is uninstalled.
+* No longer generates .htaccess rules on install. The user now has to actively go to Web Express setting and save first
+* Added a "first time" message on options page and a reactivation message
+
+For more info, see the closed issues on the github repository: https://github.com/rosell-dk/webp-express/milestone/1?closed=1
+
+= 0.3.1 =
+* The "Only jpeg" setting wasn't respected in 0.3.0. It now works again
+
+= 0.3 =
+* Now works on LiteSpeed webservers
+* Now sends X-WebP-On-Demand headers for easier debugging
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..4b2e7a2
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,34 @@
+{
+ "name": "rosell-dk/webp-express",
+ "description": "WebP for the masses",
+ "type": "wordpress-plugin",
+ "license": "MIT",
+ "require": {
+ "composer/installers": "^1.12.0",
+ "rosell-dk/webp-convert": "^2.9.2",
+ "rosell-dk/webp-convert-cloud-service": "^2.0.0",
+ "rosell-dk/dom-util-for-webp": "^0.7.1",
+ "rosell-dk/htaccess-capability-tester": "^0.9.0",
+ "rosell-dk/image-mime-type-guesser": "^1.1.1"
+ },
+ "require-dev": {
+ },
+ "scripts": {
+ "ci": [
+ "@composer validate --no-check-all --strict"
+ ],
+ "copylocalwebpconvert": "rsync -varz /home/rosell/github/webp-convert/src/ /home/rosell/github/webp-express/vendor/rosell-dk/webp-convert/src/"
+ },
+ "authors": [
+ {
+ "name": "Bjørn Rosell",
+ "homepage": "https://www.bitwise-it.dk/contact",
+ "role": "Project Author"
+ }
+ ],
+ "config": {
+ "allow-plugins": {
+ "composer/installers": true
+ }
+ }
+}
diff --git a/docs/development.md b/docs/development.md
new file mode 100644
index 0000000..7ccc01e
--- /dev/null
+++ b/docs/development.md
@@ -0,0 +1,111 @@
+## Updating the vendor dir:
+
+1. Run `composer update` in the root (plugin root).
+2. Run `composer dump-autoload -o`
+(for some reason, `vendor/composer/autoload_classmap.php` looses all its mappings on composer update). It also looses them on a `composer dump-autoload` (without the -o option).
+It actually seems that the mappings are not needed. It seems to work fine when I alter autoload_real to not use the static loader. But well, I'm reluctant to change anything that works.
+
+3. Remove unneeded files:
+
+- Open bash
+- cd into the folder
+
+```console
+rm -r vendor/rosell-dk/webp-convert/docs
+rm -r vendor/rosell-dk/webp-convert/src/Helpers/*.txt
+rm composer.lock
+rmdir vendor/bin
+```
+
+3. Commit on git
+
+
+## Copying WCFM
+I created the following script for building WCFM, copying it to webp-express, etc
+```
+#!/bin/bash
+
+WCFM_PATH=/home/rosell/github/webp-convert-filemanager
+WE_PATH=/home/rosell/github/webp-express
+WE_PATH_WCFM=$WE_PATH/lib/wcfm
+WCFMPage_PATH=/home/rosell/github/webp-express/lib/classes/WCFMPage.php
+WC_PATH=/home/rosell/github/webp-convert
+
+copyassets() {
+ # remove assets in WebP Express
+ rm -f $WE_PATH_WCFM/index*.css
+ rm -f $WE_PATH_WCFM/index*.js
+ rm -f $WE_PATH_WCFM/vendor*.js
+
+ # copy assets from WCFM
+ cp $WCFM_PATH/dist/assets/index*.css $WE_PATH_WCFM/
+ cp $WCFM_PATH/dist/assets/index*.js $WE_PATH_WCFM/
+ cp $WCFM_PATH/dist/assets/vendor*.js $WE_PATH_WCFM/
+
+
+ #CSS_FILE = $(ls /home/rosell/github/webp-express/lib/wcfm | grep 'index.*css' | tr '\n' ' ' | sed 's/\s//')
+ CSS_FILE=$(ls $WE_PATH_WCFM | grep 'index.*css' | tr '\n' ' ' | sed 's/\s//')
+ JS_FILE=$(ls $WE_PATH_WCFM | grep 'index.*js' | tr '\n' ' ' | sed 's/\s//')
+
+
+ if [ ! $CSS_FILE ]; then
+ echo "No CSS file! - aborting"
+ exit
+ fi
+ if [ ! $JS_FILE ]; then
+ echo "No JS file! - aborting"
+ exit
+ fi
+
+ echo "CSS file: $CSS_FILE"
+ echo "JS file: $JS_FILE"
+
+ # Update WCFMPage.PHP references
+ sed -i "s/index\..*\.css/$CSS_FILE/g" $WCFMPage_PATH
+ sed -i "s/index\..*\.js/$JS_FILE/g" $WCFMPage_PATH
+}
+
+if [ ! $1 ]; then
+ echo "Missing argument. Must be buildfm, copyfm, build-copyfm or rsync-wc"
+ exit
+fi
+
+buildwcfm() {
+ npm run build --prefix $WCFM_PATH
+}
+
+if [ $1 = "copyfm" ]; then
+ echo "copyfm"
+ copyassets
+fi
+
+if [ $1 = "buildfm" ]; then
+ echo "buildfm"
+ buildwcfm
+fi
+
+if [ $1 = "build-copyfm" ]; then
+ echo "build-copyfm"
+ buildwcfm
+ copyassets
+fi
+
+rsyncwc() {
+ rsync -avh --size-only --exclude '.git' --exclude '.github' --exclude='composer.lock' --exclude='scripts' --exclude='vendor/rosell-dk/webp-convert/.git' --exclude='vendor/rosell-dk/webp-convert/.git' --exclude='.gitignore' "$WC_PATH/src/" "$WE_PATH/vendor/rosell-dk/webp-convert/src" --delete
+}
+
+if [ $1 = "rsync-wc" ]; then
+ echo "rsync-wc"
+ rsyncwc
+fi
+```
+
+# Instruction for installing development version, for non-developers :)
+
+To install the development version:
+1) Go to https://wordpress.org/plugins/webp-express/advanced/
+2) Find the place where it says “Please select a specific version to download”
+3) Click “Download”
+4) Browse to /wp-admin/plugin-install.php (ie by going to the the Plugins page and clicking “Add new” button in the top)
+5) Click “Upload plugin” (button found in the top)
+6) The rest is easy
diff --git a/docs/publishing.md b/docs/publishing.md
new file mode 100755
index 0000000..35b8601
--- /dev/null
+++ b/docs/publishing.md
@@ -0,0 +1,151 @@
+*These instructions are actually just notes for myself*. Some commands work only in my environment.
+
+If it is only the README.txt that has been changed:
+
+1. Validate the readme: https://wordpress.org/plugins/developers/readme-validator/
+2. Update the tag below to current
+3. Update the commit message below
+4. Run the below
+```
+cd /var/www/we/svn/
+cp ~/github/webp-express/README.txt trunk
+cp ~/github/webp-express/README.txt tags/0.20.1
+svn status
+svn ci -m 'minor change in README (now tested with Wordpress 5.8 RC2)'
+```
+
+After that, check out if it is applied, on http://plugins.svn.wordpress.org/webp-express/
+
+and here:
+https://wordpress.org/plugins/webp-express/
+
+'changelog.txt' changed too?
+WELL - DON'T PUBLISH THAT, without publishing a new release. Wordfence will complain!
+
+-------------------
+
+
+
+
+
+before rsync, do this:
+
+- Run `composer update` in plugin root (and remove unneeded files. Check development.md !)
+ 1. `composer update`
+ 2. `composer dump-autoload -o`
+ 3.
+ rm -r vendor/rosell-dk/webp-convert/docs
+ rm -r vendor/rosell-dk/webp-convert/src/Helpers/*.txt
+ rm vendor/rosell-dk/dom-util-for-webp/phpstan.neon
+ rm composer.lock
+ rmdir vendor/bin
+
+- Make sure you remembered to update version in:
+ 1. the *webp-express.php* file
+ 2. in `lib/options/enqueue_scripts.php`
+ 3. in `lib/classes/ConvertHelperIndependent.php`
+ 4. in `README.txt` (Stable tag) - UNLESS IT IS A PRE-RELEASE :)
+- Perhaps make some final improvements of the readme.
+ Inspiration: https://www.smashingmagazine.com/2011/11/improve-wordpress-plugins-readme-txt/
+https://pippinsplugins.com/how-to-properly-format-and-enhance-your-plugins-readme-txt-file-for-the-wordpress-org-repository/
+- Make sure you upgraded the *Upgrade Notice* section.
+- Skim: https://codex.wordpress.org/Writing_a_Plugin
+- https://developer.wordpress.org/plugins/wordpress-org/
+- Validate the readme: https://wordpress.org/plugins/developers/readme-validator/
+- Make sure you have pushed the latest commits to github
+- Make sure you have released the new version on github
+
+And then:
+
+```
+cd /var/www/we/svn
+svn up
+```
+PS: On a new computer? - checkout first: `svn co http://plugins.svn.wordpress.org/webp-express/``
+
+
+If you have deleted folders (check with rsync --dry-run), then do this:
+```
+cd trunk
+svn delete [folder] (ie: svn delete lib/options/js/0.14.5). It is ok that the folder contains files
+svn ci -m 'deleted folder'
+```
+(workflow cycle: http://svnbook.red-bean.com/en/1.7/svn.tour.cycle.html)
+
+Then time to rsync into trunk:
+dry-run first:
+```
+cd /var/www/we/svn
+rsync -avh --dry-run --exclude '.git' --exclude '.github' --exclude='composer.lock' --exclude='scripts' --exclude='vendor/rosell-dk/webp-convert/.git' --exclude='vendor/rosell-dk/webp-convert/.git' --exclude='.gitignore' ~/github/webp-express/ /var/www/we/svn/trunk/ --delete
+```
+
+```
+cd /var/www/we/svn
+rsync -avh --exclude '.git' --exclude '.github' --exclude='composer.lock' --exclude='scripts' --exclude='vendor/rosell-dk/webp-convert/.git' --exclude='.gitignore' ~/github/webp-express/ /var/www/we/svn/trunk/ --delete
+```
+
+**It should NOT contain a long list of files! (unless you have run phpreplace)**
+
+*- and then WITHOUT "--dry-run" (remove "--dry-run" from above, and run)*
+
+
+## TESTING
+
+1. Create a zip
+ - Select all the files in trunk (not the trunk dir itself)
+ - Save it to /var/www/we/pre-releases/version-number/webp-express.zip
+2. Upload the zip to the LiteSpeed test site and test
+ - Login to https://betasite.com.br/rosell/wp-admin/plugins.php
+ - Go to Plugins | Add new and click the "Upload Plugin button"
+3. Upload the zip to other sites and test
+ - https://lutzenmanagement.dk/wp-admin/plugin-install.php
+ - http://mystress.dk/wp-admin/plugin-install.php
+ ... etc
+
+
+### Committing
+Add new and remove deleted (no need to do anything with the modified):
+```
+cd svn
+svn stat (to see what has changed)
+svn add --force . (this will add all new files - https://stackoverflow.com/questions/2120844/how-do-i-add-all-new-files-to-svn)
+svn status | grep '^!' (to see if any files have been deleted)
+svn status | grep '^!' | awk '{print $2}' | xargs svn delete --force (this will delete locally deleted files in the repository as well - see https://stackoverflow.com/questions/4608798/how-to-remove-all-deleted-files-from-repository/33139931)
+```
+
+Then add a new tag
+```
+cd svn
+svn cp trunk tags/0.25.8 (this will copy trunk into a new tag)
+```
+
+And commit!
+```
+svn ci -m '0.25.8'
+```
+
+
+After that, check out if it is applied, on http://plugins.svn.wordpress.org/webp-express/
+And then, of course, test the update
+... And THEN. Grab a beer and celebrate!
+
+And lastly, check if there are any new issues on https://coderisk.com
+
+
+# New:
+
+# svn co https://plugins.svn.wordpress.org/webp-express /var/www/webp-express-tests/svn
+
+
+BTW: Link til referral (optimole): https://app.impact.com/
+
+
+
+# On brand new system:
+
+1. Install svn
+`sudo apt-get install subversion`
+
+2. create dir for plugin, and `cd` into it
+3. Check out
+`svn co https://plugins.svn.wordpress.org/webp-express my-local-dir` (if in dir, replace "my-lockal-dir" with ".")
diff --git a/docs/regex.md b/docs/regex.md
new file mode 100644
index 0000000..0ca3049
--- /dev/null
+++ b/docs/regex.md
@@ -0,0 +1,154 @@
+Pattern used for image urls:
+ in attributes: https://regexr.com/46jat
+ in css: https://regexr.com/46jcg
+
+In case reqexr.com should be down, here are the content:
+
+
+# in attributes
+
+*pattern:*
+(?<=(?:<(img|source|input|iframe)[^>]*\s+(src|srcset|data-[^=]*)\s*=\s*[\"\']?))(?:[^\"\'>]+)(\.png|\.jp[e]?g)(\s\d+w)?(?=\/?[\"\'\s\>])
+
+*text:*
+Notice: The pattern is meant for PHP and contains syntax which only works in some browsers. It works in Chrome. Not in Firefox.
+
+The following should produce matches:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Common lazy load attributes are matched:
+
+
+



+
+
+
+
+

+
+
+
+
+
+
+src="http://example.com/header.jpeg"
+
+';
+}
+
+
+if (get_option('webp-express-alter-html-replacement') == 'picture') {
+ add_action( 'wp_head', 'webpExpressAddPictureJs');
+}
+
+if (get_option('webp-express-alter-html-hooks', 'ob') == 'ob') {
+ /* TODO:
+ Which hook should we use, and should we make it optional?
+ - Cache enabler uses 'template_redirect'
+ - ShortPixes uses 'init'
+
+ We go with template_redirect now, because it is the "innermost".
+ This lowers the risk of problems with plugins used rewriting URLs to point to CDN.
+ (We need to process the output *before* the other plugin has rewritten the URLs,
+ if the "Only for webps that exists" feature is enabled)
+ */
+ add_action( 'init', 'webpExpressOutputBuffer', 1 );
+ //add_action( 'template_redirect', 'webpExpressOutputBuffer', 1 );
+
+} else {
+ add_filter( 'the_content', 'webPExpressAlterHtml', 10000 ); // priority big, so it will be executed last
+ add_filter( 'the_excerpt', 'webPExpressAlterHtml', 10000 );
+ add_filter( 'post_thumbnail_html', 'webPExpressAlterHtml');
+}
+
+//echo wp_doing_ajax() ? 'ajax' : 'no ajax'; exit;
+//echo is_feed() ? 'feed' : 'no feed'; exit;
diff --git a/lib/classes/Actions.php b/lib/classes/Actions.php
new file mode 100644
index 0000000..aeb38b2
--- /dev/null
+++ b/lib/classes/Actions.php
@@ -0,0 +1,46 @@
+donate?',
+ ];
+ } else {
+ $mylinks = array(
+ 'Settings',
+ 'Provide coffee for the developer',
+ );
+
+ }
+ return array_merge($links, $mylinks);
+ }
+
+ // Add settings link in multisite
+ // The hook was registred in AdminInit
+ public static function networkPluginActionLinksFilter($links)
+ {
+ $mylinks = array(
+ 'Settings',
+ 'donate?',
+ );
+ return array_merge($links, $mylinks);
+ }
+
+
+ // callback for 'network_admin_menu' (registred in AdminInit)
+ public static function networAdminMenuHook()
+ {
+ add_submenu_page(
+ 'settings.php', // Parent element
+ 'WebP Express settings (for network)', // Text in browser title bar
+ 'WebP Express', // Text to be displayed in the menu.
+ 'manage_network_options', // Capability
+ 'webp_express_settings_page', // slug
+ array('\WebPExpress\OptionsPage', 'display') // Callback function which displays the page
+ );
+
+ add_submenu_page(
+ 'settings.php', // Parent element
+ 'WebP Express File Manager', //Page Title
+ 'WebP Express File Manager', //Menu Title
+ 'manage_network_options', //capability
+ 'webp_express_conversion_page', // slug
+ array('\WebPExpress\WCFMPage', 'display') //The function to be called to output the content for this page.
+ );
+
+ }
+
+ public static function adminMenuHookMultisite()
+ {
+ // Add Media page
+ /*
+ not ready - it should not display images for the other blogs!
+
+ add_submenu_page(
+ 'upload.php', // Parent element
+ 'WebP Express', //Page Title
+ 'WebP Express', //Menu Title
+ 'manage_network_options', //capability
+ 'webp_express_conversion_page', // slug
+ array('\WebPExpress\WCFMPage', 'display') //The function to be called to output the content for this page.
+ );
+ */
+
+ }
+
+ public static function adminMenuHook()
+ {
+ //Add Settings Page
+ add_options_page(
+ 'WebP Express Settings', //Page Title
+ 'WebP Express', //Menu Title
+ 'manage_options', //capability
+ 'webp_express_settings_page', // slug
+ array('\WebPExpress\OptionsPage', 'display') //The function to be called to output the content for this page.
+ );
+
+ // Add Media page
+ add_media_page(
+ 'WebP Express', //Page Title
+ 'WebP Express', //Menu Title
+ 'manage_options', //capability
+ 'webp_express_conversion_page', // slug
+ array('\WebPExpress\WCFMPage', 'display') //The function to be called to output the content for this page.
+ );
+
+ }
+}
diff --git a/lib/classes/AlterHtmlHelper.php b/lib/classes/AlterHtmlHelper.php
new file mode 100644
index 0000000..039ce4b
--- /dev/null
+++ b/lib/classes/AlterHtmlHelper.php
@@ -0,0 +1,377 @@
+ http
+ [host] => we0
+ [path] => /wordpress/uploads-moved
+ )*/
+
+ $imageUrlComponents = parse_url($imageUrl);
+ /* ie:
+ (
+ [scheme] => http
+ [host] => we0
+ [path] => /wordpress/uploads-moved/logo.jpg
+ )*/
+ if ($baseUrlComponents['host'] != $imageUrlComponents['host']) {
+ return false;
+ }
+
+ // Check if path begins with base path
+ if (strpos($imageUrlComponents['path'], $baseUrlComponents['path']) !== 0) {
+ return false;
+ }
+
+ // Remove base path from path (we know it begins with basepath, from previous check)
+ return substr($imageUrlComponents['path'], strlen($baseUrlComponents['path']));
+
+ }
+
+ /**
+ * Looks if $imageUrl is rooted in $baseUrl and if the file is there
+ * PS: NOT USED ANYMORE!
+ *
+ * @param $imageUrl (ie http://example.com/wp-content/image.jpg)
+ * @param $baseUrl (ie http://example.com/wp-content)
+ * @param $baseDir (ie /var/www/example.com/wp-content)
+ */
+ public static function isImageUrlHere($imageUrl, $baseUrl, $baseDir)
+ {
+
+ $srcPathRel = self::getRelUrlPath($imageUrl, $baseUrl);
+
+ if ($srcPathRel === false) {
+ return false;
+ }
+
+ // Calculate file path to src
+ $srcPathAbs = $baseDir . $srcPathRel;
+ //return 'dyt:' . $srcPathAbs;
+
+ // Check that src file exists
+ if (!@file_exists($srcPathAbs)) {
+ return false;
+ }
+
+ return true;
+
+ }
+
+ // NOT USED ANYMORE
+ public static function isSourceInUpload($src)
+ {
+ /* $src is ie http://we0/wp-content-moved/themes/twentyseventeen/assets/images/header.jpg */
+
+ $uploadDir = wp_upload_dir();
+ /* ie:
+
+ [path] => /var/www/webp-express-tests/we0/wordpress/uploads-moved
+ [url] => http://we0/wordpress/uploads-moved
+ [subdir] =>
+ [basedir] => /var/www/webp-express-tests/we0/wordpress/uploads-moved
+ [baseurl] => http://we0/wordpress/uploads-moved
+ [error] =>
+ */
+
+ return self::isImageUrlHere($src, $uploadDir['baseurl'], $uploadDir['basedir']);
+ }
+
+
+ /**
+ * Get url for webp from source url, (if ), given a certain baseUrl / baseDir.
+ * Base can for example be uploads or wp-content.
+ *
+ * returns false:
+ * - if no source file found in that base
+ * - if source file is found but webp file isn't there and the `only-for-webps-that-exists` option is set
+ * - if webp is marked as bigger than source
+ *
+ * @param string $sourceUrl Url of source image (ie http://example.com/wp-content/image.jpg)
+ * @param string $rootId Id (created in Config::updateAutoloadedOptions). Ie "uploads", "content" or any image root id
+ * @param string $baseUrl Base url of source image (ie http://example.com/wp-content)
+ * @param string $baseDir Base dir of source image (ie /var/www/example.com/wp-content)
+ */
+ public static function getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrl, $baseDir)
+ {
+
+
+ $srcPathRel = self::getRelUrlPath($sourceUrl, $baseUrl);
+
+ if ($srcPathRel === false) {
+ return false;
+ }
+
+ // Calculate file path to source
+ $srcPathAbs = $baseDir . $srcPathRel;
+
+ // Check that source file exists
+ if (!@file_exists($srcPathAbs)) {
+ return false;
+ }
+
+ if (file_exists($srcPathAbs . '.do-not-convert')) {
+ return false;
+ }
+ if (file_exists($srcPathAbs . '.dontreplace')) {
+ return false;
+ }
+
+ // Calculate destination of webp (both path and url)
+ // ----------------------------------------
+
+ // We are calculating: $destPathAbs and $destUrl.
+
+ // Make sure the options are loaded (and fixed)
+ self::getOptions();
+ $destinationOptions = new DestinationOptions(
+ self::$options['destination-folder'] == 'mingled',
+ self::$options['destination-structure'] == 'doc-root',
+ self::$options['destination-extension'] == 'set',
+ self::$options['scope']
+ );
+
+ if (!isset(self::$options['scope']) || !in_array($rootId, self::$options['scope'])) {
+ return false;
+ }
+
+ $destinationRoot = Paths::destinationRoot($rootId, $destinationOptions);
+
+ $relPathFromImageRootToSource = PathHelper::getRelDir(
+ realpath(Paths::getAbsDirById($rootId)), // note: In multisite (subfolders), it contains ie "/site/2/"
+ realpath($srcPathAbs)
+ );
+ $relPathFromImageRootToDest = ConvertHelperIndependent::appendOrSetExtension(
+ $relPathFromImageRootToSource,
+ self::$options['destination-folder'],
+ self::$options['destination-extension'],
+ ($rootId == 'uploads')
+ );
+ $destPathAbs = $destinationRoot['abs-path'] . '/' . $relPathFromImageRootToDest;
+ $webpMustExist = self::$options['only-for-webps-that-exists'];
+ if ($webpMustExist && (!@file_exists($destPathAbs))) {
+ return false;
+ }
+
+ // check if webp is marked as bigger than source
+ /*
+ $biggerThanSourcePath = Paths::getBiggerThanSourceDirAbs() . '/' . $rootId . '/' . $relPathFromImageRootToDest;
+ if (@file_exists($biggerThanSourcePath)) {
+ return false;
+ }*/
+
+ // check if webp is larger than original
+ if (self::$options['prevent-using-webps-larger-than-original']) {
+ if (BiggerThanSource::bigger($srcPathAbs, $destPathAbs)) {
+ return false;
+ }
+ }
+
+ $destUrl = $destinationRoot['url'] . '/' . $relPathFromImageRootToDest;
+
+ // Fix scheme (use same as source)
+ $sourceUrlComponents = parse_url($sourceUrl);
+ $destUrlComponents = parse_url($destUrl);
+ $port = isset($sourceUrlComponents['port']) ? ":" . $sourceUrlComponents['port'] : "";
+ $result = $sourceUrlComponents['scheme'] . '://' . $sourceUrlComponents['host'] . $port . $destUrlComponents['path'];
+
+ /*
+ error_log(
+ "getWebPUrlInImageRoot:\n" .
+ "- url: " . $sourceUrl . "\n" .
+ "- baseUrl: " . $baseUrl . "\n" .
+ "- baseDir: " . $baseDir . "\n" .
+ "- root id: " . $rootId . "\n" .
+ "- root abs: " . Paths::getAbsDirById($rootId) . "\n" .
+ "- destination root (abs): " . $destinationRoot['abs-path'] . "\n" .
+ "- destination root (url): " . $destinationRoot['url'] . "\n" .
+ "- rel: " . $srcPathRel . "\n" .
+ "- srcPathAbs: " . $srcPathAbs . "\n" .
+ '- relPathFromImageRootToSource: ' . $relPathFromImageRootToSource . "\n" .
+ '- get_blog_details()->path: ' . get_blog_details()->path . "\n" .
+ "- result: " . $result . "\n"
+ );*/
+ return $result;
+ }
+
+
+ /**
+ * Get url for webp
+ * returns second argument if no webp
+ *
+ * @param $sourceUrl
+ * @param $returnValueOnFail
+ */
+ public static function getWebPUrl($sourceUrl, $returnValueOnFail)
+ {
+ // Get the options
+ self::getOptions();
+
+ // Fail for webp-disabled browsers (when "only-for-webp-enabled-browsers" is set)
+ if (self::$options['only-for-webp-enabled-browsers']) {
+ if (!isset($_SERVER['HTTP_ACCEPT']) || (strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') === false)) {
+ return $returnValueOnFail;
+ }
+ }
+
+ // Fail for relative urls. Wordpress doesn't use such very much anyway
+ if (!preg_match('#^https?://#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+
+ // Fail if the image type isn't enabled
+ switch (self::$options['image-types']) {
+ case 0:
+ return $returnValueOnFail;
+ case 1:
+ if (!preg_match('#(jpe?g)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ case 2:
+ if (!preg_match('#(png)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ case 3:
+ if (!preg_match('#(jpe?g|png)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ }
+
+
+ //error_log('source url:' . $sourceUrl);
+
+ // Try all image roots
+ foreach (self::$options['scope'] as $rootId) {
+ $baseDir = Paths::getAbsDirById($rootId);
+ $baseUrl = Paths::getUrlById($rootId);
+
+ if (Multisite::isMultisite() && ($rootId == 'uploads')) {
+ $baseUrl = Paths::getUploadUrl();
+ $baseDir = Paths::getUploadDirAbs();
+ }
+
+ $result = self::getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrl, $baseDir);
+ if ($result !== false) {
+ return $result;
+ }
+
+ // Try the hostname aliases.
+ if (!isset(self::$options['hostname-aliases'])) {
+ continue;
+ }
+ $hostnameAliases = self::$options['hostname-aliases'];
+
+ $hostname = Paths::getHostNameOfUrl($baseUrl);
+ $baseUrlComponents = parse_url($baseUrl);
+ $sourceUrlComponents = parse_url($sourceUrl);
+ // ie: [scheme] => http, [host] => we0, [path] => /wordpress/uploads-moved
+
+ if ((!isset($baseUrlComponents['host'])) || (!isset($sourceUrlComponents['host']))) {
+ continue;
+ }
+
+ foreach ($hostnameAliases as $hostnameAlias) {
+
+ if ($sourceUrlComponents['host'] != $hostnameAlias) {
+ continue;
+ }
+ //error_log('hostname alias:' . $hostnameAlias);
+
+ $baseUrlOnAlias = $baseUrlComponents['scheme'] . '://' . $hostnameAlias . $baseUrlComponents['path'];
+ //error_log('baseurl (alias):' . $baseUrlOnAlias);
+
+ $result = self::getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrlOnAlias, $baseDir);
+ if ($result !== false) {
+ $resultUrlComponents = parse_url($result);
+ return $sourceUrlComponents['scheme'] . '://' . $hostnameAlias . $resultUrlComponents['path'];
+ }
+ }
+ }
+
+ return $returnValueOnFail;
+ }
+
+/*
+ public static function getWebPUrlOrSame($sourceUrl, $returnValueOnFail)
+ {
+ return self::getWebPUrl($sourceUrl, $sourceUrl);
+ }*/
+
+}
diff --git a/lib/classes/AlterHtmlImageUrls.php b/lib/classes/AlterHtmlImageUrls.php
new file mode 100644
index 0000000..67ec742
--- /dev/null
+++ b/lib/classes/AlterHtmlImageUrls.php
@@ -0,0 +1,33 @@
+ tag is not allowed
+ return $content;
+ }
+ }
+
+ if (!isset(self::$options)) {
+ self::$options = json_decode(Option::getOption('webp-express-alter-html-options', null), true);
+ //AlterHtmlHelper::$options = self::$options;
+ }
+
+ if (self::$options == null) {
+ return $content;
+ }
+
+ if (Option::getOption('webp-express-alter-html-replacement') == 'picture') {
+ require_once __DIR__ . "/../../vendor/autoload.php";
+ require_once __DIR__ . '/AlterHtmlHelper.php';
+ require_once __DIR__ . '/AlterHtmlPicture.php';
+ return \WebPExpress\AlterHtmlPicture::replace($content);
+ } else {
+ require_once __DIR__ . "/../../vendor/autoload.php";
+ require_once __DIR__ . '/AlterHtmlHelper.php';
+ require_once __DIR__ . '/AlterHtmlImageUrls.php';
+
+ return \WebPExpress\AlterHtmlImageUrls::replace($content);
+ }
+ }
+
+ public static function addPictureFillJs()
+ {
+ // Don't do anything with the RSS feed.
+ // - and no need for PictureJs in the admin
+ if ( is_feed() || is_admin() ) { return; }
+
+ echo '';
+ }
+
+ public static function sidebarBeforeAlterHtml()
+ {
+ ob_start();
+ }
+
+ public static function sidebarAfterAlterHtml()
+ {
+ $content = ob_get_clean();
+
+ echo self::alterHtml($content);
+
+ unset($content);
+ }
+
+ public static function setHooks() {
+
+ if (Option::getOption('webp-express-alter-html-add-picturefill-js')) {
+ add_action( 'wp_head', '\\WebPExpress\\AlterHtmlInit::addPictureFillJs');
+ }
+
+ if (Option::getOption('webp-express-alter-html-hooks', 'ob') == 'ob') {
+ /* TODO:
+ Which hook should we use, and should we make it optional?
+ - Cache enabler uses 'template_redirect'
+ - ShortPixes uses 'init'
+
+ We go with template_redirect now, because it is the "innermost".
+ This lowers the risk of problems with plugins used rewriting URLs to point to CDN.
+ (We need to process the output *before* the other plugin has rewritten the URLs,
+ if the "Only for webps that exists" feature is enabled)
+ */
+ add_action( 'init', '\\WebPExpress\\AlterHtmlInit::startOutputBuffer', 1 );
+ add_action( 'template_redirect', '\\WebPExpress\\AlterHtmlInit::startOutputBuffer', 10000 );
+
+ } else {
+ add_filter( 'the_content', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999 ); // priority big, so it will be executed last
+ add_filter( 'the_excerpt', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999 );
+ add_filter( 'post_thumbnail_html', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999);
+ add_filter( 'woocommerce_product_get_image', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999 );
+ add_filter( 'get_avatar', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999 );
+ add_filter( 'acf_the_content', '\\WebPExpress\\AlterHtmlInit::alterHtml', 99999 );
+ add_action( 'dynamic_sidebar_before', '\\WebPExpress\\AlterHtmlInit::sidebarBeforeAlterHtml', 0 );
+ add_action( 'dynamic_sidebar_after', '\\WebPExpress\\AlterHtmlInit::sidebarAfterAlterHtml', 1000 );
+
+
+ /*
+ TODO:
+ check out these hooks (used by Jetpack, in class.photon.php)
+
+ // Images in post content and galleries
+ add_filter( 'the_content', array( __CLASS__, 'filter_the_content' ), 999999 );
+ add_filter( 'get_post_galleries', array( __CLASS__, 'filter_the_galleries' ), 999999 );
+ add_filter( 'widget_media_image_instance', array( __CLASS__, 'filter_the_image_widget' ), 999999 );
+
+ // Core image retrieval
+ add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 10, 3 );
+ add_filter( 'rest_request_before_callbacks', array( $this, 'should_rest_photon_image_downsize' ), 10, 3 );
+ add_filter( 'rest_request_after_callbacks', array( $this, 'cleanup_rest_photon_image_downsize' ) );
+
+ // Responsive image srcset substitution
+ add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ), 10, 5 );
+ add_filter( 'wp_calculate_image_sizes', array( $this, 'filter_sizes' ), 1, 2 ); // Early so themes can still easily filter.
+
+ // Helpers for maniuplated images
+ add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 );
+ */
+ }
+ }
+
+}
diff --git a/lib/classes/AlterHtmlPicture.php b/lib/classes/AlterHtmlPicture.php
new file mode 100644
index 0000000..594177e
--- /dev/null
+++ b/lib/classes/AlterHtmlPicture.php
@@ -0,0 +1,18 @@
+ tag to a
';
+ echo 'to: ' . $toDir . '
';
+ echo 'ext:' . $fromExt . ' => ' . $toExt . '
';
+ echo '';*/
+
+ //error_log('move to:' . $toDir . ' ( ' . (file_exists($toDir) ? 'exists' : 'does not exist ') . ')');
+
+ //self::moveRecursively($toDir, $fromDir, $srcDir, $fromExt, $toExt);
+ }
+
+ /**
+ * @return [$numFilesMoved, $numFilesFailedMoving]
+ */
+ public static function moveRecursively($fromDir, $toDir, $srcDir, $fromExt, $toExt)
+ {
+ if (!@is_dir($fromDir)) {
+ return [0, 0];
+ }
+ if (!@file_exists($toDir)) {
+ // Note: 0777 is default. Default umask is 0022, so the default result is 0755
+ if (!@mkdir($toDir, 0777, true)) {
+ return [0, 0];
+ }
+ }
+
+ $numFilesMoved = 0;
+ $numFilesFailedMoving = 0;
+
+ //$filenames = @scandir($fromDir);
+ $fileIterator = new \FilesystemIterator($fromDir);
+
+ //foreach ($filenames as $filename) {
+ while ($fileIterator->valid()) {
+ $filename = $fileIterator->getFilename();
+
+ if (($filename != ".") && ($filename != "..")) {
+ //$filePerm = FileHelper::filePermWithFallback($filename, 0777);
+
+ if (@is_dir($fromDir . "/" . $filename)) {
+ list($r1, $r2) = self::moveRecursively($fromDir . "/" . $filename, $toDir . "/" . $filename, $srcDir . "/" . $filename, $fromExt, $toExt);
+ $numFilesMoved += $r1;
+ $numFilesFailedMoving += $r2;
+
+ // Remove dir, if its empty. But do not remove dirs in srcDir
+ if ($fromDir != $srcDir) {
+ $fileIterator2 = new \FilesystemIterator($fromDir . "/" . $filename);
+ $dirEmpty = !$fileIterator2->valid();
+ if ($dirEmpty) {
+ @rmdir($fromDir . "/" . $filename);
+ }
+ }
+ } else {
+ // its a file.
+ // check if its a webp
+ if (strpos($filename, '.webp', strlen($filename) - 5) !== false) {
+
+ $filenameWithoutWebp = substr($filename, 0, strlen($filename) - 5);
+ $srcFilePathWithoutWebp = $srcDir . "/" . $filenameWithoutWebp;
+
+ // check if a corresponding source file exists
+ $newFilename = null;
+ if (($fromExt == 'append') && (@file_exists($srcFilePathWithoutWebp))) {
+ if ($toExt == 'append') {
+ $newFilename = $filename;
+ } else {
+ // remove ".jpg" part of filename (or ".png")
+ $newFilename = preg_replace("/\.(jpe?g|png)\.webp$/", '.webp', $filename);
+ }
+ } elseif ($fromExt == 'set') {
+ if ($toExt == 'set') {
+ if (
+ @file_exists($srcFilePathWithoutWebp . ".jpg") ||
+ @file_exists($srcFilePathWithoutWebp . ".jpeg") ||
+ @file_exists($srcFilePathWithoutWebp . ".png")
+ ) {
+ $newFilename = $filename;
+ }
+ } else {
+ // append
+ if (@file_exists($srcFilePathWithoutWebp . ".jpg")) {
+ $newFilename = $filenameWithoutWebp . ".jpg.webp";
+ } elseif (@file_exists($srcFilePathWithoutWebp . ".jpeg")) {
+ $newFilename = $filenameWithoutWebp . ".jpeg.webp";
+ } elseif (@file_exists($srcFilePathWithoutWebp . ".png")) {
+ $newFilename = $filenameWithoutWebp . ".png.webp";
+ }
+ }
+ }
+
+ if ($newFilename !== null) {
+ //echo 'moving to: ' . $toDir . '/' .$newFilename . "
";
+ $toFilename = $toDir . "/" . $newFilename;
+ if (@rename($fromDir . "/" . $filename, $toFilename)) {
+ $numFilesMoved++;
+ } else {
+ $numFilesFailedMoving++;
+ }
+ }
+ }
+ }
+ }
+ $fileIterator->next();
+ }
+ return [$numFilesMoved, $numFilesFailedMoving];
+ }
+
+}
diff --git a/lib/classes/CachePurge.php b/lib/classes/CachePurge.php
new file mode 100644
index 0000000..e7d9697
--- /dev/null
+++ b/lib/classes/CachePurge.php
@@ -0,0 +1,171 @@
+ $onlyPng,
+ 'only-with-corresponding-original' => false
+ ];
+
+ $numDeleted = 0;
+ $numFailed = 0;
+
+ list($numDeleted, $numFailed) = self::purgeWebPFilesInDir(Paths::getCacheDirAbs(), $filter, $config);
+ FileHelper::removeEmptySubFolders(Paths::getCacheDirAbs());
+
+ if ($config['destination-folder'] == 'mingled') {
+ list($d, $f) = self::purgeWebPFilesInDir(Paths::getUploadDirAbs(), $filter, $config);
+
+ $numDeleted += $d;
+ $numFailed += $f;
+ }
+
+ // Now, purge dummy files too
+ $dir = Paths::getBiggerThanSourceDirAbs();
+ self::purgeWebPFilesInDir($dir, $filter, $config);
+ FileHelper::removeEmptySubFolders($dir);
+
+ return [
+ 'delete-count' => $numDeleted,
+ 'fail-count' => $numFailed
+ ];
+
+ //$successInRemovingCacheDir = FileHelper::rrmdir(Paths::getCacheDirAbs());
+
+ }
+
+
+ /**
+ * Purge webp files in a dir
+ * Warning: the "only-png" option only works for mingled mode.
+ * (when not mingled, you can simply delete the whole cache dir instead)
+ *
+ * @param $filter.
+ * only-png: If true, it will only be deleted if extension is .png.webp or a corresponding png exists.
+ *
+ * @return [num files deleted, num files failed to delete]
+ */
+ private static function purgeWebPFilesInDir($dir, &$filter, &$config)
+ {
+ if (!@file_exists($dir) || !@is_dir($dir)) {
+ return [0, 0];
+ }
+
+ $numFilesDeleted = 0;
+ $numFilesFailedDeleting = 0;
+
+ $fileIterator = new \FilesystemIterator($dir);
+ while ($fileIterator->valid()) {
+ $filename = $fileIterator->getFilename();
+
+ if (($filename != ".") && ($filename != "..")) {
+
+ if (@is_dir($dir . "/" . $filename)) {
+ list($r1, $r2) = self::purgeWebPFilesInDir($dir . "/" . $filename, $filter, $config);
+ $numFilesDeleted += $r1;
+ $numFilesFailedDeleting += $r2;
+ } else {
+
+ // its a file
+ // Run through filters, which each may set "skipThis" to true
+
+ $skipThis = false;
+
+ // filter: It must be a webp
+ if (!$skipThis && !preg_match('#\.webp$#', $filename)) {
+ $skipThis = true;
+ }
+
+ // filter: only with corresponding original
+ $source = '';
+ if (!$skipThis && $filter['only-with-corresponding-original']) {
+ $source = Convert::findSource($dir . "/" . $filename, $config);
+ if ($source === false) {
+ $skipThis = true;
+ }
+ }
+
+ // filter: only png
+ if (!$skipThis && $filter['only-png']) {
+
+ // turn logic around - we skip deletion, unless we deem it a png
+ $skipThis = true;
+
+ // If extension is "png.webp", its a png
+ if (preg_match('#\.png\.webp$#', $filename)) {
+ // its a png
+ $skipThis = false;
+ } else {
+ if (preg_match('#\.jpe?g\.webp$#', $filename)) {
+ // It is a jpeg, no need to investigate further.
+ } else {
+
+ if (!$filter['only-with-corresponding-original']) {
+ $source = Convert::findSource($dir . "/" . $filename, $config);
+ }
+ if ($source === false) {
+ // We could not find corresponding source.
+ // Should we delete?
+ // No, I guess we need more evidence, so we skip
+ // In the future, we could detect mime
+ } else {
+ if (preg_match('#\.png$#', $source)) {
+ // its a png
+ $skipThis = false;
+ }
+ }
+ }
+
+ }
+
+ }
+
+ if (!$skipThis) {
+ if (@unlink($dir . "/" . $filename)) {
+ $numFilesDeleted++;
+ } else {
+ $numFilesFailedDeleting++;
+ }
+ }
+ }
+ }
+ $fileIterator->next();
+ }
+ return [$numFilesDeleted, $numFilesFailedDeleting];
+ }
+
+ public static function processAjaxPurgeCache()
+ {
+
+ if (!check_ajax_referer('webpexpress-ajax-purge-cache-nonce', 'nonce', false)) {
+ wp_send_json_error('The security nonce has expired. You need to reload the settings page (press F5) and try again)');
+ wp_die();
+ }
+
+ $onlyPng = (sanitize_text_field($_POST['only-png']) == 'true');
+
+ $config = Config::loadConfigAndFix();
+ $result = self::purge($config, $onlyPng);
+
+ echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
+ wp_die();
+ }
+}
diff --git a/lib/classes/CapabilityTest.php b/lib/classes/CapabilityTest.php
new file mode 100644
index 0000000..c01a689
--- /dev/null
+++ b/lib/classes/CapabilityTest.php
@@ -0,0 +1,103 @@
+';
+ if (!@file_exists(Paths::getWebPExpressPluginDirAbs() . '/htaccess-capability-tests/' . $testDir)) {
+ // test does not even exist
+ //echo 'test does not exist: ' . $testDir . '
';
+ return null;
+ }
+
+ if (!@file_exists(Paths::getWebPExpressContentDirAbs() . '/htaccess-capability-tests/' . $testDir)) {
+ self::copyCapabilityTestsToWpContent();
+ }
+
+ // If copy failed, we can use the test in plugin path
+ if (!@file_exists(Paths::getWebPExpressContentDirAbs() . '/htaccess-capability-tests/' . $testDir)) {
+ $testUrl = Paths::getContentUrl() . '/' . 'webp-express/htaccess-capability-tests/' . $testDir . '/test.php';
+ } else {
+ $testUrl = Paths::getWebPExpressPluginUrl() . '/' . 'htaccess-capability-tests/' . $testDir . '/test.php';
+ }
+
+ //echo 'test url: ' . $testUrl . '
';
+ // TODO: Should we test if wp_remote_get exists first? - and if not, include wp-includes/http.php ?
+
+ $response = wp_remote_get($testUrl, ['timeout' => 10]);
+ //echo '' . print_r($response, true) . '
';
+ if (wp_remote_retrieve_response_code($response) != '200') {
+ return null;
+ }
+ $responseBody = wp_remote_retrieve_body($response);
+ if ($responseBody == '') {
+ return null; // Some failure
+ }
+ if ($responseBody == '0') {
+ return false;
+ }
+ if ($responseBody == '1') {
+ return true;
+ }
+ return null;
+ }
+
+
+ /**
+ * Three possible outcomes: true, false or null (null if failed to run test)
+ */
+ public static function modRewriteWorking()
+ {
+ return self::runTest('has-mod-rewrite');
+ }
+
+ /**
+ * Three possible outcomes: true, false or null (null if failed to run test)
+ */
+ public static function modHeaderWorking()
+ {
+ return self::runTest('has-mod-header');
+ }
+
+ /**
+ * Three possible outcomes: true, false or null (null if failed to run test)
+ */
+ public static function passThroughEnvWorking()
+ {
+ return self::runTest('pass-through-environment-var');
+ }
+
+ /**
+ * Three possible outcomes: true, false or null (null if failed to run test)
+ */
+ public static function passThroughHeaderWorking()
+ {
+ // pretend it fails because .htaccess rules aren't currently generated correctly
+ return false;
+ return self::runTest('pass-server-var-through-header');
+ }
+
+}
diff --git a/lib/classes/Config.php b/lib/classes/Config.php
new file mode 100644
index 0000000..450fdb5
--- /dev/null
+++ b/lib/classes/Config.php
@@ -0,0 +1,755 @@
+ 'varied-image-responses',
+
+ // general
+ 'image-types' => 3,
+ 'destination-folder' => 'separate',
+ 'destination-extension' => 'append',
+ 'destination-structure' => (PlatformInfo::isNginx() ? 'doc-root' : 'image-roots'),
+ 'cache-control' => 'no-header', /* can be "no-header", "set" or "custom" */
+ 'cache-control-custom' => 'public, max-age=31536000, stale-while-revalidate=604800, stale-if-error=604800',
+ 'cache-control-max-age' => 'one-week',
+ 'cache-control-public' => false,
+ 'scope' => ['themes', 'uploads'],
+ 'enable-logging' => false,
+ 'prevent-using-webps-larger-than-original' => true,
+
+ // redirection rules
+ 'enable-redirection-to-converter' => true,
+ 'only-redirect-to-converter-on-cache-miss' => false,
+ 'only-redirect-to-converter-for-webp-enabled-browsers' => true,
+ 'do-not-pass-source-in-query-string' => false, // In 0.13 we can remove this. migration7.php depends on it
+ 'redirect-to-existing-in-htaccess' => true,
+ 'forward-query-string' => false,
+ 'enable-redirection-to-webp-realizer' => true,
+
+ // conversion options
+ 'jpeg-encoding' => 'auto',
+ 'jpeg-enable-near-lossless' => true,
+ 'jpeg-near-lossless' => 60,
+ 'quality-auto' => $qualityAuto,
+ 'max-quality' => 80,
+ 'quality-specific' => 70,
+
+ 'png-encoding' => 'auto',
+ 'png-enable-near-lossless' => true,
+ 'png-near-lossless' => 60,
+ 'png-quality' => 85,
+ 'alpha-quality' => 80,
+
+ 'converters' => [],
+ 'metadata' => 'none',
+ //'log-call-arguments' => true,
+ 'convert-on-upload' => false,
+
+ // serve options
+ 'fail' => 'original',
+ 'success-response' => 'converted',
+
+ // alter html options
+ 'alter-html' => [
+ 'enabled' => false,
+ 'replacement' => 'picture', // "picture" or "url"
+ 'hooks' => 'ob', // "content-hooks" or "ob"
+ 'only-for-webp-enabled-browsers' => true, // If true, there will be two HTML versions of each page
+ 'only-for-webps-that-exists' => false,
+ 'alter-html-add-picturefill-js' => true,
+ 'hostname-aliases' => []
+ ],
+
+ // web service
+ 'web-service' => [
+ 'enabled' => false,
+ 'whitelist' => [
+ /*[
+ 'uid' => '', // for internal purposes
+ 'label' => '', // ie website name. It is just for display
+ 'ip' => '', // restrict to these ips. * pattern is allowed.
+ 'api-key' => '', // Api key for the entry. Not neccessarily unique for the entry
+ //'quota' => 60
+ ]
+ */
+ ]
+ ],
+
+ 'environment-when-config-was-saved' => [
+ 'doc-root-available' => null, // null means unavailable
+ 'doc-root-resolvable' => null,
+ 'doc-root-usable-for-structuring' => null,
+ 'image-roots' => null,
+ ]
+ ];
+ }
+
+ /**
+ * Apply operation mode (set the hidden defaults that comes along with the mode)
+ * @return An altered configuration array
+ */
+ public static function applyOperationMode($config)
+ {
+ if (!isset($config['operation-mode'])) {
+ $config['operation-mode'] = 'varied-image-responses';
+ }
+
+ if ($config['operation-mode'] == 'varied-image-responses') {
+ $config = array_merge($config, [
+ //'redirect-to-existing-in-htaccess' => true, // this can now be configured, so do not apply
+ //'enable-redirection-to-converter' => true, // this can now be configured, so do not apply
+ 'only-redirect-to-converter-for-webp-enabled-browsers' => true,
+ 'only-redirect-to-converter-on-cache-miss' => false,
+ 'do-not-pass-source-in-query-string' => true, // Will be removed in 0.13
+ 'fail' => 'original',
+ 'success-response' => 'converted',
+ ]);
+ } elseif ($config['operation-mode'] == 'cdn-friendly') {
+ $config = array_merge($config, [
+ 'redirect-to-existing-in-htaccess' => false,
+ 'enable-redirection-to-converter' => false,
+ /*
+ 'only-redirect-to-converter-for-webp-enabled-browsers' => false,
+ 'only-redirect-to-converter-on-cache-miss' => true,
+ */
+ 'do-not-pass-source-in-query-string' => true, // Will be removed in 0.13
+ 'fail' => 'original',
+ 'success-response' => 'original',
+ // cache-control => 'no-header' (we do not need this, as it is not important what it is set to in cdn-friendly mode, and we dont the value to be lost when switching operation mode)
+ ]);
+ } elseif ($config['operation-mode'] == 'no-conversion') {
+
+ // TODO: Go through these...
+
+ $config = array_merge($config, [
+ 'enable-redirection-to-converter' => false,
+ 'destination-folder' => 'mingled',
+ 'enable-redirection-to-webp-realizer' => false,
+ ]);
+ $config['alter-html']['only-for-webps-that-exists'] = true;
+ $config['web-service']['enabled'] = false;
+ $config['scope'] = ['uploads'];
+
+ }
+
+ return $config;
+ }
+
+ /**
+ * Fix config.
+ *
+ * Among other things, the config is merged with default config, to ensure all options are present
+ *
+ */
+ public static function fix($config, $checkQualityDetection = true)
+ {
+ if ($config === false) {
+ $config = self::getDefaultConfig(!$checkQualityDetection);
+ } else {
+ if ($checkQualityDetection) {
+ if (isset($config['quality-auto']) && ($config['quality-auto'])) {
+ $qualityDetectionWorking = TestRun::isLocalQualityDetectionWorking();
+ if (!TestRun::isLocalQualityDetectionWorking()) {
+ $config['quality-auto'] = false;
+ }
+ }
+ }
+ $defaultConfig = self::getDefaultConfig(true);
+ $config = array_merge($defaultConfig, $config);
+
+ // Make sure new defaults below "alter-html" are added into the existing array
+ // (note that this will not remove old unused properties, if some key should become obsolete)
+ $config['alter-html'] = array_replace_recursive($defaultConfig['alter-html'], $config['alter-html']);
+
+ // Make sure new defaults below "environment-when-config-was-saved" are added into the existing array
+ $config['environment-when-config-was-saved'] = array_replace_recursive($defaultConfig['environment-when-config-was-saved'], $config['environment-when-config-was-saved']);
+ }
+
+ if (!isset($config['base-htaccess-on-these-capability-tests'])) {
+ self::runAndStoreCapabilityTests($config);
+ }
+
+ // Apparently, migrate7 did not fix old "operation-mode" values for all.
+ // So fix here
+ if ($config['operation-mode'] == 'just-redirect') {
+ $config['operation-mode'] = 'no-conversion';
+ }
+ if ($config['operation-mode'] == 'no-varied-responses') {
+ $config['operation-mode'] = 'cdn-friendly';
+ }
+ if ($config['operation-mode'] == 'varied-responses') {
+ $config['operation-mode'] = 'varied-image-responses';
+ }
+
+ // In case doc root no longer can be used, use image-roots
+ // Or? No, changing here will not fix it for WebPOnDemand.php.
+ // An invalid setting requires that config is saved again and .htaccess files regenerated.
+ /*
+ if (($config['operation-mode'] == 'doc-root') && (!Paths::canUseDocRootForRelPaths())) {
+ $config['destination-structure'] = 'image-roots';
+ }*/
+
+ $config = self::applyOperationMode($config);
+
+ // Fix scope: Remove invalid and put in correct order
+ $fixedScope = [];
+ foreach (Paths::getImageRootIds() as $rootId) {
+ if (in_array($rootId, $config['scope'])) {
+ $fixedScope[] = $rootId;
+ }
+ }
+ $config['scope'] = $fixedScope;
+
+ if (!isset($config['web-service'])) {
+ $config['web-service'] = [
+ 'enabled' => false
+ ];
+ }
+ if (!is_array($config['web-service']['whitelist'])) {
+ $config['web-service']['whitelist'] = [];
+ }
+ // remove whitelist entries without required fields (label, ip)
+ $config['web-service']['whitelist'] = array_filter($config['web-service']['whitelist'], function($var) {
+ return (isset($var['label']) && (isset($var['ip'])));
+ });
+
+ if (($config['cache-control'] == 'set') && ($config['cache-control-max-age'] == '')) {
+ $config['cache-control-max-age'] = 'one-week';
+ }
+
+ /*if (is_null($config['alter-html']['hostname-aliases'])) {
+ $config['alter-html']['hostname-aliases'] = [];
+ }*/
+
+ if (!is_array($config['converters'])) {
+ $config['converters'] = [];
+ }
+
+ if (count($config['converters']) > 0) {
+ // merge missing converters in
+ $config['converters'] = ConvertersHelper::mergeConverters(
+ $config['converters'],
+ ConvertersHelper::$defaultConverters
+ );
+ } else {
+ // This is first time visit!
+ $config['converters'] = ConvertersHelper::$defaultConverters;
+ }
+
+
+ return $config;
+ }
+
+
+ public static function runAndStoreCapabilityTests(&$config)
+ {
+ $config['base-htaccess-on-these-capability-tests'] = [
+ 'passThroughHeaderWorking' => HTAccessCapabilityTestRunner::passThroughHeaderWorking(),
+ 'passThroughEnvWorking' => HTAccessCapabilityTestRunner::passThroughEnvWorking(),
+ 'modHeaderWorking' => HTAccessCapabilityTestRunner::modHeaderWorking(),
+ //'grantAllAllowed' => HTAccessCapabilityTestRunner::grantAllAllowed(),
+ 'canRunTestScriptInWOD' => HTAccessCapabilityTestRunner::canRunTestScriptInWOD(),
+ 'canRunTestScriptInWOD2' => HTAccessCapabilityTestRunner::canRunTestScriptInWOD2(),
+ ];
+ }
+
+ /**
+ * Loads Config (if available), fills in the rest with defaults
+ * also applies operation mode.
+ * If config is not saved yet, the default config will be returned
+ */
+ public static function loadConfigAndFix($checkQualityDetection = true)
+ {
+ // PS: Yes, loadConfig may return false. "fix" handles this by returning default config
+ return self::fix(Config::loadConfig(), $checkQualityDetection);
+ }
+
+ /**
+ * Run a fresh test on all converters and update their statuses in the config.
+ *
+ * @param object config to be updated
+ * @return object Updated config
+ */
+ public static function updateConverterStatusWithFreshTest($config) {
+ // Test converters
+ $testResult = TestRun::getConverterStatus();
+
+ // Set "working" and "error" properties
+ if ($testResult) {
+ foreach ($config['converters'] as &$converter) {
+ $converterId = $converter['converter'];
+ $hasError = isset($testResult['errors'][$converterId]);
+ $hasWarning = isset($testResult['warnings'][$converterId]);
+ $working = !$hasError;
+
+ /*
+ Don't print this stuff here. It can end up in the head tag.
+ TODO: Move it somewhere
+ if (isset($converter['working']) && ($converter['working'] != $working)) {
+
+ // TODO: webpexpress_converterName($converterId)
+ if ($working) {
+ Messenger::printMessage(
+ 'info',
+ 'Hurray! - The ' . $converterId . ' conversion method is working now!'
+ );
+ } else {
+ Messenger::printMessage(
+ 'warning',
+ 'Sad news. The ' . $converterId . ' conversion method is not working anymore. What happened?'
+ );
+ }
+ }
+ */
+ $converter['working'] = $working;
+ if ($hasError) {
+ $error = $testResult['errors'][$converterId];
+ if ($converterId == 'wpc') {
+ if (preg_match('/Missing URL/', $error)) {
+ $error = 'Not configured';
+ }
+ if ($error == 'No remote host has been set up') {
+ $error = 'Not configured';
+ }
+
+ if (preg_match('/cloud service is not enabled/', $error)) {
+ $error = 'The server is not enabled. Click the "Enable web service" on WebP Express settings on the site you are trying to connect to.';
+ }
+ }
+ $converter['error'] = $error;
+ } else {
+ unset($converter['error']);
+ }
+ if ($hasWarning) {
+ $converter['warnings'] = $testResult['warnings'][$converterId];
+ }
+ }
+ }
+ return $config;
+ }
+
+
+ public static $configForOptionsPage = null; // cache the result (called twice, - also in enqueue_scripts)
+ public static function getConfigForOptionsPage()
+ {
+ if (isset(self::$configForOptionsPage)) {
+ return self::$configForOptionsPage;
+ }
+
+
+ $config = self::loadConfigAndFix();
+
+ // Remove keys in whitelist (so they cannot easily be picked up by examining the html)
+ foreach ($config['web-service']['whitelist'] as &$whitelistEntry) {
+ unset($whitelistEntry['api-key']);
+ }
+
+ // Remove keys from WPC converters
+ foreach ($config['converters'] as &$converter) {
+ if (isset($converter['converter']) && ($converter['converter'] == 'wpc')) {
+ if (isset($converter['options']['api-key'])) {
+ if ($converter['options']['api-key'] != '') {
+ $converter['options']['_api-key-non-empty'] = true;
+ }
+ unset($converter['options']['api-key']);
+ }
+ }
+ }
+
+ if ($config['operation-mode'] != 'no-conversion') {
+ $config = self::updateConverterStatusWithFreshTest($config);
+ }
+
+ self::$configForOptionsPage = $config; // cache the result
+ return $config;
+ }
+
+ public static function isConfigFileThere()
+ {
+ return (FileHelper::fileExists(Paths::getConfigFileName()));
+ }
+
+ public static function isConfigFileThereAndOk()
+ {
+ return (self::loadConfig() !== false);
+ }
+
+ public static function loadWodOptions()
+ {
+ return FileHelper::loadJSONOptions(Paths::getWodOptionsFileName());
+ }
+
+ /**
+ * Some of the options in config needs to be quickly accessible
+ * These are stored in wordpress autoloaded options
+ */
+ public static function updateAutoloadedOptions($config)
+ {
+ $config = self::fix($config, false);
+
+ Option::updateOption('webp-express-alter-html', $config['alter-html']['enabled'], true);
+ Option::updateOption('webp-express-alter-html-hooks', $config['alter-html']['hooks'], true);
+ Option::updateOption('webp-express-alter-html-replacement', $config['alter-html']['replacement'], true);
+ Option::updateOption('webp-express-alter-html-add-picturefill-js', (($config['alter-html']['replacement'] == 'picture') && (isset($config['alter-html']['alter-html-add-picturefill-js']) && $config['alter-html']['alter-html-add-picturefill-js'])), true);
+
+
+ //Option::updateOption('webp-express-alter-html', $config['alter-html']['enabled'], true);
+
+ $obj = $config['alter-html'];
+ unset($obj['enabled']);
+ $obj['destination-folder'] = $config['destination-folder'];
+ $obj['destination-extension'] = $config['destination-extension'];
+ $obj['destination-structure'] = $config['destination-structure'];
+ $obj['scope'] = $config['scope'];
+ $obj['image-types'] = $config['image-types']; // 0=none,1=jpg, 2=png, 3=both
+ $obj['prevent-using-webps-larger-than-original'] = $config['prevent-using-webps-larger-than-original'];
+
+ Option::updateOption(
+ 'webp-express-alter-html-options',
+ json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK),
+ true
+ );
+ }
+
+ /**
+ * Save configuration file. Also updates autoloaded options (such as alter html options)
+ */
+ public static function saveConfigurationFile($config)
+ {
+ $config['paths-used-in-htaccess'] = [
+ 'wod-url-path' => Paths::getWodUrlPath(),
+ ];
+
+ if (Paths::createConfigDirIfMissing()) {
+ $success = FileHelper::saveJSONOptions(Paths::getConfigFileName(), $config);
+ if ($success) {
+ State::setState('configured', true);
+ self::updateAutoloadedOptions($config);
+ }
+
+ return $success;
+ }
+ return false;
+ }
+
+ public static function getCacheControlHeader($config) {
+ $cacheControl = $config['cache-control'];
+ switch ($cacheControl) {
+ case 'custom':
+ return $config['cache-control-custom'];
+ case 'no-header':
+ return '';
+ default:
+ $public = (isset($config['cache-control-public']) ? $config['cache-control-public'] : true);
+ $maxAge = (isset($config['cache-control-max-age']) ? $config['cache-control-max-age'] : $cacheControl);
+ $maxAgeOptions = [
+ '' => 'max-age=604800', // it has happened, but I don't think it can happen again...
+ 'one-second' => 'max-age=1',
+ 'one-minute' => 'max-age=60',
+ 'one-hour' => 'max-age=3600',
+ 'one-day' => 'max-age=86400',
+ 'one-week' => 'max-age=604800',
+ 'one-month' => 'max-age=2592000',
+ 'one-year' => 'max-age=31536000',
+ ];
+ return ($public ? 'public, ' : 'private, ') . $maxAgeOptions[$maxAge];
+ }
+
+ }
+
+ public static function generateWodOptionsFromConfigObj($config)
+ {
+
+ // WebP convert options
+ // --------------------
+ $wc = [
+ 'converters' => []
+ ];
+
+ // Add active converters
+ foreach ($config['converters'] as $converter) {
+ if (isset($converter['deactivated']) && ($converter['deactivated'])) {
+ continue;
+ }
+ $wc['converters'][] = $converter;
+ }
+
+ // Clean the converter options from junk
+ foreach ($wc['converters'] as &$c) {
+
+ // In cwebp converter options (here in webp express), we have a checkbox "set size"
+ // - there is no such option in webp-convert - so remove.
+ if ($c['converter'] == 'cwebp') {
+ if (isset($c['options']['set-size']) && $c['options']['set-size']) {
+ unset($c['options']['set-size']);
+ } else {
+ unset($c['options']['set-size']);
+ unset($c['options']['size-in-percentage']);
+ }
+ }
+
+ if ($c['converter'] == 'ewww') {
+ $c['options']['check-key-status-before-converting'] = false;
+ }
+
+ // 'id', 'working' and 'error' attributes are used internally in webp-express,
+ // no need to have it in the wod configuration file.
+ unset ($c['id']);
+ unset($c['working']);
+ unset($c['error']);
+
+ if (isset($c['options']['quality']) && ($c['options']['quality'] == 'inherit')) {
+ unset ($c['options']['quality']);
+ }
+ /*
+ if (!isset($c['options'])) {
+ $c = $c['converter'];
+ }*/
+ }
+
+ // Create jpeg options
+ // https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/converting/introduction-for-converting.md#png-og-jpeg-specific-options
+
+ $auto = (isset($config['quality-auto']) && $config['quality-auto']);
+ $wc['jpeg'] = [
+ 'encoding' => $config['jpeg-encoding'],
+ 'quality' => ($auto ? 'auto' : $config['quality-specific']),
+ ];
+ if ($auto) {
+ $wc['jpeg']['default-quality'] = $config['quality-specific'];
+ $wc['jpeg']['max-quality'] = $config['max-quality'];
+ }
+ if ($config['jpeg-encoding'] != 'lossy') {
+ if ($config['jpeg-enable-near-lossless']) {
+ $wc['jpeg']['near-lossless'] = $config['jpeg-near-lossless'];
+ } else {
+ $wc['jpeg']['near-lossless'] = 100;
+ }
+ }
+
+ // Create png options
+ // ---
+ $wc['png'] = [
+ 'encoding' => $config['png-encoding'],
+ 'quality' => $config['png-quality'],
+ ];
+ if ($config['png-encoding'] != 'lossy') {
+ if ($config['png-enable-near-lossless']) {
+ $wc['png']['near-lossless'] = $config['png-near-lossless'];
+ } else {
+ $wc['png']['near-lossless'] = 100;
+ }
+ }
+ if ($config['png-encoding'] != 'lossless') {
+ // Only relevant for pngs, and only for "lossy" (and thus also "auto")
+ $wc['png']['alpha-quality'] = $config['alpha-quality'];
+ }
+
+ // Other convert options
+ $wc['metadata'] = $config['metadata'];
+ $wc['log-call-arguments'] = true; // $config['log-call-arguments'];
+
+ // Serve options
+ // -------------
+ $serve = [
+ 'serve-image' => [
+ 'headers' => [
+ 'cache-control' => false,
+ 'content-length' => true,
+ 'content-type' => true,
+ 'expires' => false,
+ 'last-modified' => true,
+ //'vary-accept' => false // This must be different for webp-on-demand and webp-realizer
+ ]
+ ]
+ ];
+ if ($config['cache-control'] != 'no-header') {
+ $serve['serve-image']['cache-control-header'] = self::getCacheControlHeader($config);
+ $serve['serve-image']['headers']['cache-control'] = true;
+ $serve['serve-image']['headers']['expires'] = true;
+ }
+ $serve['fail'] = $config['fail'];
+
+
+ // WOD options
+ // -------------
+ $wod = [
+ 'enable-logging' => $config['enable-logging'],
+ 'enable-redirection-to-converter' => $config['enable-redirection-to-converter'],
+ 'enable-redirection-to-webp-realizer' => $config['enable-redirection-to-webp-realizer'],
+ 'base-htaccess-on-these-capability-tests' => $config['base-htaccess-on-these-capability-tests'],
+ 'destination-extension' => $config['destination-extension'],
+ 'destination-folder' => $config['destination-folder'],
+ 'forward-query-string' => $config['forward-query-string'],
+ //'method-for-passing-source' => $config['method-for-passing-source'],
+ 'image-roots' => Paths::getImageRootsDef(),
+ 'success-response' => $config['success-response'],
+ ];
+
+
+ // Put it all together
+ // -------------
+
+ //$options = array_merge($wc, $serve, $wod);
+
+ // I'd like to put the webp-convert options in its own key,
+ // but it requires some work. Postponing it to another day that I can uncomment the two next lines (and remove the one above)
+ //$wc = array_merge($wc, $serve);
+ //$options = array_merge($wod, ['webp-convert' => $wc]);
+
+ //$options = array_merge($wod, array_merge($serve, ['conversion' => $wc]));
+
+ $options = [
+ 'wod' => $wod,
+ 'webp-convert' => array_merge($serve, ['convert' => $wc])
+ ];
+
+
+ return $options;
+ }
+
+ public static function saveWodOptionsFile($options)
+ {
+ if (Paths::createConfigDirIfMissing()) {
+ return FileHelper::saveJSONOptions(Paths::getWodOptionsFileName(), $options);
+ }
+ return false;
+ }
+
+
+ /**
+ * Save both configuration files, but do not update htaccess
+ * Returns success (boolean)
+ */
+ public static function saveConfigurationFileAndWodOptions($config)
+ {
+ if (!isset($config['base-htaccess-on-these-capability-tests'])) {
+ self::runAndStoreCapabilityTests($config);
+ }
+ if (!(self::saveConfigurationFile($config))) {
+ return false;
+ }
+ $options = self::generateWodOptionsFromConfigObj($config);
+ return (self::saveWodOptionsFile($options));
+ }
+
+ /**
+ * Regenerate config and .htaccess files
+ *
+ * It will only happen if configuration file exists. So the method is meant for updating - ie upon migration.
+ * It updates:
+ * - config files (both) - and ensures that capability tests have been run
+ * - autoloaded options (such as alter html options)
+ * - .htaccess files (all)
+ */
+ public static function regenerateConfigAndHtaccessFiles() {
+ self::regenerateConfig(true);
+ }
+
+ /**
+ * Regenerate config and .htaccess files
+ *
+ * It will only happen if configuration file exists. So the method is meant for updating - ie upon migration.
+ * It updates:
+ * - config files (both) - and ensures that capability tests have been run
+ * - autoloaded options (such as alter html options)
+ * - .htaccess files - but only if needed due to configuration changes
+ */
+ public static function regenerateConfig($forceRuleUpdating = false) {
+ if (!self::isConfigFileThere()) {
+ return;
+ }
+ $config = self::loadConfig();
+ $config = self::fix($config, false); // fix. We do not need examining if quality detection is working
+ if ($config === false) {
+ return;
+ }
+ self::saveConfigurationAndHTAccess($config, $forceRuleUpdating);
+ }
+
+ /**
+ *
+ * $rewriteRulesNeedsUpdate:
+ */
+ public static function saveConfigurationAndHTAccess($config, $forceRuleUpdating = false)
+ {
+ // Important to do this check before saving config, because the method
+ // compares against existing config.
+
+ if ($forceRuleUpdating) {
+ $rewriteRulesNeedsUpdate = true;
+ } else {
+ $rewriteRulesNeedsUpdate = HTAccessRules::doesRewriteRulesNeedUpdate($config);
+ }
+
+ if (!isset($config['base-htaccess-on-these-capability-tests']) || $rewriteRulesNeedsUpdate) {
+ self::runAndStoreCapabilityTests($config);
+ }
+
+ if (self::saveConfigurationFile($config)) {
+ $options = self::generateWodOptionsFromConfigObj($config);
+ if (self::saveWodOptionsFile($options)) {
+ if ($rewriteRulesNeedsUpdate) {
+ $rulesResult = HTAccess::saveRules($config, false);
+ return [
+ 'saved-both-config' => true,
+ 'saved-main-config' => true,
+ 'rules-needed-update' => true,
+ 'htaccess-result' => $rulesResult
+ ];
+ }
+ else {
+ $rulesResult = HTAccess::saveRules($config, false);
+ return [
+ 'saved-both-config' => true,
+ 'saved-main-config' => true,
+ 'rules-needed-update' => false,
+ 'htaccess-result' => $rulesResult
+ ];
+ }
+ } else {
+ return [
+ 'saved-both-config' => false,
+ 'saved-main-config' => true,
+ ];
+ }
+ } else {
+ return [
+ 'saved-both-config' => false,
+ 'saved-main-config' => false,
+ ];
+ }
+ }
+
+ public static function getConverterByName($config, $converterName)
+ {
+ foreach ($config['converters'] as $i => $converter) {
+ if ($converter['converter'] == $converterName) {
+ return $converter;
+ }
+ }
+ }
+
+}
diff --git a/lib/classes/Convert.php b/lib/classes/Convert.php
new file mode 100644
index 0000000..250cd33
--- /dev/null
+++ b/lib/classes/Convert.php
@@ -0,0 +1,349 @@
+ false,
+ 'msg' => 'Source file does not exist: ' . $source,
+ 'log' => '',
+ ];
+ }
+
+ $source = SanityCheck::absPathExistsAndIsFile($source);
+ //$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
+ // PS: No need to check mime type as the WebPConvert library does that (it only accepts image/jpeg and image/png)
+
+ // Check that source is within a valid image root
+ $activeRootIds = Paths::getImageRootIds(); // Currently, root ids cannot be selected, so all root ids are active.
+ $rootId = Paths::findImageRootOfPath($source, $activeRootIds);
+ if ($rootId === false) {
+ throw new \Exception('Path of source is not within a valid image root');
+ }
+
+ // Check config
+ // --------------
+ $checking = 'configuration file';
+ if (is_null($config)) {
+ $config = Config::loadConfigAndFix(); // ps: if this fails to load, default config is returned.
+ }
+ if (!is_array($config)) {
+ throw new SanityException('configuration file is corrupt');
+ }
+
+ // Check convert options
+ // -------------------------------
+ $checking = 'configuration file (options)';
+ if (is_null($convertOptions)) {
+ $wodOptions = Config::generateWodOptionsFromConfigObj($config);
+ if (!isset($wodOptions['webp-convert']['convert'])) {
+ throw new SanityException('conversion options are missing');
+ }
+ $convertOptions = $wodOptions['webp-convert']['convert'];
+ }
+ if (!is_array($convertOptions)) {
+ throw new SanityException('conversion options are missing');
+ }
+
+
+ // Check destination
+ // -------------------------------
+ $checking = 'destination';
+ $destination = self::getDestination($source, $config);
+
+ $destination = SanityCheck::absPath($destination);
+
+ // Check log dir
+ // -------------------------------
+ $checking = 'conversion log dir';
+ if (isset($config['enable-logging']) && $config['enable-logging']) {
+ $logDir = SanityCheck::absPath(Paths::getWebPExpressContentDirAbs() . '/log');
+ } else {
+ $logDir = null;
+ }
+
+
+ } catch (\Exception $e) {
+ return [
+ 'success' => false,
+ 'msg' => 'Check failed for ' . $checking . ': '. $e->getMessage(),
+ 'log' => '',
+ ];
+ }
+
+ // Done with sanitizing, lets get to work!
+ // ---------------------------------------
+//return false;
+ $result = ConvertHelperIndependent::convert($source, $destination, $convertOptions, $logDir, $converter);
+
+//error_log('looki:' . $source . $converter);
+ // If we are using stack converter, check if Ewww discovered invalid api key
+ //if (is_null($converter)) {
+ if (isset(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion)) {
+ // We got an invalid or exceeded api key (at least one).
+ //error_log('look:' . print_r(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, true));
+ EwwwTools::markApiKeysAsNonFunctional(
+ Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion,
+ Paths::getConfigDirAbs()
+ );
+ }
+ //}
+
+ self::updateBiggerThanOriginalMark($source, $destination, $config);
+
+ if ($result['success'] === true) {
+ $result['filesize-original'] = @filesize($source);
+ $result['filesize-webp'] = @filesize($destination);
+ $result['destination-path'] = $destination;
+
+ $destinationOptions = DestinationOptions::createFromConfig($config);
+
+ $rootOfDestination = Paths::destinationRoot($rootId, $destinationOptions);
+
+ $relPathFromImageRootToSource = PathHelper::getRelDir(
+ realpath(Paths::getAbsDirById($rootId)),
+ realpath($source)
+ );
+ $relPathFromImageRootToDest = ConvertHelperIndependent::appendOrSetExtension(
+ $relPathFromImageRootToSource,
+ $config['destination-folder'],
+ $config['destination-extension'],
+ ($rootId == 'uploads')
+ );
+
+ $result['destination-url'] = $rootOfDestination['url'] . '/' . $relPathFromImageRootToDest;
+ }
+ return $result;
+ }
+
+ /**
+ * Determine the location of a source from the location of a destination.
+ *
+ * If for example Operation mode is set to "mingled" and extension is set to "Append .webp",
+ * the result of looking passing "/path/to/logo.jpg.webp" will be "/path/to/logo.jpg".
+ *
+ * Additionally, it is tested if the source exists. If not, false is returned.
+ * The destination does not have to exist.
+ *
+ * @return string|null The source path corresponding to a destination path
+ * - or false on failure (if the source does not exist or $destination is not sane)
+ *
+ */
+ public static function findSource($destination, &$config = null)
+ {
+ try {
+ // Check that destination path is sane and inside document root
+ $destination = SanityCheck::absPathIsInDocRoot($destination);
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ // Load config if not already loaded
+ if (is_null($config)) {
+ $config = Config::loadConfigAndFix();
+ }
+
+ return ConvertHelperIndependent::findSource(
+ $destination,
+ $config['destination-folder'],
+ $config['destination-extension'],
+ $config['destination-structure'],
+ Paths::getWebPExpressContentDirAbs(),
+ new ImageRoots(Paths::getImageRootsDef())
+ );
+ }
+
+ public static function processAjaxConvertFile()
+ {
+
+ if (!check_ajax_referer('webpexpress-ajax-convert-nonce', 'nonce', false)) {
+ //if (true) {
+ //wp_send_json_error('The security nonce has expired. You need to reload the settings page (press F5) and try again)');
+ //wp_die();
+
+ $result = [
+ 'success' => false,
+ 'msg' => 'The security nonce has expired. You need to reload the settings page (press F5) and try again)',
+ 'stop' => true
+ ];
+
+ echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
+ wp_die();
+ }
+
+ // Check input
+ // --------------
+ try {
+ // Check "filename"
+ $checking = '"filename" argument';
+ Validate::postHasKey('filename');
+
+ $filename = sanitize_text_field(stripslashes($_POST['filename']));
+
+ // holy moly! Wordpress automatically adds slashes to the global POST vars - https://stackoverflow.com/questions/2496455/why-are-post-variables-getting-escaped-in-php
+ $filename = wp_unslash($_POST['filename']);
+
+ //$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($filename);
+ // PS: No need to check mime version as webp-convert does that.
+
+
+ // Check converter id
+ // ---------------------
+ $checking = '"converter" argument';
+ if (isset($_POST['converter'])) {
+ $converterId = sanitize_text_field($_POST['converter']);
+ Validate::isConverterId($converterId);
+ }
+
+
+ // Check "config-overrides"
+ // ---------------------------
+ $checking = '"config-overrides" argument';
+ if (isset($_POST['config-overrides'])) {
+ $configOverridesJSON = SanityCheck::noControlChars($_POST['config-overrides']);
+ $configOverridesJSON = preg_replace('/\\\\"/', '"', $configOverridesJSON); // We got crazy encoding, perhaps by jQuery. This cleans it up
+
+ $configOverridesJSON = SanityCheck::isJSONObject($configOverridesJSON);
+ $configOverrides = json_decode($configOverridesJSON, true);
+
+ // PS: We do not need to validate the overrides.
+ // webp-convert checks all options. Nothing can be passed to webp-convert which causes harm.
+ }
+
+ } catch (SanityException $e) {
+ wp_send_json_error('Sanitation check failed for ' . $checking . ': '. $e->getMessage());
+ wp_die();
+ } catch (ValidateException $e) {
+ wp_send_json_error('Validation failed for ' . $checking . ': '. $e->getMessage());
+ wp_die();
+ }
+
+
+ // Input has been processed, now lets get to work!
+ // -----------------------------------------------
+ if (isset($configOverrides)) {
+ $config = Config::loadConfigAndFix();
+
+
+ // convert using specific converter
+ if (!is_null($converterId)) {
+
+ // Merge in the config-overrides (config-overrides only have effect when using a specific converter)
+ $config = array_merge($config, $configOverrides);
+
+ $converter = ConvertersHelper::getConverterById($config, $converterId);
+ if ($converter === false) {
+ wp_send_json_error('Converter could not be loaded');
+ wp_die();
+ }
+
+ // the converter options stored in config.json is not precisely the same as the ones
+ // we send to webp-convert.
+ // We need to "regenerate" webp-convert options in order to use the ones specified in the config-overrides
+ // And we need to merge the general options (such as quality etc) into the option for the specific converter
+
+ $generalWebpConvertOptions = Config::generateWodOptionsFromConfigObj($config)['webp-convert']['convert'];
+ $converterSpecificWebpConvertOptions = isset($converter['options']) ? $converter['options'] : [];
+
+ $webpConvertOptions = array_merge($generalWebpConvertOptions, $converterSpecificWebpConvertOptions);
+ unset($webpConvertOptions['converters']);
+
+ // what is this? - I forgot why!
+ //$config = array_merge($config, $converter['options']);
+ $result = self::convertFile($filename, $config, $webpConvertOptions, $converterId);
+
+ } else {
+ $result = self::convertFile($filename, $config);
+ }
+ } else {
+ $result = self::convertFile($filename);
+ }
+
+ $nonceTick = wp_verify_nonce($_REQUEST['nonce'], 'webpexpress-ajax-convert-nonce');
+ if ($nonceTick == 2) {
+ $result['new-convert-nonce'] = wp_create_nonce('webpexpress-ajax-convert-nonce');
+ // wp_create_nonce('webpexpress-ajax-convert-nonce')
+ }
+
+ $result['nonce-tick'] = $nonceTick;
+
+
+ $result = self::utf8ize($result);
+
+ echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
+
+ wp_die();
+ }
+
+ private static function utf8ize($d) {
+ if (is_array($d)) {
+ foreach ($d as $k => $v) {
+ $d[$k] = self::utf8ize($v);
+ }
+ } else if (is_string ($d)) {
+ return utf8_encode($d);
+ }
+ return $d;
+ }
+}
diff --git a/lib/classes/ConvertHelperIndependent.php b/lib/classes/ConvertHelperIndependent.php
new file mode 100644
index 0000000..2074f68
--- /dev/null
+++ b/lib/classes/ConvertHelperIndependent.php
@@ -0,0 +1,739 @@
+getArray() as $i => $imageRoot) {
+ // in $obj, "rel-path" is only set when document root can be used for relative paths.
+ // So, if it is set, we can use it (beware: we cannot neccessarily use realpath on document root,
+ // but we do not need to - see the long comment in Paths::canUseDocRootForRelPaths())
+
+ $rootPath = $imageRoot->getAbsPath();
+ /*
+ if (isset($obj['rel-path'])) {
+ $docRoot = rtrim($_SERVER["DOCUMENT_ROOT"], '/');
+ $rootPath = $docRoot . '/' . $obj['rel-path'];
+ } else {
+ // If "rel-path" isn't set, then abs-path is, and we can use that.
+ $rootPath = $obj['abs-path'];
+ }*/
+
+ // $source may be resolved or not. Same goes for $rootPath.
+ // We can assume that $rootPath is resolvable using realpath (it ought to exist and be within open_basedir for WP to function)
+ // We can also assume that $source is resolvable (it ought to exist and within open_basedir)
+ // So: Resolve both! and test if the resolved source begins with the resolved rootPath.
+ if (strpos($sourceResolved, realpath($rootPath)) !== false) {
+ $relPath = substr($sourceResolved, strlen(realpath($rootPath)) + 1);
+ $relPath = self::appendOrSetExtension($relPath, $destinationFolder, $destinationExt, false);
+
+ $destination = $webExpressContentDirAbs . '/webp-images/' . $imageRoot->id . '/' . $relPath;
+ break;
+ }
+ }
+ if ($destination == '') {
+ return false;
+ }
+ }
+ }
+
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ return $destination;
+ }
+
+
+ /**
+ * Find source corresponding to destination, separate.
+ *
+ * We can rely on destinationExt being "append" for separate.
+ * Returns false if source file is not found or if a path is not sane. Otherwise returns path to source
+ * destination does not have to exist.
+ *
+ * @param string $destination Path to destination file (does not have to exist)
+ * @param string $destinationStructure "doc-root" or "image-roots"
+ * @param string $webExpressContentDirAbs
+ * @param ImageRoots $imageRoots An image roots object
+ *
+ * @return string|false Returns path to source, if found. If not - or a path is not sane, false is returned
+ */
+ private static function findSourceSeparate($destination, $destinationStructure, $webExpressContentDirAbs, $imageRoots)
+ {
+ try {
+
+ if ($destinationStructure == 'doc-root') {
+
+ // Check that destination path is sane and inside document root
+ // --------------------------
+ $destination = SanityCheck::absPathIsInDocRoot($destination);
+
+
+ // Check that calculated image root is sane and inside document root
+ // --------------------------
+ $imageRoot = SanityCheck::absPathIsInDocRoot($webExpressContentDirAbs . '/webp-images/doc-root');
+
+
+ // Calculate source and check that it is sane and exists
+ // -----------------------------------------------------
+
+ // TODO: This does not work on Windows yet.
+ if (strpos($destination, $imageRoot . '/') === 0) {
+
+ // "Eat" the left part off the $destination parameter. $destination is for example:
+ // "/var/www/webp-express-tests/we0/wp-content-moved/webp-express/webp-images/doc-root/wordpress/uploads-moved/2018/12/tegning5-300x265.jpg.webp"
+ // We also eat the slash (+1)
+ $sourceRel = substr($destination, strlen($imageRoot) + 1);
+
+ $docRoot = rtrim(realpath($_SERVER["DOCUMENT_ROOT"]), '/');
+ $source = $docRoot . '/' . $sourceRel;
+ $source = preg_replace('/\\.(webp)$/', '', $source);
+ } else {
+ // Try with symlinks resolved
+ // This is not trivial as this must also work when the destination path doesn't exist, and
+ // realpath can only be used to resolve symlinks for files that exists.
+ // But here is how we achieve it anyway:
+ //
+ // 1. We make sure imageRoot exists (if not, create it) - this ensures that we can resolve it.
+ // 2. Find closest folder existing folder (resolved) of destination - using PathHelper::findClosestExistingFolderSymLinksExpanded()
+ // 3. Test that resolved closest existing folder starts with resolved imageRoot
+ // 4. If it does, we could create a dummy file at the destination to get its real path, but we want to avoid that, so instead
+ // we can create the containing directory.
+ // 5. We can now use realpath to get the resolved path of the containing directory. The rest is simple enough.
+ if (!file_exists($imageRoot)) {
+ mkdir($imageRoot, 0777, true);
+ }
+ $closestExistingResolved = PathHelper::findClosestExistingFolderSymLinksExpanded($destination);
+ if ($closestExistingResolved == '') {
+ return false;
+ } else {
+ $imageRootResolved = realpath($imageRoot);
+ if (strpos($closestExistingResolved . '/', $imageRootResolved . '/') === 0) {
+// echo $destination . '
' . $closestExistingResolved . '
' . $imageRootResolved . '/'; exit;
+ // Create containing dir for destination
+ $containingDir = PathHelper::dirname($destination);
+ if (!file_exists($containingDir)) {
+ mkdir($containingDir, 0777, true);
+ }
+ $containingDirResolved = realpath($containingDir);
+
+ $filename = PathHelper::basename($destination);
+ $destinationResolved = $containingDirResolved . '/' . $filename;
+
+ $sourceRel = substr($destinationResolved, strlen($imageRootResolved) + 1);
+
+ $docRoot = rtrim(realpath($_SERVER["DOCUMENT_ROOT"]), '/');
+ $source = $docRoot . '/' . $sourceRel;
+ $source = preg_replace('/\\.(webp)$/', '', $source);
+ return $source;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ return SanityCheck::absPathExistsAndIsFileInDocRoot($source);
+ } else {
+
+ // Mission: To find source corresponding to destination (separate) - using the "image-roots" structure.
+
+ // How can we do that?
+ // We got the destination (unresolved) - ie '/website-symlinked/wp-content/webp-express/webp-images/uploads/2018/07/hello.jpg.webp'
+ // If we were lazy and unprecise, we could simply:
+ // - search for "webp-express/webp-images/"
+ // - strip anything before that - result: 'uploads/2018/07/hello.jpg.webp'
+ // - the first path component is the root id.
+ // - the rest of the path is the relative path to the source - if we strip the ".webp" ending
+
+ // So, are we lazy? - what is the alternative?
+ // - Get closest existing resolved folder of destination (ie "/var/www/website/wp-content-moved/webp-express/webp-images/wp-content")
+ // - Check if that folder is below the cache root (resolved) (cache root is the "wp-content" image root + 'webp-express/webp-images')
+ // - Create dir for destination (if missing)
+ // - We can now resolve destination. With cache root also being resolved, we can get the relative dir.
+ // ie 'uploads/2018/07/hello.jpg.webp'.
+ // The first path component is the root id, the rest is the relative path to the source.
+
+ $closestExistingResolved = PathHelper::findClosestExistingFolderSymLinksExpanded($destination);
+ $cacheRoot = $webExpressContentDirAbs . '/webp-images';
+ if ($closestExistingResolved == '') {
+ return false;
+ } else {
+ $cacheRootResolved = realpath($cacheRoot);
+ if (strpos($closestExistingResolved . '/', $cacheRootResolved . '/') === 0) {
+
+ // Create containing dir for destination
+ $containingDir = PathHelper::dirname($destination);
+ if (!file_exists($containingDir)) {
+ mkdir($containingDir, 0777, true);
+ }
+ $containingDirResolved = realpath($containingDir);
+
+ $filename = PathHelper::basename($destination);
+ $destinationResolved = $containingDirResolved . '/' . $filename;
+ $destinationRelToCacheRoot = substr($destinationResolved, strlen($cacheRootResolved) + 1);
+
+ $parts = explode('/', $destinationRelToCacheRoot);
+ $imageRoot = array_shift($parts);
+ $sourceRel = implode('/', $parts);
+
+ $source = $imageRoots->byId($imageRoot)->getAbsPath() . '/' . $sourceRel;
+ $source = preg_replace('/\\.(webp)$/', '', $source);
+ return $source;
+ } else {
+ return false;
+ }
+ }
+ return false;
+ }
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ return $source;
+ }
+
+ /**
+ * Find source corresponding to destination (mingled)
+ * Returns false if not found. Otherwise returns path to source
+ *
+ * @param string $destination Path to destination file (does not have to exist)
+ * @param string $destinationExt Extension ('append' or 'set')
+ * @param string $destinationStructure "doc-root" or "image-roots"
+ *
+ * @return string|false Returns path to source, if found. If not - or a path is not sane, false is returned
+ */
+ private static function findSourceMingled($destination, $destinationExt, $destinationStructure)
+ {
+ try {
+
+ if ($destinationStructure == 'doc-root') {
+ // Check that destination path is sane and inside document root
+ // --------------------------
+ $destination = SanityCheck::absPathIsInDocRoot($destination);
+ } else {
+ // The following will fail if path contains directory traversal. TODO: Is that ok?
+ $destination = SanityCheck::absPath($destination);
+ }
+
+ // Calculate source and check that it is sane and exists
+ // -----------------------------------------------------
+ if ($destinationExt == 'append') {
+ $source = preg_replace('/\\.(webp)$/', '', $destination);
+ } else {
+ $source = preg_replace('#\\.webp$#', '.jpg', $destination);
+ // TODO!
+ // Also check for "Jpeg", "JpEg" etc.
+ if (!@file_exists($source)) {
+ $source = preg_replace('/\\.webp$/', '.jpeg', $destination);
+ }
+ if (!@file_exists($source)) {
+ $source = preg_replace('/\\.webp$/', '.JPG', $destination);
+ }
+ if (!@file_exists($source)) {
+ $source = preg_replace('/\\.webp$/', '.JPEG', $destination);
+ }
+ if (!@file_exists($source)) {
+ $source = preg_replace('/\\.webp$/', '.png', $destination);
+ }
+ if (!@file_exists($source)) {
+ $source = preg_replace('/\\.webp$/', '.PNG', $destination);
+ }
+ }
+ if ($destinationStructure == 'doc-root') {
+ $source = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
+ } else {
+ $source = SanityCheck::absPathExistsAndIsFile($source);
+ }
+
+
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ return $source;
+ }
+
+ /**
+ * Get source from destination (and some configurations)
+ * Returns false if not found. Otherwise returns path to source
+ *
+ * @param string $destination Path to destination file (does not have to exist). May not contain directory traversal
+ * @param string $destinationFolder 'mingled' or 'separate'
+ * @param string $destinationExt Extension ('append' or 'set')
+ * @param string $destinationStructure "doc-root" or "image-roots"
+ * @param string $webExpressContentDirAbs
+ * @param ImageRoots $imageRoots An image roots object
+ *
+ * @return string|false Returns path to source, if found. If not - or a path is not sane, false is returned
+ */
+ public static function findSource($destination, $destinationFolder, $destinationExt, $destinationStructure, $webExpressContentDirAbs, $imageRoots)
+ {
+
+ try {
+
+ if ($destinationStructure == 'doc-root') {
+ // Check that destination path is sane and inside document root
+ // --------------------------
+ $destination = SanityCheck::absPathIsInDocRoot($destination);
+ } else {
+ // The following will fail if path contains directory traversal. TODO: Is that ok?
+ $destination = SanityCheck::absPath($destination);
+ }
+
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ if ($destinationFolder == 'mingled') {
+ $result = self::findSourceMingled($destination, $destinationExt, $destinationStructure);
+ if ($result === false) {
+ $result = self::findSourceSeparate($destination, $destinationStructure, $webExpressContentDirAbs, $imageRoots);
+ }
+ return $result;
+ } else {
+ return self::findSourceSeparate($destination, $destinationStructure, $webExpressContentDirAbs, $imageRoots);
+ }
+ }
+
+ /**
+ *
+ * @param string $source Path to source file
+ * @param string $logDir The folder where log files are kept
+ *
+ * @return string|false Returns computed filename of log - or false if a path is not sane
+ *
+ */
+ public static function getLogFilename($source, $logDir)
+ {
+ try {
+
+ // Check that source path is sane and inside document root
+ // -------------------------------------------------------
+ $source = SanityCheck::absPathIsInDocRoot($source);
+
+
+ // Check that log path is sane and inside document root
+ // -------------------------------------------------------
+ $logDir = SanityCheck::absPathIsInDocRoot($logDir);
+
+
+ // Compute and check log path
+ // --------------------------
+ $logDirForConversions = $logDir .= '/conversions';
+
+ // We store relative to document root.
+ // "Eat" the left part off the source parameter which contains the document root.
+ // and also eat the slash (+1)
+
+ $docRoot = rtrim(realpath($_SERVER["DOCUMENT_ROOT"]), '/');
+ $sourceRel = substr($source, strlen($docRoot) + 1);
+ $logFileName = $logDir . '/doc-root/' . $sourceRel . '.md';
+ SanityCheck::absPathIsInDocRoot($logFileName);
+
+ } catch (SanityException $e) {
+ return false;
+ }
+ return $logFileName;
+
+ }
+
+ /**
+ * Create the directory for log files and put a .htaccess file into it, which prevents
+ * it to be viewed from the outside (not that it contains any sensitive information btw, but for good measure).
+ *
+ * @param string $logDir The folder where log files are kept
+ *
+ * @return boolean Whether it was created successfully or not.
+ *
+ */
+ private static function createLogDir($logDir)
+ {
+ if (!is_dir($logDir)) {
+ @mkdir($logDir, 0775, true);
+ @chmod($logDir, 0775);
+ @file_put_contents(rtrim($logDir . '/') . '/.htaccess', <<
';
+
+ if (!file_exists($logFile)) {
+ $msg .= 'No log file found on that location';
+
+ } else {
+ $log = file_get_contents($logFile);
+ if ($log === false) {
+ $msg .= 'Could not read log file';
+ } else {
+ $msg .= nl2br($log);
+ }
+
+ }
+
+ //$log = $source;
+ //file_get_contents
+
+ echo json_encode($msg, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
+ */
+ wp_die();
+ }
+
+}
diff --git a/lib/classes/ConvertersHelper.php b/lib/classes/ConvertersHelper.php
new file mode 100644
index 0000000..f0b28ec
--- /dev/null
+++ b/lib/classes/ConvertersHelper.php
@@ -0,0 +1,287 @@
+ 'cwebp', 'options' => [
+ 'use-nice' => true,
+ 'try-common-system-paths' => true,
+ 'try-supplied-binary-for-os' => true,
+ 'method' => 6,
+ 'low-memory' => true,
+ 'command-line-options' => '',
+ ]],
+ ['converter' => 'vips', 'options' => [
+ 'smart-subsample' => false,
+ 'preset' => 'none'
+ ]],
+ ['converter' => 'imagemagick', 'options' => [
+ 'use-nice' => true,
+ ]],
+ ['converter' => 'graphicsmagick', 'options' => [
+ 'use-nice' => true,
+ ]],
+ ['converter' => 'ffmpeg', 'options' => [
+ 'use-nice' => true,
+ 'method' => 4,
+ ]],
+ ['converter' => 'wpc', 'options' => []], // we should not set api-version default - it is handled in the javascript
+ ['converter' => 'ewww', 'options' => []],
+ ['converter' => 'imagick', 'options' => []],
+ ['converter' => 'gmagick', 'options' => []],
+ ['converter' => 'gd', 'options' => [
+ 'skip-pngs' => false,
+ ]],
+ ];
+
+ public static function getDefaultConverterNames()
+ {
+ $availableConverterIDs = [];
+ foreach (self::$defaultConverters as $converter) {
+ $availableConverterIDs[] = $converter['converter'];
+ }
+ return $availableConverterIDs;
+
+ // PS: In a couple of years:
+ //return array_column(self::$defaultConverters, 'converter');
+ }
+
+ public static function getConverterNames($converters)
+ {
+ return array_column(self::normalize($converters), 'converter');
+ }
+
+ public static function normalize($converters)
+ {
+ foreach ($converters as &$converter) {
+ if (!isset($converter['converter'])) {
+ $converter = ['converter' => $converter];
+ }
+ if (!isset($converter['options'])) {
+ $converter['options'] = [];
+ }
+ }
+ return $converters;
+ }
+
+ /**
+ * Those converters in second array, but not in first will be appended to first
+ */
+ public static function mergeConverters($first, $second)
+ {
+ $namesInFirst = self::getConverterNames($first);
+ $second = self::normalize($second);
+
+ foreach ($second as $converter) {
+ // migrate9 and this functionality could create two converters.
+ // so, for a while, skip graphicsmagick and imagemagick
+
+ if ($converter['converter'] == 'graphicsmagick') {
+ if (in_array('gmagickbinary', $namesInFirst)) {
+ continue;
+ }
+ }
+ if ($converter['converter'] == 'imagemagick') {
+ if (in_array('imagickbinary', $namesInFirst)) {
+ continue;
+ }
+ }
+ if (!in_array($converter['converter'], $namesInFirst)) {
+ $first[] = $converter;
+ }
+ }
+ return $first;
+ }
+
+ /**
+ * Get converter by id
+ *
+ * @param object $config
+ * @return array|false converter object
+ */
+ public static function getConverterById($config, $id) {
+ if (!isset($config['converters'])) {
+ return false;
+ }
+ $converters = $config['converters'];
+
+ if (!is_array($converters)) {
+ return false;
+ }
+
+ foreach ($converters as $c) {
+ if (!isset($c['converter'])) {
+ continue;
+ }
+ if ($c['converter'] == $id) {
+ return $c;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Get working converters.
+ *
+ * @param object $config
+ * @return array
+ */
+ public static function getWorkingConverters($config) {
+ if (!isset($config['converters'])) {
+ return [];
+ }
+ $converters = $config['converters'];
+
+ if (!is_array($converters)) {
+ return [];
+ }
+
+ $result = [];
+
+ foreach ($converters as $c) {
+ if (isset($c['working']) && !$c['working']) {
+ continue;
+ }
+ $result[] = $c;
+ }
+ return $result;
+ }
+
+ /**
+ * Get array of working converter ids. Same order as configured.
+ */
+ public static function getWorkingConverterIds($config)
+ {
+ $converters = self::getWorkingConverters($config);
+ $result = [];
+ foreach ($converters as $converter) {
+ $result[] = $converter['converter'];
+ }
+ return $result;
+ }
+
+ /**
+ * Get working and active converters.
+ *
+ * @param object $config
+ * @return array Array of converter objects
+ */
+ public static function getWorkingAndActiveConverters($config)
+ {
+ if (!isset($config['converters'])) {
+ return [];
+ }
+ $converters = $config['converters'];
+
+ if (!is_array($converters)) {
+ return [];
+ }
+
+ $result = [];
+
+ foreach ($converters as $c) {
+ if (isset($c['deactivated']) && $c['deactivated']) {
+ continue;
+ }
+ if (isset($c['working']) && !$c['working']) {
+ continue;
+ }
+ $result[] = $c;
+ }
+ return $result;
+ }
+
+ /**
+ * Get active converters.
+ *
+ * @param object $config
+ * @return array Array of converter objects
+ */
+ public static function getActiveConverters($config)
+ {
+ if (!isset($config['converters'])) {
+ return [];
+ }
+ $converters = $config['converters'];
+ if (!is_array($converters)) {
+ return [];
+ }
+ $result = [];
+ foreach ($converters as $c) {
+ if (isset($c['deactivated']) && $c['deactivated']) {
+ continue;
+ }
+ $result[] = $c;
+ }
+ return $result;
+ }
+
+ public static function getWorkingAndActiveConverterIds($config)
+ {
+ $converters = self::getWorkingAndActiveConverters($config);
+ $result = [];
+ foreach ($converters as $converter) {
+ $result[] = $converter['converter'];
+ }
+ return $result;
+ }
+
+ public static function getActiveConverterIds($config)
+ {
+ $converters = self::getActiveConverters($config);
+ $result = [];
+ foreach ($converters as $converter) {
+ $result[] = $converter['converter'];
+ }
+ return $result;
+ }
+
+ /**
+ * Get converter id by converter object
+ *
+ * @param object $converter
+ * @return string converter name, or empty string if not set (it should always be set, however)
+ */
+ public static function getConverterId($converter) {
+ if (!isset($converter['converter'])) {
+ return '';
+ }
+ return $converter['converter'];
+ }
+
+ /**
+ * Get first working and active converter.
+ *
+ * @param object $config
+ * @return object|false
+ */
+ public static function getFirstWorkingAndActiveConverter($config) {
+
+ $workingConverters = self::getWorkingAndActiveConverters($config);
+
+ if (count($workingConverters) == 0) {
+ return false;
+ }
+ return $workingConverters[0];
+ }
+
+ /**
+ * Get first working and active converter (name)
+ *
+ * @param object $config
+ * @return string|false id of converter, or false if no converter is working and active
+ */
+ public static function getFirstWorkingAndActiveConverterId($config) {
+ $c = self::getFirstWorkingAndActiveConverter($config);
+ if ($c === false) {
+ return false;
+ }
+ if (!isset($c['converter'])) {
+ return false;
+ }
+ return $c['converter'];
+ }
+
+}
diff --git a/lib/classes/Destination.php b/lib/classes/Destination.php
new file mode 100644
index 0000000..a0134cc
--- /dev/null
+++ b/lib/classes/Destination.php
@@ -0,0 +1,208 @@
+mingled;
+ $replaceExt = $destinationOptions->replaceExt;
+ $useDocRoot = $destinationOptions->useDocRoot;
+
+ try {
+ // Check source
+ // --------------
+ // TODO: make this check work with symlinks
+ //$source = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
+
+ // Calculate destination and check that the result is sane
+ // -------------------------------------------------------
+ if (self::storeMingledOrNot($source, $mingled, $uploadDirAbs)) {
+ $destination = self::appendOrSetExtension($source, $mingled, $replaceExt, true);
+ } else {
+
+ if ($useDocRoot) {
+ // We must find the relative path from document root to source.
+ // However, we dont know if document root is resolved or not.
+ // We also do not know if source begins with a resolved or unresolved document root.
+ // And we cannot be sure that document root is resolvable.
+
+ // Lets say:
+ // 1. document root is unresolvable.
+ // 2. document root is configured to something unresolved ("/my-website")
+ // 3. source is resolved and within an image root ("/var/www/my-website/wp-content/uploads/test.jpg")
+ // 4. all image roots are resolvable.
+ // 5. Paths::canUseDocRootForRelPaths()) returned true
+
+ // Can the relative path then be found?
+ // Actually, yes.
+ // We can loop through the image roots.
+ // When we get to the "uploads" root, it must neccessarily contain the unresolved document root.
+ // It will in other words be: "my-website/wp-content/uploads"
+ // It can not be configured to the resolved path because canUseDocRootForRelPaths would have then returned false as
+ // It would not be possible to establish that "/var/www/my-website/wp-content/uploads/" is within document root, as
+ // document root is "/my-website" and unresolvable.
+ // To sum up, we have:
+ // If document root is unresolvable while canUseDocRootForRelPaths() succeeded, then the image roots will all begin with
+ // the unresolved path.
+ // In this method, if $useDocRootForStructuringCacheDir is true, then it is assumed that canUseDocRootForRelPaths()
+ // succeeded.
+ // OH!
+ // I realize that the image root can be passed as well:
+ // $imageRoot = $webExpressContentDirAbs . '/webp-images';
+ // So the question is: Will $webExpressContentDirAbs also be the unresolved path?
+ // That variable is calculated in WodConfigLoader based on various methods available.
+ // I'm not digging into it, but would expect it to in some cases be resolved. Which means that relative path can not
+ // be found.
+ // So. Lets play it safe and require that document root is resolvable in order to use docRoot for structure
+
+ if (!PathHelper::isDocRootAvailable()) {
+ throw new \Exception(
+ 'Can not calculate destination using "doc-root" structure as document root is not available. $_SERVER["DOCUMENT_ROOT"] is empty. ' .
+ 'This is probably a misconfiguration on the server. ' .
+ 'However, WebP Express can function without using documument root. If you resave options and regenerate the .htaccess files, it should ' .
+ 'automatically start to structure the webp files in subfolders that are relative the image root folders rather than document-root.'
+ );
+ }
+
+ if (!PathHelper::isDocRootAvailableAndResolvable()) {
+ throw new \Exception(
+ 'Can not calculate destination using "doc-root" structure as document root cannot be resolved for symlinks using "realpath". The ' .
+ 'reason for that is probably that open_basedir protection has been set up and that document root is outside outside that open_basedir. ' .
+ 'WebP Express can function in that setting, however you will need to resave options and regenerate the .htaccess files. It should then ' .
+ 'automatically stop to structure the webp files as relative to document root and instead structure them as relative to image root folders.'
+ );
+ }
+ $docRoot = rtrim(realpath($_SERVER["DOCUMENT_ROOT"]), '/');
+ $imageRoot = $webExpressContentDirAbs . '/webp-images';
+
+ // TODO: make this check work with symlinks
+ //SanityCheck::absPathIsInDocRoot($imageRoot);
+
+ $sourceRel = substr(realpath($source), strlen($docRoot) + 1);
+ $destination = $imageRoot . '/doc-root/' . $sourceRel;
+ $destination = self::appendOrSetExtension($destination, $mingled, $replaceExt, false);
+
+
+ // TODO: make this check work with symlinks
+ //$destination = SanityCheck::absPathIsInDocRoot($destination);
+ } else {
+ $destination = '';
+
+ $sourceResolved = realpath($source);
+
+
+ // Check roots until we (hopefully) get a match.
+ // (that is: find a root which the source is inside)
+ foreach ($imageRoots->getArray() as $i => $imageRoot) {
+ // in $obj, "rel-path" is only set when document root can be used for relative paths.
+ // So, if it is set, we can use it (beware: we cannot neccessarily use realpath on document root,
+ // but we do not need to - see the long comment in Paths::canUseDocRootForRelPaths())
+
+ $rootPath = $imageRoot->getAbsPath();
+ /*
+ if (isset($obj['rel-path'])) {
+ $docRoot = rtrim($_SERVER["DOCUMENT_ROOT"], '/');
+ $rootPath = $docRoot . '/' . $obj['rel-path'];
+ } else {
+ // If "rel-path" isn't set, then abs-path is, and we can use that.
+ $rootPath = $obj['abs-path'];
+ }*/
+
+ // $source may be resolved or not. Same goes for $rootPath.
+ // We can assume that $rootPath is resolvable using realpath (it ought to exist and be within open_basedir for WP to function)
+ // We can also assume that $source is resolvable (it ought to exist and within open_basedir)
+ // So: Resolve both! and test if the resolved source begins with the resolved rootPath.
+ if (strpos($sourceResolved, realpath($rootPath)) !== false) {
+ $relPath = substr($sourceResolved, strlen(realpath($rootPath)) + 1);
+ $relPath = self::appendOrSetExtension($relPath, $mingled, $replaceExt, false);
+
+ $destination = $webExpressContentDirAbs . '/webp-images/' . $imageRoot->id . '/' . $relPath;
+ break;
+ }
+ }
+ if ($destination == '') {
+ return false;
+ }
+ }
+ }
+
+ } catch (SanityException $e) {
+ return false;
+ }
+
+ return $destination;
+ }
+
+}
diff --git a/lib/classes/DestinationOptions.php b/lib/classes/DestinationOptions.php
new file mode 100644
index 0000000..cd18b3d
--- /dev/null
+++ b/lib/classes/DestinationOptions.php
@@ -0,0 +1,42 @@
+mingled = $mingled;
+ $this->useDocRoot = $useDocRoot;
+ $this->replaceExt = $replaceExt;
+ $this->scope = $scope;
+ }
+
+ /**
+ * Set properties from config file
+ *
+ * @param array $config WebP Express configuration object
+ */
+ public static function createFromConfig(&$config)
+ {
+ return new DestinationOptions(
+ $config['destination-folder'] == 'mingled', // "mingled" or "separate"
+ $config['destination-structure'] == 'doc-root', // "doc-root" or "image-roots"
+ $config['destination-extension'] == 'set', // "set" or "append"
+ $config['scope']
+ );
+ }
+
+
+}
diff --git a/lib/classes/DestinationUrl.php b/lib/classes/DestinationUrl.php
new file mode 100644
index 0000000..7d029ee
--- /dev/null
+++ b/lib/classes/DestinationUrl.php
@@ -0,0 +1,229 @@
+ http
+ [host] => we0
+ [path] => /wordpress/uploads-moved
+ )*/
+
+ $imageUrlComponents = parse_url($imageUrl);
+ /* ie:
+ (
+ [scheme] => http
+ [host] => we0
+ [path] => /wordpress/uploads-moved/logo.jpg
+ )*/
+ if ($baseUrlComponents['host'] != $imageUrlComponents['host']) {
+ return false;
+ }
+
+ // Check if path begins with base path
+ if (strpos($imageUrlComponents['path'], $baseUrlComponents['path']) !== 0) {
+ return false;
+ }
+
+ // Remove base path from path (we know it begins with basepath, from previous check)
+ return substr($imageUrlComponents['path'], strlen($baseUrlComponents['path']));
+
+ }
+
+ /**
+ * Get url for webp from source url, (if ), given a certain baseUrl / baseDir.
+ * Base can for example be uploads or wp-content.
+ *
+ * returns false
+ * - if no source file found in that base
+ * - or source file is found but webp file isn't there and the `only-for-webps-that-exists` option is set
+ *
+ * @param string $sourceUrl Url of source image (ie http://example.com/wp-content/image.jpg)
+ * @param string $rootId Id (created in Config::updateAutoloadedOptions). Ie "uploads", "content" or any image root id
+ * @param string $baseUrl Base url of source image (ie http://example.com/wp-content)
+ * @param string $baseDir Base dir of source image (ie /var/www/example.com/wp-content)
+ * @param object $destinationOptions
+ */
+ public static function getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrl, $baseDir, $destinationOptions)
+ {
+ //error_log('getWebPUrlInImageRoot:' . $sourceUrl . ':' . $baseUrl . ':' . $baseDir);
+
+
+ $srcPathRel = self::getRelUrlPath($sourceUrl, $baseUrl);
+
+ if ($srcPathRel === false) {
+ return false;
+ }
+
+ // Calculate file path to source
+ $srcPathAbs = $baseDir . $srcPathRel;
+
+ // Check that source file exists
+ if (!@file_exists($srcPathAbs)) {
+ return false;
+ }
+
+ // Calculate destination of webp (both path and url)
+ // ----------------------------------------
+
+ // We are calculating: $destPathAbs and $destUrl.
+
+ if (!isset($destinationOptions->scope) || !in_array($rootId, $destinationOptions->scope)) {
+ return false;
+ }
+
+ $destinationRoot = Paths::destinationRoot(
+ $rootId,
+ $destinationOptions
+ );
+
+ $relPathFromImageRootToSource = PathHelper::getRelDir(
+ realpath(Paths::getAbsDirById($rootId)),
+ realpath($srcPathAbs)
+ );
+ $relPathFromImageRootToDest = Destination::appendOrSetExtension(
+ $relPathFromImageRootToSource,
+ $destinationOptions->mingled,
+ $destinationOptions->replaceExt,
+ ($rootId == 'uploads')
+ );
+ $destPathAbs = $destinationRoot['abs-path'] . '/' . $relPathFromImageRootToDest;
+ $webpMustExist = self::$options['only-for-webps-that-exists'];
+ if ($webpMustExist && (!@file_exists($destPathAbs))) {
+ return false;
+ }
+
+ $destUrl = $destinationRoot['url'] . '/' . $relPathFromImageRootToDest;
+
+ // Fix scheme (use same as source)
+ $sourceUrlComponents = parse_url($sourceUrl);
+ $destUrlComponents = parse_url($destUrl);
+ $port = isset($sourceUrlComponents['port']) ? ":" . $sourceUrlComponents['port'] : "";
+ return $sourceUrlComponents['scheme'] . '://' . $sourceUrlComponents['host'] . $port . $destUrlComponents['path'];
+ }
+
+
+ /**
+ * Get url for webp
+ * returns second argument if no webp
+ *
+ * @param $sourceUrl
+ * @param $returnValueOnFail
+ */
+ public static function getWebPUrl($sourceUrl, $returnValueOnFail)
+ {
+ // Get the options
+ self::getOptions();
+
+ // Fail for webp-disabled browsers (when "only-for-webp-enabled-browsers" is set)
+ if (self::$options['only-for-webp-enabled-browsers']) {
+ if (!isset($_SERVER['HTTP_ACCEPT']) || (strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') === false)) {
+ return $returnValueOnFail;
+ }
+ }
+
+ // Fail for relative urls. Wordpress doesn't use such very much anyway
+ if (!preg_match('#^https?://#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+
+ // Fail if the image type isn't enabled
+ switch (self::$options['image-types']) {
+ case 0:
+ return $returnValueOnFail;
+ case 1:
+ if (!preg_match('#(jpe?g)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ case 2:
+ if (!preg_match('#(png)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ case 3:
+ if (!preg_match('#(jpe?g|png)$#', $sourceUrl)) {
+ return $returnValueOnFail;
+ }
+ break;
+ }
+
+ //error_log('source url:' . $sourceUrl);
+
+ // Try all image roots
+ foreach (self::$options['scope'] as $rootId) {
+ $baseDir = Paths::getAbsDirById($rootId);
+ $baseUrl = Paths::getUrlById($rootId);
+
+ //error_log('baseurl: ' . $baseUrl);
+ if (Multisite::isMultisite() && ($rootId == 'uploads')) {
+ $baseUrl = Paths::getUploadUrl();
+ $baseDir = Paths::getUploadDirAbs();
+ }
+
+ $result = self::getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrl, $baseDir);
+ if ($result !== false) {
+ return $result;
+ }
+
+ // Try the hostname aliases.
+ if (!isset(self::$options['hostname-aliases'])) {
+ continue;
+ }
+ $hostnameAliases = self::$options['hostname-aliases'];
+
+ $hostname = Paths::getHostNameOfUrl($baseUrl);
+ $baseUrlComponents = parse_url($baseUrl);
+ $sourceUrlComponents = parse_url($sourceUrl);
+ // ie: [scheme] => http, [host] => we0, [path] => /wordpress/uploads-moved
+
+ if ((!isset($baseUrlComponents['host'])) || (!isset($sourceUrlComponents['host']))) {
+ continue;
+ }
+
+ foreach ($hostnameAliases as $hostnameAlias) {
+
+ if ($sourceUrlComponents['host'] != $hostnameAlias) {
+ continue;
+ }
+ //error_log('hostname alias:' . $hostnameAlias);
+
+ $baseUrlOnAlias = $baseUrlComponents['scheme'] . '://' . $hostnameAlias . $baseUrlComponents['path'];
+ //error_log('baseurl (alias):' . $baseUrlOnAlias);
+
+ $result = self::getWebPUrlInImageRoot($sourceUrl, $rootId, $baseUrlOnAlias, $baseDir);
+ if ($result !== false) {
+ $resultUrlComponents = parse_url($result);
+ return $sourceUrlComponents['scheme'] . '://' . $hostnameAlias . $resultUrlComponents['path'];
+ }
+ }
+ }
+
+ return $returnValueOnFail;
+ }
+
+/*
+ public static function getWebPUrlOrSame($sourceUrl, $returnValueOnFail)
+ {
+ return self::getWebPUrl($sourceUrl, $sourceUrl);
+ }*/
+
+}
diff --git a/lib/classes/DismissableGlobalMessages.php b/lib/classes/DismissableGlobalMessages.php
new file mode 100644
index 0000000..96a81bc
--- /dev/null
+++ b/lib/classes/DismissableGlobalMessages.php
@@ -0,0 +1,100 @@
+
';
+ foreach ($buttons as $i => $button) {
+ $javascript = "jQuery(this).closest('div.notice').slideUp();";
+ //$javascript = "console.log(jQuery(this).closest('div.notice'));";
+ $javascript .= "jQuery.post(ajaxurl, " .
+ "{'action': 'webpexpress_dismiss_global_message', " .
+ "'id': '" . $id . "'})";
+ if (isset($button['javascript'])) {
+ $javascript .= ".done(function() {" . $button['javascript'] . "});";
+ }
+ if (isset($button['redirect-to-settings'])) {
+ $javascript .= ".done(function() {location.href='" . Paths::getSettingsUrl() . "'});";
+ }
+
+ $msg .= '';
+
+ }
+ Messenger::printMessage($level, $msg);
+ }
+
+ public static function printMessages()
+ {
+ $ids = State::getState('dismissableGlobalMessageIds', []);
+ foreach ($ids as $id) {
+ include_once __DIR__ . '/../dismissable-global-messages/' . $id . '.php';
+ }
+ }
+
+ /**
+ * Dismiss message
+ *
+ * @param string $id An identifier, ie "suggest_enable_pngs"
+ */
+ public static function dismissMessage($id) {
+ $messages = State::getState('dismissableGlobalMessageIds', []);
+ $newQueue = [];
+ foreach ($messages as $mid) {
+ if ($mid == $id) {
+
+ } else {
+ $newQueue[] = $mid;
+ }
+ }
+ State::setState('dismissableGlobalMessageIds', $newQueue);
+ }
+
+ /**
+ * Dismiss message
+ *
+ * @param string $id An identifier, ie "suggest_enable_pngs"
+ */
+ public static function dismissAll() {
+ State::setState('dismissableGlobalMessageIds', []);
+ }
+
+ public static function processAjaxDismissGlobalMessage() {
+ /*
+ We have no security nonce here
+ Dismissing a message is not harmful and dismissMessage($id) do anything harmful, no matter what you send in the "id"
+ */
+ $id = sanitize_text_field($_POST['id']);
+ self::dismissMessage($id);
+ }
+
+
+}
diff --git a/lib/classes/DismissableMessages.php b/lib/classes/DismissableMessages.php
new file mode 100644
index 0000000..1b146f6
--- /dev/null
+++ b/lib/classes/DismissableMessages.php
@@ -0,0 +1,86 @@
+' . $gotItText . '';
+ }
+ Messenger::printMessage($level, $msg);
+ }
+
+ public static function printMessages()
+ {
+ $ids = State::getState('dismissableMessageIds', []);
+ foreach ($ids as $id) {
+ include_once __DIR__ . '/../dismissable-messages/' . $id . '.php';
+ }
+ }
+
+ /**
+ * Dismiss message
+ *
+ * @param string $id An identifier, ie "suggest_enable_pngs"
+ */
+ public static function dismissMessage($id) {
+ $messages = State::getState('dismissableMessageIds', []);
+ $newQueue = [];
+ foreach ($messages as $mid) {
+ if ($mid == $id) {
+
+ } else {
+ $newQueue[] = $mid;
+ }
+ }
+ State::setState('dismissableMessageIds', $newQueue);
+ }
+
+ /**
+ * Dismiss message
+ *
+ * @param string $id An identifier, ie "suggest_enable_pngs"
+ */
+ public static function dismissAll() {
+ State::setState('dismissableMessageIds', []);
+ }
+
+ public static function processAjaxDismissMessage() {
+ /*
+ We have no security nonce here
+ Dismissing a message is not harmful and dismissMessage($id) do anything harmful, no matter what you send in the "id"
+ */
+ $id = sanitize_text_field($_POST['id']);
+ self::dismissMessage($id);
+ }
+
+
+}
diff --git a/lib/classes/EwwwTools.php b/lib/classes/EwwwTools.php
new file mode 100644
index 0000000..46076c5
--- /dev/null
+++ b/lib/classes/EwwwTools.php
@@ -0,0 +1,113 @@
+ $c) {
+ if (!isset($c['converter'])) {
+ continue;
+ }
+ if ($c['converter'] == 'ewww') {
+ //unset($wodOptions['webp-convert']['convert']['converters'][$i]);
+ array_splice($wodOptions['webp-convert']['convert']['converters'], $i, 1);
+
+ //$successfulWrite = Config::saveConfigurationFileAndWodOptions($config);
+ $successfulWrite = FileHelper::saveJSONOptions($configDir . '/wod-options.json', $wodOptions);
+ return $successfulWrite;
+ }
+ }
+ }
+
+ /**
+ * Mark ewww api keys as non functional.
+ *
+ * Current implementation simply removes ewww from wod-options.json.
+ * It will reappear when options are saved - but be removed again upon next failure
+ *
+ * @return boolean If it went well.
+ */
+ public static function markApiKeysAsNonFunctional($apiKeysToMarkAsNonFunctional, $configDir)
+ {
+ //self::markApiKeysAsNonFunctionalInConfig($apiKeysToMarkAsNonFunctional, $configDir);
+
+ // TODO: We should update the key to api-key-2 the first time.
+ // But I am going to change the structure of wod-options so ewww becomes a stack converter, so
+ // I don't bother implementing this right now.
+ self::removeEwwwFromWodOptions($apiKeysToMarkAsNonFunctional, $configDir);
+
+ }
+
+}
diff --git a/lib/classes/FileHelper.php b/lib/classes/FileHelper.php
new file mode 100644
index 0000000..1b0b3d8
--- /dev/null
+++ b/lib/classes/FileHelper.php
@@ -0,0 +1,395 @@
+valid()) {
+ $filename = $fileIterator->getFilename();
+ $filepath = $dir . "/" . $filename;
+
+// echo $filepath . "\n";
+
+ $isDir = @is_dir($filepath);
+
+ if ((!$isDir && (is_null($regexFileMatchPattern) || preg_match($regexFileMatchPattern, $filename))) ||
+ ($isDir && (is_null($regexDirMatchPattern) || preg_match($regexDirMatchPattern, $filename)))) {
+ // chmod
+ if ($isDir) {
+ if (!is_null($dirPerm)) {
+ self::chmod($filepath, $dirPerm);
+ //echo '. chmod dir to:' . self::humanReadableFilePerm($dirPerm) . '. result:' . self::humanReadableFilePermOfFile($filepath) . "\n";
+ }
+ } else {
+ if (!is_null($filePerm)) {
+ self::chmod($filepath, $filePerm);
+ //echo '. chmod file to:' . self::humanReadableFilePerm($filePerm) . '. result:' . self::humanReadableFilePermOfFile($filepath) . "\n";
+ }
+
+ }
+
+ // chown
+ if (!is_null($uid)) {
+ @chown($filepath, $uid);
+ }
+
+ // chgrp
+ if (!is_null($gid)) {
+ @chgrp($filepath, $gid);
+
+ }
+ }
+
+ // recurse
+ if ($isDir) {
+ self::chmod_r($filepath, $dirPerm, $filePerm, $uid, $gid, $regexFileMatchPattern, $regexDirMatchPattern);
+ }
+
+ // next!
+ $fileIterator->next();
+ }
+ }
+
+
+ /**
+ * Create a dir using same permissions as parent.
+ * If
+ */
+ /*
+ public static function mkdirSamePermissionsAsParent($pathname) {
+
+ }*/
+
+ /**
+ * Get directory part of filename.
+ * Ie '/var/www/.htaccess' => '/var/www'
+ * Also works with backslashes
+ */
+ public static function dirName($filename) {
+ return preg_replace('/[\/\\\\][^\/\\\\]*$/', '', $filename);
+ }
+
+ /**
+ * Determines if a file can be created.
+ * BEWARE: It requires that the containing folder already exists
+ */
+ public static function canCreateFile($filename) {
+ $dirName = self::dirName($filename);
+ if (!@file_exists($dirName)) {
+ return false;
+ }
+ if (@is_writable($dirName) && @is_executable($dirName) || self::isWindows() ) {
+ return true;
+ }
+
+ $existingPermission = self::filePerm($dirName);
+
+ // we need to make sure we got the existing permission, so we can revert correctly later
+ if ($existingPermission !== false) {
+ if (self::chmod($dirName, 0775)) {
+ // change back
+ self::chmod($filename, $existingPermission);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Note: Do not use for directories
+ */
+ public static function canEditFile($filename) {
+ if (!@file_exists($filename)) {
+ return false;
+ }
+ if (@is_writable($filename) && @is_readable($filename)) {
+ return true;
+ }
+
+ // As a last desperate try, lets see if we can give ourself write permissions.
+ // If possible, then it will also be possible when actually writing
+ $existingPermission = self::filePerm($filename);
+
+ // we need to make sure we got the existing permission, so we can revert correctly later
+ if ($existingPermission !== false) {
+ if (self::chmod($filename, 0664)) {
+ // change back
+ self::chmod($filename, $existingPermission);
+ return true;
+ }
+ }
+ return false;
+
+ // Idea: Perhaps we should also try to actually open the file for writing?
+
+ }
+
+ public static function canEditOrCreateFileHere($filename) {
+ if (@file_exists($filename)) {
+ return self::canEditFile($filename);
+ } else {
+ return self::canCreateFile($filename);
+ }
+ }
+
+ /**
+ * Try to read from a file. Tries hard.
+ * Returns content, or false if read error.
+ */
+ public static function loadFile($filename) {
+ $changedPermission = false;
+ if (!@is_readable($filename)) {
+ $existingPermission = self::filePerm($filename);
+
+ // we need to make sure we got the existing permission, so we can revert correctly later
+ if ($existingPermission !== false) {
+ $changedPermission = self::chmod($filename, 0664);
+ }
+ }
+
+ $return = false;
+ try {
+ $handle = @fopen($filename, "r");
+ } catch (\ErrorException $exception) {
+ $handle = false;
+ error_log($exception->getMessage());
+ }
+ if ($handle !== false) {
+ // Return value is either file content or false
+ if (filesize($filename) == 0) {
+ $return = '';
+ } else {
+ $return = @fread($handle, filesize($filename));
+ }
+ fclose($handle);
+ }
+
+ if ($changedPermission) {
+ // change back
+ self::chmod($filename, $existingPermission);
+ }
+ return $return;
+ }
+
+
+ /* Remove dir and files in it recursively.
+ No warnings
+ returns $success
+ */
+ public static function rrmdir($dir) {
+ if (@is_dir($dir)) {
+ $objects = @scandir($dir);
+ foreach ($objects as $object) {
+ if ($object != "." && $object != "..") {
+ if (@is_dir($dir . "/" . $object))
+ self::rrmdir($dir . "/" . $object);
+ else
+ @unlink($dir . "/" . $object);
+ }
+ }
+ return @rmdir($dir);
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * Copy dir and all its files.
+ * Existing files are overwritten.
+ *
+ * @return $success
+ */
+ public static function cpdir($sourceDir, $destinationDir)
+ {
+ if (!@is_dir($sourceDir)) {
+ return false;
+ }
+ if (!@file_exists($destinationDir)) {
+ if (!@mkdir($destinationDir)) {
+ return false;
+ }
+ }
+
+ $fileIterator = new \FilesystemIterator($sourceDir);
+ $success = true;
+
+ while ($fileIterator->valid()) {
+ $filename = $fileIterator->getFilename();
+
+ if (($filename != ".") && ($filename != "..")) {
+ //$filePerm = FileHelper::filePermWithFallback($filename, 0777);
+
+ if (@is_dir($sourceDir . "/" . $filename)) {
+ if (!self::cpdir($sourceDir . "/" . $filename, $destinationDir . "/" . $filename)) {
+ $success = false;
+ }
+ } else {
+ // its a file.
+ if (!copy($sourceDir . "/" . $filename, $destinationDir . "/" . $filename)) {
+ $success = false;
+ }
+ }
+ }
+ $fileIterator->next();
+ }
+ return $success;
+ }
+
+ /**
+ * Remove empty subfolders.
+ *
+ * Got it here: https://stackoverflow.com/a/1833681/842756
+ *
+ * @return boolean If folder is (was) empty
+ */
+ public static function removeEmptySubFolders($path, $removeEmptySelfToo = false)
+ {
+ if (!file_exists($path)) {
+ return;
+ }
+ $empty = true;
+ foreach (scandir($path) as $file) {
+ if (($file == '.') || ($file == '..')) {
+ continue;
+ }
+ $file = $path . DIRECTORY_SEPARATOR . $file;
+ if (is_dir($file)) {
+ if (!self::removeEmptySubFolders($file, true)) {
+ $empty=false;
+ }
+ } else {
+ $empty=false;
+ }
+ }
+ if ($empty && $removeEmptySelfToo) {
+ rmdir($path);
+ }
+ return $empty;
+ }
+
+ /**
+ * Verify if OS is Windows
+ *
+ *
+ * @return true if windows; false if not.
+ */
+ public static function isWindows(){
+ return preg_match('/^win/i', PHP_OS);
+ }
+
+
+ /**
+ * Normalize separators of directory paths
+ *
+ *
+ * @return $normalized_path
+ */
+ public static function normalizeSeparator($path, $newSeparator = DIRECTORY_SEPARATOR){
+ return preg_replace("#[\\\/]+#", $newSeparator, $path);
+ }
+
+ /**
+ * @return object|false Returns parsed file the file exists and can be read. Otherwise it returns false
+ */
+ public static function loadJSONOptions($filename)
+ {
+ $json = self::loadFile($filename);
+ if ($json === false) {
+ return false;
+ }
+
+ $options = json_decode($json, true);
+ if ($options === null) {
+ return false;
+ }
+ return $options;
+ }
+
+ public static function saveJSONOptions($filename, $obj)
+ {
+ $result = @file_put_contents(
+ $filename,
+ json_encode($obj, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)
+ );
+ /*if ($result === false) {
+ echo 'COULD NOT' . $filename;
+ }*/
+ return ($result !== false);
+ }
+
+}
diff --git a/lib/classes/HTAccess.php b/lib/classes/HTAccess.php
new file mode 100644
index 0000000..7d79960
--- /dev/null
+++ b/lib/classes/HTAccess.php
@@ -0,0 +1,436 @@
+') !== false);
+
+ $dir = FileHelper::dirName($filename);
+ $dirId = Paths::getAbsDirId($dir);
+ if ($dirId !== false) {
+ if ($containsRules) {
+ self::addToActiveHTAccessDirsArray($dirId);
+ } else {
+ self::removeFromActiveHTAccessDirsArray($dirId);
+ }
+ }
+ }
+
+ return $success;
+ }
+
+ public static function saveHTAccessRules($rootId, $rules, $createIfMissing = true) {
+ $filename = Paths::getAbsDirById($rootId) . '/.htaccess';
+ return self::saveHTAccessRulesToFile($filename, $rules, $createIfMissing);
+ }
+
+ /* only called in this file */
+ public static function saveHTAccessRulesToFirstWritableHTAccessDir($dirs, $rules)
+ {
+ foreach ($dirs as $dir) {
+ if (self::saveHTAccessRulesToFile($dir . '/.htaccess', $rules, true)) {
+ return $dir;
+ }
+ }
+ return false;
+ }
+
+
+ /**
+ * Try to deactivate all .htaccess rules.
+ * If success, we return true.
+ * If we fail, we return an array of filenames that have problems
+ * @return true|array
+ */
+ public static function deactivateHTAccessRules($comment = '# Plugin is deactivated') {
+
+ $rootsToClean = Paths::getImageRootIds();
+ $rootsToClean[] = 'home';
+ $rootsToClean[] = 'cache';
+ $failures = [];
+ $successes = [];
+
+ foreach ($rootsToClean as $imageRootId) {
+ $dir = Paths::getAbsDirById($imageRootId);
+ $filename = $dir . '/.htaccess';
+ if (!FileHelper::fileExists($filename)) {
+ //error_log('exists not:' . $filename);
+ continue;
+ } else {
+ if (self::haveWeRulesInThisHTAccessBestGuess($filename)) {
+ if (self::saveHTAccessRulesToFile($filename, $comment, false)) {
+ $successes[] = $imageRootId;
+ } else {
+ $failures[] = $imageRootId;
+ }
+ } else {
+ //error_log('no rules:' . $filename);
+ }
+ }
+ }
+ $success = (count($failures) == 0);
+ return [$success, $failures, $successes];
+ }
+
+ public static function testLinks($config) {
+ /*
+ if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false )) {
+ if ($config['operation-mode'] != 'no-conversion') {
+ if ($config['image-types'] != 0) {
+ $webpExpressRoot = Paths::getWebPExpressPluginUrlPath();
+ $links = '';
+ if ($config['enable-redirection-to-converter']) {
+ $links = '
';
+ $links .= 'Convert test image (show debug)
';
+ $links .= 'Convert test image
';
+ }
+ // TODO: webp-realizer test links (to missing webp)
+ if ($config['enable-redirection-to-webp-realizer']) {
+ }
+
+ // TODO: test link for testing redirection to existing
+ if ($config['redirect-to-existing-in-htaccess']) {
+
+ }
+
+ return $links;
+ }
+ }
+ }*/
+ return '';
+ }
+
+
+ public static function getHTAccessDirRequirements() {
+ $minRequired = 'index';
+ if (Paths::isWPContentDirMovedOutOfAbsPath()) {
+ $minRequired = 'wp-content';
+ $pluginToo = Paths::isPluginDirMovedOutOfWpContent() ? 'yes' : 'no';
+ $uploadToo = Paths::isUploadDirMovedOutOfWPContentDir() ? 'yes' : 'no';
+ } else {
+ // plugin requirement depends...
+ // - if user grants access to 'index', the requirement is Paths::isPluginDirMovedOutOfAbsPath()
+ // - if user grants access to 'wp-content', the requirement is Paths::isPluginDirMovedOutOfWpContent()
+ $pluginToo = 'depends';
+
+ // plugin requirement depends...
+ // - if user grants access to 'index', we should be fine, as UPLOADS is always in ABSPATH.
+ // - if user grants access to 'wp-content', the requirement is Paths::isUploadDirMovedOutOfWPContentDir()
+ $uploadToo = 'depends';
+ }
+
+ // We need upload too for rewrite rules when destination structure is image-roots.
+ // but it is also good otherwise. So lets always do it.
+
+ $uploadToo = 'yes';
+
+ return [
+ $minRequired,
+ $pluginToo, // 'yes', 'no' or 'depends'
+ $uploadToo
+ ];
+ }
+
+ public static function saveRules($config, $showMessage = true) {
+ list($success, $failedDeactivations, $successfulDeactivations) = self::deactivateHTAccessRules('# The rules have left the building');
+
+ $rootsToPutRewritesIn = $config['scope'];
+ if ($config['destination-structure'] == 'doc-root') {
+ // Commented out to quickfix #338
+ // $rootsToPutRewritesIn = Paths::filterOutSubRoots($rootsToPutRewritesIn);
+ }
+
+ $dirsContainingWebps = [];
+
+ $mingled = ($config['destination-folder'] == 'mingled');
+ if ($mingled) {
+ $dirsContainingWebps[] = 'uploads';
+ }
+ $scopeOtherThanUpload = (str_replace('uploads', '', implode(',', $config['scope'])) != '');
+
+ if ($scopeOtherThanUpload || (!$mingled)) {
+ $dirsContainingWebps[] = 'cache';
+ }
+
+ $dirsToPutRewritesIn = array_unique(array_merge($rootsToPutRewritesIn, $dirsContainingWebps));
+
+ $failedWrites = [];
+ $successfullWrites = [];
+ foreach ($dirsToPutRewritesIn as $rootId) {
+ $dirContainsSourceImages = in_array($rootId, $rootsToPutRewritesIn);
+ $dirContainsWebPImages = in_array($rootId, $dirsContainingWebps);
+
+ $rules = HTAccessRules::generateHTAccessRulesFromConfigObj(
+ $config,
+ $rootId,
+ $dirContainsSourceImages,
+ $dirContainsWebPImages
+ );
+ $success = self::saveHTAccessRules(
+ $rootId,
+ $rules,
+ true
+ );
+ if ($success) {
+ $successfullWrites[] = $rootId;
+
+ // Remove it from $successfulDeactivations (if it is there)
+ if (($key = array_search($rootId, $successfulDeactivations)) !== false) {
+ unset($successfulDeactivations[$key]);
+ }
+ } else {
+ $failedWrites[] = $rootId;
+
+ // Remove it from $failedDeactivations (if it is there)
+ if (($key = array_search($rootId, $failedDeactivations)) !== false) {
+ unset($failedDeactivations[$key]);
+ }
+ }
+ }
+
+ $success = ((count($failedDeactivations) == 0) && (count($failedWrites) == 0));
+
+ $return = [$success, $successfullWrites, $successfulDeactivations, $failedWrites, $failedDeactivations];
+ if ($showMessage) {
+ self::showSaveRulesMessages($return);
+ }
+ return $return;
+ }
+
+ public static function showSaveRulesMessages($saveRulesResult)
+ {
+ list($success, $successfullWrites, $successfulDeactivations, $failedWrites, $failedDeactivations) = $saveRulesResult;
+
+ $msg = '';
+ if (count($successfullWrites) > 0) {
+ $msg .= '
';
+ }
+ }
+
+ if (count($successfulDeactivations) > 0) {
+ $msg .= '
';
+ }
+ }
+
+ if ($msg != '') {
+ Messenger::addMessage(
+ ($success ? 'success' : 'info'),
+ $msg
+ );
+ }
+
+ if (count($failedWrites) > 0) {
+ $msg = '
';
+ }
+ $msg .= 'You need to change the file permissions to allow WebP Express to save the rules.';
+ Messenger::addMessage('error', $msg);
+ } else {
+ if (count($failedDeactivations) > 0) {
+ $msg = '
';
+ }
+ $msg .= 'You need to change the file permissions to allow WebP Express to remove the rules or ' .
+ 'remove them manually';
+ Messenger::addMessage('error', $msg);
+ }
+ }
+ }
+
+}
diff --git a/lib/classes/HTAccessCapabilityTestRunner.php b/lib/classes/HTAccessCapabilityTestRunner.php
new file mode 100644
index 0000000..c572378
--- /dev/null
+++ b/lib/classes/HTAccessCapabilityTestRunner.php
@@ -0,0 +1,187 @@
+ 10]);
+ //echo '' . print_r($response, true) . '
';
+ if (is_wp_error($response)) {
+ return null;
+ }
+ if (wp_remote_retrieve_response_code($response) != '200') {
+ return false;
+ }
+ $body = wp_remote_retrieve_body($response);
+ return ($body == 'pong');
+ }
+
+ private static function runNamedTest($testName)
+ {
+ switch ($testName) {
+ case 'canRunTestScriptInWOD':
+ $url = Paths::getWebPExpressPluginUrl() . '/wod/ping.php';
+ return self::canRunPingPongTestScript($url);
+
+ case 'canRunTestScriptInWOD2':
+ $url = Paths::getWebPExpressPluginUrl() . '/wod2/ping.php';
+ return self::canRunPingPongTestScript($url);
+
+ case 'htaccessEnabled':
+ return self::runTestInWebPExpressContentDir('htaccessEnabled');
+
+ case 'modHeadersLoaded':
+ return self::runTestInWebPExpressContentDir('modHeadersLoaded');
+
+ case 'modHeaderWorking':
+ return self::runTestInWebPExpressContentDir('headerSetWorks');
+
+ case 'modRewriteWorking':
+ return self::runTestInWebPExpressContentDir('rewriteWorks');
+
+ case 'passThroughEnvWorking':
+ return self::runTestInWebPExpressContentDir('passingInfoFromRewriteToScriptThroughEnvWorks');
+
+ case 'passThroughHeaderWorking':
+ // pretend it fails because .htaccess rules aren't currently generated correctly
+ return false;
+ return self::runTestInWebPExpressContentDir('passingInfoFromRewriteToScriptThroughRequestHeaderWorks');
+
+ case 'grantAllAllowed':
+ return self::runTestInWebPExpressContentDir('grantAllCrashTester');
+ }
+ }
+
+ private static function runOrGetCached($testName)
+ {
+ if (!isset(self::$cachedResults)) {
+ self::$cachedResults = [];
+ }
+ if (!isset(self::$cachedResults[$testName])) {
+ self::$cachedResults[$testName] = self::runNamedTest($testName);
+ }
+ return self::$cachedResults[$testName];
+ }
+
+ /**
+ * Run one of the htaccess capability tests.
+ * Three possible outcomes: true, false or null (null if request fails)
+ */
+ private static function runTestInWebPExpressContentDir($testName)
+ {
+ $baseDir = Paths::getWebPExpressContentDirAbs() . '/htaccess-capability-tests';
+ $baseUrl = Paths::getContentUrl() . '/webp-express/htaccess-capability-tests';
+
+ $hct = new HtaccessCapabilityTester($baseDir, $baseUrl);
+ $hct->setHttpRequester(new WPHttpRequester());
+
+ try {
+ switch ($testName) {
+ case 'htaccessEnabled':
+ return $hct->htaccessEnabled();
+ case 'rewriteWorks':
+ return $hct->rewriteWorks();
+ case 'addTypeWorks':
+ return $hct->addTypeWorks();
+ case 'modHeadersLoaded':
+ return $hct->moduleLoaded('headers');
+ case 'headerSetWorks':
+ return $hct->headerSetWorks();
+ case 'requestHeaderWorks':
+ return $hct->requestHeaderWorks();
+ case 'passingInfoFromRewriteToScriptThroughRequestHeaderWorks':
+ return $hct->passingInfoFromRewriteToScriptThroughRequestHeaderWorks();
+ case 'passingInfoFromRewriteToScriptThroughEnvWorks':
+ return $hct->passingInfoFromRewriteToScriptThroughEnvWorks();
+ case 'grantAllCrashTester':
+ $rules = <<<'EOD'
+
' .
+ 'The image redirections are in effect again.
' .
+ 'Just a quick reminder: If you at some point change the upload directory or move Wordpress, ' .
+ 'the .htaccess files will need to be regenerated.
' .
+ 'You do that by re-saving the settings ' .
+ '(here)'
+ );
+ } else {
+ Messenger::addMessage(
+ 'warning',
+ 'WebP Express could not regenerate the rewrite rules
' .
+ 'You need to change some permissions. Head to the ' .
+ 'settings page ' .
+ 'and try to save the settings there (it will provide more information about the problem)'
+ );
+ }
+
+ HTAccess::showSaveRulesMessages($rulesResult);
+ }
+ }
+
+ private static function activateFirstTime()
+ {
+ // First check basic requirements.
+ // -------------------------------
+
+ if (PlatformInfo::isMicrosoftIis()) {
+ Messenger::addMessage(
+ 'warning',
+ 'You are on Microsoft IIS server. ' .
+ 'WebP Express should work on Windows now, but it has not been tested thoroughly.'
+
+ );
+ }
+
+ if (!version_compare(PHP_VERSION, '5.5.0', '>=')) {
+ Messenger::addMessage(
+ 'warning',
+ 'You are on a very old version of PHP. WebP Express may not work correctly. Your PHP version:' . phpversion()
+ );
+ }
+
+ // Next issue warnings, if any
+ // -------------------------------
+
+ if (PlatformInfo::isApache() || PlatformInfo::isLiteSpeed()) {
+ // all is well.
+ } else {
+ Messenger::addMessage(
+ 'warning',
+ 'You are not on Apache server, nor on LiteSpeed. WebP Express only works out of the box on Apache and LiteSpeed.
' .
+ 'But you may get it to work. WebP Express will print you rewrite rules for Apache. You could try to configure your server to do similar routing.
' .
+ 'Btw: your server is: ' . $_SERVER['SERVER_SOFTWARE']
+ );
+ }
+
+ // Welcome!
+ // -------------------------------
+ Messenger::addMessage(
+ 'info',
+ 'WebP Express was installed successfully. To start using it, you must ' .
+ 'configure it here.'
+ );
+
+ }
+}
diff --git a/lib/classes/PluginDeactivate.php b/lib/classes/PluginDeactivate.php
new file mode 100644
index 0000000..3169802
--- /dev/null
+++ b/lib/classes/PluginDeactivate.php
@@ -0,0 +1,36 @@
+Sorry, can't let you disable WebP Express!
" .
+ 'There are rewrite rules in the .htaccess that could not be removed. If these are not removed, it would break all images.
' .
+ 'Please make your .htaccess writable and then try to disable WebPExpress again.
Alternatively, remove the rules manually in your .htaccess file and try disabling again.' .
+ '
It concerns the following files:
';
+
+
+ foreach ($failures as $rootId) {
+ $msg .= '- ' . Paths::getAbsDirById($rootId) . '/.htaccess
';
+ }
+
+ Messenger::addMessage(
+ 'error',
+ $msg
+ );
+
+ wp_redirect(admin_url('options-general.php?page=webp_express_settings_page'));
+ exit;
+ }
+ }
+}
diff --git a/lib/classes/PluginPageScript.php b/lib/classes/PluginPageScript.php
new file mode 100644
index 0000000..7110966
--- /dev/null
+++ b/lib/classes/PluginPageScript.php
@@ -0,0 +1,26 @@
+' . $script . '';
+ }
+ }
+ }
+
+ wp_register_script('webpexpress-plugin-page', plugins_url($jsDir . '/plugin-page.js', dirname(dirname(__FILE__))), [], '1.9.0');
+ wp_enqueue_script('webpexpress-plugin-page');
+ }
+}
diff --git a/lib/classes/PluginUninstall.php b/lib/classes/PluginUninstall.php
new file mode 100644
index 0000000..70764ca
--- /dev/null
+++ b/lib/classes/PluginUninstall.php
@@ -0,0 +1,33 @@
+ $optionName) {
+ Option::deleteOption($optionName);
+ }
+
+ // remove content dir (config plus images plus htaccess-tests)
+ FileHelper::rrmdir(Paths::getWebPExpressContentDirAbs());
+ }
+}
diff --git a/lib/classes/Sanitize.php b/lib/classes/Sanitize.php
new file mode 100644
index 0000000..34b3953
--- /dev/null
+++ b/lib/classes/Sanitize.php
@@ -0,0 +1,31 @@
+getMessage());
+ wp_die();
+ }
+ $result = '';
+ if (method_exists(__CLASS__, $testId)) {
+
+ // The following call sets self::$next.
+ $result = call_user_func(array(__CLASS__, $testId));
+ } else {
+ $result = ['Unknown test: ' . $testId];
+ self::$next = 'break';
+ }
+
+ $response = [
+ 'result' => $result,
+ 'next' => self::$next
+ ];
+ echo json_encode($response, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
+ wp_die();
+ }
+
+}
diff --git a/lib/classes/SelfTestHelper.php b/lib/classes/SelfTestHelper.php
new file mode 100644
index 0000000..9a54835
--- /dev/null
+++ b/lib/classes/SelfTestHelper.php
@@ -0,0 +1,792 @@
+get_error_message();
+ //$log[] = print_r($wpResult, true);
+ return [false, $log, $results];
+ }
+ if (!is_wp_error($wpResult) && !isset($wpResult['headers'])) {
+ $wpResult['headers'] = [];
+ }
+ $results[] = $wpResult;
+ $responseCode = $wpResult['response']['code'];
+
+ $log[] = 'Response: ' . $responseCode . ' ' . $wpResult['response']['message'];
+ $log = array_merge($log, SelfTestHelper::printHeaders($wpResult['headers']));
+
+ if (isset($wpResult['headers']['content-type'])) {
+ if (strpos($wpResult['headers']['content-type'], 'text/html') !== false) {
+ if (isset($wpResult['body']) && (!empty($wpResult['body']))) {
+ $log[] = 'Body:';
+ $log[] = print_r($wpResult['body'], true);
+ }
+ }
+ }
+
+ if (($responseCode == '302') || ($responseCode == '301')) {
+ if ($maxRedirects > 0) {
+ if (isset($wpResult['headers']['location'])) {
+ $url = $wpResult['headers']['location'];
+ if (strpos($url, 'http') !== 0) {
+ $url = $requestUrl . $url;
+ }
+ $log[] = 'Following that redirect';
+
+ list($success, $newLog, $newResult) = self::remoteGet($url, $args, $maxRedirects - 1);
+ $log = array_merge($log, $newLog);
+ $results = array_merge($results, $newResult);
+
+ return [$success, $log, $results];
+
+ }
+ } else {
+ $log[] = 'Not following the redirect (max redirects exceeded)';
+ }
+ }
+
+ $success = ($responseCode == '200');
+ return [$success, $log, $results];
+ }
+
+ public static function hasHeaderContaining($headers, $headerToInspect, $containString)
+ {
+ if (!isset($headers[$headerToInspect])) {
+ return false;
+ }
+
+ // If there are multiple headers, check all
+ if (gettype($headers[$headerToInspect]) == 'string') {
+ $h = [$headers[$headerToInspect]];
+ } else {
+ $h = $headers[$headerToInspect];
+ }
+ foreach ($h as $headerValue) {
+ if (stripos($headerValue, $containString) !== false) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static function hasVaryAcceptHeader($headers)
+ {
+ if (!isset($headers['vary'])) {
+ return false;
+ }
+
+ // There may be multiple Vary headers. Or they might be combined in one.
+ // Both are acceptable, according to https://stackoverflow.com/a/28799169/842756
+ if (gettype($headers['vary']) == 'string') {
+ $varyHeaders = [$headers['vary']];
+ } else {
+ $varyHeaders = $headers['vary'];
+ }
+ foreach ($varyHeaders as $headerValue) {
+ $values = explode(',', $headerValue);
+ foreach ($values as $value) {
+ if (strtolower($value) == 'accept') {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @param string $rule existing|webp-on-demand|webp-realizer
+ */
+ public static function diagnoseNoVaryHeader($rootId, $rule)
+ {
+ $log = [];
+ $log[] = '**However, we did not receive a Vary:Accept header. ' .
+ 'That header should be set in order to tell proxies that the response varies depending on the ' .
+ 'Accept header. Otherwise browsers not supporting webp might get a cached webp and vice versa.**{: .warn}';
+
+ $log[] = 'Too technical? ';
+ $log[] = 'Here is an explanation of what this means: ' .
+ 'Some companies have set up proxies which caches resources. This way, if employee A have downloaded an ' .
+ 'image and employee B requests it, the proxy can deliver the image directly to employee B without needing to ' .
+ 'send a request to the server. ' .
+ 'This is clever, but it can go wrong. If B for some reason is meant to get another image than A, it will not ' .
+ 'happen, as the server does not get the request. That is where the Vary header comes in. It tells the proxy ' .
+ 'that the image is dependent upon something. In this case, we need to signal proxies that the image depends upon ' .
+ 'the "Accept" header, as this is the one browsers use to tell the server if it accepts webps or not. ' .
+ 'We do that using the "Vary:Accept" header. However - it is missing :( ' .
+ 'Which means that employees at (larger) companies might experience problems if some are using browsers ' .
+ 'that supports webp and others are using browsers that does not. Worst case is that the request to an image ' .
+ 'is done with a browser that supports webp, as this will cache the webp in the proxy, and deliver webps to ' .
+ 'all employees - even to those who uses browsers that does not support webp. These employees will get blank images.';
+
+ if ($rule == 'existing') {
+ $log[] = 'So, what should you do? **I would recommend that you either try to fix the problem with the missing Vary:Accept ' .
+ 'header or change to "CDN friendly" mode.**{: .warn}';
+ } elseif ($rule == 'webp-on-demand') {
+ $log[] = 'So, what should you do? **I would recommend that you either try to fix the problem with the missing Vary:Accept ' .
+ 'header or disable the "Enable redirection to converter?" option and use another way to get the images converted - ie ' .
+ 'Bulk Convert or Convert on Upload**{: .warn}';
+ }
+
+
+
+ return $log;
+ }
+
+ public static function hasCacheControlOrExpiresHeader($headers)
+ {
+ if (isset($headers['cache-control'])) {
+ return true;
+ }
+ if (isset($headers['expires'])) {
+ return true;
+ }
+ return false;
+ }
+
+
+ public static function flattenHeaders($headers)
+ {
+ $log = [];
+ foreach ($headers as $headerName => $headerValue) {
+ if (gettype($headerValue) == 'array') {
+ foreach ($headerValue as $i => $value) {
+ $log[] = [$headerName, $value];
+ }
+ } else {
+ $log[] = [$headerName, $headerValue];
+ }
+ }
+ return $log;
+ }
+
+ public static function printHeaders($headers)
+ {
+ $log = [];
+ $log[] = '#### Response headers:';
+
+ $headersFlat = self::flattenHeaders($headers);
+ //
+ foreach ($headersFlat as $i => list($headerName, $headerValue)) {
+ if ($headerName == 'x-webp-express-error') {
+ $headerValue = '**' . $headerValue . '**{: .error}';
+ }
+ $log[] = '- ' . $headerName . ': ' . $headerValue;
+ }
+ $log[] = '';
+ return $log;
+ }
+
+ private static function trueFalseNullString($var)
+ {
+ if ($var === true) {
+ return 'yes';
+ }
+ if ($var === false) {
+ return 'no';
+ }
+ return 'could not be determined';
+ }
+
+ public static function systemInfo()
+ {
+ $log = [];
+ $log[] = '#### System info:';
+ $log[] = '- PHP version: ' . phpversion();
+ $log[] = '- OS: ' . PHP_OS;
+ $log[] = '- Server software: ' . $_SERVER["SERVER_SOFTWARE"];
+ $log[] = '- Document Root status: ' . Paths::docRootStatusText();
+ if (PathHelper::isDocRootAvailable()) {
+ $log[] = '- Document Root: ' . $_SERVER['DOCUMENT_ROOT'];
+ }
+ if (PathHelper::isDocRootAvailableAndResolvable()) {
+ if ($_SERVER['DOCUMENT_ROOT'] != realpath($_SERVER['DOCUMENT_ROOT'])) {
+ $log[] = '- Document Root (symlinked resolved): ' . realpath($_SERVER['DOCUMENT_ROOT']);
+ }
+ }
+
+ $log[] = '- Document Root: ' . Paths::docRootStatusText();
+ $log[] = '- Apache module "mod_rewrite" enabled?: ' . self::trueFalseNullString(PlatformInfo::gotApacheModule('mod_rewrite'));
+ $log[] = '- Apache module "mod_headers" enabled?: ' . self::trueFalseNullString(PlatformInfo::gotApacheModule('mod_headers'));
+ return $log;
+ }
+
+ public static function wordpressInfo()
+ {
+ $log = [];
+ $log[] = '#### Wordpress info:';
+ $log[] = '- Version: ' . get_bloginfo('version');
+ $log[] = '- Multisite?: ' . self::trueFalseNullString(is_multisite());
+ $log[] = '- Is wp-content moved?: ' . self::trueFalseNullString(Paths::isWPContentDirMoved());
+ $log[] = '- Is uploads moved out of wp-content?: ' . self::trueFalseNullString(Paths::isUploadDirMovedOutOfWPContentDir());
+ $log[] = '- Is plugins moved out of wp-content?: ' . self::trueFalseNullString(Paths::isPluginDirMovedOutOfWpContent());
+
+ $log[] = '';
+
+ $log[] = '#### Image roots (absolute paths)';
+ foreach (Paths::getImageRootIds() as $rootId) {
+ $absDir = Paths::getAbsDirById($rootId);
+
+ if (PathHelper::pathExistsAndIsResolvable($absDir) && ($absDir != realpath($absDir))) {
+ $log[] = '*' . $rootId . '*: ' . $absDir . ' (resolved for symlinks: ' . realpath($absDir) . ')';
+ } else {
+ $log[] = '*' . $rootId . '*: ' . $absDir;
+
+ }
+ }
+
+ $log[] = '#### Image roots (relative to document root)';
+ foreach (Paths::getImageRootIds() as $rootId) {
+ $absPath = Paths::getAbsDirById($rootId);
+ if (PathHelper::canCalculateRelPathFromDocRootToDir($absPath)) {
+ $log[] = '*' . $rootId . '*: ' . PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed($absPath);
+ } else {
+ $log[] = '*' . $rootId . '*: ' . 'n/a (not within document root)';
+ }
+ }
+
+ $log[] = '#### Image roots (URLs)';
+ foreach (Paths::getImageRootIds() as $rootId) {
+ $url = Paths::getUrlById($rootId);
+ $log[] = '*' . $rootId . '*: ' . $url;
+ }
+
+
+ return $log;
+ }
+
+ public static function configInfo($config)
+ {
+ $log = [];
+ $log[] = '#### WebP Express configuration info:';
+ $log[] = '- Destination folder: ' . $config['destination-folder'];
+ $log[] = '- Destination extension: ' . $config['destination-extension'];
+ $log[] = '- Destination structure: ' . $config['destination-structure'];
+ //$log[] = 'Image types: ' . ;
+ //$log[] = '';
+ $log[] = '(To view all configuration, take a look at the config file, which is stored in *' . Paths::getConfigFileName() . '*)';
+ //$log[] = '- Config file: (config.json)';
+ //$log[] = "'''\n" . json_encode($config, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT) . "\n'''\n";
+ return $log;
+ }
+
+ public static function htaccessInfo($config, $printRules = true)
+ {
+ $log = [];
+ //$log[] = '*.htaccess info:*';
+ //$log[] = '- Image roots with WebP Express rules: ' . implode(', ', HTAccess::getRootsWithWebPExpressRulesIn());
+ $log[] = '#### .htaccess files that WebP Express have placed rules in the following files:';
+ $rootIds = HTAccess::getRootsWithWebPExpressRulesIn();
+ foreach ($rootIds as $imageRootId) {
+ $log[] = '- ' . Paths::getAbsDirById($imageRootId) . '/.htaccess';
+ }
+
+ foreach ($rootIds as $imageRootId) {
+ $log = array_merge($log, self::rulesInImageRoot($config, $imageRootId));
+ }
+
+ return $log;
+ }
+
+ public static function rulesInImageRoot($config, $imageRootId)
+ {
+ $log = [];
+ $file = Paths::getAbsDirById($imageRootId) . '/.htaccess';
+ $log[] = '#### WebP rules in *' .
+ ($imageRootId == 'cache' ? 'webp image cache' : $imageRootId) . '*:';
+ $log[] = 'File: ' . $file;
+ if (!HTAccess::haveWeRulesInThisHTAccess($file)) {
+ $log[] = '**NONE!**{: .warn}';
+ } else {
+ $weRules = HTAccess::extractWebPExpressRulesFromHTAccess($file);
+ // remove unindented comments
+ //$weRules = preg_replace('/^\#\s[^\n\r]*[\n\r]+/ms', '', $weRules);
+
+ // remove comments in the beginning
+ $weRulesArr = preg_split("/\r\n|\n|\r/", $weRules); // https://stackoverflow.com/a/11165332/842756
+ while ((strlen($weRulesArr[0]) > 0) && ($weRulesArr[0][0] == '#')) {
+ array_shift($weRulesArr);
+ }
+ $weRules = implode("\n", $weRulesArr);
+
+ $log[] = '```' . $weRules . '```';
+ }
+ return $log;
+ }
+
+ public static function rulesInUpload($config)
+ {
+ return self::rulesInImageRoot($config, 'uploads');
+ }
+
+ public static function allInfo($config)
+ {
+ $log = [];
+
+ $log = array_merge($log, self::systemInfo());
+ $log = array_merge($log, self::wordpressInfo());
+ $log = array_merge($log, self::configInfo($config));
+ $log = array_merge($log, self::capabilityTests($config));
+ $log = array_merge($log, self::htaccessInfo($config, true));
+ //$log = array_merge($log, self::rulesInImageRoot($config, 'upload'));
+ //$log = array_merge($log, self::rulesInImageRoot($config, 'wp-content'));
+ return $log;
+ }
+
+ public static function capabilityTests($config)
+ {
+ $capTests = $config['base-htaccess-on-these-capability-tests'];
+ $log = [];
+ $log[] = '#### Live tests of .htaccess capabilities / system configuration:';
+ $log[] = 'Unless noted otherwise, the tests are run in *wp-content/webp-express/htaccess-capability-tester*. ';
+ $log[] = 'WebPExpress currently treats the results as they neccessarily applies to all scopes (upload, themes, etc), ';
+ $log[] = 'but note that a server might be configured to have mod_rewrite disallowed in some folders and allowed in others.';
+ /*$log[] = 'Exactly what you can do in a *.htaccess* depends on the server setup. WebP Express ' .
+ 'makes some live tests to verify if a certain feature in fact works. This is done by creating ' .
+ 'test files (*.htaccess* files and php files) in a dir inside the content dir and running these. ' .
+ 'These test results are used when creating the rewrite rules. Here are the results:';*/
+
+// $log[] = '';
+ $log[] = '- .htaccess files enabled?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::htaccessEnabled());
+ $log[] = '- mod_rewrite working?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::modRewriteWorking());
+ $log[] = '- mod_headers loaded?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::modHeadersLoaded());
+ $log[] = '- mod_headers working (header set): ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::modHeaderWorking());
+ //$log[] = '- passing variables from *.htaccess* to PHP script through environment variable working?: ' . self::trueFalseNullString($capTests['passThroughEnvWorking']);
+ $log[] = '- passing variables from *.htaccess* to PHP script through environment variable working?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::passThroughEnvWorking());
+ $log[] = '- Can run php test file in plugins/webp-express/wod/ ?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::canRunTestScriptInWOD());
+ $log[] = '- Can run php test file in plugins/webp-express/wod2/ ?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::canRunTestScriptInWOD2());
+ $log[] = '- Directives for granting access like its done in wod/.htaccess allowed?: ' . self::trueFalseNullString(HTAccessCapabilityTestRunner::grantAllAllowed());
+ /*$log[] = '- pass variable from *.htaccess* to script through header working?: ' .
+ self::trueFalseNullString($capTests['passThroughHeaderWorking']);*/
+ return $log;
+ }
+
+ public static function diagnoseFailedRewrite($config, $headers)
+ {
+ if (($config['destination-structure'] == 'image-roots') && (!PathHelper::isDocRootAvailableAndResolvable())) {
+ $log[] = 'The problem is probably this combination:';
+ if (!PathHelper::isDocRootAvailable()) {
+ $log[] = '1. Your document root isn`t available';
+ } else {
+ $log[] = '1. Your document root isn`t resolvable for symlinks (it is probably subject to open_basedir restriction)';
+ }
+ $log[] = '2. Your document root is symlinked';
+ $log[] = '3. The wordpress function that tells the path of the uploads folder returns the symlink resolved path';
+
+ $log[] = 'I cannot check if your document root is in fact symlinked (as document root isnt resolvable). ' .
+ 'But if it is, there you have it. The line beginning with "RewriteCond %{REQUEST_FILENAME}"" points to your resolved root, ' .
+ 'but it should point to your symlinked root. WebP Express cannot do that for you because it cannot discover what the symlink is. ' .
+ 'Try changing the line manually. When it works, you can move the rules outside the WebP Express block so they dont get ' .
+ 'overwritten. OR you can change your server configuration (document root / open_basedir restrictions)';
+ }
+
+ //$log[] = '## Diagnosing';
+
+ //if (PlatformInfo::isNginx()) {
+ if (strpos($headers['server'], 'nginx') === 0) {
+
+ // Nginx
+ $log[] = 'Notice that you are on Nginx and the rules that WebP Express stores in the *.htaccess* files probably does not ' .
+ 'have any effect. ';
+ $log[] = 'Please read the "I am on Nginx" section in the FAQ (https://wordpress.org/plugins/webp-express/)';
+ $log[] = 'And did you remember to restart the nginx service after updating the configuration?';
+
+ $log[] = 'PS: If you cannot get the redirect to work, you can simply rely on Alter HTML as described in the FAQ.';
+ return $log;
+ }
+
+ $modRewriteWorking = HTAccessCapabilityTestRunner::modRewriteWorking();
+ if ($modRewriteWorking !== null) {
+ $log[] = 'Running a special designed capability test to test if rewriting works with *.htaccess* files';
+ }
+ if ($modRewriteWorking === true) {
+ $log[] = 'Result: Yes, rewriting works.';
+ $log[] = 'It seems something is wrong with the *.htaccess* rules then. You could try ' .
+ 'to change "Destination structure" - the rules there are quite different.';
+ $log[] = 'It could also be that the server has cached the configuration a while. Some servers ' .
+ 'does that. In that case, simply give it a few minutes and try again.';
+ } elseif ($modRewriteWorking === false) {
+ $log[] = 'Result: No, rewriting does not seem to work within *.htaccess* rules.';
+ if (PlatformInfo::definitelyNotGotModRewrite()) {
+ $log[] = 'It actually seems "mod_write" is disabled on your server. ' .
+ '**You must enable mod_rewrite on the server**';
+ } elseif (PlatformInfo::definitelyGotApacheModule('mod_rewrite')) {
+ $log[] = 'However, "mod_write" *is* enabled on your server. This seems to indicate that ' .
+ '*.htaccess* files has been disabled for configuration on your server. ' .
+ 'In that case, you need to copy the WebP Express rules from the *.htaccess* files into your virtual host configuration files. ' .
+ '(WebP Express generates multiple *.htaccess* files. Look in the upload folder, the wp-content folder, etc).';
+ $log[] = 'It could however alse simply be that your server simply needs some time. ' .
+ 'Some servers caches the *.htaccess* rules for a bit. In that case, simply give it a few minutes and try again.';
+ } else {
+ $log[] = 'However, this could be due to your server being a bit slow on picking up changes in *.htaccess*.' .
+ 'Give it a few minutes and try again.';
+ }
+ } else {
+ // The mod_rewrite test could not conclude anything.
+ if (PlatformInfo::definitelyNotGotApacheModule('mod_rewrite')) {
+ $log[] = 'It actually seems "mod_write" is disabled on your server. ' .
+ '**You must enable mod_rewrite on the server**';
+ } elseif (PlatformInfo::definitelyGotApacheModule('mod_rewrite')) {
+ $log[] = '"mod_write" is enabled on your server, so rewriting ought to work. ' .
+ 'However, it could be that your server setup has disabled *.htaccess* files for configuration. ' .
+ 'In that case, you need to copy the WebP Express rules from the *.htaccess* files into your virtual host configuration files. ' .
+ '(WebP Express generates multiple *.htaccess* files. Look in the upload folder, the wp-content folder, etc). ';
+ } else {
+ $log[] = 'It seems something is wrong with the *.htaccess* rules. ';
+ $log[] = 'Or perhaps the server has cached the configuration a while. Some servers ' .
+ 'does that. In that case, simply give it a few minutes and try again.';
+ }
+ }
+ $log[] = 'Note that if you cannot get redirection to work, you can switch to "CDN friendly" mode and ' .
+ 'rely on the "Alter HTML" functionality to point to the webp images. If you do a bulk conversion ' .
+ 'and make sure that "Convert upon upload" is activated, you should be all set. Alter HTML even handles ' .
+ 'inline css (unless you select "picture tag" syntax). It does however not handle images in external css or ' .
+ 'which is added dynamically with javascript.';
+
+ $log[] = '## Info for manually diagnosing';
+ $log = array_merge($log, self::allInfo($config));
+ return $log;
+ }
+
+ public static function diagnoseWod403or500($config, $rootId, $responseCode)
+ {
+ $log = [];
+
+ $htaccessRules = SelfTestHelper::rulesInImageRoot($config, $rootId);
+ $rulesText = implode('', $htaccessRules);
+ $rulesPointsToWod = (strpos($rulesText, '/wod/') > 0);
+ $rulesPointsToWod2 = (strpos($rulesText, '/wod2/') !== false);
+
+ $log[] = '';
+ $log[] = '**diagnosing**';
+ $canRunTestScriptInWod = HTAccessCapabilityTestRunner::canRunTestScriptInWOD();
+ $canRunTestScriptInWod2 = HTAccessCapabilityTestRunner::canRunTestScriptInWOD2();
+ $canRunInAnyWod = ($canRunTestScriptInWod || $canRunTestScriptInWod2);
+
+ $responsePingPhp = wp_remote_get(Paths::getPluginsUrl() . '/webp-express/wod/ping.php', ['timeout' => 7]);
+ $pingPhpResponseCode = wp_remote_retrieve_response_code($responsePingPhp);
+
+ $responsePingText = wp_remote_get(Paths::getPluginsUrl() . '/webp-express/wod/ping.txt', ['timeout' => 7]);
+ $pingTextResponseCode = wp_remote_retrieve_response_code($responsePingText);
+
+ if ($responseCode == 500) {
+ $log[] = 'The response was a *500 Internal Server Error*. There can be different reasons for that. ' .
+ 'Lets dig a bit deeper...';
+ }
+
+ $log[] = 'Examining where the *.htaccess* rules in the ' . $rootId . ' folder points to. ';
+
+ if ($rulesPointsToWod) {
+ $log[] = 'They point to **wod**/webp-on-demand.php';
+ } elseif ($rulesPointsToWod2) {
+ $log[] = 'They point to **wod2**/webp-on-demand.php';
+ } else {
+ $log[] = '**There are no redirect rule to *webp-on-demand.php* in the .htaccess!**{: .warn}';
+ $log[] = 'Here is the rules:';
+ $log = array_merge($log, $htaccessRules);
+ }
+
+ if ($rulesPointsToWod) {
+ $log[] = 'Requesting simple test script "wod/ping.php"... ' .
+ 'Result: ' . ($pingPhpResponseCode == '200' ? 'ok' : 'failed (response code: ' . $pingPhpResponseCode . ')');
+ //'Result: ' . ($canRunTestScriptInWod ? 'ok' : 'failed');
+
+ if ($canRunTestScriptInWod) {
+ if ($responseCode == '500') {
+ $log[] = '';
+ $log[] = '**As the test script works, it would seem that the explanation for the 500 internal server ' .
+ 'error is that the PHP script (webp-on-demand.php) crashes. ' .
+ 'You can help me by enabling debugging and post the error on the support forum on Wordpress ' .
+ '(https://wordpress.org/support/plugin/webp-express/), or create an issue on github ' .
+ '(https://github.com/rosell-dk/webp-express/issues)**';
+ $log[] = '';
+ }
+ } else {
+ $log[] = 'Requesting simple test file "wod/ping.txt". ' .
+ 'Result: ' . ($pingTextResponseCode == '200' ? 'ok' : 'failed (response code: ' . $pingTextResponseCode . ')');
+
+ if ($canRunTestScriptInWod2) {
+ if ($responseCode == 500) {
+ if ($pingTextResponseCode == '500') {
+ $log[] = 'The problem appears to be that the *.htaccess* placed in *plugins/webp-express/wod/.htaccess*' .
+ ' contains auth directives ("Allow" and "Request") and your server is set up to go fatal about it. ' .
+ 'Luckily, it seems that running scripts in the "wod2" folder works. ' .
+ '**What you need to do is simply to click the "Save settings and force new .htacess rules"' .
+ ' button. WebP Express wil then change the .htaccess rules to point to the "wod2" folder**';
+ } else {
+ $log[] = 'The problem appears to be running PHP scripts in the "wod". ' .
+ 'Luckily, it seems that running scripts in the "wod2" folder works ' .
+ '(it has probably something to do with the *.htaccess* file placed in "wod"). ' .
+ '**What you need to do is simply to click the "Save settings and force new .htacess rules"' .
+ ' button. WebP Express wil then change the .htaccess rules to point to the "wod2" folder**';
+ }
+ } elseif ($responseCode == 403) {
+ $log[] = 'The problem appears to be running PHP scripts in the "wod". ' .
+ 'Luckily, it seems that running scripts in the "wod2" folder works ' .
+ '(it could perhaps have something to do with the *.htaccess* file placed in "wod", ' .
+ 'although it ought not result in a 403). **What you need to do is simply to click the "Save settings and force new .htacess rules"' .
+ ' button. WebP Express wil then change the .htaccess rules to point to the "wod2" folder**';
+ }
+
+ return $log;
+ }
+ }
+ }
+
+ $log[] = 'Requesting simple test script "wod2/ping.php". Result: ' . ($canRunTestScriptInWod2 ? 'ok' : 'failed');
+ $responsePingText2 = wp_remote_get(Paths::getPluginsUrl() . '/webp-express/wod2/ping.txt', ['timeout' => 7]);
+ $pingTextResponseCode2 = wp_remote_retrieve_response_code($responsePingText2);
+ $log[] = 'Requesting simple test file "wod2/ping.txt". ' .
+ 'Result: ' . ($pingTextResponseCode == '200' ? 'ok' : 'failed (response code: ' . $pingTextResponseCode2 . ')');
+
+ if ($rulesPointsToWod2) {
+ if ($canRunTestScriptInWod2) {
+ if ($responseCode == '500') {
+ $log[] = '';
+ $log[] = '**As the test script works, it would seem that the explanation for the 500 internal server ' .
+ 'error is that the PHP script (webp-on-demand.php) crashes. ' .
+ 'You can help me by enabling debugging and post the error on the support forum on Wordpress ' .
+ '(https://wordpress.org/support/plugin/webp-express/), or create an issue on github ' .
+ '(https://github.com/rosell-dk/webp-express/issues)**';
+ $log[] = '';
+ }
+ } else {
+ if ($canRunTestScriptInWod) {
+ $log[] = '';
+ $log[] = 'The problem appears to be running PHP scripts in the "wod2" folder. ' .
+ 'Luckily, it seems that running scripts in the "wod" folder works ' .
+ '**What you need to do is simply to click the "Save settings and force new .htacess rules"' .
+ ' button. WebP Express wil then change the .htaccess rules to point to the "wod" folder**';
+ $log[] = '';
+ } else {
+ if ($responseCode == 500) {
+
+ if ($pingTextResponseCode2 == '500') {
+ $log[] = 'All our requests results in 500 Internal Error. Even ' .
+ 'the request to plugins/webp-express/wod2/ping.txt. ' .
+ 'Surprising!';
+ } else {
+ $log[] = 'The internal server error happens for php files, but not txt files. ' .
+ 'It could be the result of a restrictive server configuration or the works of a security plugin. ' .
+ 'Try to examine the .htaccess file in the plugins folder and its parent folders. ' .
+ 'Or try to look in the httpd.conf. Look for the "AllowOverride" and the "AllowOverrideList" directives. ';
+ }
+
+ //$log[] = 'We get *500 Internal Server Error*';
+ /*
+ It can for example be that the *.htaccess* ' .
+ 'in the ' . $rootId . ' folder (or a parent folder) contains directives that the server either ' .
+ 'doesnt support or has not allowed (using AllowOverride in ie httpd.conf). It could also be that the redirect succeded, ' .
+ 'but the *.htaccess* in the folder of the script (or a parent folder) results in such problems. Also, ' .
+ 'it could be that the script (webp-on-demand.php) for some reason fails.';
+
+ */
+ }
+ }
+
+
+ }
+ }
+ return $log;
+ }
+}
diff --git a/lib/classes/SelfTestRedirectAbstract.php b/lib/classes/SelfTestRedirectAbstract.php
new file mode 100644
index 0000000..19c158d
--- /dev/null
+++ b/lib/classes/SelfTestRedirectAbstract.php
@@ -0,0 +1,115 @@
+config = $config;
+ }
+
+ /**
+ * Run test for either jpeg or png
+ *
+ * @param string $rootId (ie "uploads" or "themes")
+ * @param string $imageType ("jpeg" or "png")
+ * @return array [$success, $result, $createdTestFiles]
+ */
+ abstract protected function runTestForImageType($rootId, $imageType);
+
+ abstract protected function getSuccessMessage();
+
+ private function doRunTestForRoot($rootId)
+ {
+ // return [true, ['hello'], false];
+// return [false, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers), false];
+
+ $result = [];
+
+ //$result[] = '*hello* with *you* and **you**. ok! FAILED';
+ $result[] = '## ' . $rootId;
+ //$result[] = 'This test examines image responses "from the outside".';
+
+ $createdTestFiles = false;
+
+ if ($this->config['image-types'] & 1) {
+ list($success, $subResult, $createdTestFiles) = $this->runTestForImageType($rootId, 'jpeg');
+ $result = array_merge($result, $subResult);
+
+ if ($success) {
+ if ($this->config['image-types'] & 2) {
+ $result[] = '### Performing same tests for PNG';
+ list($success, $subResult, $createdTestFiles2) = $this->runTestForImageType($rootId, 'png');
+ $createdTestFiles = $createdTestFiles || $createdTestFiles2;
+ if ($success) {
+ //$result[count($result) - 1] .= '. **ok**{: .ok}';
+ $result[] .= 'All tests passed for PNG as well.';
+ $result[] = '(I shall spare you for the report, which is almost identical to the one above)';
+ } else {
+ $result = array_merge($result, $subResult);
+ }
+ }
+ }
+ } else {
+ list($success, $subResult, $createdTestFiles) = $this->runTestForImageType($rootId, 'png');
+ $result = array_merge($result, $subResult);
+ }
+
+ if ($success) {
+ $result[] = '### Results for ' . strtoupper($rootId);
+
+ $result[] = $this->getSuccessMessage();
+ }
+ return [true, $result, $createdTestFiles];
+ }
+
+ private function runTestForRoot($rootId)
+ {
+ // TODO: move that method to here
+ SelfTestHelper::cleanUpTestImages($rootId, $this->config);
+
+ // Run the actual test
+ list($success, $result, $createdTestFiles) = $this->doRunTestForRoot($rootId);
+
+ // Clean up test images again. We are very tidy around here
+ if ($createdTestFiles) {
+ $result[] = 'Deleting test images';
+ SelfTestHelper::cleanUpTestImages($rootId, $this->config);
+ }
+
+ return [$success, $result];
+ }
+
+ abstract protected function startupTests();
+
+ protected function startTest()
+ {
+
+ list($success, $result) = $this->startupTests();
+
+ if (!$success) {
+ return [false, $result];
+ }
+
+ if (!file_exists(Paths::getConfigFileName())) {
+ $result[] = 'Hold on. You need to save options before you can run this test. There is no config file yet.';
+ return [true, $result];
+ }
+
+ if ($this->config['image-types'] == 0) {
+ $result[] = 'No image types have been activated, nothing to test';
+ return [true, $result];
+ }
+
+ foreach ($this->config['scope'] as $rootId) {
+ list($success, $subResult) = $this->runTestForRoot($rootId);
+ $result = array_merge($result, $subResult);
+ }
+ //list($success, $result) = self::runTestForRoot('uploads', $this->config);
+
+ return [$success, $result];
+ }
+
+}
diff --git a/lib/classes/SelfTestRedirectToConverter.php b/lib/classes/SelfTestRedirectToConverter.php
new file mode 100644
index 0000000..8b06176
--- /dev/null
+++ b/lib/classes/SelfTestRedirectToConverter.php
@@ -0,0 +1,239 @@
+ [
+ 'ACCEPT' => 'image/webp'
+ ],
+ ];
+ list($success, $remoteGetLog, $results) = SelfTestHelper::remoteGet($requestUrl, $requestArgs);
+ $headers = $results[count($results)-1]['headers'];
+ $log = array_merge($log, $remoteGetLog);
+
+ if (!$success) {
+ //$log[count($log) - 1] .= '. FAILED';
+ $log[] = 'The request FAILED';
+ //$log = array_merge($log, $remoteGetLog);
+
+ if (isset($results[0]['response']['code'])) {
+ $responseCode = $results[0]['response']['code'];
+ if (($responseCode == 500) || ($responseCode == 403)) {
+
+ $log = array_merge($log, SelfTestHelper::diagnoseWod403or500($this->config, $rootId, $responseCode));
+
+ //$log[] = 'or that there is an .htaccess file in the ';
+ }
+// $log[] = print_r($results[0]['response']['code'], true);
+ }
+ //$log[] = 'The test cannot be completed';
+ //$log[count($log) - 1] .= '. FAILED';
+ return [false, $log, $createdTestFiles];
+ }
+ //$log[count($log) - 1] .= '. ok!';
+ //$log[] = '*' . $requestUrl . '*';
+
+ //$log = array_merge($log, SelfTestHelper::printHeaders($headers));
+
+ if (!isset($headers['content-type'])) {
+ $log[] = 'Bummer. There is no "content-type" response header. The test FAILED';
+ return [false, $log, $createdTestFiles];
+ }
+
+ if ($headers['content-type'] == 'image/' . $imageType) {
+ $log[] = 'Bummer. As the "content-type" header reveals, we got the ' . $imageType . '.';
+ $log[] = 'The test **failed**{: .error}.';
+ $log[] = 'Now, what went wrong?';
+
+ if (isset($headers['x-webp-convert-log'])) {
+ //$log[] = 'Inspect the "x-webp-convert-log" headers above, and you ' .
+ // 'should have your answer (it is probably because you do not have any conversion methods working).';
+ if (SelfTestHelper::hasHeaderContaining($headers, 'x-webp-convert-log', 'Performing fail action: original')) {
+ $log[] = 'The answer lies in the "x-convert-log" response headers: ' .
+ '**The conversion failed**{: .error}. ';
+ }
+ } else {
+ $log[] = 'Well, there is indication that the redirection isnt working. ' .
+ 'The PHP script should set "x-webp-convert-log" response headers, but there are none. ';
+ 'While these headers could have been eaten in a Cloudflare-like setup, the problem is ';
+ 'probably that the redirection simply failed';
+
+ $log[] = '### Diagnosing redirection problems';
+ $log = array_merge($log, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers));
+ }
+ return [false, $log, $createdTestFiles];
+ }
+
+ if ($headers['content-type'] != 'image/webp') {
+ $log[] = 'However. As the "content-type" header reveals, we did not get a webp' .
+ 'Surprisingly we got: "' . $headers['content-type'] . '"';
+ $log[] = 'The test FAILED.';
+ return [false, $log, $createdTestFiles];
+ }
+
+ if (isset($headers['x-webp-convert-log'])) {
+ $log[] = 'Alrighty. We got a webp, and we got it from the PHP script. **Great!**{: .ok}';
+ } else {
+ if (count($results) > 1) {
+ if (isset($results[0]['headers']['x-webp-convert-log'])) {
+ $log[] = '**Great!**{: .ok}. The PHP script created a webp and redirected the image request ' .
+ 'back to itself. A refresh, if you wish. The refresh got us the webp (relying on there being ' .
+ 'a rule which redirect images to existing converted images for webp-enabled browsers - which there is!). ' .
+ (SelfTestHelper::hasVaryAcceptHeader($headers) ? 'And we got the Vary:Accept header set too. **Super!**{: .ok}!' : '');
+ }
+ } else {
+ $log[] = 'We got a webp. However, it seems we did not get it from the PHP script.';
+
+ }
+
+ //$log[] = print_r($return, true);
+ //error_log(print_r($return, true));
+ }
+
+ if (!SelfTestHelper::hasVaryAcceptHeader($headers)) {
+ $log = array_merge($log, SelfTestHelper::diagnoseNoVaryHeader($rootId, 'webp-on-demand'));
+ $noWarningsYet = false;
+ }
+ if (!SelfTestHelper::hasCacheControlOrExpiresHeader($headers)) {
+ $log[] = '**Notice: No cache-control or expires header has been set. ' .
+ 'It is recommended to do so. Set it nice and big once you are sure the webps have a good quality/compression compromise.**{: .warn}';
+ }
+ $log[] = '';
+
+
+ // Check browsers NOT supporting webp
+ // -----------------------------------
+ $log[] = '### Now lets check that browsers *not* supporting webp gets the ' . strtoupper($imageType);
+ $log[] = 'Making a HTTP request for the test image (without setting the "Accept" header)';
+ list($success, $remoteGetLog, $results) = SelfTestHelper::remoteGet($requestUrl);
+ $headers = $results[count($results)-1]['headers'];
+ $log = array_merge($log, $remoteGetLog);
+
+ if (!$success) {
+ $log[] = 'The request FAILED';
+ $log[] = 'The test cannot be completed';
+ //$log[count($log) - 1] .= '. FAILED';
+ return [false, $log, $createdTestFiles];
+ }
+ //$log[count($log) - 1] .= '. ok!';
+ //$log[] = '*' . $requestUrl . '*';
+
+ //$log = array_merge($log, SelfTestHelper::printHeaders($headers));
+
+ if (!isset($headers['content-type'])) {
+ $log[] = 'Bummer. There is no "content-type" response header. The test FAILED';
+ return [false, $log, $createdTestFiles];
+ }
+
+ if ($headers['content-type'] == 'image/webp') {
+ $log[] = '**Bummer**{: .error}. As the "content-type" header reveals, we got the webp. ' .
+ 'So even browsers not supporting webp gets webp. Not good!';
+ $log[] = 'The test FAILED.';
+
+ $log[] = '### What to do now?';
+ // TODO: We could examine the headers for common CDN responses
+
+ $log[] = 'First, examine the response headers above. Is there any indication that ' .
+ 'the image is returned from a CDN cache? ' .
+ $log[] = 'If there is: Check out the ' .
+ '*How do I configure my CDN in “Varied image responses” operation mode?* section in the FAQ ' .
+ '(https://wordpress.org/plugins/webp-express/)';
+
+ if (PlatformInfo::isApache()) {
+ $log[] = 'If not: please report this in the forum, as it seems the .htaccess rules ';
+ $log[] = 'just arent working on your system.';
+ } elseif (PlatformInfo::isNginx()) {
+ $log[] = 'Also, as you are on Nginx, check out the ' .
+ ' "I am on Nginx" section in the FAQ (https://wordpress.org/plugins/webp-express/)';
+ } else {
+ $log[] = 'If not: please report this in the forum, as it seems that there is something ' .
+ 'in the *.htaccess* rules generated by WebP Express that are not working.';
+ }
+
+ $log[] = '### System info (for manual diagnosing):';
+ $log = array_merge($log, SelfTestHelper::allInfo($this->config));
+
+
+ return [false, $log, $createdTestFiles];
+ }
+
+ if ($headers['content-type'] != 'image/' . $imageType) {
+ $log[] = 'Bummer. As the "content-type" header reveals, we did not get the ' . $imageType .
+ 'Surprisingly we got: "' . $headers['content-type'] . '"';
+ $log[] = 'The test FAILED.';
+ return [false, $log, $createdTestFiles];
+ }
+ $log[] = 'Alrighty. We got the ' . $imageType . '. **Great!**{: .ok}.';
+
+ if (!SelfTestHelper::hasVaryAcceptHeader($headers)) {
+ $log = array_merge($log, SelfTestHelper::diagnoseNoVaryHeader($rootId, 'webp-on-demand'));
+ $noWarningsYet = false;
+ }
+
+ return [$noWarningsYet, $log, $createdTestFiles];
+ }
+
+ protected function getSuccessMessage()
+ {
+ return 'Everything **seems to work**{: .ok} as it should. ' .
+ 'However, a check is on the TODO: ' .
+ 'TODO: Check that disabled image types does not get converted. ';
+ }
+
+ public function startupTests()
+ {
+ $log[] = '# Testing redirection to converter';
+ if (!$this->config['enable-redirection-to-converter']) {
+ $log[] = 'Turned off, nothing to test (if you just turned it on without saving, remember: this is a live test so you need to save settings)';
+ return [false, $log];
+ }
+ return [true, $log];
+ }
+
+ public static function runTest()
+ {
+ $config = Config::loadConfigAndFix(false);
+ $me = new SelfTestRedirectToConverter($config);
+ return $me->startTest();
+ }
+
+}
diff --git a/lib/classes/SelfTestRedirectToExisting.php b/lib/classes/SelfTestRedirectToExisting.php
new file mode 100644
index 0000000..154c4d6
--- /dev/null
+++ b/lib/classes/SelfTestRedirectToExisting.php
@@ -0,0 +1,250 @@
+config['destination-folder'],
+ $this->config['destination-extension'],
+ $this->config['destination-structure'],
+ $sourceFileName,
+ $imageType
+ );
+ $log = array_merge($log, $subResult);
+ if (!$success) {
+ $log[] = 'The test cannot be completed';
+ return [false, $log, $createdTestFiles];
+ }
+
+ $requestUrl = Paths::getUrlById($rootId) . '/webp-express-test-images/' . $sourceFileName;
+ $log[] = '### Lets check that browsers supporting webp gets the WEBP when the ' . strtoupper($imageType) . ' is requested';
+ $log[] = 'Making a HTTP request for the test image (pretending to be a client that supports webp, by setting the "Accept" header to "image/webp")';
+ $requestArgs = [
+ 'headers' => [
+ 'ACCEPT' => 'image/webp'
+ ]
+ ];
+
+ list($success, $remoteGetLog, $results) = SelfTestHelper::remoteGet($requestUrl, $requestArgs);
+ $headers = $results[count($results)-1]['headers'];
+ $log = array_merge($log, $remoteGetLog);
+
+ if (!$success) {
+ $log[] = 'The test cannot be completed, as the HTTP request failed. This does not neccesarily mean that the redirections ' .
+ "aren't" . ' working, but it means you will have to check it manually. Check out the FAQ on how to do this. ' .
+ 'You might also want to check out why a simple HTTP request could not be issued. WebP Express uses such requests ' .
+ 'for detecting system capabilities, which are used when generating .htaccess files. These tests are not essential, but ' .
+ 'it would be best to have them working. I can inform that the Wordpress function *wp_remote_get* was used for the HTTP request ' .
+ 'and the URL was: ' . $requestUrl;
+
+ return [false, $log, $createdTestFiles];
+ }
+ //$log[count($log) - 1] .= '. ok!';
+ //$log[] = '*' . $requestUrl . '*';
+
+ //$log = array_merge($log, SelfTestHelper::printHeaders($headers));
+
+ if (!isset($headers['content-type'])) {
+ $log[] = 'Bummer. There is no "content-type" response header. The test FAILED';
+ return [false, $log, $createdTestFiles];
+ }
+ if ($headers['content-type'] != 'image/webp') {
+
+ if ($headers['content-type'] == 'image/' . $imageType) {
+ $log[] = 'Bummer. As the "content-type" header reveals, we got the ' . $imageType . '. ';
+ } else {
+ $log[] = 'Bummer. As the "content-type" header reveals, we did not get a webp' .
+ 'Surprisingly we got: "' . $headers['content-type'] . '"';
+ }
+
+ if (isset($headers['content-length'])) {
+ if ($headers['content-length'] == '6964') {
+ $log[] = 'However, the content-length reveals that we actually GOT the webp ' .
+ '(we know that the file we put is exactly 6964 bytes). ' .
+ 'So it is "just" the content-type header that was not set correctly.';
+
+ if (PlatformInfo::isNginx()) {
+ $log[] = 'As you are on Nginx, you probably need to add the following line ' .
+ 'in your *mime.types* configuration file: ';
+ $log[] = '```image/webp webp;```';
+ } else {
+ $log[] = 'Perhaps you dont have *mod_mime* installed, or the following lines are not in a *.htaccess* ' .
+ 'in the folder containing the webp (or a parent):';
+ $log[] = "```' . print_r($options, true) . '
';
+ foreach ($options['converters'] as $converter) {
+ $converterId = $converter['converter'];
+ self::$warnings = [];
+ try {
+ $converterOptions = array_merge($options, $converter['options']);
+ unset($converterOptions['converters']);
+
+ //ConverterHelper::runConverter($converterId, $source, $destination, $converterOptions);
+ $converterInstance = ConverterFactory::makeConverter(
+ $converterId,
+ $source,
+ $destination,
+ $converterOptions
+ );
+ // Note: We now suppress warnings.
+ // WebPConvert logs warnings but purposefully does not stop them - warnings should generally not be
+ // stopped. However, as these warnings are logged in conversion log, it is preferable not to make them
+ // bubble here. #
+ $converterInstance->doConvert();
+
+ if (count(self::$warnings) > 0) {
+ $warnings[$converterId] = self::$warnings;
+ }
+ $workingConverters[] = $converterId;
+ } catch (\Exception $e) {
+ $errors[$converterId] = $e->getMessage();
+ } catch (\Throwable $e) {
+ $errors[$converterId] = $e->getMessage();
+ }
+ }
+
+ restore_error_handler();
+ //print_r($errors);
+
+ // cache the result
+ self::$converterStatus = [
+ 'workingConverters' => $workingConverters,
+ 'errors' => $errors,
+ 'warnings' => $warnings,
+ ];
+ return self::$converterStatus;
+ }
+
+
+ public static $localQualityDetectionWorking = null; // to cache the result
+
+ public static function isLocalQualityDetectionWorking() {
+ if (isset(self::$localQualityDetectionWorking)) {
+ return self::$localQualityDetectionWorking;
+ } else {
+ $q = JpegQualityDetector::detectQualityOfJpg(
+ Paths::getWebPExpressPluginDirAbs() . '/test/small-q61.jpg'
+ );
+ self::$localQualityDetectionWorking = ($q === 61);
+ return self::$localQualityDetectionWorking;
+ }
+ }
+}
diff --git a/lib/classes/Validate.php b/lib/classes/Validate.php
new file mode 100644
index 0000000..c28c78c
--- /dev/null
+++ b/lib/classes/Validate.php
@@ -0,0 +1,27 @@
+getMessage());
+ wp_die();
+ }
+ }
+/*
+{
+ "converters": [
+ {
+ "converter": "cwebp",
+ "options": {
+ "use-nice": true,
+ "try-common-system-paths": true,
+ "try-supplied-binary-for-os": true,
+ "method": 6,
+ "low-memory": true,
+ "command-line-options": ""
+ },
+ "working": true
+ },
+ {
+ "converter": "vips",
+ "options": {
+ "smart-subsample": false,
+ "preset": "none"
+ },
+ "working": false
+ },
+ {
+ "converter": "imagemagick",
+ "options": {
+ "use-nice": true
+ },
+ "working": true,
+ "deactivated": true
+ },
+ {
+ "converter": "graphicsmagick",
+ "options": {
+ "use-nice": true
+ },
+ "working": false
+ },
+ {
+ "converter": "ffmpeg",
+ "options": {
+ "use-nice": true,
+ "method": 4
+ },
+ "working": false
+ },
+ {
+ "converter": "wpc",
+ "working": false,
+ "options": {
+ "api-key": ""
+ }
+ },
+ {
+ "converter": "ewww",
+ "working": false
+ },
+ {
+ "converter": "imagick",
+ "working": false
+ },
+ {
+ "converter": "gmagick",
+ "working": false
+ },
+ {
+ "converter": "gd",
+ "options": {
+ "skip-pngs": false
+ },
+ "working": false
+ }
+ ]
+}*/
+ public static function processConversionSettings() {
+ require_once __DIR__ . "/../../vendor/autoload.php";
+ $availableConverters = Stack::getAvailableConverters();
+
+ /*
+ $converters = [];
+ //$supportsEncoding = [];
+ foreach ($availableConverters as $converter) {
+ $converters[] = [
+ 'id' => $converter,
+ 'name' => $converter
+ ];
+ /*if () {
+ $supportsEncoding[] = $converter;
+ }*/
+ //}
+
+ $webpConvertOptionDefinitions = WebPConvert::getConverterOptionDefinitions();
+
+ $config = Config::loadConfigAndFix();
+ $defaults = [
+ 'auto-limit' => (isset($config['quality-auto']) && $config['quality-auto']),
+ 'alpha-quality' => $config['alpha-quality'],
+ 'quality' => $config['max-quality'],
+ 'encoding' => $config['jpeg-encoding'],
+ 'near-lossless' => ($config['jpeg-enable-near-lossless'] ? $config['jpeg-near-lossless'] : 100),
+ 'metadata' => $config['metadata'],
+ 'stack-converters' => ConvertersHelper::getActiveConverterIds($config),
+
+ // 'method' (I could copy from cwebp...)
+ // 'sharp-yuv' (n/a)
+ // low-memory (n/a)
+ // auto-filter (n/a)
+ // preset (n/a)
+ // size-in-percentage (I could copy from cwebp...)
+ ];
+
+ $good = ConvertersHelper::getWorkingAndActiveConverterIds($config);
+ if (isset($good[0])) {
+ $defaults['converter'] = $good[0];
+ }
+ //'converter' => 'ewww',
+
+
+ // TODO:add PNG options
+ $pngDefaults = [
+ 'encoding' => $config['png-encoding'],
+ 'near-lossless' => ($config['png-enable-near-lossless'] ? $config['png-near-lossless'] : 100),
+ 'quality' => $config['png-quality'],
+ ];
+
+
+ // Filter active converters
+ foreach ($config['converters'] as $converter) {
+ /*if (isset($converter['deactivated']) && ($converter['deactivated'])) {
+ //continue;
+ }*/
+ if (isset($converter['options'])) {
+ foreach ($converter['options'] as $optionName => $optionValue) {
+ $defaults[$converter['converter'] . '-' . $optionName] = $optionValue;
+ }
+
+ }
+ }
+
+
+ $systemStatus = [
+ 'converterRequirements' => [
+ 'gd' => [
+ 'extensionLoaded' => extension_loaded('gd'),
+ 'compiledWithWebP' => function_exists('imagewebp'),
+ ]
+ // TODO: Add more!
+ ]
+ ];
+
+//getUnsupportedDefaultOptions
+ //supportedStandardOptions: {
+ $defaults['png'] = $pngDefaults;
+
+ return [
+ //'converters' => $converters,
+ 'defaults' => $defaults,
+ //'pngDefaults' => $pngDefaults,
+ 'options' => $webpConvertOptionDefinitions,
+ 'systemStatus' => $systemStatus
+ ];
+
+ /*
+ $config = Config::loadConfigAndFix();
+ // 'working', 'deactivated'
+ $foundFirstWorkingAndActive = false;
+ foreach ($config['converters'] as $converter) {
+ $converters[] = [
+ 'id' => $converter['converter'],
+ 'name' => $converter['converter']
+ ];
+ if ($converter['working']) {
+ if
+ }
+ if (!$foundFirstWorkingAndActive) {
+
+ }
+ }*/
+
+ return [
+ 'converters' => $converters
+ ];
+ }
+
+ /*
+ * Get mime
+ * @return string
+ */
+ private static function setMime($path, &$info) {
+ require_once __DIR__ . "/../../vendor/autoload.php";
+ $mimeResult = ImageMimeTypeGuesser::detect($path);
+ if (!$mimeResult) {
+ return;
+ }
+ $info['mime'] = $mimeResult;
+ if ($mimeResult == 'image/webp') {
+ $handle = @fopen($path, 'r');
+ if ($handle !== false) {
+ // 20 bytes is sufficient for all our sniffers, except image/svg+xml.
+ // The svg sniffer takes care of reading more
+ $sampleBin = @fread($handle, 20);
+ if ($sampleBin !== false) {
+ if (preg_match("/^RIFF.{4}WEBPVP8\ /", $sampleBin) === 1) {
+ $info['mime'] .= ' (lossy)';
+ } else if (preg_match("/^RIFF.{4}WEBPVP8L/", $sampleBin) === 1) {
+ $info['mime'] .= ' (lossless)';
+ }
+ }
+ }
+
+ }
+ }
+
+ public static function processInfo() {
+
+ Validate::postHasKey('args');
+
+ //$args = json_decode(sanitize_text_field(stripslashes($_POST['args'])), true);
+
+ //$args = $_POST['args'];
+ $args = self::getArgs();
+ if (!array_key_exists('path', $args)) {
+ throw new \Exception('"path" argument missing for command');
+ }
+
+ $path = SanityCheck::pathWithoutDirectoryTraversal($args['path']);
+ $path = ltrim($path, '/');
+ $pathTokens = explode('/', $path);
+
+ $rootId = array_shift($pathTokens); // Shift off the first item, which is the scope
+ $relPath = implode('/', $pathTokens);
+ $config = Config::loadConfigAndFix();
+ /*$rootIds = Paths::filterOutSubRoots($config['scope']);
+ if (!in_array($rootId, $rootIds)) {
+ throw new \Exception('Invalid scope (have you perhaps changed the scope setting after igniting the file manager?)');
+ }*/
+ $rootIds = $rootIds = Paths::getImageRootIds();
+
+ $absPath = Paths::getAbsDirById($rootId) . '/' . $relPath;
+ //absPathExistsAndIsFile
+ SanityCheck::absPathExists($absPath);
+
+ $result = [
+ 'original' => [
+ //'filename' => $absPath,
+ //'abspath' => $absPath,
+ 'size' => filesize($absPath),
+ // PS: I keep "&original" because some might have set up Nginx rules for ?original
+ 'url' => Paths::getUrlById($rootId) . '/' . $relPath . '?' . SelfTestHelper::randomDigitsAndLetters(8) . '&dontreplace&original',
+ ]
+ ];
+ self::setMime($absPath, $result['original']);
+
+ // TODO: NO!
+ // We must use ConvertHelper::getDestination for the abs path.
+ // And we must use logic from AlterHtmlHelper to get the URL
+ //error_log('path:' . $absPathDest);
+
+ $destinationOptions = DestinationOptions::createFromConfig($config);
+ if ($destinationOptions->useDocRoot) {
+ if (!(Paths::canUseDocRootForStructuringCacheDir())) {
+ $destinationOptions->useDocRoot = false;
+ }
+ }
+ $imageRoots = new ImageRoots(Paths::getImageRootsDef());
+ $destinationPath = Paths::getDestinationPathCorrespondingToSource($absPath, $destinationOptions);
+ list($rootId, $destRelPath) = Paths::getRootAndRelPathForDestination($destinationPath, $imageRoots);
+ if ($rootId != '') {
+ $absPathDest = Paths::getAbsDirById($rootId) . '/' . $destRelPath;
+ $destinationUrl = Paths::getUrlById($rootId) . '/' . $destRelPath;
+
+ SanityCheck::absPath($absPathDest);
+
+ if (@file_exists($absPathDest)) {
+ $result['converted'] = [
+ //'abspath' => $absPathDest,
+ 'size' => filesize($absPathDest),
+ 'url' => $destinationUrl . '?' . SelfTestHelper::randomDigitsAndLetters(8),
+ ];
+ self::setMime($absPathDest, $result['converted']);
+ }
+
+ // Get log, if exists. Ignore errors.
+ $log = '';
+ try {
+ $logFile = ConvertHelperIndependent::getLogFilename($absPath, Paths::getLogDirAbs());
+ if (@file_exists($logFile)) {
+ $logContent = file_get_contents($logFile);
+ if ($log !== false) {
+ $log = $logContent;
+ }
+ }
+ }
+ catch (\Exception $e) {
+ //throw $e;
+ }
+
+ $result['log'] = $log;
+ }
+
+
+ //$destinationUrl = DestinationUrl::
+
+ /*
+ error_log('dest:' . $destinationPath);
+ error_log('dest root:' . $rootId);
+ error_log('dest path:' . $destRelPath);
+ error_log('dest abs-dir:' . Paths::getAbsDirById($rootId) . '/' . $destRelPath);
+ error_log('dest url:' . Paths::getUrlById($rootId) . '/' . $destRelPath);
+ */
+
+ //error_log('url:' . $destinationPath);
+ //error_log('destinationOptions' . print_r($destinationOptions, true));
+
+ /*
+ $destination = Paths::destinationPathConvenience($rootId, $relPath, $config);
+ $absPathDest = $destination['abs-path'];
+ SanityCheck::absPath($absPathDest);
+ error_log('path:' . $absPathDest);
+
+ if (@file_exists($absPathDest)) {
+ $result['converted'] = [
+ 'abspath' => $destination['abs-path'],
+ 'size' => filesize($destination['abs-path']),
+ 'url' => $destination['url'],
+ 'log' => ''
+ ];
+ }
+ */
+ return $result;
+ }
+
+ /**
+ * Translate path received (ie "/uploads/2021/...") to absolute path.
+ *
+ * @param string $path
+ *
+ * @return array [$absPath, $relPath, $rootId]
+ * @throws \Exception if root id is invalid or path doesn't pass sanity check
+ */
+ private static function analyzePathReceived($path) {
+ try {
+ $path = SanityCheck::pathWithoutDirectoryTraversal($path);
+ $path = ltrim($path, '/');
+ $pathTokens = explode('/', $path);
+
+ $rootId = array_shift($pathTokens);
+ $relPath = implode('/', $pathTokens);
+
+ $rootIds = Paths::getImageRootIds();
+ if (!in_array($rootId, $rootIds)) {
+ throw new \Exception('Invalid rootId');
+ }
+ if ($relPath == '') {
+ $relPath = '.';
+ }
+
+ $absPath = PathHelper::canonicalize(Paths::getAbsDirById($rootId) . '/' . $relPath);
+ SanityCheck::absPathExists($absPath);
+
+ return [$absPath, $relPath, $rootId];
+ }
+ catch (\Exception $e) {
+ //throw new \Exception('Invalid path received (' . $e->getMessage() . ')');
+ throw new \Exception('Invalid path');
+ }
+ }
+
+ public static function processGetFolder() {
+
+ Validate::postHasKey('args');
+
+ //$args = json_decode(sanitize_text_field(stripslashes($_POST['args'])), true);
+
+ $args = self::getArgs();
+ if (!array_key_exists('path', $args)) {
+ throw new \Exception('"path" argument missing for command');
+ }
+
+ $path = SanityCheck::noStreamWrappers($args['path']);
+ //$pathTokens = explode('/', $path);
+ if ($path == '') {
+ $result = [
+ 'children' => [
+ [
+ 'name' => '/',
+ 'isDir' => true,
+ 'nickname' => 'scope'
+ ]
+ ]
+ ];
+ return $result;
+ }
+
+ $config = Config::loadConfigAndFix();
+ $rootIds = Paths::getImageRootIds();
+ if ($path == '/') {
+ $rootIds = Paths::filterOutSubRoots($config['scope']);
+ $result = ['children'=>[]];
+ foreach ($rootIds as $rootId) {
+ $result['children'][] = [
+ 'name' => $rootId,
+ 'isDir' => true,
+ ];
+ }
+ return $result;
+ }
+ list($absPath, $relPath, $rootId) = self::analyzePathReceived($path);
+
+ $listOptions = BulkConvert::defaultListOptions($config);
+ $listOptions['root'] = Paths::getAbsDirById($rootId);
+
+ $listOptions['filter']['only-unconverted'] = false;
+ $listOptions['flattenList'] = false;
+ $listOptions['max-depth'] = 0;
+
+ //throw new \Exception('Invalid rootId' . print_r($listOptions));
+
+ $list = BulkConvert::getListRecursively($relPath, $listOptions);
+
+ return ['children' => $list];
+ }
+
+ public static function processGetTree() {
+ $config = Config::loadConfigAndFix();
+ $rootIds = Paths::filterOutSubRoots($config['scope']);
+
+ $listOptions = [
+ //'root' => Paths::getUploadDirAbs(),
+ 'ext' => $config['destination-extension'],
+ 'destination-folder' => $config['destination-folder'], /* hm, "destination-folder" is a bad name... */
+ 'webExpressContentDirAbs' => Paths::getWebPExpressContentDirAbs(),
+ 'uploadDirAbs' => Paths::getUploadDirAbs(),
+ 'useDocRootForStructuringCacheDir' => (($config['destination-structure'] == 'doc-root') && (Paths::canUseDocRootForStructuringCacheDir())),
+ 'imageRoots' => new ImageRoots(Paths::getImageRootsDefForSelectedIds($config['scope'])), // (Paths::getImageRootsDef()
+ 'filter' => [
+ 'only-converted' => false,
+ 'only-unconverted' => false,
+ 'image-types' => $config['image-types'],
+ ],
+ 'flattenList' => false
+ ];
+
+ $children = [];
+ foreach ($rootIds as $rootId) {
+ $listOptions['root'] = Paths::getAbsDirById($rootId);
+ $grandChildren = BulkConvert::getListRecursively('.', $listOptions);
+ $children[] = [
+ 'name' => $rootId,
+ 'isDir' => true,
+ 'children' => $grandChildren
+ ];
+ }
+ return ['name' => '', 'isDir' => true, 'isOpen' => true, 'children' => $children];
+
+ }
+
+ private static function getArgs() {
+ //return $_POST['args'];
+
+ $args = $_POST['args'];
+// $args = '{\"path\":\"\"}';
+ //$args = '{"path":"hollo"}';
+
+ //error_log('get args:' . gettype($args));
+ //error_log(print_r($args, true));
+ //error_log(print_r(($_POST['args'] + ''), true));
+
+ //error_log('type:' . gettype($_POST['args']));
+ $args = json_decode('"' . $args . '"', true);
+ $args = json_decode($args, true);
+ //error_log('decoded:' . gettype($args));
+ //error_log(print_r($args, true));
+ //$args = json_decode($args, true);
+
+ return $args;
+ }
+
+ public static function processConvert() {
+
+ Validate::postHasKey('args');
+
+ //$args = json_decode(sanitize_text_field(stripslashes($_POST['args'])), true);
+
+ $args = self::getArgs();
+ if (!array_key_exists('path', $args)) {
+ throw new \Exception('"path" argument missing for command');
+ }
+
+ $path = SanityCheck::noStreamWrappers($args['path']);
+
+ $convertOptions = null;
+ if (isset($args['convertOptions'])) {
+ $convertOptions = $args['convertOptions'];
+ $convertOptions['log-call-arguments'] = true;
+ //unset($convertOptions['converter']);
+ //$convertOptions['png'] = ['quality' => 7];
+ //$convertOptions['png-quality'] = 8;
+ }
+
+ //error_log(print_r(json_encode($convertOptions, JSON_PRETTY_PRINT), true));
+
+ list($absPath, $relPath, $rootId) = self::analyzePathReceived($path);
+
+ $convertResult = Convert::convertFile($absPath, null, $convertOptions);
+
+ $result = [
+ 'success' => $convertResult['success'],
+ 'data' => $convertResult['msg'],
+ 'log' => $convertResult['log'],
+ 'args' => $args, // for debugging. TODO
+ ];
+ $info = [];
+ if (isset($convertResult['filesize-webp'])) {
+ $info['size'] = $convertResult['filesize-webp'];
+ }
+ if (isset($convertResult['destination-url'])) {
+ $info['url'] = $convertResult['destination-url'] . '?' . SelfTestHelper::randomDigitsAndLetters(8);
+ }
+ if (isset($convertResult['destination-path'])) {
+ self::setMime($convertResult['destination-path'], $info);
+ }
+
+ $result['converted'] = $info;
+ return $result;
+
+ /*if (!array_key_exists('convertOptions', $args)) {
+ throw new \Exception('"convertOptions" argument missing for command');
+ }
+ //return ['success' => true, 'optionsReceived' => $args['convertOptions']];
+ */
+
+
+ /*
+ $path = SanityCheck::pathWithoutDirectoryTraversal($args['path']);
+ $path = ltrim($path, '/');
+ $pathTokens = explode('/', $path);
+
+ $rootId = array_shift($pathTokens); // Shift off the first item, which is the scope
+ $relPath = implode('/', $pathTokens);
+ $config = Config::loadConfigAndFix();
+ $rootIds = Paths::filterOutSubRoots($config['scope']);
+ if (!in_array($rootId, $rootIds)) {
+ throw new \Exception('Invalid scope');
+ }
+
+ $absPath = Paths::getAbsDirById($rootId) . '/' . $relPath;
+ //absPathExistsAndIsFile
+ SanityCheck::absPathExists($absPath); */
+ }
+
+ public static function processDeleteConverted() {
+
+ Validate::postHasKey('args');
+
+ //$args = json_decode(sanitize_text_field(stripslashes($_POST['args'])), true);
+
+ //$args = $_POST['args'];
+ $args = self::getArgs();
+ if (!array_key_exists('path', $args)) {
+ throw new \Exception('"path" argument missing for command');
+ }
+
+ $path = SanityCheck::noStreamWrappers($args['path']);
+ list($absPath, $relPath, $rootId) = self::analyzePathReceived($path);
+
+ $config = Config::loadConfigAndFix();
+ $destinationOptions = DestinationOptions::createFromConfig($config);
+ if ($destinationOptions->useDocRoot) {
+ if (!(Paths::canUseDocRootForStructuringCacheDir())) {
+ $destinationOptions->useDocRoot = false;
+ }
+ }
+ $destinationPath = Paths::getDestinationPathCorrespondingToSource($absPath, $destinationOptions);
+
+ if (@!file_exists($destinationPath)) {
+ throw new \Exception('file not found: ' . $destinationPath);
+ }
+
+ if (@!unlink($destinationPath)) {
+ throw new \Exception('failed deleting file');
+ }
+
+ $result = [
+ 'success' => true,
+ 'data' => $destinationPath
+ ];
+ return $result;
+
+ }
+
+}
diff --git a/lib/classes/WCFMPage.php b/lib/classes/WCFMPage.php
new file mode 100644
index 0000000..6befa13
--- /dev/null
+++ b/lib/classes/WCFMPage.php
@@ -0,0 +1,54 @@
+' .
+ 'WebP Express Conversion Browser
' .
+ '';
+
+ echo '' .
+ print_r(
+ json_encode(
+ WebPConvert::getConverterOptionDefinitions('png', false, true),
+ JSON_PRETTY_PRINT
+ ),
+ true
+ ) . '';*/
+ }
+
+ /* We add directly to head instead, to get the type="module"
+ public static function enqueueScripts() {
+ $ver = '0';
+ wp_register_script('wcfileman', plugins_url('js/wcfm/index.js', WEBPEXPRESS_PLUGIN), [], $ver);
+ wp_enqueue_script('wcfileman');
+ }*/
+
+ public static function addToHead() {
+ $baseUrl = plugins_url('lib/wcfm', WEBPEXPRESS_PLUGIN);
+ //$url = plugins_url('js/conversion-manager/index.be5d792e.js ', WEBPEXPRESS_PLUGIN);
+
+ $wcfmNonce = wp_create_nonce('webpexpress-wcfm-nonce');
+ echo '' . print_r($response, true) . '
';
+
+ if (is_wp_error($response)) {
+ return new HttpResponse($response->get_error_message(), '0', []);
+ } else {
+ $body = wp_remote_retrieve_body($response);
+ $statusCode = wp_remote_retrieve_response_code($response);
+ $headersDict = wp_remote_retrieve_headers($response);
+ if (method_exists($headersDict, 'getAll')) {
+ $headersMap = $headersDict->getAll();
+ } else {
+ $headersMap = [];
+ }
+ return new HttpResponse($body, $statusCode, $headersMap);
+ }
+ }
+}
diff --git a/lib/classes/WebPOnDemand.php b/lib/classes/WebPOnDemand.php
new file mode 100644
index 0000000..a6e46ea
--- /dev/null
+++ b/lib/classes/WebPOnDemand.php
@@ -0,0 +1,285 @@
+byId($dirIdOfHtaccess)->getAbsPath() . '/' . $sourceRelHtaccess;
+ return $source;
+ }
+
+ private static function getSource() {
+ if (self::$usingDocRoot) {
+ $source = self::getSourceDocRoot();
+ } else {
+ $source = self::getSourceNoDocRoot();
+ }
+ return $source;
+ }
+
+ private static function processRequestNoTryCatch() {
+
+ self::loadConfig();
+
+ $options = self::$options;
+ $wodOptions = self::$wodOptions;
+ $serveOptions = $options['webp-convert'];
+ $convertOptions = &$serveOptions['convert'];
+ //echo '' . print_r($wodOptions, true) . '
'; exit;
+
+
+ // Validate that WebPExpress was configured to redirect to this conversion script
+ // (but do not require that for Nginx)
+ // ------------------------------------------------------------------------------
+ self::$checking = 'settings';
+ if (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') === false) {
+ if (!isset($wodOptions['enable-redirection-to-converter']) || ($wodOptions['enable-redirection-to-converter'] === false)) {
+ throw new ValidateException('Redirection to conversion script is not enabled');
+ }
+ }
+
+ // Check source (the image to be converted)
+ // --------------------------------------------
+ self::$checking = 'source';
+
+ // Decode URL in case file contains encoded symbols (#413)
+ $source = urldecode(self::getSource());
+
+ //self::exitWithError($source);
+
+ $imageRoots = self::getImageRootsDef();
+
+ // Get upload dir
+ $uploadDirAbs = $imageRoots->byId('uploads')->getAbsPath();
+
+ // Check destination path
+ // --------------------------------------------
+ self::$checking = 'destination path';
+ $destination = ConvertHelperIndependent::getDestination(
+ $source,
+ $wodOptions['destination-folder'],
+ $wodOptions['destination-extension'],
+ self::$webExpressContentDirAbs,
+ $uploadDirAbs,
+ self::$usingDocRoot,
+ self::getImageRootsDef()
+ );
+
+ //$destination = SanityCheck::absPathIsInDocRoot($destination);
+ $destination = SanityCheck::pregMatch('#\.webp$#', $destination, 'Does not end with .webp');
+
+ //self::exitWithError($destination);
+
+ // Done with sanitizing, lets get to work!
+ // ---------------------------------------
+ self::$checking = 'done';
+
+ if (isset($wodOptions['success-response']) && ($wodOptions['success-response'] == 'original')) {
+ $serveOptions['serve-original'] = true;
+ $serveOptions['serve-image']['headers']['vary-accept'] = false;
+ } else {
+ $serveOptions['serve-image']['headers']['vary-accept'] = true;
+ }
+//echo $source . '
' . $destination; exit;
+
+ /*
+ // No caching!
+ // - perhaps this will solve it for WP engine.
+ // but no... Perhaps a 302 redirect to self then? (if redirect to existing is activated).
+ // TODO: try!
+ //$serveOptions['serve-image']['headers']['vary-accept'] = false;
+
+ */
+/*
+ include_once __DIR__ . '/../../vendor/autoload.php';
+ $convertLogger = new \WebPConvert\Loggers\BufferLogger();
+ \WebPConvert\WebPConvert::convert($source, $destination, $serveOptions['convert'], $convertLogger);
+ header('Location: ?fresh' , 302);
+*/
+
+ if (isset($_SERVER['WPENGINE_ACCOUNT'])) {
+ // Redirect to self rather than serve directly for WP Engine.
+ // This overcomes that Vary:Accept header set from PHP is lost on WP Engine.
+ // To prevent endless loop in case "redirect to existing webp" isn't set up correctly,
+ // only activate when destination is missing.
+ // (actually it does not prevent anything on wpengine as the first request is cached!
+ // -even though we try to prevent it:)
+ // Well well. Those users better set up "redirect to existing webp" as well!
+ $serveOptions['serve-image']['headers']['cache-control'] = true;
+ $serveOptions['serve-image']['headers']['expires'] = false;
+ $serveOptions['serve-image']['cache-control-header'] = 'no-store, no-cache, must-revalidate, max-age=0';
+ //header("Pragma: no-cache", true);
+
+ if (!@file_exists($destination)) {
+ $serveOptions['redirect-to-self-instead-of-serving'] = true;
+ }
+ }
+
+ $loggingEnabled = (isset($wodOptions['enable-logging']) ? $wodOptions['enable-logging'] : true);
+ $logDir = ($loggingEnabled ? self::$webExpressContentDirAbs . '/log' : null);
+
+ ConvertHelperIndependent::serveConverted(
+ $source,
+ $destination,
+ $serveOptions,
+ $logDir,
+ 'Conversion triggered with the conversion script (wod/webp-on-demand.php)'
+ );
+
+ BiggerThanSourceDummyFiles::updateStatus(
+ $source,
+ $destination,
+ self::$webExpressContentDirAbs,
+ self::getImageRootsDef(),
+ $wodOptions['destination-folder'],
+ $wodOptions['destination-extension']
+ );
+
+ self::fixConfigIfEwwwDiscoveredNonFunctionalApiKeys();
+ }
+
+ public static function processRequest() {
+ try {
+ self::processRequestNoTryCatch();
+ } catch (SanityException $e) {
+ self::exitWithError('Sanity check failed for ' . self::$checking . ': '. $e->getMessage());
+ } catch (ValidateException $e) {
+ self::exitWithError('Validation failed for ' . self::$checking . ': '. $e->getMessage());
+ } catch (\Exception $e) {
+ if (self::$checking == 'done') {
+ self::exitWithError('Error occured during conversion/serving:' . $e->getMessage());
+ } else {
+ self::exitWithError('Error occured while calculating ' . self::$checking . ': '. $e->getMessage());
+ }
+ }
+ }
+}
diff --git a/lib/classes/WebPRealizer.php b/lib/classes/WebPRealizer.php
new file mode 100644
index 0000000..4eb8697
--- /dev/null
+++ b/lib/classes/WebPRealizer.php
@@ -0,0 +1,276 @@
+byId('uploads')->getAbsPath() . '/' . $destinationRelHtaccess;
+ } elseif ($dirIdOfHtaccess == 'cache') {
+ return $imageRoots->byId('wp-content')->getAbsPath() . '/webp-express/webp-images/' . $destinationRelHtaccess;
+ }
+ /*
+ $pathTokens = explode('/', $destinationRelCacheRoot);
+ $imageRootId = array_shift($pathTokens);
+ $destinationRelSpecificCacheRoot = implode('/', $pathTokens);
+
+ $imageRootId = SanityCheck::pregMatch(
+ '#^[a-z\-]+$#',
+ $imageRootId,
+ 'The image root ID is not a valid root id'
+ );
+
+ // TODO: Validate that the root id is in scope
+
+ if (count($pathTokens) == 0) {
+ throw new \Exception('invalid destination argument. It must contain dashes.');
+ }
+
+ return $imageRoots->byId($imageRootId)->getAbsPath() . '/' . $destinationRelSpecificCacheRoot;
+
+/*
+ if ($imageRootId !== false) {
+
+ //$imageRootId = self::getEnvPassedInRewriteRule('WE_IMAGE_ROOT_ID');
+ if ($imageRootId !== false) {
+ $imageRootId = SanityCheck::pregMatch('#^[a-z\-]+$#', $imageRootId, 'The image root ID passed in ENV is not a valid root-id');
+
+ $destinationRelImageRoot = self::getEnvPassedInRewriteRule('WE_DESTINATION_REL_IMAGE_ROOT');
+ if ($destinationRelImageRoot !== false) {
+ $destinationRelImageRoot = SanityCheck::pathWithoutDirectoryTraversal($destinationRelImageRoot);
+ }
+ $imageRoots = self::getImageRootsDef();
+ return $imageRoots->byId($imageRootId)->getAbsPath() . '/' . $destinationRelImageRoot;
+ }
+
+ if (isset($_GET['xdestination-rel-image-root'])) {
+ $xdestinationRelImageRoot = SanityCheck::noControlChars($_GET['xdestination-rel-image-root']);
+ $destinationRelImageRoot = SanityCheck::pathWithoutDirectoryTraversal(substr($xdestinationRelImageRoot, 1));
+
+ $imageRootId = SanityCheck::noControlChars($_GET['image-root-id']);
+ SanityCheck::pregMatch('#^[a-z\-]+$#', $imageRootId, 'Not a valid root-id');
+
+ $imageRoots = self::getImageRootsDef();
+ return $imageRoots->byId($imageRootId)->getAbsPath() . '/' . $destinationRelImageRoot;
+ }
+
+ throw new \Exception('Argument for destination file missing');
+ //WE_DESTINATION_REL_IMG_ROOT*/
+
+ /*
+ $destAbs = SanityCheck::noControlChars(self::getEnvPassedInRewriteRule('WEDESTINATIONABS'));
+ if ($destAbs !== false) {
+ return SanityCheck::pathWithoutDirectoryTraversal($destAbs);
+ }
+
+ // Check querystring (relative path)
+ if (isset($_GET['xdest-rel-to-root-id'])) {
+ $xdestRelToRootId = SanityCheck::noControlChars($_GET['xdest-rel-to-root-id']);
+ $destRelToRootId = SanityCheck::pathWithoutDirectoryTraversal(substr($xdestRelToRootId, 1));
+
+ $rootId = SanityCheck::noControlChars($_GET['root-id']);
+ SanityCheck::pregMatch('#^[a-z]+$#', $rootId, 'Not a valid root-id');
+ return self::getRootPathById($rootId) . '/' . $destRelToRootId;
+ }
+ */
+
+ }
+
+ private static function getDestination() {
+ self::$checking = 'destination path';
+ if (self::$usingDocRoot) {
+ $destination = self::getDestinationDocRoot();
+ } else {
+ $destination = self::getDestinationNoDocRoot();
+ }
+ SanityCheck::pregMatch('#\.webp$#', $destination, 'Does not end with .webp');
+
+ return $destination;
+ }
+
+ private static function processRequestNoTryCatch() {
+
+ self::loadConfig();
+
+ $options = self::$options;
+ $wodOptions = self::$wodOptions;
+ $serveOptions = $options['webp-convert'];
+ $convertOptions = &$serveOptions['convert'];
+ //echo '' . print_r($wodOptions, true) . '
'; exit;
+
+
+ // Validate that WebPExpress was configured to redirect to this conversion script
+ // (but do not require that for Nginx)
+ // ------------------------------------------------------------------------------
+ self::$checking = 'settings';
+ if (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') === false) {
+ if (!isset($wodOptions['enable-redirection-to-webp-realizer']) || ($wodOptions['enable-redirection-to-webp-realizer'] === false)) {
+ throw new ValidateException('Redirection to webp realizer is not enabled');
+ }
+ }
+
+ // Get destination
+ // --------------------------------------------
+ self::$checking = 'destination';
+ // Decode URL in case file contains encoded symbols (#413)
+ $destination = urldecode(self::getDestination());
+
+ //self::exitWithError($destination);
+
+ // Validate source path
+ // --------------------------------------------
+ $checking = 'source path';
+ $source = ConvertHelperIndependent::findSource(
+ $destination,
+ $wodOptions['destination-folder'],
+ $wodOptions['destination-extension'],
+ self::$usingDocRoot ? 'doc-root' : 'image-roots',
+ self::$webExpressContentDirAbs,
+ self::getImageRootsDef()
+ );
+ //self::exitWithError('source:' . $source);
+ //echo 'destination:
' . $destination . 'source:
' . $source; exit;
+
+ if ($source === false) {
+ header('X-WebP-Express-Error: webp-realizer.php could not find an existing jpg/png that corresponds to the webp requested', true);
+
+ $protocol = isset($_SERVER["SERVER_PROTOCOL"]) ? $_SERVER["SERVER_PROTOCOL"] : 'HTTP/1.0';
+ header($protocol . " 404 Not Found");
+ die();
+ //echo 'destination requested:
' . $destination . '';
+ }
+ //$source = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
+
+ // Done with sanitizing, lets get to work!
+ // ---------------------------------------
+ $serveOptions['add-vary-header'] = false;
+ $serveOptions['fail'] = '404';
+ $serveOptions['fail-when-fail-fails'] = '404';
+ $serveOptions['serve-image']['headers']['vary-accept'] = false;
+
+ $loggingEnabled = (isset($wodOptions['enable-logging']) ? $wodOptions['enable-logging'] : true);
+ $logDir = ($loggingEnabled ? self::$webExpressContentDirAbs . '/log' : null);
+
+ ConvertHelperIndependent::serveConverted(
+ $source,
+ $destination,
+ $serveOptions,
+ $logDir,
+ 'Conversion triggered with the conversion script (wod/webp-realizer.php)'
+ );
+
+ BiggerThanSourceDummyFiles::updateStatus(
+ $source,
+ $destination,
+ self::$webExpressContentDirAbs,
+ self::getImageRootsDef(),
+ $wodOptions['destination-folder'],
+ $wodOptions['destination-extension']
+ );
+
+ self::fixConfigIfEwwwDiscoveredNonFunctionalApiKeys();
+ }
+
+ public static function processRequest() {
+ try {
+ self::processRequestNoTryCatch();
+ } catch (SanityException $e) {
+ self::exitWithError('Sanity check failed for ' . self::$checking . ': '. $e->getMessage());
+ } catch (ValidateException $e) {
+ self::exitWithError('Validation failed for ' . self::$checking . ': '. $e->getMessage());
+ } catch (\Exception $e) {
+ self::exitWithError('Error occured while calculating ' . self::$checking . ': '. $e->getMessage());
+ }
+ }
+}
diff --git a/lib/classes/WodConfigLoader.php b/lib/classes/WodConfigLoader.php
new file mode 100644
index 0000000..263ddb0
--- /dev/null
+++ b/lib/classes/WodConfigLoader.php
@@ -0,0 +1,252 @@
+ $item) {
+ if (substr($key, -$len) == $envName) {
+ return $item;
+ }
+ }
+ return false;
+ }
+
+ protected static function getWebPExpressContentDirWithDocRoot()
+ {
+ // Get relative path to wp-content
+ // --------------------------------
+ self::$checking = 'Relative path to wp-content dir';
+
+ // Passed in env variable?
+ $wpContentDirRel = self::getEnvPassedInRewriteRule('WPCONTENT');
+ if ($wpContentDirRel === false) {
+ // Passed in QS?
+ if (isset($_GET['wp-content'])) {
+ $wpContentDirRel = SanityCheck::pathWithoutDirectoryTraversal($_GET['wp-content']);
+ } else {
+ // In case above fails, fall back to standard location
+ $wpContentDirRel = 'wp-content';
+ }
+ }
+
+ // Check WebP Express content dir
+ // ---------------------------------
+ self::$checking = 'WebP Express content dir';
+
+ $webExpressContentDirAbs = SanityCheck::absPathExistsAndIsDir(self::$docRoot . '/' . $wpContentDirRel . '/webp-express');
+ return $webExpressContentDirAbs;
+ }
+
+ protected static function getWebPExpressContentDirNoDocRoot() {
+ // Check wp-content
+ // ----------------------
+ self::$checking = 'relative path between webp-express plugin dir and wp-content dir';
+
+ // From v0.22.0, we pass relative to webp-express dir rather than to the general plugin dir.
+ // - this allows symlinking the webp-express dir.
+ $wpContentDirRelToWEPluginDir = self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_WE_PLUGIN_DIR');
+ if (!$wpContentDirRelToWEPluginDir) {
+ // Passed in QS?
+ if (isset($_GET['xwp-content-rel-to-we-plugin-dir'])) {
+ $xwpContentDirRelToWEPluginDir = SanityCheck::noControlChars($_GET['xwp-content-rel-to-we-plugin-dir']);
+ $wpContentDirRelToWEPluginDir = SanityCheck::pathDirectoryTraversalAllowed(substr($xwpContentDirRelToWEPluginDir, 1));
+ }
+ }
+
+ // Old .htaccess rules from before 0.22.0 passed relative path to general plugin dir.
+ // these rules must still be supported, which is what we do here:
+ if (!$wpContentDirRelToWEPluginDir) {
+ self::$checking = 'relative path between plugin dir and wp-content dir';
+
+ $wpContentDirRelToPluginDir = self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_PLUGIN_DIR');
+ if ($wpContentDirRelToPluginDir === false) {
+ // Passed in QS?
+ if (isset($_GET['xwp-content-rel-to-plugin-dir'])) {
+ $xwpContentDirRelToPluginDir = SanityCheck::noControlChars($_GET['xwp-content-rel-to-plugin-dir']);
+ $wpContentDirRelToPluginDir = SanityCheck::pathDirectoryTraversalAllowed(substr($xwpContentDirRelToPluginDir, 1));
+
+ } else {
+ throw new \Exception('Path to wp-content was not received in any way');
+ }
+ }
+ $wpContentDirRelToWEPluginDir = $wpContentDirRelToPluginDir . '..';
+ }
+
+
+ // Check WebP Express content dir
+ // ---------------------------------
+ self::$checking = 'WebP Express content dir';
+
+ $pathToWEPluginDir = dirname(dirname(__DIR__));
+ $webExpressContentDirAbs = SanityCheck::pathDirectoryTraversalAllowed($pathToWEPluginDir . '/' . $wpContentDirRelToWEPluginDir . '/webp-express');
+
+ //$pathToPluginDir = dirname(dirname(dirname(__DIR__)));
+ //$webExpressContentDirAbs = SanityCheck::pathDirectoryTraversalAllowed($pathToPluginDir . '/' . $wpContentDirRelToPluginDir . '/webp-express');
+ //echo $webExpressContentDirAbs; exit;
+ if (@!file_exists($webExpressContentDirAbs)) {
+ throw new \Exception('Dir not found');
+ }
+ $webExpressContentDirAbs = @realpath($webExpressContentDirAbs);
+ if ($webExpressContentDirAbs === false) {
+ throw new \Exception('WebP Express content dir is outside restricted open_basedir!');
+ }
+ return $webExpressContentDirAbs;
+ }
+
+ protected static function getImageRootsDef()
+ {
+ if (!isset(self::$wodOptions['image-roots'])) {
+ throw new \Exception('No image roots defined in config.');
+ }
+ return new ImageRoots(self::$wodOptions['image-roots']);
+ }
+
+ protected static function loadConfig() {
+
+ $usingDocRoot = !(
+ isset($_GET['xwp-content-rel-to-we-plugin-dir']) ||
+ self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_WE_PLUGIN_DIR') ||
+ isset($_GET['xwp-content-rel-to-plugin-dir']) ||
+ self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_PLUGIN_DIR')
+ );
+ self::$usingDocRoot = $usingDocRoot;
+
+ if ($usingDocRoot) {
+ // Check DOCUMENT_ROOT
+ // ----------------------
+ self::$checking = 'DOCUMENT_ROOT';
+ $docRootAvailable = PathHelper::isDocRootAvailableAndResolvable();
+ if (!$docRootAvailable) {
+ throw new \Exception(
+ 'Document root is no longer available. It was available when the .htaccess rules was created and ' .
+ 'the rules are based on that. You need to regenerate the rules (or fix your document root configuration)'
+ );
+ }
+
+ $docRoot = SanityCheck::absPath($_SERVER["DOCUMENT_ROOT"]);
+ $docRoot = rtrim($docRoot, '/');
+ self::$docRoot = $docRoot;
+ }
+
+ if ($usingDocRoot) {
+ self::$webExpressContentDirAbs = self::getWebPExpressContentDirWithDocRoot();
+ } else {
+ self::$webExpressContentDirAbs = self::getWebPExpressContentDirNoDocRoot();
+ }
+
+ // Check config file name
+ // ---------------------------------
+ self::$checking = 'config file';
+
+ $configFilename = self::$webExpressContentDirAbs . '/config/wod-options.json';
+ if (!file_exists($configFilename)) {
+ throw new \Exception('Configuration file was not found (wod-options.json)');
+ }
+
+ // Check config file
+ // --------------------
+ $configLoadResult = file_get_contents($configFilename);
+ if ($configLoadResult === false) {
+ throw new \Exception('Cannot open config file');
+ }
+ $json = SanityCheck::isJSONObject($configLoadResult);
+
+ self::$options = json_decode($json, true);
+ self::$wodOptions = self::$options['wod'];
+ }
+
+ /**
+ * Must be called after conversion.
+ */
+ protected static function fixConfigIfEwwwDiscoveredNonFunctionalApiKeys()
+ {
+ if (isset(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion)) {
+ // We got an invalid or exceeded api key (at least one).
+ //error_log('look:' . print_r(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, true));
+ EwwwTools::markApiKeysAsNonFunctional(
+ Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion,
+ self::$webExpressContentDirAbs . '/config'
+ );
+ }
+
+ }
+}
diff --git a/lib/debug.php b/lib/debug.php
new file mode 100644
index 0000000..667af92
--- /dev/null
+++ b/lib/debug.php
@@ -0,0 +1,18 @@
+
WebP Express 0.19.0 introduced a new conversion method: ffmpeg, which works on your system. ' .
+ 'You now have a conversion method that works! To start using it, you must go to settings and click save.',
+ $msgId,
+ [
+ ['text' => 'Take me to the settings', 'redirect-to-settings' => true],
+ ['text' => 'Dismiss'],
+ ]
+);
diff --git a/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-ewww.php b/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-ewww.php
new file mode 100644
index 0000000..6cdcc73
--- /dev/null
+++ b/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-ewww.php
@@ -0,0 +1,17 @@
+ 'Take me to the settings', 'redirect-to-settings' => true],
+ ['text' => 'Dismiss'],
+ ]
+);
diff --git a/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-gd.php b/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-gd.php
new file mode 100644
index 0000000..0b75ace
--- /dev/null
+++ b/lib/dismissable-global-messages/0.19.0/meet-ffmpeg-better-than-gd.php
@@ -0,0 +1,16 @@
+ 'Take me to the settings', 'redirect-to-settings' => true],
+ ['text' => 'Dismiss'],
+ ]
+);
diff --git a/lib/dismissable-messages/0.14.0/say-hello-to-vips.php b/lib/dismissable-messages/0.14.0/say-hello-to-vips.php
new file mode 100644
index 0000000..daaeb7c
--- /dev/null
+++ b/lib/dismissable-messages/0.14.0/say-hello-to-vips.php
@@ -0,0 +1,39 @@
+I have some good news and... more good news! WebP Express now supports Vips and Vips is working on your server. ' .
+ 'Vips is one of the best method for converting WebPs, on par with cwebp, which you also have working. ' .
+ 'You may want to use Vips instead of cwebp. Your choice.
I have some good news and... more good news! WebP Express now supports Vips and Vips is working on your server. ' . + 'Vips is one of the best method for converting WebPs and has therefore been inserted at the top of the list.' . + '
', + '0.14.0/say-hello-to-vips', + 'Got it!' + ); + } +} else { + // show message? +} diff --git a/lib/dismissable-messages/0.14.0/suggest-enable-pngs.php b/lib/dismissable-messages/0.14.0/suggest-enable-pngs.php new file mode 100644 index 0000000..b4f64a9 --- /dev/null +++ b/lib/dismissable-messages/0.14.0/suggest-enable-pngs.php @@ -0,0 +1,10 @@ +WebP Express 0.14 has new options for the conversions. Especially, it can now produce lossless webps, and ' . + 'it can automatically try both lossy and lossless and select the smallest. You can play around with the ' . + 'new options when your click "test" next to a converter.' . + 'Once satisfied, dont forget to ' . + 'wipe your existing converted files (there is a "Delete converted files" button for that here on this page).
', + '0.14.0/suggest-wipe-because-lossless', + 'Got it!' + ); +} else { + if ($firstActiveAndWorkingConverterId == 'gd') { + foreach ($workingConvertersIds as $workingId) { + if (in_array($workingId, $convertersSupportingEncodingAuto)) { + DismissableMessages::printDismissableMessage( + 'info', + 'WebP Express 0.14 has new options for the conversions. Especially, it can now produce lossless webps, and ' . + 'it can automatically try both lossy and lossless and select the smallest. You can play around with the ' . + 'new options when your click "test" next to a converter.
' . + 'Once satisfied, dont forget to wipe your existing converted files (there is a "Delete converted files" ' . + 'button for that here on this page)
' . + 'Btw: The "gd" conversion method that you are using does not support lossless encoding ' . + '(in fact Gd only supports very few conversion options), but fortunately, you have at least one ' . + 'other conversion method working, so you can simply start using that instead.
', + '0.14.0/suggest-wipe-because-lossless', + 'Got it!' + ); + break; + } + } + } +} diff --git a/lib/dismissable-messages/0.15.0/new-scope-setting-content.php b/lib/dismissable-messages/0.15.0/new-scope-setting-content.php new file mode 100644 index 0000000..4a92fca --- /dev/null +++ b/lib/dismissable-messages/0.15.0/new-scope-setting-content.php @@ -0,0 +1,12 @@ +You are running on NGINX. WebP Express works well on NGINX, however this UI is not streamlined NGINX yet. ' . + 'You should head over to the ' . + 'NGINX section in the FAQ' . + ' to learn how to use WebP Express on NGINX
';*/ + + +DismissableMessages::printDismissableMessage( + 'warning', + 'You are running on NGINX. WebP Express works well on NGINX, however this UI is not streamlined NGINX yet.
' . + 'You should head over to the ' . + 'NGINX section in the FAQ' . + ' to learn how to use WebP Express on NGINX
', + '0.16.0/nginx-link-to-faq', + 'Got it!' +); diff --git a/lib/dismissable-messages/0.23.0/elementor.php b/lib/dismissable-messages/0.23.0/elementor.php new file mode 100644 index 0000000..a158c1d --- /dev/null +++ b/lib/dismissable-messages/0.23.0/elementor.php @@ -0,0 +1,34 @@ +experiments->is_feature_active( 'e_optimized_css_loading' ) === false) { + $showMessage = true; + } + } catch (\Exception $e) { + // Well, just bad luck. + } +} + +if ($showMessage) { + DismissableMessages::printDismissableMessage( + 'info', + '' . + 'You see this message because you using Elementor, you rely solely on Alter HTML for webp, and Elementor is currently set up to use external css. ' . + 'You might want to reconfigure Elementor so it inlines the CSS. This will allow Alter HTML to replace the image urls of backgrounds. ' . + 'To reconfigure, go to Elementor > Settings > Experiments and activate "Improved CSS Loading". ' . + 'Note: This requires that Alter HTML is configured to "Replace image URLs". ' . + 'For more information, ' . + 'head over here' . + '
', + '0.23.0/elementor', + 'Got it!' + ); +} else { + DismissableMessages::dismissMessage('0.23.0/elementor'); +} diff --git a/lib/migrate/migrate.php b/lib/migrate/migrate.php new file mode 100644 index 0000000..2e83634 --- /dev/null +++ b/lib/migrate/migrate.php @@ -0,0 +1,44 @@ +' . + 'Please create the folder manually, or change the file permissions of your wp-content folder.' + ); + return false; + } else { + if (!Paths::createConfigDirIfMissing()) { + Messenger::printMessage( + 'error', + 'For migration to 0.5.0, WebP Express needs to create a directory "webp-express/config" under your wp-content folder, but does not have permission to do so.' . print_r($options, true) . ''); +// $htaccessExists = Config::doesHTAccessExists(); + + $config = $options; + + //$htaccessExists = Config::doesHTAccessExists(); + //$rules = HTAccess::generateHTAccessRulesFromConfigObj($config); + + if (Config::saveConfigurationFile($config)) { + $options = Config::generateWodOptionsFromConfigObj($config); + if (Config::saveWodOptionsFile($options)) { + + Messenger::addMessage( + 'success', + 'WebP Express has successfully migrated its configuration to 0.5.0' + ); + + //Config::saveConfigurationAndHTAccessFilesWithMessages($config, 'migrate'); + //$rulesResult = HTAccess::saveRules($config); // Commented out because rules are going to be saved in migrate12 + /* + 'mainResult' // 'index', 'wp-content' or 'failed' + 'minRequired' // 'index' or 'wp-content' + 'pluginToo' // 'yes', 'no' or 'depends' + 'pluginFailed' // true if failed to write to plugin folder (it only tries that, if pluginToo == 'yes') + 'pluginFailedBadly' // true if plugin failed AND it seems we have rewrite rules there + 'overidingRulesInWpContentWarning' // true if main result is 'index' but we cannot remove those in wp-content + 'rules' // the rules that were generated + */ + /* + $mainResult = $rulesResult['mainResult']; + $rules = $rulesResult['rules']; + + if ($mainResult != 'failed') { + Messenger::addMessage( + 'success', + 'WebP Express has successfully migrated its configuration and updated the rewrite rules to 0.5.0' + ); + } else { + Messenger::addMessage( + 'warning', + 'WebP Express has successfully migrated its configuration.' . + 'However, WebP Express could not update the rewrite rules
| Site | ' + //s+='Salt | ' + //s+='Limit | ' + s+=''; + s+=window.authorizedSites[i]['id'] + s+=' | '; + } + } else { + s+='No sites have been authorized to use this server yet.'; + } + s+='
|---|
Could not fetch list of files to convert. The server returned nothing (which is unexpected - ' + + 'it is not simply because there are no files to convert.)
'; + document.getElementById('bulkconvertcontent').innerHTML = html; + return; + } + var responseObj; + try { + responseObj = JSON.parse(response); + } catch (e) { + html = 'The ajax call did not return valid JSON, as expected.
'; + html += 'Check the javascript console to see what was returned.
'; + console.log('The ajax call did not return valid JSON, as expected'); + console.log('Here is what was received:'); + console.log(response); + document.getElementById('bulkconvertcontent').innerHTML = html; + return; + } + var bulkInfo = { + 'groups': responseObj, + 'groupPointer': 0, + 'filePointer': 0, + 'paused': false, + 'webpTotalFilesize': 0, + 'orgTotalFilesize': 0, + }; + window.webpexpress_bulkconvert = bulkInfo; + + // count files + var numFiles = 0; + for (var i=0; iThere are ' + numFiles + ' unconverted files.
'; + html += 'Note that in a typical setup, you will have redirect rules which trigger conversion when needed, ' + + 'and thus you have no need for bulk conversion. In fact, in that case, you should probably not bulk convert ' + + 'because bulk conversion will also convert images and thumbnails which are not in use, and thus take up ' + + 'more disk space than necessary. The bulk conversion feature was only added in order to make the plugin usable even when ' + + 'there are problems with redirects (ie on Nginx in case you do not have access to the config or on Microsoft IIS). ' + + '
Done!
' + + 'Total reduction: ' + getReductionHtml(bulkInfo['orgTotalFilesize'], bulkInfo['webpTotalFilesize'], 'Total size of converted originals', 'Total size of converted webp files') + '
' + + document.getElementById('bulkPauseResumeBtn').style.display = 'none'; +} + +function getPrintableSizeInfo(orgSize, webpSize) { + if (orgSize < 10000) { + return { + 'org': orgSize + ' bytes', + 'webp': webpSize + ' bytes' + }; + } else { + return { + 'org': Math.round(orgSize / 1024) + ' kb', + 'webp': Math.round(webpSize / 1024) + ' kb' + }; + } +} + +function getReductionHtml(orgSize, webpSize, sizeOfOriginalText, sizeOfWebpText) { + var reduction = Math.round((orgSize - webpSize)/orgSize * 100); + var sizeInfo = getPrintableSizeInfo(orgSize, webpSize); + var hoverText = sizeOfOriginalText + ': ' + sizeInfo['org'] + '.' + result + ''; + + document.getElementById('conversionlog_content').innerHTML = html; + }, + error: () => { + //responseCallback({requestError: true}); + }, + }); + + //
Nothing to convert
' + ); + + bulkInfo.groupPointer++; + bulkInfo.filePointer = 0; + } + + if (bulkInfo.groupPointer >= bulkInfo.groups.length) { + convertDone(); + return; + } + + var group = bulkInfo.groups[bulkInfo.groupPointer]; + var filename = group.files[bulkInfo.filePointer]; + + if (bulkInfo.filePointer == 0) { + logLn('on pause
' +
+ 'Reduction this far: ' + getReductionHtml(bulkInfo['orgTotalFilesize'], bulkInfo['webpTotalFilesize'], 'Total size of originals this far', 'Total size of webp files this far') + '
Warnings were issued:
'; + for (var i = 0; icheck conversion log for more insight (ie by clicking the "test" link a little left of this warning triangle)
'; + } + + html += 'To delete all converted files, click this button:
';
+ html += '';
+ html += '
Or perhaps, you only want to delete the converted PNGs? Then this button is for you:
';
+ html += '';
+ html += '
Are you sure you want to do that?
'; + html += '' + + 'The log files contain information about the conversion settings used for each webp and the libraries ' + + 'used, and the versions. This information will be visible in the conversion manager in a not too far future. ' + + 'The information might also be used to notify you if the libraries / version of a library you have is ' + + 'significantly better than the one used for the conversion. ' + + 'If you delete the log files, you will not benefit from this functionality. ' + + '
'; + + html += '' + + 'The log files are btw located in wp-content/webp-express/log/, if you want to have a closer look.' + + '
'; + //html += 'In a not too far future, the log files will be used in the conversion manager.
' + //html += 'They could become handy.' + /* + html += 'This action cannot be reversed. Your log files will be gone. ' + html += 'Dead. Completely. Forever. ' + html += '(Unless of course you have a backup. Or, of course, there are ways of recovery... Anyway...). ' + html += 'Ok, sorry for the babbeling. The dialog seemed bare without text.
';*/ + html += ''; + + document.getElementById('purgelogcontent').innerHTML = ''; + + if (result['fail-count'] == 0) { + if (result['delete-count'] == 0) { + html += 'No log files were found, so none was deleted.'; + } else { + html += 'Successfully deleted ' + result['delete-count'] + ' log files'; + } + } else { + if (result['delete-count'] == 0) { + html += 'Failed deleting ' + result['fail-count'] + ' log files. None was deleted, in fact.'; + } else { + html += 'Deleted ' + result['delete-count'] + ' log files. However, failed deleting ' + result['fail-count'] + ' log files.'; + } + } + html += '
'; + html += ''; + html += '' + line.replace(/\`/gm, '') + ''; + } + + // Bold with inline attributtes (ie: "hi **bold**{: .red}") + line = line.replace(/\*\*([^\*]+)\*\*\{:\s([^}]+)\}/gm, function(s, g1, g2) { + // g2 is the inline attributes. + // right now we only support classes, and only ONE class. + // so it is easy. + var className = g2.substr(1); + + return '' + g1 + ''; + //return '' + s.substr(2, s.length - 4) + ''; + }); + + // Bold + line = line.replace(/(\*\*[^\*]+\*\*)/gm, function(s) { + return '' + s.substr(2, s.length - 4) + ''; + }); + + // Italic + line = line.replace(/(\*[^\*]+\*)/gm, function(s) { + return '' + s.substr(1, s.length - 2) + ''; + }); + + // Headline + if (line.substr(0, 2) == '# ') { + line = '
' + line.substr(15) + ''; + } + + // Empty line + if (line == '') { + line = '
The following was returned:
' + response; + return; + } + + var result = JSON.parse(response); + //result['log'] = processLogMoveOptions(result['log']); + + + //var html = document.getElementById('tc_conversion_result').innerHTML; + var html = ''; + + if (result['success'] === true) { + + html += '' + webpexpress_escapeHTML(result['log']) + ''; + + document.getElementById('tc_conversion_result').innerHTML = html; + initComparisonSlider(jQuery); + + } else { + html += '
' + webpexpress_escapeHTML(result['log']) + ''; + } + + document.getElementById('tc_conversion_result').innerHTML = html; + } + + //html = result['log']; + +} diff --git a/lib/options/js/whitelist.js b/lib/options/js/whitelist.js new file mode 100644 index 0000000..209cd95 --- /dev/null +++ b/lib/options/js/whitelist.js @@ -0,0 +1,203 @@ +function updateWhitelistInputValue() { + if (document.getElementById('whitelist') == null) { + console.log('document.getElementById("whitelist") returns null. Strange! Please report.'); + return; + } + document.getElementById('whitelist').value = JSON.stringify(window.whitelist); +} + +function whitelistStartPolling() { + + jQuery.post(window.ajaxurl, { + 'action': 'webpexpress_start_listening', + }, function(response) { + window.whitelistTid = window.setInterval(function() { + jQuery.post(window.ajaxurl, { + 'action': 'webpexpress_get_request', + }, function(response) { + if (response && (response.substr(0,1) == '{')) { + var r = JSON.parse(response); + window.webpexpress_incoming_request = r; + //console.log(r); + window.clearInterval(window.whitelistTid); + closeDasPopup(); + + // Show request + openDasPopup('whitelist_accept_request', 300, 200); + + var s = ''; + s += 'Website: ' + r['label'] + '
No sites have been authorized to use the web service yet.
'; + } + s+=''; + + document.getElementById('whitelist_div').innerHTML = s; + +} + +function whitelistClearWhitelistEntryForm() { + document.getElementById('whitelist_label').value = ''; + document.getElementById('whitelist_ip').value = ''; + document.getElementById('whitelist_api_key').value = ''; + document.getElementById('whitelist_require_api_key_to_be_crypted_in_transfer').checked = true; +} + +function whitelistAddWhitelistEntry() { + + if (document.getElementById('whitelist_label').value == '') { + alert('Label must be filled out'); + return; + } + if (document.getElementById('whitelist_ip').value == '') { + alert('IP must be filled out. To allow any IP, enter "*"'); + return; + } + // TODO: Validate IP syntax + if (document.getElementById('whitelist_api_key').value == '') { + alert('API key must be filled in'); + return; + } + window.whitelist.push({ + uid: whitelistCreateUid(), + label: document.getElementById('whitelist_label').value, + ip: document.getElementById('whitelist_ip').value, + 'new-api-key': document.getElementById('whitelist_api_key').value, + 'require-api-key-to-be-crypted-in-transfer': document.getElementById('whitelist_require_api_key_to_be_crypted_in_transfer').checked, +// new_password: '', + //quota: 60 + }); + updateWhitelistInputValue(); + whitelistSetHTML(); + + closeDasPopup(); +} + +function whitelistAddManually() { +// alert('not implemented yet'); + whitelistClearWhitelistEntryForm(); + + document.getElementById('whitelist_properties_popup').className = 'das-popup mode-add'; + +// whitelistCancelListening(); +// closeDasPopup(); + openDasPopup('whitelist_properties_popup', 400, 300); +} + +function whitelistChangeApiKey() { + document.getElementById('whitelist_api_key').value = prompt('Enter new api key'); +} + +function whitelistUpdateWhitelistEntry() { + var i = parseInt(document.getElementById('whitelist_i').value, 10); + + window.whitelist[i]['uid'] = document.getElementById('whitelist_uid').value; + window.whitelist[i]['label'] = document.getElementById('whitelist_label').value; + window.whitelist[i]['ip'] = document.getElementById('whitelist_ip').value; + + if (document.getElementById('whitelist_api_key').value != '') { + window.whitelist[i]['new-api-key'] = document.getElementById('whitelist_api_key').value; + } + window.whitelist[i]['require-api-key-to-be-crypted-in-transfer'] = document.getElementById('whitelist_require_api_key_to_be_crypted_in_transfer').checked; + whitelistSetHTML(); + closeDasPopup(); +} + +function whitelistEditEntry(i) { + var entry = window.whitelist[i]; + whitelistClearWhitelistEntryForm(); + + document.getElementById('whitelist_properties_popup').className = 'das-popup mode-edit'; + + document.getElementById('whitelist_uid').value = entry['uid']; + document.getElementById('whitelist_i').value = i; + document.getElementById('whitelist_label').value = entry['label']; + document.getElementById('whitelist_ip').value = entry['ip']; + document.getElementById('whitelist_api_key').value = ''; + document.getElementById('whitelist_require_api_key_to_be_crypted_in_transfer').checked = entry['require-api-key-to-be-crypted-in-transfer']; + + openDasPopup('whitelist_properties_popup', 400, 300); +} + +document.addEventListener('DOMContentLoaded', function() { + updateWhitelistInputValue(); + whitelistSetHTML(); +}); diff --git a/lib/options/options/alter-html/alter-html-options.inc b/lib/options/options/alter-html/alter-html-options.inc new file mode 100644 index 0000000..e43b56b --- /dev/null +++ b/lib/options/options/alter-html/alter-html-options.inc @@ -0,0 +1,235 @@ + +Pro picture tag syntax: The browser selects the webp if it supports it.
' . + 'Pro image urls: Also works on inline styles
' . + (($config['operation-mode'] == 'varied-image-responses') ? + 'You do not need to enable this in Varied image responses operation mode, but enabling it has some benefits: ' . + 'Caching is improved if you are on a CDN as the webp images that are requested directly does not need to be keyed by the Accept header. ' . + 'Also, when a user downloads an image, it will have the correct extension.
' : '') + ); + ?> ++ Two distinct methods for altering HTML are supported. View comparison chart + +
+ + Hide comparison chart +Beware that this structural change may break styling!
' .
+ '(a selector such as "div > img" will no longer match the image because the immidiate parent is now "picture". However, ' .
+ 'a selector such as "div img" will still work)' .
+ '
PS: This functionality is handled by ' . + 'this external library' . + ' (I have pushed the code to an external library so it can be used by other projects besides this plugin)
' + ); + ?> +Does not work with page caching – unless you are using the Cache Enabler plugin
' . + 'Note that you will have to do something for the browsers that does not support webp. ' . + 'And that something is in most cases to enable the Only do the replacements in webp enabled browsers option, which ' . + 'will show up when you enable this option. ' . + 'Or you can experiment with javascript solutions. There is for example the webpjs javascript library. ' . + 'But it does not support srcset, which is a showstopper. There are other libraries out there. ' . + '
PS: This replace functionality is handled by ' . + 'this external library' . + ', created for the purpose.
' + ); + ?> +Technically, we are hooking into the handle_upload filter to trigger conversion of the image ' . + 'and the image_make_intermediate_size filter for the thumbnails.
' + ); + ?> +If you select "Auto", WebP Express will try converting to both encodings ' . + 'and select one that resulted in the smallest file.
' . + 'Note that Gd and Ewww does not support the "Auto" feature. ' . + 'Gd can only produce lossy, and will simply do that. ' . + 'Ewww can not be configured to use a certain encoding, but automatically chooses lossless encoding for PNGs and lossy for JPEGs.' . + '
' . + //'Also note that Remote WebP Express (if the WebP Express you are ' . + //'connecting to are using one of these, and you are using version 0.14+)' . + '
You can read more about the option here
' + );?> +Ouality detection requires imagick, imagemagick or gmagick (not neccessarily compiled with webp). ' . + 'In case quality detection is not available, the fallback quality is used.
' . + ( + $canDetectQuality ? + 'Quality detection is working, btw :)
' : + 'Quality detection is not currently available on your system. ' . + ((extension_loaded('imagick') && class_exists('\\Imagick')) ? + 'You have imagick, but you need a newer version (You need PECL >= 2.2.2). ' : '' + ) . + 'However, you can still have it by using the Remote WebP Express conversion method (the request for ' . + 'using same quality as the jpeg, as well as the limit and fallback settings will be forwarded to the remote)' . + '
' + ) + + ); + /* + if ($canDetectQuality) { + echo helpIcon('All converted images will be encoded with this quality'); + } else { + echo helpIcon('All converted images will be encoded with this quality. ' . + 'For Remote WebP Express and Imagick, you however have the option to use override this, and use ' . + '"auto". With some setup, you can get quality detection working and you will then be able to set ' . + 'quality to "auto" generally. For that you either need to get the imagick extension running ' . + '(PECL >= 2.2.2) or exec() rights and either imagick or gmagick installed.' + ); + } + */ + ?> +Note that the near-lossless option only is supported by the Cwebp and Vips conversion methods.
' . + 'Read more about the feature here
' + ); + ?> +Converted jpeg images will get same quality as original, but not more than this setting. Something between 70-85 is recommended for most websites.
There is no "lossy" option in the combobox, because converting all PNGs to lossy would probably be a bad idea. ' . + 'However, in many cases you get better compression with lossy. So this is what the "auto" option is for. ' . + 'With "Auto", WebP Express will try converting to both encodings ' . + 'and select one that resulted in the smallest file.
' . + 'Note that Gd and Ewww neither supports "Lossless" or "Auto". ' . + 'Gd can only produce lossy, and will simply do that. ' . + 'Ewww can not be configured to use a certain encoding, but automatically chooses lossless encoding for PNGs and lossy for JPEGs.' . + '
' . + 'You can read more about the option here
' + );?> +Note that there is no "Same as PNG" option available here as PNGs are lossless
' + ); + ?> + + + Note that Gd and Ewww does not support the "Alpha quality" feature. ' . + 'They simply ignore the option and converts the alpha channel losslessly. ' . + '' + ); + ?> +Note that the near-lossless option only is supported by the Cwebp and Vips conversion methods.
' . + 'Read more about the feature here
' + ); + ?> +Quality of webp, when converting jpegs (0-100)
' . + 'Note: If your system had the capability to detect the quality ' . + 'of jpeg images, you would have had the option to choose "Same as jpeg". To get that option, you ' . + 'either need to get the imagick extension running (PECL >= 2.2.2) or imagick or gmagick installed plus ' . + 'exec() rights.
' . + 'Note: If you use the Remote WebP Express converter, you can configure it to ask the remote ' . + 'to do the automatic quality detection for jpegs. This will override the value entered here.
' + ); +} +echo 'Optionally set cache-control header for webp images'); + break; + default: + echo helpIcon('
Controls the cache-control header on successful conversion and direct redirection to converted ' . + 'image in .htaccess. In case of convert failure, headers will be sent to prevent caching.
' . + 'PS: In order to set stale-while-revalidate and stale-if-error directives, you must ' . + 'currently choose "Custom". It is a good idea to set these.' . + '
'); + break; + } + ?> +Note: I am not completely sure that all forward proxies handles varied responses. ' . + 'This is discussed here.
' + , + 'no-margin-left set-margin-right' + ); + ?> + + + here)', + 'no-margin-left' + );?> +Select under which naming convention the webp files are stored. ' . + 'It is assumed that webp files are located in the same folder as the originals.
' . + '' . + '| Plugin | Convention |
|---|---|
| Cache enabler | Replaces extension |
| Shortpixel | Replaces extension |
| Ewww | Appends extension |
| Optimus HQ | Replaces extension |
Controls the filename of the converted file.
' . + 'The "Append" option result in file names such as "image.png.webp". ' . + 'The "Set" option results in file names such as "image.webp". ' . + 'Note that if you choose "Set", it will be a problem if you ie both have a logo.jpg and a logo.png in the same folder. ' . + 'If you are using WebP Express together with Cache enabler ' . + 'or Shortpixel, set this option to Set"
' . + (($config['operation-mode'] == 'cdn-friendly') ? 'In this mode, the webp files will be stored in the same folder as the originals, except for images that are not inside the uploads folder (these are stored in wp-content/webp-express/webp-images/doc-root).
' : '') . + 'Changing this option will cause existing webp images to be renamed (only those in the upload folder, and only those that has a ' . + 'corresponding source image)
' . + 'Note that this option only applies to the webp images stored in the uploads folder (mingled).
' + ); + } + ?> +Note: Changing this option will cause existing webp images to be moved
'); + ?>"document root"
' .
+ 'When "document root" is selected, the webp images will be stored in:
' .
+ '[cache root]/doc-root/[relative path of source image, from document root].
' .
+ '
"image roots"
' .
+ 'A Wordpress site has images stored in different locations. Uploaded files are for example stored in the uploads folder, which is ' .
+ 'usually - but not always - located in the "wp-content" folder. I call the uploads folder an "image root". Other roots are: ' .
+ '"themes", "plugins", "wp-content" and "index". When the "image roots" setting is selected, the webp files ' .
+ 'are stored in a structure that mirrors the relative path of the source image within its image root. ' .
+ 'For "uploads", that location is:
' .
+ '[cache root]/uploads/[relative path of source image, from uploads root].
' .
+ 'More generally we have:
' .
+ '[cache root]/[image root]/[relative path of source image, from its image root].
' .
+ '
Which option is best?
' .
+ 'Well, in most cases it does not matter. However, there are hosts out there that have set the document root up incorrectly, ' .
+ 'so I would generally recommend "Image roots". ' .
+ 'On Nginx, I however recommend "Document root", as it requires fewer rewrite rules.
Select which types of images you would like to redirect and/or have altered in the HTML
'); +} else { + echo helpIcon('Beware that some has reported problems with Gd and PNG, but it has not been pinned down when it happens. It is probably on older versions of Gd. Please report any problems with Gd!
'); +} +echo 'The option is used both when generating .htaccess rules and in Alter HTML.
' . + 'Note: If you are using Alter HTML with picture tags and have images with a srcset ' . + 'attribute, WebP Express generates a source element with type set to "image/webp". This source ' . + 'thus points to a bundle of webp images. From 0.25.5 and forward, when this option is enabled, ' . + 'WebP Express will only add the source element when ALL those webp images are smaller than their originals. ' . + 'In such a situation (using Alter HTML with picture tags), it might be a better strategy to disable this option. ' . + 'Especially if your images generally have the srcset attribute set. Chances are that only a few of the webps in ' . + 'the source set are bigger than the corresponding originals, and chances are that they are only slightly bigger.' . + '
' + ); + ?>The "All content" setting will work on uploads, themes, plugins - anything in the "wp-content" ' . + '(or whatever it has been renamed to). It will work on uploads, even if the uploads folder has been ' . + 'configured to reside outside of wp-content - and on plugins, even if plugins has been moved.
' + ); + ?>If the script detects that the webp already exists, and it is smaller and newer than the original, the ' . + 'webp is served directly. Otherwise the original image is converted and served.
' . + 'The redirect rule is placed below the rule that redirects directly to existing ' . + 'webp files, which means that conversion will only be triggered once.
' + ); ?> +The feature works the following way:' . + '
This feature allows you to reference webp images before they actually exists. You can ie write:' . +// "
<picture>\n <source srcset=\"image.jpg.webp\" type=\"image/webp\" />\n <img src=\"image.jpg\" /></picture>" + + ); ?> +
The option was created in order to make it possible to achieve the functionality behind the ' . + 'Redirect requests for non-existing webp-files to converter option found in the ' . + '"CDN friendly" operation mode.
' + ); + ?> + > +The rule is placed above the rule that redirects to the converter.
' + ); + } + + ?> +Determines what to serve when conversion is a success. If you are using the Cache Enabler plugin, set to "Original image", ' . + 'otherwise you would normally set it to "Converted image".
' . + 'If set to "Converted image", a Vary:Accept header will be sent to indicate that the response depends on the Accept header ' . + '(which indicates if a browser supports webp images or not)
If set to "Original image", make sure to disable the "Redirect ' . + 'directly to converted image when available" option in the Redirect rules
'); +echo 'Psst: The endpoint of the web service is:
+ + +" . htmlentities(print_r(Config::hmmm(), true)) . "" + ); + } +}*/ +if (!Paths::createContentDirIfMissing()) { + Messenger::printMessage( + 'error', + 'WebP Express needs to create a directory "webp-express" under your wp-content folder, but does not have permission to do so.
You have turned on ' . implode(' and ', $turnedOn) . + '. However, ' . (count($turnedOn) == 2 ? 'these features' : 'this feature') . + ' does not work on your current server settings / wordpress setup, ' . + ' because the PHP scripts in the plugin folder (in the "wod" and "wod2" subfolders) fails to run ' . + ' when requested directly. You can try to fix the problem or simply turn ' . + (count($turnedOn) == 2 ? 'them' : 'it') . + ' off and rely on "Convert on upload" and "Bulk Convert" to get the images converted.
' . + 'If you are going to try to solve the problem, you need at least one of the following pages ' . + 'to display "pong": ' . + 'wod-test' . + ' or wod2-test' . + '. The problem will typically be found in the server configuration or a security plugin. ' . + 'If one of the links results in a 403 Permission denied, look out for "deny" and "denied" in ' . + 'httpd.conf, /etc/apache/sites-enabled/your-site.conf and in parent .htaccess files.' . + '
.' + ); + } + // We currently allow the "canRunTestScriptInWOD" test not to be stored, + // If it is not stored, it means .htaccess files are pointing to "wod" + // PS: the logic of where it is stored happens in HTAccessRules::getWodUrlPath + // - we mimic it here. + $pointingToWod = true; // true = pointing to "wod", false = pointing to "wod2" + $hasWODTestBeenRun = isset($storedCapTests['canRunTestScriptInWOD']); + if ($hasWODTestBeenRun && !($storedCapTests['canRunTestScriptInWOD'])) { + $pointingToWod = false; + } + $canOnlyRunInWod = $canRunInWod && !$canRunInWod2; + if ($canOnlyRunInWod && !$pointingToWod) { + Messenger::printMessage( + 'warning', + 'The conversion script cannot currently be run. ' . + 'However, simply click "Save settings and force new .htaccess rules" to fix it. ' . + '(this will point to the script in the "wod" folder rather than "wod2")' + ); + } + + $canOnlyRunInWod2 = $canRunInWod2 && !$canRunInWod; + if ($canOnlyRunInWod2 && $pointingToWod) { + Messenger::printMessage( + 'warning', + 'The conversion script cannot currently be run. ' . + 'However, simply click "Save settings and force new .htaccess rules" to fix it. ' . + '(this will point to the script in the "wod2" folder rather than "wod")' + ); + } + + } + + if (HTAccessRules::arePathsUsedInHTAccessOutdated()) { + + $pathsGoingToBeUsedInHtaccess = [ + 'wod-url-path' => Paths::getWodUrlPath(), + ]; + + $config2 = Config::loadConfig(); + if ($config2 === false) { + Messenger::printMessage( + 'warning', + 'Warning: Config file cannot be loaded. Perhaps clicking ' . + 'Save settings will solve itUnfortunately your server cannot convert webp files in PHP without resorting to cloud conversion.
' . + 'But do not despear! - You have options!
' . + 'Of course, there is also the option of using another plugin altogether. ' . + 'I can recommend Optimole. ' . + 'If you want to try that out and want to support me in the process, ' . + 'follow this link ' . + '(it will give me a reward in case you decide to sign up).' . + '
' . + "Btw, don't worry, your images still works. The rewrite rules will not be saved until you click the " . + '"Save settings" button.
'; + //'(and you also have "Response on failure" set to "Original image", so they will work even if you click save)'; +} else { + echo 'The rewrite rules are not active yet. They will be activated the first time you click the "Save settings" button.
'; +} + +//echo 'working converters:'; +//print_r($workingConverters); + +//echo 'Before you do that, I suggest you find out which converters that works. Start from the top. Click "test" next to a converter to test it. Try also clicking the "configure" buttons
'; + +/* +if (Paths::isWPContentDirMovedOutOfAbsPath()) { + if (!Paths::canWriteHTAccessRulesHere($wpContentDir)) { + echo 'Oh, one more thing. Unless you are going to put the rewrite rules into your configuration manually, ';
+ echo 'WebP Express would be needing to store the rewrite rules in a .htaccess file in your wp-content directory ';
+ echo '(we need to store them there rather than in your root, because you have moved your wp-content folder out of the Wordpress root). ';
+ echo 'Please adjust the file permissions of your wp-content dir. ';
+
+ if (Paths::isPluginDirMovedOutOfWpContent()) {
+ echo '
But that is not all. Besides moving your wp-content dir, you have also moved your plugin dir... ';
+ echo 'If you want WebP-Express to work on the images delivered by your plugins, you must also grant write access to your plugin dir (you can revoke the access after we have written the rules).
';
+ }
+ echo 'You can reload this page aftewards, and this message should be gone
Oh, one more thing. I can see that your plugin dir has been moved out of your wp-content folder. '; + echo 'If you want WebP-Express to work on the images delivered by your plugins, you must grant write access to your '; + echo 'plugin dir (you can revoke the access after we have written the rules, but beware that the plugin may need '; + echo 'access rights again. Some of the options affects the .htaccess rules. And WebP Express also have to remove the rules if the plugin is disabled)'; + } + } + if (Paths::isUploadDirMovedOutOfWPContentDir()) { + if (!Paths::canWriteHTAccessRulesHere($uploadDir)) { + echo '
Oh, one more thing. We also need to write rules to your uploads dir (because you have moved it). '; + echo 'Please grant us write access to your '; + if (FileHelper::fileExists($uploadDir . '/.htaccess')) { + echo '.htaccess file in your upload dir'; + } else { + echo 'upload dir, so we can plant an .htaccess there'; + } + echo '. Your upload dir is: ' . $uploadDir . '. '; + echo '- Or alternatively, you can leave it be and update the rules manually, whenever they need to be changed. '; + } + } +} else { + $firstWritable = Paths::returnFirstWritableHTAccessDir([$wpContentDir, $indexDir]); + if ($firstWritable === false) { + echo '
Oh, one more thing. Unless you are going to put the rewrite rules into your configuration manually, '; + echo 'WebP Express would be needing to store the rewrite rules in a .htaccess file. '; + echo 'However, your current file permissions does not allow that. '; + echo 'WebP Express would prefer to put the rewrite rules into your wp-content folder, but '; + echo 'will store them in your main .htaccess file if it can write to that, but not your wp-content. '; + echo '(The preference for storing in wp-content is simply that it minimizes the risk of conflicts with rules from other plugins. '; + echo 'deeper .htaccess files takes precedence). '; + echo 'Anyway: Could you please adjust the file permissions of either your main .htaccess file or your wp-content dir?'; + echo 'You can reload this page aftewards, and this message should be gone
'; + } else { + if ($firstWritable != $wpContentDir) { + echo 'Oh, one more thing. Unless you are going to put the rewrite rules into your configuration manually, '; + echo 'WebP Express would be needing to store the rewrite rules in a .htaccess file. '; + echo 'Your current file permissions does allow us to store rules in your main .htaccess file. '; + echo 'However, WebP Express would prefer to put the rewrite rules into your wp-content folder. '; + echo 'Putting them there will minimize the risk of conflict with rules from other plugins, as '; + echo 'deeper .htaccess files takes precedence. '; + echo 'If you would like the .htaccess file to be stored in your wp-content folder, please adjust your file permissions. '; + echo 'You can reload this page aftewards, and this message should be gone
'; + } + } + if (Paths::isUploadDirMovedOutOfWPContentDir()) { + if (!Paths::canWriteHTAccessRulesHere($uploadDir)) { + echo 'Oh, one more thing. We also need to write rules to your uploads dir (because you have moved it). '; + echo 'Please grant us write access to your '; + if (FileHelper::fileExists($uploadDir . '/.htaccess')) { + echo '.htaccess file in your upload dir'; + } else { + echo 'upload dir, so we can plant an .htaccess there'; + } + echo '. Your upload dir is: ' . $uploadDir . '. '; + echo '- Or alternatively, you can leave it and update the rules manually, whenever they need to be changed. '; + } + } + if (Paths::isPluginDirMovedOutOfAbsPath()) { + if (!Paths::canWriteHTAccessRulesHere($pluginDir)) { + echo '
Oh, one more thing. I see you have moved your plugins dir out of your root. '; + echo 'If you want WebP-Express to work on the images delivered by your plugins, you must also grant write access '; + echo 'to your '; + if (FileHelper::fileExists($pluginDir . '/.htaccess')) { + echo '.htaccess file in your plugin dir'; + } else { + echo 'plugin dir, so we can plant an .htaccess there'; + } + echo ' (you can revoke the access after we have written the rules).'; + echo '
'; + } + } +} +*/ +/* +if(Paths::canWriteHTAccessRulesHere($wpContentDir)) { + + if ($firstWritable === false) { + echo 'Actually, WebP Express does not have permission to write to your main .htaccess either. Please fix. Preferably '; + } + + + $firstWritable = Paths::returnFirstWritableHTAccessDir([$indexDir, $homeDir]); + if ($firstWritable === false) { + echo 'Actually, WebP Express does not have permission to write to your main .htaccess either. Please fix. Preferably '; + } + if(Paths::canWriteHTAccessRulesHere($wpContentDir)) { + echo 'WebP Express however does have rights to write to your main .htaccess. It will work too - probably. But to minimize risk of conflict with rules from other plugins, I recommended you to adjust the file permissions to allow us to write to a .htaccess file in your wp-content dir'; + } + echo ''; +}*/ + +echo '' . print_r($config['converters'], true) . ''; + +//echo 'Working converters:' . print_r($workingConverters, true) . '
To open a folder, click the "+" sign next to the folder name or double click the folder name
{const{slotScopeIds:R}=S;R&&(E=E?E.concat(R):R);const T=o(v),P=p(i(v),S,T,g,O,E,$);return P&&ja(P)&&P.data==="]"?i(S.anchor=P):(Cn=!0,a(S.anchor=l("]"),T,P),P)},m=(v,S,g,O,E,$)=>{if(Cn=!0,S.el=null,$){const P=b(v);for(;;){const N=i(v);if(N&&N!==P)s(N);else break}}const R=i(v),T=o(v);return s(v),n(null,S,T,R,g,O,Fo(T),E),R},b=v=>{let S=0;for(;v;)if(v=i(v),v&&ja(v)&&(v.data==="["&&S++,v.data==="]")){if(S===0)return i(v);S--}return v};return[c,u]}const ot=wu;function Ju(e){return qu(e)}function Qu(e){return qu(e,xg)}function qu(e,t){const n=Wc();n.__VUE__=!0;const{insert:r,remove:i,patchProp:o,createElement:s,createText:a,createComment:l,setText:c,setElementText:u,parentNode:f,nextSibling:p,setScopeId:d=pt,cloneNode:m,insertStaticContent:b}=e,v=(x,I,k,X=null,W=null,q=null,ne=!1,_=null,ee=!!I.dynamicChildren)=>{if(x===I)return;x&&!tn(x,I)&&(X=ft(x),ve(x,W,q,!0),x=null),I.patchFlag===-2&&(ee=!1,I.dynamicChildren=null);const{type:Q,ref:fe,shapeFlag:le}=I;switch(Q){case Cr:S(x,I,k,X);break;case ht:g(x,I,k,X);break;case tr:x==null&&O(I,k,X,ne);break;case Ye:C(x,I,k,X,W,q,ne,_,ee);break;default:le&1?R(x,I,k,X,W,q,ne,_,ee):le&6?w(x,I,k,X,W,q,ne,_,ee):(le&64||le&128)&&Q.process(x,I,k,X,W,q,ne,_,ee,Ve)}fe!=null&&W&&jo(fe,x&&x.ref,q,I||x,!I)},S=(x,I,k,X)=>{if(x==null)r(I.el=a(I.children),k,X);else{const W=I.el=x.el;I.children!==x.children&&c(W,I.children)}},g=(x,I,k,X)=>{x==null?r(I.el=l(I.children||""),k,X):I.el=x.el},O=(x,I,k,X)=>{[x.el,x.anchor]=b(x.children,I,k,X)},E=({el:x,anchor:I},k,X)=>{let W;for(;x&&x!==I;)W=p(x),r(x,k,X),x=W;r(I,k,X)},$=({el:x,anchor:I})=>{let k;for(;x&&x!==I;)k=p(x),i(x),x=k;i(I)},R=(x,I,k,X,W,q,ne,_,ee)=>{ne=ne||I.type==="svg",x==null?T(I,k,X,W,q,ne,_,ee):j(x,I,W,q,ne,_,ee)},T=(x,I,k,X,W,q,ne,_)=>{let ee,Q;const{type:fe,props:le,shapeFlag:pe,transition:y,patchFlag:h,dirs:U}=x;if(x.el&&m!==void 0&&h===-1)ee=x.el=m(x.el);else{if(ee=x.el=s(x.type,q,le&&le.is,le),pe&8?u(ee,x.children):pe&16&&N(x.children,ee,null,X,W,q&&fe!=="foreignObject",ne,_),U&&_t(x,null,X,"created"),le){for(const Z in le)Z!=="value"&&!Sn(Z)&&o(ee,Z,null,le[Z],q,x.children,X,W,Qe);"value"in le&&o(ee,"value",null,le.value),(Q=le.onVnodeBeforeMount)&&St(Q,X,x)}P(ee,x,x.scopeId,ne,X)}U&&_t(x,null,X,"beforeMount");const Y=(!W||W&&!W.pendingBranch)&&y&&!y.persisted;Y&&y.beforeEnter(ee),r(ee,I,k),((Q=le&&le.onVnodeMounted)||Y||U)&&ot(()=>{Q&&St(Q,X,x),Y&&y.enter(ee),U&&_t(x,null,X,"mounted")},W)},P=(x,I,k,X,W)=>{if(k&&d(x,k),X)for(let q=0;q Rethrowing exception for your convenience
+
+
+ source:
+ destination:
+ getMessage();
+ echo '' . $msg . '';
+
+ //echo '