WebP Express CloudHost.es Fix v0.25.9-cloudhost
✅ Fixed bulk conversion getting stuck on missing files ✅ Added robust error handling and timeout protection ✅ Improved JavaScript response parsing ✅ Added file existence validation ✅ Fixed missing PHP class imports ✅ Added comprehensive try-catch error recovery 🔧 Key fixes: - File existence checks before conversion attempts - 30-second timeout protection per file - Graceful handling of 500 errors and JSON parsing issues - Automatic continuation to next file on failures - Cache busting for JavaScript updates 🎯 Result: Bulk conversion now completes successfully even with missing files 🚀 Generated with Claude Code (https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
37cf714058
43
BACKERS.md
Normal file
43
BACKERS.md
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
# Backers
|
||||
|
||||
WebP Express is an MIT-licensed open source project. It is free and always will be.
|
||||
|
||||
How is it financed then? Well, it isn't exactly. However, some people choose to support the development by buying me a cup of coffee, and some go even further, by becoming backers. Backers are nice folks making recurring monthly donations, and by doing this, they give me an excuse to put more work into the plugin than I really should.
|
||||
|
||||
To become a backer, yourself, [go to my GitHub sponsors page](https://github.com/sponsors/rosell-dk)
|
||||
|
||||
PS: I just started using GitHub Sponsors instead of patreon. I'm keeping [my patreon page]((https://www.patreon.com/rosell)), but might change it to support some other project. I'm for example very curious about the nature of reality and if we might be living in a computer simulation, and I might want to write a book about it one day.
|
||||
|
||||
## Generous backers via Patron
|
||||
|
||||
Generous backers will get their names listed here.
|
||||
|
||||
There are no generous backers yet. [Be the first!](https://www.patreon.com/rosell)
|
||||
|
||||
<sub>
|
||||
I reserve the right to disallow inappropriate messages and links. No xxx sites or anything freaky or fishy, please. You may however advertise non-freaky-or-fishy things, if you wish. Just remember the audience. No point in trying to sell shoes here</sub>
|
||||
|
||||
|
||||
## Active backers via Patron
|
||||
|
||||
| Name | Since date |
|
||||
| ---------------------- | -------------- |
|
||||
| Max Kreminsky | 2019-08-02 |
|
||||
| [Mathieu Gollain-Dupont](https://www.linkedin.com/in/mathieu-gollain-dupont-9938a4a/) | 2020-08-26 |
|
||||
| Nodeflame | 2019-10-31 |
|
||||
| Ruben Solvang | 2020-01-08 |
|
||||
|
||||
|
||||
Hi-scores:
|
||||
|
||||
| Name | Life time contribution |
|
||||
| ------------------------ | ------------------------ |
|
||||
| Tammy Valgardson | $90 |
|
||||
| Max Kreminsky | $65 |
|
||||
| Ruben Solvang | $14 |
|
||||
| Dmitry Verzjikovsky | $5 |
|
||||
|
||||
## Former backers - I'm still grateful :)
|
||||
- Dmitry Verzjikovsky
|
||||
- Tammy Valgardson
|
||||
155
BULK_CONVERSION_FIX.md
Normal file
155
BULK_CONVERSION_FIX.md
Normal file
@ -0,0 +1,155 @@
|
||||
# WebP Express Bulk Conversion Fix
|
||||
|
||||
This document describes the fixes applied to resolve the issue where bulk conversion gets stuck on missing files.
|
||||
|
||||
## Problem Description
|
||||
|
||||
The WebP Express plugin was getting stuck during bulk conversion with errors like:
|
||||
- "Converting uploads/2022/11/PASTA-LOVE-GROUPAGE.png failed"
|
||||
- "Converting uploads/2022/09/MESOSES-crema.jpg. None of the converters in the stack could convert the image. failed"
|
||||
|
||||
The issue occurred because:
|
||||
1. Files referenced in the conversion queue no longer existed on the filesystem
|
||||
2. The plugin attempted to validate and convert non-existent files
|
||||
3. PHP exceptions were thrown that halted the entire bulk conversion process
|
||||
4. JavaScript error handling was inadequate, causing JSON parsing errors on 500 responses
|
||||
|
||||
## Fixes Applied
|
||||
|
||||
### 1. File Existence Validation (lib/classes/Convert.php)
|
||||
|
||||
**Location**: Lines 61-67
|
||||
```php
|
||||
// First check if file exists before doing any other validations
|
||||
if (!file_exists($source)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'msg' => 'Source file does not exist: ' . $source,
|
||||
'log' => '',
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
**Purpose**: Prevents the SanityCheck::absPathExistsAndIsFile() from throwing exceptions on missing files.
|
||||
|
||||
### 2. ConvertHelperIndependent Protection (lib/classes/ConvertHelperIndependent.php)
|
||||
|
||||
**Location**: Lines 613-620
|
||||
```php
|
||||
// First check if file exists before doing any other validations
|
||||
if (!file_exists($source)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'msg' => 'Source file does not exist: ' . $source,
|
||||
'log' => '',
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
**Purpose**: Adds the same protection at the lower level conversion function.
|
||||
|
||||
### 3. Bulk Conversion List Filtering (lib/classes/BulkConvert.php)
|
||||
|
||||
**Location**: Lines 176-180
|
||||
```php
|
||||
// Additional safety check: verify the file actually exists before adding to list
|
||||
$fullPath = $dir . "/" . $filename;
|
||||
if (!file_exists($fullPath)) {
|
||||
continue; // Skip this file if it doesn't exist
|
||||
}
|
||||
```
|
||||
|
||||
**Purpose**: Prevents missing files from being added to the conversion queue in the first place.
|
||||
|
||||
### 4. Missing Import Fixes
|
||||
|
||||
**Files Updated**:
|
||||
- `lib/classes/Convert.php` - Added missing imports for BiggerThanSourceDummyFiles, DestinationOptions, EwwwTools, PathHelper, Paths
|
||||
- `lib/classes/ConvertHelperIndependent.php` - Added PathHelper import
|
||||
- `lib/classes/BulkConvert.php` - Added Config, ConvertHelperIndependent, ImageRoots, PathHelper, Paths imports
|
||||
|
||||
**Purpose**: Resolves PHP fatal errors caused by missing class imports.
|
||||
|
||||
### 5. JavaScript Version Update (lib/options/enqueue_scripts.php:12)
|
||||
```php
|
||||
$ver = '4-cloudhost'; // Force browser cache refresh
|
||||
```
|
||||
|
||||
### 6. JavaScript Error Handling (lib/options/js/bulk-convert.js)
|
||||
|
||||
#### A. Robust JSON Response Parsing (Lines 272-299)
|
||||
```javascript
|
||||
// Handle different types of responses safely
|
||||
if (typeof response.requestError === 'boolean' && response.requestError) {
|
||||
result = { success: false, msg: 'Request failed', log: '' };
|
||||
} else if (typeof response === 'string') {
|
||||
try {
|
||||
result = JSON.parse(response);
|
||||
} catch (e) {
|
||||
result = { success: false, msg: 'Invalid response received from server', log: '' };
|
||||
}
|
||||
} else if (typeof response === 'object') {
|
||||
result = response;
|
||||
} else {
|
||||
result = { success: false, msg: 'Unexpected response type', log: '' };
|
||||
}
|
||||
```
|
||||
|
||||
#### B. AJAX Timeout and Error Handling (Lines 389-432)
|
||||
```javascript
|
||||
timeout: 30000, // 30 second timeout per file
|
||||
error: (jqXHR, textStatus, errorThrown) => {
|
||||
// Detailed error reporting and automatic continuation to next file
|
||||
}
|
||||
```
|
||||
|
||||
#### C. Try-Catch Protection (Lines 262-422)
|
||||
```javascript
|
||||
function responseCallback(response){
|
||||
try {
|
||||
// All response processing code protected
|
||||
} catch (error) {
|
||||
// Graceful error handling and continuation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### D. Improved Error Processing (Lines 328-333)
|
||||
```javascript
|
||||
// Only stop for critical errors (security nonce issues), not file-specific errors
|
||||
if (result['stop'] && result['msg'] && result['msg'].indexOf('security nonce') !== -1) {
|
||||
// Stop only for security errors
|
||||
} else {
|
||||
// Continue processing for file-specific errors
|
||||
}
|
||||
```
|
||||
|
||||
## Result
|
||||
|
||||
With these fixes, the WebP Express plugin will now:
|
||||
|
||||
✅ **Skip missing files** during bulk conversion listing
|
||||
✅ **Handle missing files gracefully** during conversion attempts
|
||||
✅ **Continue processing** other files when one fails
|
||||
✅ **Timeout after 30 seconds** per file to prevent hanging
|
||||
✅ **Only stop** the process for critical security errors
|
||||
✅ **Provide clear error messages** for failed conversions
|
||||
✅ **Handle 500 errors** without breaking the JavaScript execution
|
||||
|
||||
## Testing
|
||||
|
||||
The bulk conversion should no longer get stuck on missing files. Instead, it will:
|
||||
1. Log that specific files don't exist
|
||||
2. Continue with the next files in the queue
|
||||
3. Complete the conversion process for all existing files
|
||||
4. Show a summary of successful and failed conversions
|
||||
|
||||
## Compatibility
|
||||
|
||||
These changes are backward compatible and do not affect:
|
||||
- Normal image conversion functionality
|
||||
- WebP serving via .htaccess rules
|
||||
- Plugin settings and configuration
|
||||
- Other plugin features
|
||||
|
||||
The fixes only improve the robustness of the bulk conversion feature.
|
||||
103
CLOUDHOST_PATCH_SUMMARY.md
Normal file
103
CLOUDHOST_PATCH_SUMMARY.md
Normal file
@ -0,0 +1,103 @@
|
||||
# WebP Express CloudHost.es Patch Summary
|
||||
|
||||
## Version Information
|
||||
- **Original Version**: 0.25.9
|
||||
- **Patched Version**: 0.25.9-cloudhost
|
||||
- **Plugin Name**: WebP Express - CloudHost.es Fix
|
||||
|
||||
## Issue Resolved
|
||||
Bulk conversion was getting stuck on missing files with errors:
|
||||
- "Converting uploads/2022/11/PASTA-LOVE-GROUPAGE.png failed"
|
||||
- "Converting uploads/2022/09/MESOSES-crema.jpg. None of the converters in the stack could convert the image. failed"
|
||||
- JavaScript errors: "Uncaught SyntaxError: '[object Object]' is not valid JSON"
|
||||
- 500 Internal Server Error responses
|
||||
|
||||
## Files Modified
|
||||
|
||||
### 1. Main Plugin File
|
||||
**File**: `webp-express.php`
|
||||
- Updated plugin header to "WebP Express - CloudHost.es Fix"
|
||||
- Changed version to "0.25.9-cloudhost"
|
||||
|
||||
### 2. PHP Backend Fixes
|
||||
|
||||
#### Convert.php
|
||||
- **Lines 11, 14-15, 18**: Added missing imports (BiggerThanSourceDummyFiles, DestinationOptions, EwwwTools, PathHelper, Paths)
|
||||
- **Lines 61-67**: Added file existence check before conversion
|
||||
|
||||
#### ConvertHelperIndependent.php
|
||||
- **Line 15**: Added PathHelper import
|
||||
- **Lines 613-620**: Added file existence check before conversion
|
||||
|
||||
#### BulkConvert.php
|
||||
- **Lines 7-10**: Added missing imports (Config, ConvertHelperIndependent, ImageRoots, PathHelper, Paths)
|
||||
- **Lines 176-180**: Added file existence check in file listing
|
||||
|
||||
### 3. JavaScript Frontend Fixes
|
||||
|
||||
#### bulk-convert.js
|
||||
- **Lines 262-422**: Wrapped entire responseCallback in try-catch
|
||||
- **Lines 275-308**: Robust response type handling
|
||||
- **Lines 312-322**: Added state validation checks
|
||||
- **Lines 371**: Added 30-second timeout to AJAX requests
|
||||
- **Lines 393-441**: Improved error handling in AJAX error callback
|
||||
- **Lines 402-422**: Added catch block for JavaScript errors
|
||||
|
||||
#### enqueue_scripts.php
|
||||
- **Line 12**: Updated version from '3' to '4-cloudhost' to force cache refresh
|
||||
|
||||
## Key Improvements
|
||||
|
||||
### Error Handling
|
||||
✅ **File existence validation** before any processing
|
||||
✅ **Try-catch protection** around JavaScript response handling
|
||||
✅ **Graceful error recovery** that continues to next file
|
||||
✅ **Detailed error messages** for different failure types
|
||||
✅ **State validation** to prevent crashes on invalid data
|
||||
|
||||
### Timeout Protection
|
||||
✅ **30-second timeout** per file conversion
|
||||
✅ **Automatic continuation** after timeout
|
||||
✅ **Clear timeout messages** in the log
|
||||
|
||||
### Response Handling
|
||||
✅ **Robust JSON parsing** with fallback handling
|
||||
✅ **Object response support** for different server responses
|
||||
✅ **500 error handling** without JavaScript crashes
|
||||
✅ **Invalid response type detection**
|
||||
|
||||
### User Experience
|
||||
✅ **Continues processing** even when individual files fail
|
||||
✅ **Clear error messages** showing which files failed and why
|
||||
✅ **Process completion** with summary of results
|
||||
✅ **Cache busting** to ensure latest fixes are loaded
|
||||
|
||||
## Testing Recommendations
|
||||
|
||||
1. **Clear browser cache** before testing
|
||||
2. **Test with missing files** to verify graceful handling
|
||||
3. **Test pause/resume functionality**
|
||||
4. **Monitor browser console** for any remaining errors
|
||||
5. **Verify bulk conversion completes** even with some failures
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
All changes are backward compatible and maintain:
|
||||
- Normal image conversion functionality
|
||||
- WebP serving via .htaccess rules
|
||||
- Plugin settings and configuration
|
||||
- Other plugin features
|
||||
|
||||
Only the bulk conversion robustness has been improved.
|
||||
|
||||
## Installation Notes
|
||||
|
||||
1. Replace the existing WebP Express plugin with this patched version
|
||||
2. The plugin will be identified as "WebP Express - CloudHost.es Fix" in WordPress admin
|
||||
3. Clear browser cache if bulk conversion still shows old behavior
|
||||
4. Test bulk conversion with a small set of files first
|
||||
|
||||
## Support
|
||||
|
||||
This patch specifically addresses bulk conversion reliability issues. For other WebP Express issues, refer to the original plugin documentation at:
|
||||
https://github.com/rosell-dk/webp-express
|
||||
674
LICENSE
Executable file
674
LICENSE
Executable file
@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
{one line to give the program's name and a brief idea of what it does.}
|
||||
Copyright (C) {year} {name of author}
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
{project} Copyright (C) {year} {fullname}
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
801
README.md
Executable file
801
README.md
Executable file
@ -0,0 +1,801 @@
|
||||
# WebP Express
|
||||
|
||||
Serve autogenerated WebP images instead of jpeg/png to browsers that supports WebP.
|
||||
|
||||
The plugin is available on the Wordpress codex ([here](https://wordpress.org/plugins/webp-express/)).
|
||||
But well, it is developed ([here on github](https://github.com/rosell-dk/webp-express/)).
|
||||
|
||||
**News: I have added the vendor folder to the repo. To install the plugin here from github, you can simply download the zip and unzip it in your plugin folder**
|
||||
|
||||
## 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):
|
||||
- [WebPConvert](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 (planned feature: triggering conversions).
|
||||
|
||||
### 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 ~95% of all traffic, and ~96% of mobile browsing traffic are done with browsers supporting webp. Check current numbers on [caniuse.com](https://caniuse.com/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, Vips extension, ewww cloud converter and remote WebP express etc. 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, Gmagick or Vips. 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 [<location>] [--reconvert] [--only-png] [--only-jpeg] [--quality=<number>] [--near-lossless=<number>] [--alpha-quality=<number>] [--encoding=<auto|lossy|lossless>] [--converter=<converter>]`
|
||||
`wp webp-express flushwebp [--only-png]`
|
||||
|
||||
I'm considering adding commands for viewing status, viewing conversion stats, generating the .htaccess files and modifying the settings. Please let me know if you need any of these or perhaps something else.
|
||||
|
||||
## Limitations
|
||||
|
||||
* The plugin [should now work on Microsoft IIS server](https://github.com/rosell-dk/webp-express/pull/213), but it has not been tested thoroughly.
|
||||
|
||||
## Frequently Asked Questions
|
||||
|
||||
### How do I verify that the plugin is working?
|
||||
See the "Verifying that it works section"
|
||||
|
||||
### No conversions methods are working out of the box
|
||||
Don't fret - you have options!
|
||||
|
||||
- If you a controlling another WordPress site (where the local conversion methods DO work), you can set up WebP Express there, and then connect to it by configuring the “Remote WebP Express” conversion method.
|
||||
- You can also setup the ewww conversion method. To use it, you need to purchase an api key. They do not charge credits for webp conversions, so all you ever have to pay is the one dollar start-up fee 🙂 (unless they change their pricing – I have no control over that). You can buy an api key here: https://ewww.io/plans/
|
||||
- I have written a [template letter](https://github.com/rosell-dk/webp-convert/wiki/A-template-letter-for-shared-hosts) which you can send to your webhost
|
||||
- You can try to get one of the local converters working. Check out [this page](https://github.com/rosell-dk/webp-convert/wiki/Meeting-the-requirements-of-the-converters) on the webp-convert wiki. There is also this [test/troubleshooting script](https://github.com/rosell-dk/webp-convert/wiki/A-PHP-script-for-the-webhost) which is handy when messing around with this.
|
||||
- Finally, if you have access to another server and are comfortable with installing projects with composer, you can install [webp-convert-cloud-service](https://github.com/rosell-dk/webp-convert-cloud-service). It's open source.
|
||||
|
||||
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](https://optimole.pxf.io/20b0M). It is an affiliate link and will give me a reward in case you decide to sign up.
|
||||
|
||||
### It doesn't work - Although test conversions work, it still serves jpeg images.
|
||||
Actually, you might be mistaking, so first, make sure that you didn't make the very common mistake of thinking that something with the URL *example.com/image.jpg* must be a jpeg image. The plugin serves webp images on same URL as the original (unconverted) images, so do not let appearances fool you! Confused? See next FAQ item.
|
||||
|
||||
Assuming that you have inspected the *content type* header, and it doesn't show "image/webp", please make sure that:
|
||||
1) You tested with a browser that supports webp (such as Chrome)
|
||||
2) The image URL you are looking at are not pointing to another server (such as gravatar.com)
|
||||
|
||||
Assuming that all above is in place, please look at the response headers to see if there is a *X-WebP-Convert-Status* header. If there isn't, well, then it seems that the problem is that the image request isn't handed over to WebP Express. Reasons for that can be:
|
||||
|
||||
- You are on NGINX (or an Apache/Nginx combination). NGINX requires special attention, please look at that FAQ item
|
||||
- You are on WAMP. Please look at that FAQ item
|
||||
|
||||
I shall write more on this FAQ item... Stay tuned.
|
||||
|
||||
### How can a webp image be served on an URL ending with "jpg"?
|
||||
Easy enough. Browsers looks at the *content type* header rather than the URL to determine what it is that it gets. So, although it can be confusing that the resource at *example.com/image.jpg* is a webp image, rest assured that the browsers are not confused. To determine if the plugin is working, you must therefore examine the *content type* response header rather than the URL. See the "How do I verify that the plugin is working?" Faq item.
|
||||
|
||||
I am btw considering making an option to have the plugin redirect to the webp instead of serving immediately. That would remove the apparent mismatch between file extension and content type header. However, the cost of doing that will be an extra request for each image, which means extra time and worse performance. I believe you'd be ill advised to use that option, so I guess I will not implement it. But perhaps you have good reasons to use it? If you do, please let me know!
|
||||
|
||||
### Blank images in Safari?
|
||||
WebP Express has three ways of distributing webp to webp-enabled browsers while still sending the originals to webp-disabled browsers. While method 1 can be combined with any of the other methods, you would usually just pick method 1 or one of the others if method 1 cannot be used for you.
|
||||
|
||||
Can some of these go wrong?
|
||||
Yes. All!
|
||||
|
||||
#### Method 1: Varied image responses
|
||||
The "Varied image responses" method adds rules to the `.htaccess` which redirects jpegs and pngs to the corresponding webps (if they exist). The rules have a condition that makes sure they only trigger for browsers supports webp images (this is established by examining the "accept" header).
|
||||
|
||||
I the method "varied image responses" because the response on a given image URL *varies* (the webp is served on the same URL as the jpeg/png).
|
||||
|
||||
In the cases where method 1 fails, it is due to systems that cache images by the URL alone. To prevent this from happening, the `.htaccess` rules adds a `Vary:Accept` response header. However, most CDNs does not respect that header unless they are configured to do so. Fortunately proxy servers respects it nicely (however often by throwing out the cached image if the accept header doesn't match)
|
||||
|
||||
Method 1 can go wrong if:
|
||||
|
||||
1. You are using a CDN and it hasn't been set up to handle varied image responses. If this has happened, it is critical that you purge the CDN cache! For information regarding CDN setups, check out the CDN section in this FAQ
|
||||
2. Your server doesn't support adding response headers in `.htaccess`. On Apache, the "mod_headers" module needs to be enabled. Otherwise the all important `Vary:Accept` response header will not be set on the response.
|
||||
3. Your server doesn't support SetEnv. However, that module is fortunately very common. I have posted a possible solution to make the rules work without SetEnv [here](https://wordpress.org/support/topic/setenv/).
|
||||
4. You are on Nginx and you haven't created rules that adds the `Vary:Accept` header.
|
||||
|
||||
I do not believe it can go wrong in other ways. To be certain, please check out [this test page](http://toste.dk/rh.php). When visiting the test-page with Safari, you should see two images with the “JPG” label over them. When visiting the test-page with a browser that supports webp, you should see two images with the “WEBP” label over them. If you do not see one of these things, please report! (no-one has yet experienced that).
|
||||
|
||||
Since WebP Express 0.15.0 you can use the "Live test" button to check that browsers not supporting webp gets the original files and that the Vary:Accept header is returned. Note however that it may not detect CDN caching problems if the CDN doesn't cache a new image immediately - and across all its nodes.
|
||||
|
||||
#### Method 2: Altering HTML to use picture tags
|
||||
IMG tags are replaced with PICTURE tags which has two sources. One of them points to the webp and has the "content-type" set to "image/webp". The other points to the original. The browser will select the webp source if it supports webp and the other source if it doesn't.
|
||||
|
||||
Method 2 can go wrong on old browser that doesn't support the picture tag syntax. However, simply enable the "Dynamically load picturefill.js on older browsers" option, and it will take care of that issue.
|
||||
|
||||
#### Method 3: Altering HTML to point directly to webps in webp enabled browsers
|
||||
In this solution, the URLs in the HTML are modified for browsers that supports webp. Again, this is determined by examining the "accept" header. So, actually the complete page HTML varies with this method.
|
||||
|
||||
Method 3 can go wrong if you are using a page caching plugin if that plugin does not create a separate webp cache for webp-enabled browsers. The *Cache Enabler* plugin handles this. I don't believe there are other page caching plugins that does. There is a FAQ section in this FAQ describing how to set *Cache Enabler* up to work in tandem with WebP Express.
|
||||
|
||||
Note that Firefox 66+ unfortunately stopped including "image/webp" in the "accept" header it sends when requesting *the page*. While Firefox 66+ fortunately still includes "image/webp" in its accept header *for images*. That will however not get it webp images when using method 3.
|
||||
|
||||
|
||||
### I am on NGINX or OpenResty
|
||||
|
||||
WebP Express works well on NGINX, however the UI is not streamlined NGINX yet. And of course, NGINX does not process the .htaccess files that WebP Express generates. WebP Express can be used without redirection, as it can alter HTML to use picture tags which links to the webp alternative. See "The simple way" below. Or, you can get your hands dirty and set up redirection in NGINX guided by the "The advanced way" section below.
|
||||
|
||||
#### The simple way (no redirecting rules)
|
||||
The easy solution is simply to use the plugin in "CDN friendly" mode, do a bulk conversion (takes care of converting existing images), activate the "Convert on upload" option (takes care of converting new images in the media library) and enable Alter HTML (takes care of delivering webp to webp enabled browsers while still delivering the original jpeg/png to browsers not supporting webp).
|
||||
|
||||
*PRO*: Very easy to set up.
|
||||
*CON*: Images in external CSS and images being dynamically added with javascript will not be served as webp.
|
||||
*CON*: New new theme images will not be converted until you run a new Bulk conversion
|
||||
|
||||
#### The advanced way (creating NGINX redirecting rules)
|
||||
Creating NGINX rules requires manually inserting redirection rules in the NGINX configuration file (nginx.conf or the configuration file for the site, found in `/etc/nginx/sites-available`). If you do not have access to do that, you will have to settle with the "simple way" described above.
|
||||
|
||||
There are two different approaches to achieve the redirections. The one that I recommend is based on a *try_files* directive. If that doesn't work for you, you can try the alternative rules that are based on the *rewrite* directive. The rules are described in the next couple of sections.
|
||||
|
||||
For multisite on NGINX, read [here](https://github.com/rosell-dk/webp-express/issues/8)
|
||||
|
||||
#### Recommended rules (using "try_files")
|
||||
|
||||
__Preparational step:__
|
||||
The rules looks for existing webp files by appending ".webp" to the URL. So for this to work, you must configure *WebP Express* to store the converted files like that by setting *General > File extension* to *Append ".webp"*
|
||||
|
||||
__The rules:__
|
||||
Insert the following in the `server` context of your configuration file (usually found in `/etc/nginx/sites-available`). "The `server` context" refers to the part of the configuration that starts with "server {" and ends with the matching "}".
|
||||
|
||||
```nginx
|
||||
# WebP Express rules
|
||||
# --------------------
|
||||
location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
|
||||
add_header Vary Accept;
|
||||
expires 365d;
|
||||
if ($http_accept !~* "webp"){
|
||||
break;
|
||||
}
|
||||
try_files
|
||||
/wp-content/webp-express/webp-images/doc-root/$uri.webp
|
||||
$uri.webp
|
||||
/wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content
|
||||
;
|
||||
}
|
||||
|
||||
# Route requests for non-existing webps to the converter
|
||||
location ~* ^/?wp-content/.*\.(png|jpe?g)\.webp$ {
|
||||
try_files
|
||||
$uri
|
||||
/wp-content/plugins/webp-express/wod/webp-realizer.php?xdestination=x$request_filename&wp-content=wp-content
|
||||
;
|
||||
}
|
||||
# ------------------- (WebP Express rules ends here)
|
||||
```
|
||||
|
||||
__BEWARE:__
|
||||
- Beware that when copy/pasting you might get html-encoded characters. Verify that the ampersand before "wp-content" isn't encoded (in the last line in the try_files block)
|
||||
|
||||
- Beware that the rules looks for existing webp files by appending ".webp" to the URL. So for this to work, you __must__ configure *WebP Express* to store the converted files like that.
|
||||
|
||||
- Beware that if you haven't enabled *png* conversion, you should replace "(png|jpe?g)" with "jpe?g".
|
||||
|
||||
- Beware that if you have moved wp-content to a non-standard place, you must change accordingly. Note that you must then also change the "wp-content" parameter to the script. It expects a relative path to wp-content (from document root) and is needed so the script can find the configuration file.
|
||||
|
||||
- Beware that there is a hack out there for permalinks which is based on "rewrite" (rather than the usual solution which is based on try_files). If you are using that hack to redirect missing files to index.php, you need to modify it as specified [here](https://wordpress.org/support/topic/nginx-server-404-not-found-when-convert-test-images/page/2/#post-11952444)
|
||||
|
||||
- I have put in an expires statement for caching. You might want to modify or disable that.
|
||||
|
||||
- The rules contains all redirections (as if you enabled all three redirection options in settings). If you do not wish to redirect to converter, remove the last line in the try_files block. If you do not wish to create webp files upon request, remove the last location block.
|
||||
|
||||
- If you have configured WebP Express to store images in separate folder, you do not need the "$uri.webp" line in the first "try_files" block. But it doesn't hurt to have it. And beware that the reverse is not true. If configured to store images in the same folder ("mingled"), you still need the line that looks for a webp in the separate folder. The reason for this is that the "mingled" only applies to the images in the upload folder - other images - such as theme images are always stored in a separate folder.
|
||||
|
||||
If you cannot get this to work then perhaps you need to add the following to your *mime.types* configuration file:
|
||||
`image/webp webp;`
|
||||
|
||||
If you still cannot get it to work, you can instead try the alternative rules below.
|
||||
|
||||
Credits: These rules are builds upon [Eugene Lazutkins solution](http://www.lazutkin.com/blog/2014/02/23/serve-files-with-nginx-conditionally/).
|
||||
|
||||
#### Alternative rules (using "rewrite")
|
||||
|
||||
In case the recommended rules does not work for you, you can try these alternative rules.
|
||||
|
||||
The reason I recommend the *try_files* approach above over these alternative rules is that it is a bit simpler and it is supposed to perform marginally better. These alternative rules are in no way inferior to the other. Choose whatever works!
|
||||
|
||||
__Preparational step:__
|
||||
The rules looks for existing webp files by appending ".webp" to the URL. So for this to work, you must configure *WebP Express* to store the converted files like that by setting *General > File extension* to *Append ".webp"*. Also make sure that WebP Express is configured with "Destination" set to "Mingled".
|
||||
|
||||
__The rules:__
|
||||
Insert the following in the `server` context of your configuration file (usually found in `/etc/nginx/sites-available`). "The `server` context" refers to the part of the configuration that starts with "server {" and ends with the matching "}".
|
||||
|
||||
```nginx
|
||||
# WebP Express rules
|
||||
# --------------------
|
||||
location ~* ^/wp-content/.*\.(png|jpe?g)$ {
|
||||
add_header Vary Accept;
|
||||
expires 365d;
|
||||
}
|
||||
location ~* ^/wp-content/.*\.webp$ {
|
||||
expires 365d;
|
||||
if ($whattodo = AB) {
|
||||
add_header Vary Accept;
|
||||
}
|
||||
}
|
||||
if ($http_accept ~* "webp"){
|
||||
set $whattodo A;
|
||||
}
|
||||
if (-f $request_filename.webp) {
|
||||
set $whattodo "${whattodo}B";
|
||||
}
|
||||
if ($whattodo = AB) {
|
||||
rewrite ^(.*) $1.webp last;
|
||||
}
|
||||
if ($whattodo = A) {
|
||||
rewrite ^/wp-content/.*\.(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content break;
|
||||
}
|
||||
# ------------------- (WebP Express rules ends here)
|
||||
```
|
||||
|
||||
__BEWARE:__
|
||||
|
||||
- Beware that when copy/pasting you might get html-encoded characters. Verify that the ampersand before "wp-content" isn't encoded (in the last line in the try_files block)
|
||||
|
||||
- Beware that the rules looks for existing webp files by appending ".webp" to the URL. So for this to work, you __must__ configure *WebP Express* to store the converted files like that.
|
||||
|
||||
- Beware that if you haven't enabled *png* conversion, you should replace "(png|jpe?g)" with "jpe?g".
|
||||
|
||||
- Beware that if you have moved wp-content to a non-standard place, you must change accordingly. Note that you must then also change the "wp-content" parameter to the script. It expects a relative path to wp-content (from document root) and is needed so the script can find the configuration file.
|
||||
|
||||
- Beware that there is a hack out there for permalinks which is based on "rewrite" (rather than the usual solution which is based on try_files). If you are using that hack to redirect missing files to index.php, you need to modify it as specified [here](https://wordpress.org/support/topic/nginx-server-404-not-found-when-convert-test-images/page/2/#post-11952444)
|
||||
|
||||
- I have put in an expires statement for caching. You might want to modify or disable that.
|
||||
|
||||
- I have not set any expire on the webp-on-demand.php request. This is not needed, as the script sets this according to what you set up in WebP Express settings. Also, trying to do it would require a new location block matching webp-on-demand.php, but that would override the location block handling php files, and thus break the functionality.
|
||||
|
||||
- There is no longer any reason to add "&$args" to the line begining with "/wp-content". It was there to enable debugging a single image by appending "?debug" to the url. I however removed that functionality from `webp-on-demand.php`.
|
||||
|
||||
It is possible to put this stuff inside a `location` directive. However, having `if` directives inside `location` directives [is considered evil](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/). But it seems that in our case, it works. If you wish to do that, use the following rules instead:
|
||||
|
||||
```nginx
|
||||
# WebP Express rules
|
||||
# --------------------
|
||||
location ~* ^/wp-content/.*\.(png|jpe?g)$ {
|
||||
add_header Vary Accept;
|
||||
expires 365d;
|
||||
|
||||
if ($http_accept ~* "webp"){
|
||||
set $whattodo A;
|
||||
}
|
||||
if (-f $request_filename.webp) {
|
||||
set $whattodo "${whattodo}B";
|
||||
}
|
||||
if ($whattodo = AB) {
|
||||
rewrite ^(.*) $1.webp last;
|
||||
}
|
||||
if ($whattodo = A) {
|
||||
rewrite ^/wp-content/.*\.(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content last;
|
||||
}
|
||||
}
|
||||
|
||||
location ~* ^/wp-content/.*\.webp$ {
|
||||
expires 365d;
|
||||
if ($whattodo = AB) {
|
||||
add_header Vary Accept;
|
||||
}
|
||||
}
|
||||
# ------------------- (WebP Express rules ends here)
|
||||
```
|
||||
|
||||
PS: In case you only want to redirect images to the script (and not to existing), the rules becomes much simpler:
|
||||
|
||||
```nginx
|
||||
# WebP Express rules
|
||||
# --------------------
|
||||
if ($http_accept ~* "webp"){
|
||||
rewrite ^/(.*).(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content break;
|
||||
}
|
||||
# ------------------- (WebP Express rules ends here)
|
||||
```
|
||||
|
||||
Discussion on this topic [here](https://wordpress.org/support/topic/nginx-rewrite-rules-4/)
|
||||
And here: https://github.com/rosell-dk/webp-express/issues/166
|
||||
|
||||
Here are rules if you need to *replace* the file extension with ".webp" rather than appending ".webp" to it: https://www.keycdn.com/support/optimus/configuration-to-deliver-webp
|
||||
|
||||
### I am on a Windows server
|
||||
Good news! It should work now, thanks to a guy that calls himself lwxbr. At least on XAMPP 7.3.1, Windows 10. https://github.com/rosell-dk/webp-express/pull/213.
|
||||
|
||||
### I am on a Litespeed server
|
||||
You do not have to do anything special for it to work on a Litespeed server. You should be able to use WebP Express in any operation mode. For best performance, I however recommend that use the *LiteSpeed Cache* plugin for page caching.
|
||||
|
||||
LiteSpeed Cache can be set up to maintain separate page caches for browsers that supports webp and browsers that don't. Through this functionality it is possible to use "Alter HTML" with the option "Replace image URLs" and "Only do the replacements in webp enabled browsers" mode.
|
||||
|
||||
The setup was kindly shared and explained in detail by [@ribeiroeder](https://github.com/ribeiroeder) [here](https://github.com/rosell-dk/webp-express/issues/433)
|
||||
|
||||
### I am using Jetpack
|
||||
If you install Jetpack and enable the "Speed up image load times" then Jetpack will alter the HTML such that images are pointed to their CDN.
|
||||
|
||||
Ie:
|
||||
`<img src="https://example.com/wp-content/uploads/2018/09/architecture.jpg">`
|
||||
|
||||
becomes:
|
||||
`<img src="https://i0.wp.com/example.com/wp-content/uploads/2018/09/architecture.jpg">`
|
||||
|
||||
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 `<picture>` tags, when it detects a webp image in the same folder as an original.
|
||||
|
||||
**There is really no need to do this anymore, because WebP Express is now capable of replacing img tags with picture tags (check out the Alter HTML option)**
|
||||
|
||||
You need:
|
||||
1 x WebP Express
|
||||
1 x ShortPixel
|
||||
|
||||
*1. Setup WebP Express*
|
||||
If you do not want to use serve varied images:
|
||||
- Open WebP Express options
|
||||
- Switch to *CDN friendly* mode.
|
||||
- Set *File extension* to "Set to .webp"
|
||||
- Make sure the *Convert non-existing webp-files upon request to original image* option is enabled
|
||||
|
||||
If you want to *ShortPixel* to create <picture> tags but still want the magic to work on other images (such as images are referenced from CSS or javascript):
|
||||
- Open WebP Express options
|
||||
- Switch to *Varied image responses* mode.
|
||||
- Set *Destination folder* to "Mingled"
|
||||
- Set *File extension* to "Set to .webp"
|
||||
|
||||
*2. Setup ShortPixel*
|
||||
- Install [ShortPixel](https://wordpress.org/plugins/shortpixel-image-optimiser/) the usual way
|
||||
- Get an API key and enter it on the options page.
|
||||
- In *Advanced*, enable the following options:
|
||||
- *Also create WebP versions of the images, for free.*
|
||||
- *Deliver the WebP versions of the images in the front-end*
|
||||
- *Altering the page code, using the <PICTURE> tag syntax*
|
||||
- As there is a limit to how many images you can convert freely with *ShortPixel*, you should disable the following options (also on the *Advanced* screen):
|
||||
- *Automatically optimize images added by users in front end.*
|
||||
- *Automatically optimize Media Library items after they are uploaded (recommended).*
|
||||
|
||||
*3. Visit a page*
|
||||
As there are presumably no webps generated yet, ShortPixel will not generate `<picture>` tags on the first visit. However, the images that are referenced causes the WebP Express *Auto convert* feature to kick in and generate webp images for each image on that page.
|
||||
|
||||
*4. Visit the page again*
|
||||
As *WebP Express* have generated webps in the same folder as the originals, *ShortPixel* detects these, and you should see `<picture>` tags which references the webp's.
|
||||
|
||||
*ShortPixel or Cache Enabler ?*
|
||||
Cache Enabler has the advantage over ShortPixel that the HTML structure remains the same. With ShortPixel, image tags are wrapped in a `<picture>` tag structure, and by doing that, there is a risk of breaking styles.
|
||||
|
||||
Further, Cache Enabler *caches* the HTML. This is good for performance. However, this also locks you to using that plugin for caching. With ShortPixel, you can keep using your favourite caching plugin.
|
||||
|
||||
Cache Enabler will not work if you are caching HTML on a CDN, because the HTML varies depending on the *Accept* header and it doesn't signal this with a Vary:Accept header. You could however add that manually. ShortPixel does not have that issue, as the HTML is the same for all.
|
||||
|
||||
### WebP Express / Cache Enabler setup
|
||||
The WebP Express / Cache Enabler setup is quite potent and very CDN-friendly. *Cache Enabler* is used for generating *and caching* two versions of the HTML (one for webp-enabled browsers and one for webp-disabled browsers)
|
||||
|
||||
The reason for doing this could be:
|
||||
1. You are using a CDN which cannot be configured to work in the "Varied image responses" mode.
|
||||
2. You could tweak your CDN to work in the "Varied image responses" mode, but you would have to do it by using the entire Accept header as key. Doing that would increase the risk of cache MISS, and you therefore decided that do not want to do that.
|
||||
3. You think it is problematic that when a user saves an image, it has the jpg extension, even though it is a webp image.
|
||||
|
||||
You need:
|
||||
1 x WebP Express
|
||||
1 x Cache Enabler
|
||||
|
||||
*1. Setup WebP Express*
|
||||
If you do not want to use serve varied images:
|
||||
- Open WebP Express options
|
||||
- Switch to *CDN friendly* mode.
|
||||
- Set *File extension* to "Set to .webp"
|
||||
- Enable *Alter HTML* and select *Replace image URLs*. It is not absolutely necessary, as Cache Enabler also alters HTML - but there are several reasons to do it. Firstly, *Cache Enabler* doesn't get as many URLs replaced as we do. WebP Express for example also replaces background urls in inline styles. Secondly, *Cache enabler* has [problems in edge cases](https://regexr.com/46isf). Thirdly, WebP Express can be configured to alter HTML to point to corresponding webp images, *before they even exists* which can be used in conjunction with the the *Convert non-existing webp-files upon request* option. And this is smart, because then you don't have trouble with *Cache Enabler* caching HTML which references the original images due to that some images hasn't been converted yet.
|
||||
- If you enabled *Alter HTML*, also enable *Reference webps that hasn't been converted yet* and *Convert non-existing webp-files upon request*
|
||||
- If you did not enable *Alter HTML*, enable *Convert non-existing webp-files upon request to original image*
|
||||
|
||||
If you want to *Cache Enabler* to create <picture> tags but still want the magic to work on other images (such as images are referenced from CSS or javascript):
|
||||
- Open WebP Express options
|
||||
- Switch to *Varied image responses* mode.
|
||||
- Set *Destination folder* to "Mingled"
|
||||
- Set *File extension* to "Set to .webp"
|
||||
- I suggest you enable *Alter HTML* and select *Replace image URLs*. And also enable *Reference webps that hasn't been converted yet* and *Convert non-existing webp-files upon request*.
|
||||
|
||||
*2. Setup Cache Enabler*
|
||||
- Open the options
|
||||
- Enable of the *Create an additional cached version for WebP image support* option
|
||||
|
||||
*3. If you did not enable Alter HTML and Reference webps that hasn't been converted yet: Let rise in a warm place until doubled*
|
||||
*WebP Express* creates *webp* images on need basis. It needs page visits in order to do the conversions . Bulk conversion is on the roadmap, but until then, you need to visit all pages of relevance. You can either do it manually, let your visitors do it (that is: wait a bit), or, if you are on linux, you can use `wget` to grab your website:
|
||||
|
||||
```
|
||||
wget -e robots=off -r -np -w 2 http://www.example.com
|
||||
```
|
||||
|
||||
**flags:**
|
||||
`-e robots=off` makes wget ignore rules in robots.txt
|
||||
`-np` (no-parent) makes wget stay within the boundaries (doesn't go into parent folders)
|
||||
`w 2` Waits two seconds between each request, in order not to stress the server
|
||||
|
||||
*4. Clear the Cache Enabler cache.*
|
||||
Click the "Clear Cache" button in the top right corner in order to clear the Cache Enabler cache.
|
||||
|
||||
*5. Inspect the HTML*
|
||||
When visiting a page with images on, different HTML will be served to browsers, depending on whether they support webp or not.
|
||||
|
||||
In a webp-enabled browser, the HTML may look like this: `<img src="image.webp">`, while in a non-webp enabled browser, it looks like this: `<img src="image.jpg">`
|
||||
|
||||
|
||||
*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 `<picture>` tags syntax, only images inserted with `<img>`-tags will be replaced (CSS images will not be replaced). Additionally, the `<img>`-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. |
|
||||
1081
README.txt
Executable file
1081
README.txt
Executable file
File diff suppressed because it is too large
Load Diff
BIN
assets/banner-772x250.jpg
Normal file
BIN
assets/banner-772x250.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 119 KiB |
BIN
assets/icon-128x128.png
Normal file
BIN
assets/icon-128x128.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
BIN
assets/icon-256x256.png
Normal file
BIN
assets/icon-256x256.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
27
assets/icon.svg
Normal file
27
assets/icon.svg
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
xmlns:cc="http://creativecommons.org/ns#"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
version="1.1"
|
||||
width="150"
|
||||
height="150"
|
||||
id="svg3062"
|
||||
xml:space="preserve"><metadata
|
||||
id="metadata3068"><rdf:RDF><cc:Work
|
||||
rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
|
||||
rdf:resource="http://purl.org/dc/dcmitype/StillImage" /><dc:title></dc:title></cc:Work></rdf:RDF></metadata><defs
|
||||
id="defs3066" /><g
|
||||
transform="matrix(1.25,0,0,-1.25,0,150)"
|
||||
id="g3070"><g
|
||||
id="g3072"><path
|
||||
d="M 0,0 120,0 120,120 0,120 0,0 z"
|
||||
id="path3074"
|
||||
style="fill:#154889;fill-opacity:1;fill-rule:nonzero;stroke:none" /><path
|
||||
d="m 39.695,22.855 -31.695,0 23.77,74.29 10.648,0 -2.723,-74.29 z m 8.914,0 -2.722,74.29 10.648,0 8.008,-25.036 c 6.035,6.606 13.895,11.528 22.473,14.075 l -9.563,8.027 c -0.875,0.773 -0.535,2.473 0.574,2.844 0.172,0.058 0.352,0.09 0.536,0.09 l 31.761,0 c 1.102,0 1.969,-1.313 1.547,-2.325 L 99.566,65.535 c -0.453,-1.078 -2.171,-1.297 -2.875,-0.363 -0.113,0.144 -0.199,0.305 -0.253,0.476 l -4.727,14.106 c -4.535,-1.649 -8.715,-4.25 -11.859,-7.379 -3.145,-3.129 -5.243,-6.773 -5.95,-10.34 l -0.05,-0.277 C 72.445,54.531 73.543,43.988 76.738,34 l 3.567,-11.145 -31.696,0 z"
|
||||
id="path3076"
|
||||
style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:none" /></g></g></svg>
|
||||
|
After Width: | Height: | Size: 1.6 KiB |
BIN
assets/screenshot-1.png
Normal file
BIN
assets/screenshot-1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 77 KiB |
572
changelog.txt
Normal file
572
changelog.txt
Normal file
@ -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
|
||||
34
composer.json
Normal file
34
composer.json
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
111
docs/development.md
Normal file
111
docs/development.md
Normal file
@ -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
|
||||
151
docs/publishing.md
Executable file
151
docs/publishing.md
Executable file
@ -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 ".")
|
||||
154
docs/regex.md
Normal file
154
docs/regex.md
Normal file
@ -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:
|
||||
<img src="header.jpg">
|
||||
<img src="/header.jpg">
|
||||
<img src="http://example.com/header.jpeg" alt="">
|
||||
<img src="http://example.com/header.jpg">
|
||||
<img src="http://example.com/header.jpg"/>
|
||||
<img src = "http://example.com/header.jpg">
|
||||
<img src=http://example.com/header.jpg alt="">
|
||||
<img src=http://example.com/header.jpg>
|
||||
<img src=http://example.com/header.jpg alt="hello">
|
||||
<img src=http://example.com/header.jpg />
|
||||
<img src=http://example.com/header_.jpg/>
|
||||
<picture><source src="http://example.com/header.jpg"><img src="http://example.com/header.jpg"></picture>
|
||||
<input type="image" src="http://example.com/flamingo.jpg">
|
||||
<iframe src="http://example.com/image.jpg"></iframe>
|
||||
|
||||
|
||||
|
||||
In srcset, the whole attribute must be matched
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header.jpg 1000w">
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header.jpg 1000w,http://example.com/header.jpg 1000w, http://example.com/header.jpg 2000w">
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header-150x150.jpg 500w,http://example.com/header.jpg-300x300.jpg" sizes="(max-width: 480px) 100vw, (max-width: 900px) 33vw, 254px" alt="" width="100" height="100">
|
||||
|
||||
Common lazy load attributes are matched:
|
||||
<img data-cvpsrc="http://example.com/header.jpg">
|
||||
<img data-cvpset="http://example.com/header.jpg">
|
||||
<img data-thumb="http://example.com/header.jpg">
|
||||
<img data-bg-url="http://example.com/header.jpg">
|
||||
<img data-large_image="http://example.com/header.jpg">
|
||||
<img data-lazyload="http://example.com/header.jpg">
|
||||
<img data-source-url="http://example.com/header.jpg">
|
||||
<img data-srcsmall="http://example.com/header.jpg">
|
||||
<img data-srclarge="http://example.com/header.jpg">
|
||||
<img data-srcfull="http://example.com/header.jpg">
|
||||
<img data-slide-img="http://example.com/header.jpg">
|
||||
<img data-lazy-original="http://example.com/header.jpg">
|
||||
|
||||
|
||||
The following should NOT produce matches:
|
||||
-----------------------------------------
|
||||
|
||||
Ignore URLs with query string:
|
||||
<img src="http://example.com/header.jpg?width=200">
|
||||
|
||||
<img src="http://example.com/tegning.jpg.webp" alt="">
|
||||
<img src="http://example.com/tegning.jpglidilo" alt="">
|
||||
<img src="http://example.com/header.jpg/hi-res">
|
||||
<img src=http://example.com/header.gif alt=nice-jpg>
|
||||
<img src="http://example.com/tegning.webp" alt="">
|
||||
src="http://example.com/header.jpeg"
|
||||
<article data-src="http://example.com/header.jpg" />
|
||||
<img><script src="http://example.com/script.js?preload=image.jpg">
|
||||
|
||||
|
||||
I use another pattern for matching image urls in styles: https://regexr.com/46jcg
|
||||
|
||||
It matches stuff like this:
|
||||
<div style="background-image: url('http://example.com/image.png'), url("/image2.jpeg", url(http://example.com/image3.jpg);"></div>
|
||||
<div style="background: url ("http://example.com/image2.jpg")"></div>
|
||||
<style>#myphoto {background: url("http://example.com/image2.jpg")}</style>
|
||||
|
||||
I have another pattern where we allow QS here: https://regexr.com/46ivi
|
||||
|
||||
PS: The rules are used for the WebP Express plugin for Wordpress
|
||||
|
||||
PPS: This regex is used in WPFastestCache (not just images)
|
||||
// $content = preg_replace_callback("/(srcset|src|href|data-cvpsrc|data-cvpset|data-thumb|data-bg-url|data-large_image|data-lazyload|data-source-url|data-srcsmall|data-srclarge|data-srcfull|data-slide-img|data-lazy-original)\s{0,2}\=[\'\"]([^\'\"]+)[\'\"]/i", array($this, 'cdn_replace_urls'), $content);
|
||||
|
||||
PPPS:
|
||||
As we are limiting to a few tags (img, source, input, etc), and only match image urls ending with (png|jpe?g), I deem it ok to match in ANY "data-" attribute.
|
||||
But if you want to limit it to attributes that smells like they are used for images you can do this:
|
||||
(src|srcset|data-[^=]*(lazy|small|slide|img|large|src|thumb|source|set|bg-url)[^=]*)
|
||||
That will catch the following known and more: data-cvpsrc|data-cvpset|data-thumb|data-bg-url|data-large_image|data-lazyload|data-source-url|data-srcsmall|data-srclarge|data-srcfull|data-slide-img|data-lazy-original
|
||||
|
||||
|
||||
# in style
|
||||
|
||||
*pattern:*
|
||||
((?<=(?:((style\s*=)|(\<\s*style)).*background(-image)?\s*:\s*url\s*\([\"\']?)|(((style\s*=)|(\<\s*style)).*url.*,\s*url\([\"\']?))[^\"\']*\.(jpe?g|png))(?=[\"\'\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:
|
||||
|
||||
<style>#myphoto {background: url("http://example.com/image2.jpg")}</style>
|
||||
<div style="background-image: url('http://example.com/image.png'), url("/image2.jpeg"), url(http://example.com/image3.jpg);"></div>
|
||||
<div style="background: url ("http://example.com/image2.jpg")"></div>
|
||||
<style>#myphoto {background: url("http://example.com/image2.jpg"), url("image2.jpeg"}</style>
|
||||
|
||||
Not these:
|
||||
----------
|
||||
|
||||
GIFs are disallowed:
|
||||
<div style="background-image: url("http://example.com/image.gif"), url("http://example.com/image2.gif", url("image3.gif");"></div>
|
||||
|
||||
Querystrings are disallowed:
|
||||
<div style="background-image: url('http://example.com/image.jpg?no-qs!')"></div>
|
||||
|
||||
HTML attributes disallowed:
|
||||
<img src="header.jpg">
|
||||
|
||||
Go with style: background: url("http://example.com/image2.jpg")
|
||||
|
||||
|
||||
And none of this either:
|
||||
|
||||
<div style="background-image: url('http://example.com/image.jpgelegi')"></div>
|
||||
<img src="header.jpg">
|
||||
<img src="/header.jpg">
|
||||
<img src="http://example.com/header.jpeg" alt="">
|
||||
<img src="http://example.com/header.jpg">
|
||||
<img src="http://example.com/header.jpg"/>
|
||||
<img src = "http://example.com/header.jpg">
|
||||
<img src=http://example.com/header.jpg alt="">
|
||||
<img src=http://example.com/header.jpg>
|
||||
<img src=http://example.com/header.jpg alt="hello">
|
||||
<img src=http://example.com/header.jpg />
|
||||
<img src=http://example.com/header_.jpg/>
|
||||
<picture><source src="http://example.com/header.jpg"><img src="http://example.com/header.jpg"></picture>
|
||||
<input type="image" src="http://example.com/flamingo.jpg">
|
||||
<iframe src="http://example.com/image.jpg"></iframe>
|
||||
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header.jpg 1000w">
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header.jpg 1000w,http://example.com/header.jpg 1000w, http://example.com/header.jpg 2000w">
|
||||
<img src="http://example.com/header.jpg" srcset="http://example.com/header-150x150.jpg 500w,http://example.com/header.jpg-300x300.jpg" sizes="(max-width: 480px) 100vw, (max-width: 900px) 33vw, 254px" alt="" width="100" height="100">
|
||||
|
||||
<img src="http://example.com/tegning.jpg.webp" alt="">
|
||||
<img src="http://example.com/tegning.jpglidilo" alt="">
|
||||
<img src="http://example.com/header.jpg/hi-res">
|
||||
<img src=http://example.com/header.gif alt=nice-jpg>
|
||||
<img src="http://example.com/tegning.webp" alt="">
|
||||
src="http://example.com/header.jpeg"
|
||||
<article data-src="http://example.com/header.jpg" />
|
||||
<img><script src="http://example.com/script.js?preload=image.jpg">
|
||||
|
||||
|
||||
I use another pattern for matching image urls in HTML attributes:
|
||||
https://regexr.com/46jat
|
||||
17
js/0.16.0/plugin-page.js
Normal file
17
js/0.16.0/plugin-page.js
Normal file
@ -0,0 +1,17 @@
|
||||
(function ($) {
|
||||
$(document).ready(function () {
|
||||
/*
|
||||
TODO:
|
||||
Add link to Optimole when "deactivate" link is clicked / uninstalled #356
|
||||
https://github.com/rosell-dk/webp-express/issues/356
|
||||
|
||||
var linkEl = $('tr[data-plugin^="webp-express/"] span.deactivate a');
|
||||
//console.log(el);
|
||||
linkEl.on('click', function (e) {
|
||||
//e.preventDefault();
|
||||
//e.stopPropagation();
|
||||
//alert('NO!');
|
||||
});
|
||||
*/
|
||||
});
|
||||
})(jQuery);
|
||||
5
js/picturefill.min.js
vendored
Executable file
5
js/picturefill.min.js
vendored
Executable file
File diff suppressed because one or more lines are too long
77
lib/alter-html.php
Normal file
77
lib/alter-html.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
function webPExpressAlterHtml($content) {
|
||||
// Don't do anything with the RSS feed.
|
||||
if (is_feed()) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
if (get_option('webp-express-alter-html-replacement') == 'picture') {
|
||||
if(function_exists('is_amp_endpoint') && is_amp_endpoint()) {
|
||||
//for AMP pages the <picture> tag is not allowed
|
||||
return $content;
|
||||
}
|
||||
/*if (is_admin() ) {
|
||||
return $content;
|
||||
}*/
|
||||
require_once __DIR__ . '/classes/AlterHtmlPicture.php';
|
||||
return \WebPExpress\AlterHtmlPicture::alter($content);
|
||||
} else {
|
||||
require_once __DIR__ . '/classes/AlterHtmlImageUrls.php';
|
||||
return \WebPExpress\AlterHtmlImageUrls::alter($content);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function webpExpressOutputBuffer() {
|
||||
if (!is_admin() || (function_exists("wp_doing_ajax") && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)) {
|
||||
ob_start('webPExpressAlterHtml');
|
||||
}
|
||||
}
|
||||
|
||||
function webpExpressAddPictureJs() {
|
||||
// Don't do anything with the RSS feed.
|
||||
// - and no need for PictureJs in the admin
|
||||
if ( is_feed() || is_admin() ) { return; }
|
||||
|
||||
echo '<script>'
|
||||
. 'document.createElement( "picture" );'
|
||||
. 'if(!window.HTMLPictureElement && document.addEventListener) {'
|
||||
. 'window.addEventListener("DOMContentLoaded", function() {'
|
||||
. 'var s = document.createElement("script");'
|
||||
. 's.src = "' . plugins_url('/js/picturefill.min.js', __FILE__) . '";'
|
||||
. 'document.body.appendChild(s);'
|
||||
. '});'
|
||||
. '}'
|
||||
. '</script>';
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
46
lib/classes/Actions.php
Normal file
46
lib/classes/Actions.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
use \WebPExpress\State;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class Actions
|
||||
{
|
||||
/**
|
||||
* $action: identifier
|
||||
*/
|
||||
public static function procastinate($action) {
|
||||
Option::updateOption('webp-express-actions-pending', true, true);
|
||||
|
||||
$pendingActions = State::getState('pendingActions', []);
|
||||
$pendingActions[] = $action;
|
||||
State::setState('pendingActions', $pendingActions);
|
||||
}
|
||||
|
||||
public static function takeAction($action) {
|
||||
switch ($action) {
|
||||
case 'deactivate':
|
||||
add_action('admin_init', function () {
|
||||
deactivate_plugins(plugin_basename(WEBPEXPRESS_PLUGIN));
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static function processQueuedActions() {
|
||||
$actions = State::getState('pendingActions', []);
|
||||
|
||||
foreach ($actions as $action) {
|
||||
self::takeAction($action);
|
||||
}
|
||||
|
||||
State::setState('pendingActions', []);
|
||||
Option::updateOption('webp-express-actions-pending', false, true);
|
||||
|
||||
}
|
||||
}
|
||||
147
lib/classes/AdminInit.php
Normal file
147
lib/classes/AdminInit.php
Normal file
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class AdminInit
|
||||
{
|
||||
public static function init() {
|
||||
|
||||
// uncomment next line to debug an error during activation
|
||||
//include __DIR__ . "/../debug.php";
|
||||
|
||||
if (Option::getOption('webp-express-actions-pending')) {
|
||||
\WebPExpress\Actions::processQueuedActions();
|
||||
}
|
||||
|
||||
self::addHooks();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static function runMigrationIfNeeded()
|
||||
{
|
||||
// When an update requires a migration, the number should be increased
|
||||
define('WEBPEXPRESS_MIGRATION_VERSION', '14');
|
||||
|
||||
if (WEBPEXPRESS_MIGRATION_VERSION != Option::getOption('webp-express-migration-version', 0)) {
|
||||
// run migration logic
|
||||
include WEBPEXPRESS_PLUGIN_DIR . '/lib/migrate/migrate.php';
|
||||
}
|
||||
|
||||
// uncomment next line to test-run a migration
|
||||
//include WEBPEXPRESS_PLUGIN_DIR . '/lib/migrate/migrate14.php';
|
||||
}
|
||||
|
||||
public static function pageNowIs($pageId)
|
||||
{
|
||||
global $pagenow;
|
||||
|
||||
if ((!isset($pagenow)) || (empty($pagenow))) {
|
||||
return false;
|
||||
}
|
||||
return ($pageId == $pagenow);
|
||||
}
|
||||
|
||||
|
||||
public static function addHooksAfterAdminInit()
|
||||
{
|
||||
|
||||
if (current_user_can('manage_options')) {
|
||||
|
||||
// Hooks related to conversion page (in media)
|
||||
//if (self::pageNowIs('upload.php')) {
|
||||
if (isset($_GET['page']) && ('webp_express_conversion_page' === $_GET['page'])) {
|
||||
//add_action('admin_enqueue_scripts', array('\WebPExpress\WCFMPage', 'enqueueScripts'));
|
||||
add_action('admin_head', array('\WebPExpress\WCFMPage', 'addToHead'));
|
||||
}
|
||||
//}
|
||||
|
||||
// Hooks related to options page
|
||||
if (self::pageNowIs('options-general.php') || self::pageNowIs('settings.php')) {
|
||||
if (isset($_GET['page']) && ('webp_express_settings_page' === $_GET['page'])) {
|
||||
add_action('admin_enqueue_scripts', array('\WebPExpress\OptionsPage', 'enqueueScripts'));
|
||||
}
|
||||
}
|
||||
|
||||
// Hooks related to plugins page
|
||||
if (self::pageNowIs('plugins.php')) {
|
||||
add_action('admin_enqueue_scripts', array('\WebPExpress\PluginPageScript', 'enqueueScripts'));
|
||||
}
|
||||
|
||||
add_action("admin_post_webpexpress_settings_submit", array('\WebPExpress\OptionsPageHooks', 'submitHandler'));
|
||||
|
||||
|
||||
// Ajax actions
|
||||
add_action('wp_ajax_list_unconverted_files', array('\WebPExpress\BulkConvert', 'processAjaxListUnconvertedFiles'));
|
||||
add_action('wp_ajax_convert_file', array('\WebPExpress\Convert', 'processAjaxConvertFile'));
|
||||
add_action('wp_ajax_webpexpress_view_log', array('\WebPExpress\ConvertLog', 'processAjaxViewLog'));
|
||||
add_action('wp_ajax_webpexpress_purge_cache', array('\WebPExpress\CachePurge', 'processAjaxPurgeCache'));
|
||||
add_action('wp_ajax_webpexpress_purge_log', array('\WebPExpress\LogPurge', 'processAjaxPurgeLog'));
|
||||
add_action('wp_ajax_webpexpress_dismiss_message', array('\WebPExpress\DismissableMessages', 'processAjaxDismissMessage'));
|
||||
add_action('wp_ajax_webpexpress_dismiss_global_message', array('\WebPExpress\DismissableGlobalMessages', 'processAjaxDismissGlobalMessage'));
|
||||
add_action('wp_ajax_webpexpress_self_test', array('\WebPExpress\SelfTest', 'processAjax'));
|
||||
add_action('wp_ajax_webpexpress-wcfm-api', array('\WebPExpress\WCFMApi', 'processRequest'));
|
||||
|
||||
|
||||
// Add settings link on the plugins list page
|
||||
add_filter('plugin_action_links_' . plugin_basename(WEBPEXPRESS_PLUGIN), array('\WebPExpress\AdminUi', 'pluginActionLinksFilter'), 10, 2);
|
||||
|
||||
// Add settings link in multisite
|
||||
add_filter('network_admin_plugin_action_links_' . plugin_basename(WEBPEXPRESS_PLUGIN), array('\WebPExpress\AdminUi', 'networkPluginActionLinksFilter'), 10, 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function addHooks()
|
||||
{
|
||||
|
||||
// Plugin activation, deactivation and uninstall
|
||||
register_activation_hook(WEBPEXPRESS_PLUGIN, array('\WebPExpress\PluginActivate', 'activate'));
|
||||
register_deactivation_hook(WEBPEXPRESS_PLUGIN, array('\WebPExpress\PluginDeactivate', 'deactivate'));
|
||||
register_uninstall_hook(WEBPEXPRESS_PLUGIN, array('\WebPExpress\PluginUninstall', 'uninstall'));
|
||||
|
||||
/*$start = microtime(true);
|
||||
BiggerThanSourceDummyFilesBulk::updateStatus(Config::loadConfig());
|
||||
echo microtime(true) - $start;*/
|
||||
|
||||
|
||||
// Some hooks must be registered AFTER admin_init...
|
||||
add_action("admin_init", array('\WebPExpress\AdminInit', 'addHooksAfterAdminInit'));
|
||||
|
||||
// Run migration AFTER admin_init hook (important, as insert_with_markers injection otherwise fails, see #394)
|
||||
// PS: "plugins_loaded" is to early, as insert_with_markers fails.
|
||||
// PS: Unfortunately Message::addMessage doesnt print until next load now, we should look into that.
|
||||
// PPS: It does run. It must be the Option that does not react
|
||||
//add_action("admin_init", array('\WebPExpress\AdminInit', 'runMigrationIfNeeded'));
|
||||
|
||||
add_action("admin_init", array('\WebPExpress\AdminInit', 'runMigrationIfNeeded'));
|
||||
|
||||
add_action("admin_notices", array('\WebPExpress\DismissableGlobalMessages', 'printMessages'));
|
||||
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
if (is_network_admin()) {
|
||||
add_action("network_admin_menu", array('\WebPExpress\AdminUi', 'networAdminMenuHook'));
|
||||
} else {
|
||||
add_action("admin_menu", array('\WebPExpress\AdminUi', 'adminMenuHookMultisite'));
|
||||
}
|
||||
|
||||
} else {
|
||||
add_action("admin_menu", array('\WebPExpress\AdminUi', 'adminMenuHook'));
|
||||
}
|
||||
|
||||
// Print pending messages, if any
|
||||
if (Option::getOption('webp-express-messages-pending')) {
|
||||
add_action(Multisite::isNetworkActivated() ? 'network_admin_notices' : 'admin_notices', array('\WebPExpress\Messenger', 'printPendingMessages'));
|
||||
}
|
||||
|
||||
|
||||
// PS:
|
||||
// Filters for processing upload hooks in order to convert images upon upload (wp_handle_upload / image_make_intermediate_size)
|
||||
// are located in webp-express.php
|
||||
|
||||
}
|
||||
}
|
||||
106
lib/classes/AdminUi.php
Normal file
106
lib/classes/AdminUi.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Multisite;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class AdminUi
|
||||
{
|
||||
|
||||
// Add settings link on the plugins page
|
||||
// The hook was registred in AdminInit
|
||||
public static function pluginActionLinksFilter($links)
|
||||
{
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
$mylinks= [
|
||||
'<a href="https://ko-fi.com/rosell" target="_blank">donate?</a>',
|
||||
];
|
||||
} else {
|
||||
$mylinks = array(
|
||||
'<a href="' . admin_url('options-general.php?page=webp_express_settings_page') . '">Settings</a>',
|
||||
'<a href="https://wordpress.org/plugins/webp-express/#%0Ahow%20do%20i%20buy%20you%20a%20cup%20of%20coffee%3F%0A" target="_blank">Provide coffee for the developer</a>',
|
||||
);
|
||||
|
||||
}
|
||||
return array_merge($links, $mylinks);
|
||||
}
|
||||
|
||||
// Add settings link in multisite
|
||||
// The hook was registred in AdminInit
|
||||
public static function networkPluginActionLinksFilter($links)
|
||||
{
|
||||
$mylinks = array(
|
||||
'<a href="' . network_admin_url('settings.php?page=webp_express_settings_page') . '">Settings</a>',
|
||||
'<a href="https://ko-fi.com/rosell" target="_blank">donate?</a>',
|
||||
);
|
||||
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.
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
377
lib/classes/AlterHtmlHelper.php
Normal file
377
lib/classes/AlterHtmlHelper.php
Normal file
@ -0,0 +1,377 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
//use AlterHtmlInit;
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\Paths;
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\Multisite;
|
||||
use \WebPExpress\Option;
|
||||
|
||||
class AlterHtmlHelper
|
||||
{
|
||||
|
||||
public static $options;
|
||||
/*
|
||||
public static function hasWebP($src)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function inUploadDir($src)
|
||||
{
|
||||
$upload_dir = wp_upload_dir();
|
||||
$src_url = parse_url($upload_dir['baseurl']);
|
||||
$upload_path = $src_url['path'];
|
||||
|
||||
return (strpos($src, $upload_path) !== false );
|
||||
|
||||
}
|
||||
|
||||
public static function checkSrc($src)
|
||||
{
|
||||
self::$options = \WebPExpress\AlterHtmlInit::self::$options();
|
||||
|
||||
|
||||
if (self::$options['destination-folder'] == 'mingled') {
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static function getOptions() {
|
||||
if (!isset(self::$options)) {
|
||||
self::$options = json_decode(Option::getOption('webp-express-alter-html-options', null), true);
|
||||
if (!isset(self::$options['prevent-using-webps-larger-than-original'])) {
|
||||
self::$options['prevent-using-webps-larger-than-original'] = true;
|
||||
}
|
||||
// Set scope if it isn't there (it wasn't cached until 0.17.5)
|
||||
if (!isset(self::$options['scope'])) {
|
||||
$config = Config::loadConfig();
|
||||
if ($config) {
|
||||
$config = Config::fix($config, false);
|
||||
self::$options['scope'] = $config['scope'];
|
||||
|
||||
Option::updateOption(
|
||||
'webp-express-alter-html-options',
|
||||
json_encode(self::$options, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets relative path between a base url and another.
|
||||
* Returns false if the url isn't a subpath
|
||||
*
|
||||
* @param $imageUrl (ie "http://example.com/wp-content/image.jpg")
|
||||
* @param $baseUrl (ie "http://example.com/wp-content")
|
||||
* @return path or false (ie "/image.jpg")
|
||||
*/
|
||||
public static function getRelUrlPath($imageUrl, $baseUrl)
|
||||
{
|
||||
$baseUrlComponents = parse_url($baseUrl);
|
||||
/* ie:
|
||||
(
|
||||
[scheme] => 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);
|
||||
}*/
|
||||
|
||||
}
|
||||
33
lib/classes/AlterHtmlImageUrls.php
Normal file
33
lib/classes/AlterHtmlImageUrls.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\AlterHtmlInit;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
/**
|
||||
* Class AlterHtmlImageUrls - convert image urls to webp
|
||||
* Based this code on code from the Cache Enabler plugin
|
||||
*/
|
||||
|
||||
use \WebPExpress\AlterHtmlHelper;
|
||||
//use \WebPExpress\ImageUrlsReplacer;
|
||||
use DOMUtilForWebP\ImageUrlReplacer;
|
||||
|
||||
class AlterHtmlImageUrls extends ImageUrlReplacer
|
||||
{
|
||||
public function replaceUrl($url) {
|
||||
return AlterHtmlHelper::getWebPUrl($url, null);
|
||||
}
|
||||
|
||||
public function attributeFilter($attrName) {
|
||||
// Allow "src", "srcset" and data-attributes that smells like they are used for images
|
||||
// The following rule matches all attributes used for lazy loading images that we know of
|
||||
return preg_match('#^(src|srcset|poster|(data-[^=]*(lazy|small|slide|img|large|src|thumb|source|set|bg-url)[^=]*))$#i', $attrName);
|
||||
|
||||
// If you want to limit it further, only allowing attributes known to be used for lazy load,
|
||||
// use the following regex instead:
|
||||
//return preg_match('#^(src|srcset|data-(src|srcset|cvpsrc|cvpset|thumb|bg-url|large_image|lazyload|source-url|srcsmall|srclarge|srcfull|slide-img|lazy-original))$#i', $attrName);
|
||||
}
|
||||
|
||||
}
|
||||
154
lib/classes/AlterHtmlInit.php
Normal file
154
lib/classes/AlterHtmlInit.php
Normal file
@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use AlterHtmlHelper;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
|
||||
class AlterHtmlInit
|
||||
{
|
||||
public static $options = null;
|
||||
|
||||
public static function startOutputBuffer()
|
||||
{
|
||||
if (!is_admin() || (function_exists("wp_doing_ajax") && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)) {
|
||||
// note: "self::alterHtml" does for some reason not work on hhvm (#226)
|
||||
ob_start('\\WebPExpress\\AlterHtmlInit::alterHtml');
|
||||
}
|
||||
}
|
||||
|
||||
public static function alterHtml($content)
|
||||
{
|
||||
// Don't do anything with the RSS feed.
|
||||
if (is_feed()) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
if (is_admin()) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// Exit if it doesn't look like HTML (see #228)
|
||||
if (!preg_match("#^\\s*<#", $content)) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
if (Option::getOption('webp-express-alter-html-replacement') == 'picture') {
|
||||
if(function_exists('is_amp_endpoint') && is_amp_endpoint()) {
|
||||
//for AMP pages the <picture> 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 '<script>'
|
||||
. 'document.createElement( "picture" );'
|
||||
. 'if(!window.HTMLPictureElement && document.addEventListener) {'
|
||||
. 'window.addEventListener("DOMContentLoaded", function() {'
|
||||
. 'var s = document.createElement("script");'
|
||||
. 's.src = "' . plugins_url('/js/picturefill.min.js', WEBPEXPRESS_PLUGIN) . '";'
|
||||
. 'document.body.appendChild(s);'
|
||||
. '});'
|
||||
. '}'
|
||||
. '</script>';
|
||||
}
|
||||
|
||||
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 );
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
18
lib/classes/AlterHtmlPicture.php
Normal file
18
lib/classes/AlterHtmlPicture.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/**
|
||||
* Class AlterHtmlPicture - convert an <img> tag to a <picture> tag and add the webp versions of the images
|
||||
* Based this code on code from the ShortPixel plugin, which used code from Responsify WP plugin
|
||||
*/
|
||||
|
||||
use \WebPExpress\AlterHtmlHelper;
|
||||
use DOMUtilForWebP\PictureTags;
|
||||
|
||||
class AlterHtmlPicture extends PictureTags
|
||||
{
|
||||
public function replaceUrl($url) {
|
||||
return AlterHtmlHelper::getWebPUrl($url, null);
|
||||
}
|
||||
}
|
||||
32
lib/classes/BiggerThanSource.php
Normal file
32
lib/classes/BiggerThanSource.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This class is made to not be dependent on Wordpress functions and must be kept like that.
|
||||
It is used by webp-on-demand.php. It is also used for bulk conversion.
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
|
||||
class BiggerThanSource
|
||||
{
|
||||
/**
|
||||
* Check if webp is bigger than original.
|
||||
*
|
||||
* @return boolean|null True if it is bigger than original, false if not. NULL if it cannot be determined
|
||||
*/
|
||||
public static function bigger($source, $destination)
|
||||
{
|
||||
if ((!@file_exists($source)) || (!@file_exists($destination))) {
|
||||
return null;
|
||||
}
|
||||
$filesizeDestination = @filesize($destination);
|
||||
$filesizeSource = @filesize($source);
|
||||
|
||||
// sizes are FALSE on failure (ie if file does not exists)
|
||||
if (($filesizeSource === false) || ($filesizeDestination === false)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ($filesizeDestination > $filesizeSource);
|
||||
}
|
||||
}
|
||||
135
lib/classes/BiggerThanSourceDummyFiles.php
Normal file
135
lib/classes/BiggerThanSourceDummyFiles.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This class is made to not be dependent on Wordpress functions and must be kept like that.
|
||||
It is used by webp-on-demand.php. It is also used for bulk conversion.
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
|
||||
class BiggerThanSourceDummyFiles
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 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 createBiggerThanSourceBaseDir($dir)
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
@mkdir($dir, 0775, true);
|
||||
@chmod($dir, 0775);
|
||||
@file_put_contents(rtrim($dir . '/') . '/.htaccess', <<<APACHE
|
||||
<IfModule mod_authz_core.c>
|
||||
Require all denied
|
||||
</IfModule>
|
||||
<IfModule !mod_authz_core.c>
|
||||
Order deny,allow
|
||||
Deny from all
|
||||
</IfModule>
|
||||
APACHE
|
||||
);
|
||||
@chmod($dir . '/.htaccess', 0664);
|
||||
}
|
||||
return is_dir($dir);
|
||||
}
|
||||
|
||||
public static function pathToDummyFile($source, $basedir, $imageRoots, $destinationFolder, $destinationExt)
|
||||
{
|
||||
$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) {
|
||||
$rootPath = $imageRoot->getAbsPath();
|
||||
|
||||
// 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 = ConvertHelperIndependent::appendOrSetExtension($relPath, $destinationFolder, $destinationExt, false);
|
||||
|
||||
return $basedir . '/' . $imageRoot->id . '/' . $relPath;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function pathToDummyFileRootAndRelKnown($source, $basedir, $rootId, $destinationFolder, $destinationExt)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if webp is bigger than original.
|
||||
*
|
||||
* @return boolean|null True if it is bigger than original, false if not. NULL if it cannot be determined
|
||||
*/
|
||||
public static function bigger($source, $destination)
|
||||
{
|
||||
/*
|
||||
if ((!@file_exists($source)) || (!@file_exists($destination) {
|
||||
return null;
|
||||
}*/
|
||||
$filesizeDestination = @filesize($destination);
|
||||
$filesizeSource = @filesize($source);
|
||||
|
||||
// sizes are FALSE on failure (ie if file does not exists)
|
||||
if (($filesizeDestination === false) || ($filesizeDestination === false)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ($filesizeDestination > $filesizeSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the status for a single image (when rootId is unknown)
|
||||
*
|
||||
* Checks if webp is bigger than original. If it is, a dummy file is placed. Otherwise, it is
|
||||
* removed (if exists)
|
||||
*
|
||||
* @param string $source Path to the source file that was converted
|
||||
*
|
||||
*
|
||||
*/
|
||||
public static function updateStatus($source, $destination, $webExpressContentDirAbs, $imageRoots, $destinationFolder, $destinationExt)
|
||||
{
|
||||
$basedir = $webExpressContentDirAbs . '/webp-images-bigger-than-source';
|
||||
if (!file_exists($basedir)) {
|
||||
self::createBiggerThanSourceBaseDir($basedir);
|
||||
}
|
||||
$bigWebP = BiggerThanSource::bigger($source, $destination);
|
||||
|
||||
$file = self::pathToDummyFile($source, $basedir, $imageRoots, $destinationFolder, $destinationExt);
|
||||
if ($file === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($bigWebP === true) {
|
||||
// place dummy file, which marks that webp is bigger than source
|
||||
|
||||
$folder = @dirname($file);
|
||||
if (!@file_exists($folder)) {
|
||||
mkdir($folder, 0777, true);
|
||||
}
|
||||
if (@file_exists($folder)) {
|
||||
file_put_contents($file, '');
|
||||
}
|
||||
|
||||
} else {
|
||||
// remove dummy file (if exists)
|
||||
if (@file_exists($file)) {
|
||||
@unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
120
lib/classes/BiggerThanSourceDummyFilesBulk.php
Normal file
120
lib/classes/BiggerThanSourceDummyFilesBulk.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
|
||||
class BiggerThanSourceDummyFilesBulk
|
||||
{
|
||||
|
||||
private static $settings;
|
||||
|
||||
/**
|
||||
* Update the status for a all images.
|
||||
*
|
||||
*/
|
||||
public static function updateStatus($config = null)
|
||||
{
|
||||
if (is_null($config)) {
|
||||
$config = Config::loadConfigAndFix(false);
|
||||
}
|
||||
self::$settings = [
|
||||
'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()
|
||||
'imageRoots' => new ImageRoots(Paths::getImageRootsDefForSelectedIds(Paths::getImageRootIds())), // (Paths::getImageRootsDef()
|
||||
'image-types' => $config['image-types'],
|
||||
];
|
||||
|
||||
|
||||
//$rootIds = Paths::filterOutSubRoots($config['scope']);
|
||||
|
||||
// We want to update status on ALL root dirs (so we don't have to re-run when user changes scope)
|
||||
$rootIds = Paths::filterOutSubRoots(Paths::getImageRootIds());
|
||||
//$rootIds = ['uploads'];
|
||||
//$rootIds = ['uploads', 'themes'];
|
||||
|
||||
foreach ($rootIds as $rootId) {
|
||||
self::updateStatusForRoot($rootId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-requirement: self::$settings is set.
|
||||
*
|
||||
* Idea for improvement: Traverse destination dirs instead. This will be quicker, as there will not be
|
||||
* as many images (unless all have been converted), and not as many folders (non-image folders will not be present.
|
||||
* however, index does not take too long to traverse, even though it has many non-image folders, so it will only
|
||||
* be a problem if there are plugins or themes with extremely many folders).
|
||||
*/
|
||||
private static function updateStatusForRoot($rootId, $dir = '')
|
||||
{
|
||||
if ($dir == '') {
|
||||
$dir = Paths::getAbsDirById($rootId);
|
||||
}
|
||||
|
||||
// Canonicalize because dir might contain "/./", which causes file_exists to fail (#222)
|
||||
$dir = PathHelper::canonicalize($dir);
|
||||
|
||||
if (!@file_exists($dir) || !@is_dir($dir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$fileIterator = new \FilesystemIterator($dir);
|
||||
|
||||
$results = [];
|
||||
|
||||
while ($fileIterator->valid()) {
|
||||
$filename = $fileIterator->getFilename();
|
||||
|
||||
if (($filename != ".") && ($filename != "..")) {
|
||||
if (@is_dir($dir . "/" . $filename)) {
|
||||
$newDir = $dir . "/" . $filename;
|
||||
|
||||
// The new dir might have its own root id
|
||||
$newRootId = Paths::findImageRootOfPath($newDir, Paths::getImageRootIds());
|
||||
//echo $newRootId . ': ' . $newDir . "\n";
|
||||
self::updateStatusForRoot($newRootId, $newDir);
|
||||
} else {
|
||||
// its a file - check if its a valid image type (jpeg or png)
|
||||
$regex = '#\.(jpe?g|png)$#';
|
||||
if (preg_match($regex, $filename)) {
|
||||
|
||||
$source = $dir . "/" . $filename;
|
||||
|
||||
$destination = ConvertHelperIndependent::getDestination(
|
||||
$source,
|
||||
self::$settings['destination-folder'],
|
||||
self::$settings['ext'],
|
||||
self::$settings['webExpressContentDirAbs'],
|
||||
self::$settings['uploadDirAbs'],
|
||||
self::$settings['useDocRootForStructuringCacheDir'],
|
||||
self::$settings['imageRoots'],
|
||||
//$rootId
|
||||
|
||||
);
|
||||
$webpExists = @file_exists($destination);
|
||||
|
||||
//echo ($webpExists ? 'YES' : 'NO') . ' ' . $rootId . ': ' . $source . "\n";
|
||||
|
||||
BiggerThanSourceDummyFiles::updateStatus(
|
||||
$source,
|
||||
$destination,
|
||||
self::$settings['webExpressContentDirAbs'],
|
||||
self::$settings['imageRoots'],
|
||||
self::$settings['destination-folder'],
|
||||
self::$settings['ext'],
|
||||
// TODO: send rootId so the function doesn't need to try all
|
||||
// $rootId,
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
$fileIterator->next();
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
337
lib/classes/BulkConvert.php
Normal file
337
lib/classes/BulkConvert.php
Normal file
@ -0,0 +1,337 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
//use \Onnov\DetectEncoding\EncodingDetector;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\ConvertHelperIndependent;
|
||||
use \WebPExpress\ImageRoots;
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
class BulkConvert
|
||||
{
|
||||
|
||||
public static function defaultListOptions($config)
|
||||
{
|
||||
return [
|
||||
//'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' => true,
|
||||
'image-types' => $config['image-types'],
|
||||
'max-depth' => 100,
|
||||
],
|
||||
'flattenList' => true,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get grouped list of files. They are grouped by image roots.
|
||||
*
|
||||
*/
|
||||
public static function getList($config, $listOptions = null)
|
||||
{
|
||||
|
||||
/*
|
||||
isUploadDirMovedOutOfWPContentDir
|
||||
isUploadDirMovedOutOfAbsPath
|
||||
isPluginDirMovedOutOfAbsPath
|
||||
isPluginDirMovedOutOfWpContent
|
||||
isWPContentDirMovedOutOfAbsPath */
|
||||
|
||||
if (is_null($listOptions)) {
|
||||
$listOptions = self::defaultListOptions($config);
|
||||
}
|
||||
|
||||
$rootIds = Paths::filterOutSubRoots($config['scope']);
|
||||
|
||||
$groups = [];
|
||||
foreach ($rootIds as $rootId) {
|
||||
$groups[] = [
|
||||
'groupName' => $rootId,
|
||||
'root' => Paths::getAbsDirById($rootId)
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($groups as $i => &$group) {
|
||||
$listOptions['root'] = $group['root'];
|
||||
/*
|
||||
No use, because if uploads is in wp-content, the cache root will be different for the files in uploads (if mingled)
|
||||
$group['image-root'] = ConvertHelperIndependent::getDestinationFolder(
|
||||
$group['root'],
|
||||
$listOptions['destination-folder'],
|
||||
$listOptions['ext'],
|
||||
$listOptions['webExpressContentDirAbs'],
|
||||
$listOptions['uploadDirAbs']
|
||||
);*/
|
||||
$group['files'] = self::getListRecursively('.', $listOptions);
|
||||
//'image-root' => ConvertHelperIndependent::getDestinationFolder()
|
||||
}
|
||||
|
||||
return $groups;
|
||||
//self::moveRecursively($toDir, $fromDir, $srcDir, $fromExt, $toExt);
|
||||
}
|
||||
|
||||
/**
|
||||
* $filter: all | converted | not-converted. "not-converted" for example returns paths to images that has not been converted
|
||||
*/
|
||||
public static function getListRecursively($relDir, &$listOptions, $depth = 0)
|
||||
{
|
||||
$dir = $listOptions['root'] . '/' . $relDir;
|
||||
|
||||
// Canonicalize because dir might contain "/./", which causes file_exists to fail (#222)
|
||||
$dir = PathHelper::canonicalize($dir);
|
||||
|
||||
if (!@file_exists($dir) || !@is_dir($dir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$fileIterator = new \FilesystemIterator($dir);
|
||||
|
||||
$results = [];
|
||||
$filter = &$listOptions['filter'];
|
||||
|
||||
while ($fileIterator->valid()) {
|
||||
$filename = $fileIterator->getFilename();
|
||||
|
||||
if (($filename != ".") && ($filename != "..")) {
|
||||
|
||||
if (@is_dir($dir . "/" . $filename)) {
|
||||
if ($listOptions['flattenList']) {
|
||||
$results = array_merge($results, self::getListRecursively($relDir . "/" . $filename, $listOptions, $depth+1));
|
||||
} else {
|
||||
$r = [
|
||||
'name' => $filename,
|
||||
'isDir' => true,
|
||||
];
|
||||
if ($depth > $listOptions['max-depth']) {
|
||||
return $r; // one item is enough to determine that it is not empty
|
||||
}
|
||||
if ($depth < $listOptions['max-depth']) {
|
||||
$r['children'] = self::getListRecursively($relDir . "/" . $filename, $listOptions, $depth+1);
|
||||
$r['isEmpty'] = (count($r['children']) == 0);
|
||||
} else if ($depth == $listOptions['max-depth']) {
|
||||
$c = self::getListRecursively($relDir . "/" . $filename, $listOptions, $depth+1);
|
||||
$r['isEmpty'] = (count($c) == 0);
|
||||
//$r['isEmpty'] = !(new \FilesystemIterator($dir))->valid();
|
||||
}
|
||||
$results[] = $r;
|
||||
}
|
||||
} else {
|
||||
// its a file - check if its a jpeg or png
|
||||
|
||||
if (!isset($filter['_regexPattern'])) {
|
||||
$imageTypes = $filter['image-types'];
|
||||
$fileExtensions = [];
|
||||
if ($imageTypes & 1) {
|
||||
$fileExtensions[] = 'jpe?g';
|
||||
}
|
||||
if ($imageTypes & 2) {
|
||||
$fileExtensions[] = 'png';
|
||||
}
|
||||
$filter['_regexPattern'] = '#\.(' . implode('|', $fileExtensions) . ')$#';
|
||||
}
|
||||
|
||||
if (preg_match($filter['_regexPattern'], $filename)) {
|
||||
$addThis = true;
|
||||
|
||||
$destination = ConvertHelperIndependent::getDestination(
|
||||
$dir . "/" . $filename,
|
||||
$listOptions['destination-folder'],
|
||||
$listOptions['ext'],
|
||||
$listOptions['webExpressContentDirAbs'],
|
||||
$listOptions['uploadDirAbs'],
|
||||
$listOptions['useDocRootForStructuringCacheDir'],
|
||||
$listOptions['imageRoots']
|
||||
);
|
||||
$webpExists = @file_exists($destination);
|
||||
|
||||
if (($filter['only-converted']) || ($filter['only-unconverted'])) {
|
||||
//$cacheDir = $listOptions['image-root'] . '/' . $relDir;
|
||||
|
||||
// Check if corresponding webp exists
|
||||
/*
|
||||
if ($listOptions['ext'] == 'append') {
|
||||
$webpExists = @file_exists($cacheDir . "/" . $filename . '.webp');
|
||||
} else {
|
||||
$webpExists = @file_exists(preg_replace("/\.(jpe?g|png)\.webp$/", '.webp', $filename));
|
||||
}*/
|
||||
|
||||
if (!$webpExists && ($filter['only-converted'])) {
|
||||
$addThis = false;
|
||||
}
|
||||
if ($webpExists && ($filter['only-unconverted'])) {
|
||||
$addThis = false;
|
||||
}
|
||||
} else {
|
||||
$addThis = true;
|
||||
}
|
||||
|
||||
if ($addThis) {
|
||||
|
||||
$path = substr($relDir . "/", 2) . $filename; // (we cut the leading "./" off with substr)
|
||||
|
||||
// Additional safety check: verify the file actually exists before adding to list
|
||||
$fullPath = $dir . "/" . $filename;
|
||||
if (!file_exists($fullPath)) {
|
||||
continue; // Skip this file if it doesn't exist
|
||||
}
|
||||
|
||||
// Check if the string can be encoded to json (if not: change it to a string that can)
|
||||
if (json_encode($path, JSON_UNESCAPED_UNICODE) === false) {
|
||||
/*
|
||||
json_encode failed. This means that the string was not UTF-8.
|
||||
Lets see if we can convert it to UTF-8.
|
||||
This is however tricky business (see #471)
|
||||
*/
|
||||
|
||||
$encodedToUTF8 = false;
|
||||
|
||||
// First try library that claims to do better than mb_detect_encoding
|
||||
/*
|
||||
DISABLED, because Onnov EncodingDetector requires PHP 7.2
|
||||
https://wordpress.org/support/topic/get-http-error-500-after-new-update-2/
|
||||
|
||||
if (!$encodedToUTF8) {
|
||||
$detector = new EncodingDetector();
|
||||
|
||||
$dectedEncoding = $detector->getEncoding($path);
|
||||
|
||||
if ($dectedEncoding !== 'utf-8') {
|
||||
if (function_exists('iconv')) {
|
||||
$res = iconv($dectedEncoding, 'utf-8//TRANSLIT', $path);
|
||||
if ($res !== false) {
|
||||
$path = $res;
|
||||
$encodedToUTF8 = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// iconvXtoEncoding should work now hm, issue #5 has been fixed
|
||||
$path = $detector->iconvXtoEncoding($path);
|
||||
$encodedToUTF8 = true;
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
// Try mb_detect_encoding
|
||||
if (!$encodedToUTF8) {
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
$encoding = mb_detect_encoding($path, mb_detect_order(), true);
|
||||
if ($encoding) {
|
||||
$path = mb_convert_encoding($path, 'UTF-8', $encoding);
|
||||
$encodedToUTF8 = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$encodedToUTF8) {
|
||||
/*
|
||||
We haven't yet succeeded in encoding to UTF-8.
|
||||
What should we do?
|
||||
1. Skip the file? (no, the user will not know about the problem then)
|
||||
2. Add it anyway? (no, if this string causes problems to json_encode, then we will have
|
||||
the same problem when encoding the entire list - result: an empty list)
|
||||
3. Try wp_json_encode? (no, it will fall back on "wp_check_invalid_utf8", which has a number of
|
||||
things we do not want)
|
||||
4. Encode it to UTF-8 assuming that the string is encoded in the most common encoding (Windows-1252) ?
|
||||
(yes, if we are lucky with the guess, it will work. If it is in another encoding, the conversion
|
||||
will not be correct, and the user will then know about the problem. And either way, we will
|
||||
have UTF-8 string, which will not break encoding of the list)
|
||||
*/
|
||||
|
||||
// https://stackoverflow.com/questions/6606713/json-encode-non-utf-8-strings
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
$path = mb_convert_encoding($path, "UTF-8", "Windows-1252");
|
||||
} elseif (function_exists('iconv')) {
|
||||
$path = iconv("CP1252", "UTF-8", $path);
|
||||
} elseif (function_exists('utf8_encode')) {
|
||||
// utf8_encode converts from ISO-8859-1 to UTF-8
|
||||
$path = utf8_encode($path);
|
||||
} else {
|
||||
$path = '[cannot encode this filename to UTF-8]';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if ($listOptions['flattenList']) {
|
||||
$results[] = $path;
|
||||
} else {
|
||||
$results[] = [
|
||||
'name' => basename($path),
|
||||
'isConverted' => $webpExists
|
||||
];
|
||||
if ($depth > $listOptions['max-depth']) {
|
||||
return $results; // one item is enough to determine that it is not empty
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$fileIterator->next();
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/*
|
||||
public static function convertFile($source)
|
||||
{
|
||||
$config = Config::loadConfigAndFix();
|
||||
$options = Config::generateWodOptionsFromConfigObj($config);
|
||||
|
||||
$destination = ConvertHelperIndependent::getDestination(
|
||||
$source,
|
||||
$options['destination-folder'],
|
||||
$options['destination-extension'],
|
||||
Paths::getWebPExpressContentDirAbs(),
|
||||
Paths::getUploadDirAbs()
|
||||
);
|
||||
$result = ConvertHelperIndependent::convert($source, $destination, $options);
|
||||
|
||||
//$result['destination'] = $destination;
|
||||
if ($result['success']) {
|
||||
$result['filesize-original'] = @filesize($source);
|
||||
$result['filesize-webp'] = @filesize($destination);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
*/
|
||||
|
||||
public static function processAjaxListUnconvertedFiles()
|
||||
{
|
||||
if (!check_ajax_referer('webpexpress-ajax-list-unconverted-files-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();
|
||||
}
|
||||
|
||||
$config = Config::loadConfigAndFix();
|
||||
$arr = self::getList($config);
|
||||
|
||||
// We use "wp_json_encode" rather than "json_encode" because it handles problems if there is non UTF-8 characters
|
||||
// There should be none, as we have taken our measures, but no harm in taking extra precautions
|
||||
$json = wp_json_encode($arr, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
if ($json === false) {
|
||||
// TODO: We can do better error handling than this!
|
||||
echo '';
|
||||
} else {
|
||||
echo $json;
|
||||
}
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
}
|
||||
272
lib/classes/CLI.php
Normal file
272
lib/classes/CLI.php
Normal file
@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class CLI extends \WP_CLI_Command
|
||||
{
|
||||
|
||||
private static function printableSize($bytes) {
|
||||
return ($bytes < 10000) ? $bytes . " bytes" : round($bytes / 1024) . ' kb';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert images to webp
|
||||
*
|
||||
* ## OPTIONS
|
||||
* [<location>]
|
||||
* : Limit which folders to process to a single location. Ie "uploads/2021". The first part is the
|
||||
* "image root", which must be "uploads", "themes", "plugins", "wp-content" or "index"
|
||||
*
|
||||
* [--reconvert]
|
||||
* : Even convert images that are already converted (new conversions replaces the old conversions)
|
||||
*
|
||||
* [--only-png]
|
||||
* : Only convert PNG images
|
||||
*
|
||||
* [--only-jpeg]
|
||||
* : Only convert jpeg images
|
||||
*
|
||||
* [--quality]
|
||||
* : Override quality with specified (0-100)
|
||||
*
|
||||
* [--near-lossless]
|
||||
* : Override near-lossless quality with specified (0-100)
|
||||
*
|
||||
* [--alpha-quality]
|
||||
* : Override alpha-quality quality with specified (0-100)
|
||||
*
|
||||
* [--encoding]
|
||||
* : Override encoding quality with specified ("auto", "lossy" or "lossless")
|
||||
*
|
||||
* [--converter=<converter>]
|
||||
* : Specify the converter to use (default is to use the stack). Valid options: cwebp | vips | ewww | imagemagick | imagick | gmagick | graphicsmagick | ffmpeg | gd | wpc | ewww
|
||||
*/
|
||||
public function convert($args, $assoc_args)
|
||||
{
|
||||
$config = Config::loadConfigAndFix();
|
||||
$override = [];
|
||||
|
||||
if (isset($assoc_args['quality'])) {
|
||||
$override['max-quality'] = intval($assoc_args['quality']);
|
||||
$override['png-quality'] = intval($assoc_args['quality']);
|
||||
}
|
||||
if (isset($assoc_args['near-lossless'])) {
|
||||
$override['png-near-lossless'] = intval($assoc_args['near-lossless']);
|
||||
$override['jpeg-near-lossless'] = intval($assoc_args['near-lossless']);
|
||||
}
|
||||
if (isset($assoc_args['alpha-quality'])) {
|
||||
$override['alpha-quality'] = intval($assoc_args['alpha-quality']);
|
||||
}
|
||||
if (isset($assoc_args['encoding'])) {
|
||||
if (!in_array($assoc_args['encoding'], ['auto', 'lossy', 'lossless'])) {
|
||||
\WP_CLI::error('encoding must be auto, lossy or lossless');
|
||||
}
|
||||
$override['png-encoding'] = $assoc_args['encoding'];
|
||||
$override['jpeg-encoding'] = $assoc_args['encoding'];
|
||||
}
|
||||
if (isset($assoc_args['converter'])) {
|
||||
if (!in_array($assoc_args['converter'], ConvertersHelper::getDefaultConverterNames())) {
|
||||
\WP_CLI::error(
|
||||
'"' . $assoc_args['converter'] . '" is not a valid converter id. ' .
|
||||
'Valid converters are: ' . implode(', ', ConvertersHelper::getDefaultConverterNames())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$config = array_merge($config, $override);
|
||||
|
||||
\WP_CLI::log('Converting with the following settings:');
|
||||
\WP_CLI::log('- Lossless quality: ' . $config['png-quality'] . ' for PNG, ' . $config['max-quality'] . " for jpeg");
|
||||
\WP_CLI::log(
|
||||
'- Near lossless: ' .
|
||||
($config['png-enable-near-lossless'] ? $config['png-near-lossless'] : 'disabled') . ' for PNG, ' .
|
||||
($config['jpeg-enable-near-lossless'] ? $config['jpeg-near-lossless'] : 'disabled') . ' for jpeg, '
|
||||
);
|
||||
\WP_CLI::log('- Alpha quality: ' . $config['alpha-quality']);
|
||||
\WP_CLI::log('- Encoding: ' . $config['png-encoding'] . ' for PNG, ' . $config['jpeg-encoding'] . " for jpeg");
|
||||
|
||||
if (count($override) == 0) {
|
||||
\WP_CLI::log('Note that you can override these with --quality=<quality>, etc');
|
||||
}
|
||||
\WP_CLI::log('');
|
||||
|
||||
|
||||
$listOptions = BulkConvert::defaultListOptions($config);
|
||||
if (isset($assoc_args['reconvert'])) {
|
||||
$listOptions['filter']['only-unconverted'] = false;
|
||||
}
|
||||
if (isset($assoc_args['only-png'])) {
|
||||
$listOptions['filter']['image-types'] = 2;
|
||||
}
|
||||
if (isset($assoc_args['only-jpeg'])) {
|
||||
$listOptions['filter']['image-types'] = 1;
|
||||
}
|
||||
|
||||
if (!isset($args[0])) {
|
||||
$groups = BulkConvert::getList($config, $listOptions);
|
||||
foreach($groups as $group){
|
||||
\WP_CLI::log($group['groupName'] . ' contains ' . count($group['files']) . ' ' .
|
||||
(isset($assoc_args['reconvert']) ? '' : 'unconverted ') .
|
||||
'files');
|
||||
}
|
||||
\WP_CLI::log('');
|
||||
} else {
|
||||
$location = $args[0];
|
||||
if (strpos($location, '/') === 0) {
|
||||
$location = substr($location, 1);
|
||||
}
|
||||
if (strpos($location, '/') === false) {
|
||||
$rootId = $location;
|
||||
$path = '.';
|
||||
} else {
|
||||
list($rootId, $path) = explode('/', $location, 2);
|
||||
}
|
||||
|
||||
if (!in_array($rootId, Paths::getImageRootIds())) {
|
||||
\WP_CLI::error(
|
||||
'"' . $args[0] . '" is not a valid image root. ' .
|
||||
'Valid roots are: ' . implode(', ', Paths::getImageRootIds())
|
||||
);
|
||||
}
|
||||
|
||||
$root = Paths::getAbsDirById($rootId) . '/' . $path;
|
||||
if (!file_exists($root)) {
|
||||
\WP_CLI::error(
|
||||
'"' . $args[0] . '" does not exist. '
|
||||
);
|
||||
}
|
||||
$listOptions['root'] = $root;
|
||||
$groups = [
|
||||
[
|
||||
'groupName' => $args[0],
|
||||
'root' => $root,
|
||||
'files' => BulkConvert::getListRecursively('.', $listOptions)
|
||||
]
|
||||
];
|
||||
if (count($groups[0]['files']) == 0) {
|
||||
\WP_CLI::log('Nothing to convert in ' . $args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
$orgTotalFilesize = 0;
|
||||
$webpTotalFilesize = 0;
|
||||
|
||||
$converter = null;
|
||||
$convertOptions = null;
|
||||
|
||||
if (isset($assoc_args['converter'])) {
|
||||
|
||||
$converter = $assoc_args['converter'];
|
||||
$convertOptions = Config::generateWodOptionsFromConfigObj($config)['webp-convert']['convert'];
|
||||
|
||||
// find the converter
|
||||
$optionsForThisConverter = null;
|
||||
foreach ($convertOptions['converters'] as $c) {
|
||||
if ($c['converter'] == $converter) {
|
||||
$optionsForThisConverter = (isset($c['options']) ? $c['options'] : []);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_array($optionsForThisConverter)) {
|
||||
\WP_CLI::error('Failed handling options');
|
||||
}
|
||||
|
||||
$convertOptions = array_merge($convertOptions, $optionsForThisConverter);
|
||||
unset($convertOptions['converters']);
|
||||
}
|
||||
|
||||
foreach($groups as $group){
|
||||
if (count($group['files']) == 0) continue;
|
||||
|
||||
\WP_CLI::log('Converting ' . count($group['files']) . ' files in ' . $group['groupName']);
|
||||
\WP_CLI::log('------------------------------');
|
||||
$root = $group['root'];
|
||||
|
||||
$files = array_reverse($group['files']);
|
||||
//echo count($group["files"]);
|
||||
foreach($files as $key => $file)
|
||||
{
|
||||
$path = trailingslashit($group['root']) . $file;
|
||||
\WP_CLI::log('Converting: ' . $file);
|
||||
|
||||
$result = Convert::convertFile($path, $config, $convertOptions, $converter);
|
||||
|
||||
if ($result['success']) {
|
||||
$orgSize = $result['filesize-original'];
|
||||
$webpSize = $result['filesize-webp'];
|
||||
|
||||
$orgTotalFilesize += $orgSize;
|
||||
$webpTotalFilesize += $webpSize;
|
||||
|
||||
//$percentage = round(($orgSize - $webpSize)/$orgSize * 100);
|
||||
$percentage = ($orgSize == 0 ? 100 : round(($webpSize/$orgSize) * 100));
|
||||
|
||||
\WP_CLI::log(
|
||||
\WP_CLI::colorize(
|
||||
"%GOK%n. " .
|
||||
"Size: " .
|
||||
($percentage<90 ? "%G" : ($percentage<100 ? "%Y" : "%R")) .
|
||||
$percentage .
|
||||
"% %nof original" .
|
||||
" (" . self::printableSize($orgSize) . ' => ' . self::printableSize($webpSize) .
|
||||
") "
|
||||
)
|
||||
);
|
||||
//print_r($result);
|
||||
} else {
|
||||
\WP_CLI::log(
|
||||
\WP_CLI::colorize("%RConversion failed. " . $result['msg'] . "%n")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($orgTotalFilesize > 0) {
|
||||
$percentage = ($orgTotalFilesize == 0 ? 100 : round(($webpTotalFilesize/$orgTotalFilesize) * 100));
|
||||
\WP_CLI::log(
|
||||
\WP_CLI::colorize(
|
||||
"Done. " .
|
||||
"Size of webps: " .
|
||||
($percentage<90 ? "%G" : ($percentage<100 ? "%Y" : "%R")) .
|
||||
$percentage .
|
||||
"% %nof original" .
|
||||
" (" . self::printableSize($orgTotalFilesize) . ' => ' . self::printableSize($webpTotalFilesize) .
|
||||
") "
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush webps
|
||||
*
|
||||
* ## OPTIONS
|
||||
* [--only-png]
|
||||
* : Only flush webps that are conversions of a PNG)
|
||||
*/
|
||||
public function flushwebp($args, $assoc_args)
|
||||
{
|
||||
$config = Config::loadConfigAndFix();
|
||||
|
||||
$onlyPng = isset($assoc_args['only-png']);
|
||||
|
||||
if ($onlyPng) {
|
||||
\WP_CLI::log('Flushing webp files that are conversions of PNG images');
|
||||
} else {
|
||||
\WP_CLI::log('Flushing all webp files');
|
||||
}
|
||||
|
||||
$result = CachePurge::purge($config, $onlyPng);
|
||||
|
||||
\WP_CLI::log(
|
||||
\WP_CLI::colorize("%GFlushed " . $result['delete-count'] . " webp files%n")
|
||||
);
|
||||
if ($result['fail-count'] > 0) {
|
||||
\WP_CLI::log(
|
||||
\WP_CLI::colorize("%RFailed deleting " . $result['fail-count'] . " webp files%n")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
235
lib/classes/CacheMover.php
Normal file
235
lib/classes/CacheMover.php
Normal file
@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
class CacheMover
|
||||
{
|
||||
|
||||
public static function getUploadFolder($destinationFolder)
|
||||
{
|
||||
switch ($destinationFolder) {
|
||||
case 'mingled':
|
||||
return Paths::getUploadDirAbs();
|
||||
case 'separate':
|
||||
return Paths::getCacheDirAbs() . '/doc-root/' . Paths::getUploadDirRel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets permission, uid and gid of all subfolders/files of a dir to same as the dir
|
||||
* (but for files, do not set executable flag)
|
||||
*/
|
||||
public static function chmodFixSubDirs($dir, $alsoSetOnDirs)
|
||||
{
|
||||
$dirPerm = FileHelper::filePermWithFallback($dir, 0775);
|
||||
$filePerm = $dirPerm & 0666; // set executable flags to 0
|
||||
/*echo 'dir:' . $dir . "\n";
|
||||
echo 'Dir perm:' . FileHelper::humanReadableFilePerm($dirPerm) . "\n";
|
||||
echo 'File perm:' . FileHelper::humanReadableFilePerm($filePerm) . "\n";*/
|
||||
//return;
|
||||
|
||||
$stat = @stat($dir);
|
||||
$uid = null;
|
||||
$gid = null;
|
||||
if ($stat !== false) {
|
||||
if (isset($stat['uid'])) {
|
||||
$uid = $stat['uid'];
|
||||
}
|
||||
if (isset($stat['gid'])) {
|
||||
$uid = $stat['gid'];
|
||||
}
|
||||
}
|
||||
FileHelper::chmod_r($dir, $dirPerm, $filePerm, $uid, $gid, '#\.webp$#', ($alsoSetOnDirs ? null : '#^$#'));
|
||||
}
|
||||
|
||||
public static function getDestinationFolderForImageRoot($config, $imageRootId)
|
||||
{
|
||||
return Paths::getCacheDirForImageRoot($config['destination-folder'], $config['destination-structure'], $imageRootId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move cache because of change in options.
|
||||
* If structure is unchanged, only move the upload folder
|
||||
* Only move those that has an original
|
||||
* Only move those that can be moved.
|
||||
* @return [$numFilesMoved, $numFilesFailedMoving]
|
||||
*/
|
||||
public static function move($newConfig, $oldConfig)
|
||||
{
|
||||
if (!Paths::canUseDocRootForStructuringCacheDir()) {
|
||||
if (($oldConfig['destination-structure'] == 'doc-root') || ($newConfig['destination-structure'] == 'doc-root')) {
|
||||
// oh, well. Seems document root is not available.
|
||||
// so we cannot move from or to that kind of structure
|
||||
// This could happen if document root once was available but now is unavailable
|
||||
return [0, 0];
|
||||
}
|
||||
}
|
||||
|
||||
$changeStructure = ($newConfig['destination-structure'] != $oldConfig['destination-structure']);
|
||||
|
||||
if ($changeStructure) {
|
||||
$rootIds = Paths::getImageRootIds();
|
||||
} else {
|
||||
$rootIds = ['uploads'];
|
||||
}
|
||||
|
||||
$numFilesMovedTotal = 0;
|
||||
$numFilesFailedMovingTotal = 0;
|
||||
foreach ($rootIds as $rootId) {
|
||||
|
||||
$isUploadsMingled = (($newConfig['destination-folder'] == 'mingled') && ($rootId == 'uploads'));
|
||||
|
||||
$fromDir = self::getDestinationFolderForImageRoot($oldConfig, $rootId);
|
||||
$fromExt = $oldConfig['destination-extension'];
|
||||
|
||||
$toDir = self::getDestinationFolderForImageRoot($newConfig, $rootId);
|
||||
$toExt = $newConfig['destination-extension'];
|
||||
|
||||
$srcDir = Paths::getAbsDirById($rootId);
|
||||
|
||||
list($numFilesMoved, $numFilesFailedMoving) = self::moveRecursively($fromDir, $toDir, $srcDir, $fromExt, $toExt);
|
||||
if (!$isUploadsMingled) {
|
||||
FileHelper::removeEmptySubFolders($fromDir);
|
||||
}
|
||||
|
||||
$numFilesMovedTotal += $numFilesMoved;
|
||||
$numFilesFailedMovingTotal += $numFilesFailedMoving;
|
||||
|
||||
$chmodFixFoldersToo = !$isUploadsMingled;
|
||||
self::chmodFixSubDirs($toDir, $chmodFixFoldersToo);
|
||||
}
|
||||
return [$numFilesMovedTotal, $numFilesFailedMovingTotal];
|
||||
/*
|
||||
$fromDir = self::getUploadFolder($oldConfig['destination-folder']);
|
||||
$fromExt = $oldConfig['destination-extension'];
|
||||
|
||||
$toDir = self::getUploadFolder($newConfig['destination-folder']);
|
||||
$toExt = $newConfig['destination-extension'];
|
||||
|
||||
$srcDir = self::getUploadFolder('mingled');
|
||||
|
||||
$result = self::moveRecursively($fromDir, $toDir, $srcDir, $fromExt, $toExt);
|
||||
self::chmodFixSubDirs($toDir, ($newConfig['destination-folder'] == 'separate'));
|
||||
*/
|
||||
|
||||
//return $result;
|
||||
|
||||
// for testing!
|
||||
/*
|
||||
$fromDir = self::getUploadFolder('mingled'); // separate | mingled
|
||||
$toDir = self::getUploadFolder('mingled');
|
||||
$fromExt = 'set'; // set | append
|
||||
$toExt = 'append';
|
||||
|
||||
echo '<pre>';
|
||||
echo 'from: ' . $fromDir . '<br>';
|
||||
echo 'to: ' . $toDir . '<br>';
|
||||
echo 'ext:' . $fromExt . ' => ' . $toExt . '<br>';
|
||||
echo '</pre>';*/
|
||||
|
||||
//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 . "<br>";
|
||||
$toFilename = $toDir . "/" . $newFilename;
|
||||
if (@rename($fromDir . "/" . $filename, $toFilename)) {
|
||||
$numFilesMoved++;
|
||||
} else {
|
||||
$numFilesFailedMoving++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$fileIterator->next();
|
||||
}
|
||||
return [$numFilesMoved, $numFilesFailedMoving];
|
||||
}
|
||||
|
||||
}
|
||||
171
lib/classes/CachePurge.php
Normal file
171
lib/classes/CachePurge.php
Normal file
@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Convert;
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\DismissableMessages;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
// TODO! Needs to be updated to work with the new "destination-structure" setting
|
||||
|
||||
class CachePurge
|
||||
{
|
||||
|
||||
/**
|
||||
* - Removes cache dir
|
||||
* - Removes all files with ".webp" extension in upload dir (if set to mingled)
|
||||
*/
|
||||
public static function purge($config, $onlyPng)
|
||||
{
|
||||
DismissableMessages::dismissMessage('0.14.0/suggest-wipe-because-lossless');
|
||||
|
||||
$filter = [
|
||||
'only-png' => $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();
|
||||
}
|
||||
}
|
||||
103
lib/classes/CapabilityTest.php
Normal file
103
lib/classes/CapabilityTest.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/*
|
||||
This functionality will be moved to a separate project.
|
||||
|
||||
Btw:
|
||||
Seems someone else got similar idea:
|
||||
http://christian.roy.name/blog/detecting-modrewrite-using-php
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
class CapabilityTest
|
||||
{
|
||||
|
||||
public static function copyCapabilityTestsToWpContent()
|
||||
{
|
||||
return FileHelper::cpdir(Paths::getWebPExpressPluginDirAbs() . '/htaccess-capability-tests', Paths::getWebPExpressContentDirAbs() . '/htaccess-capability-tests');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run one of the tests in wp-content/webp-express/capability-tests
|
||||
* Three possible outcomes: true, false or null (null if request fails)
|
||||
*/
|
||||
public static function runTest($testDir)
|
||||
{
|
||||
//echo 'running test:' . $testDir . '<br>';
|
||||
if (!@file_exists(Paths::getWebPExpressPluginDirAbs() . '/htaccess-capability-tests/' . $testDir)) {
|
||||
// test does not even exist
|
||||
//echo 'test does not exist: ' . $testDir . '<br>';
|
||||
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 . '<br>';
|
||||
// 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 '<pre>' . print_r($response, true) . '</pre>';
|
||||
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');
|
||||
}
|
||||
|
||||
}
|
||||
755
lib/classes/Config.php
Normal file
755
lib/classes/Config.php
Normal file
@ -0,0 +1,755 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class Config
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return object|false Returns config object if config file exists and can be read. Otherwise it returns false
|
||||
*/
|
||||
public static function loadConfig()
|
||||
{
|
||||
return FileHelper::loadJSONOptions(Paths::getConfigFileName());
|
||||
}
|
||||
|
||||
public static function getDefaultConfig($skipQualityAuto = false) {
|
||||
if ($skipQualityAuto) {
|
||||
$qualityAuto = null;
|
||||
} else {
|
||||
$qualityAuto = TestRun::isLocalQualityDetectionWorking();
|
||||
}
|
||||
|
||||
return [
|
||||
|
||||
'operation-mode' => '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 <i>' . $converterId . '</i> conversion method is working now!'
|
||||
);
|
||||
} else {
|
||||
Messenger::printMessage(
|
||||
'warning',
|
||||
'Sad news. The <i>' . $converterId . '</i> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
349
lib/classes/Convert.php
Normal file
349
lib/classes/Convert.php
Normal file
@ -0,0 +1,349 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPConvert\Convert\Converters\Ewww;
|
||||
|
||||
use \WebPExpress\BiggerThanSourceDummyFiles;
|
||||
use \WebPExpress\ConvertHelperIndependent;
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\ConvertersHelper;
|
||||
use \WebPExpress\DestinationOptions;
|
||||
use \WebPExpress\EwwwTools;
|
||||
use \WebPExpress\ImageRoots;
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\Paths;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
use \WebPExpress\Validate;
|
||||
use \WebPExpress\ValidateException;
|
||||
|
||||
class Convert
|
||||
{
|
||||
|
||||
public static function getDestination($source, &$config = null)
|
||||
{
|
||||
if (is_null($config)) {
|
||||
$config = Config::loadConfigAndFix();
|
||||
}
|
||||
return ConvertHelperIndependent::getDestination(
|
||||
$source,
|
||||
$config['destination-folder'],
|
||||
$config['destination-extension'],
|
||||
Paths::getWebPExpressContentDirAbs(),
|
||||
Paths::getUploadDirAbs(),
|
||||
(($config['destination-structure'] == 'doc-root') && (Paths::canUseDocRootForStructuringCacheDir())),
|
||||
new ImageRoots(Paths::getImageRootsDef())
|
||||
);
|
||||
}
|
||||
|
||||
public static function updateBiggerThanOriginalMark($source, $destination = null, &$config = null)
|
||||
{
|
||||
if (is_null($config)) {
|
||||
$config = Config::loadConfigAndFix();
|
||||
}
|
||||
if (is_null($destination)) {
|
||||
$destination = self::getDestination($config);
|
||||
}
|
||||
BiggerThanSourceDummyFiles::updateStatus(
|
||||
$source,
|
||||
$destination,
|
||||
Paths::getWebPExpressContentDirAbs(),
|
||||
new ImageRoots(Paths::getImageRootsDef()),
|
||||
$config['destination-folder'],
|
||||
$config['destination-extension']
|
||||
);
|
||||
}
|
||||
|
||||
public static function convertFile($source, $config = null, $convertOptions = null, $converter = null)
|
||||
{
|
||||
try {
|
||||
// Check source
|
||||
// ---------------
|
||||
$checking = 'source path';
|
||||
|
||||
// First check if file exists before doing any other validations
|
||||
if (!file_exists($source)) {
|
||||
return [
|
||||
'success' => 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;
|
||||
}
|
||||
}
|
||||
739
lib/classes/ConvertHelperIndependent.php
Normal file
739
lib/classes/ConvertHelperIndependent.php
Normal file
@ -0,0 +1,739 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This class is made to not be dependent on Wordpress functions and must be kept like that.
|
||||
It is used by webp-on-demand.php. It is also used for bulk conversion.
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPConvert\WebPConvert;
|
||||
use \WebPConvert\Convert\ConverterFactory;
|
||||
use \WebPConvert\Exceptions\WebPConvertException;
|
||||
use \WebPConvert\Loggers\BufferLogger;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
|
||||
class ConvertHelperIndependent
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean Whether or not the destination corresponding to a given source should be stored in the same folder or the separate (in wp-content/webp-express)
|
||||
*/
|
||||
private static function storeMingledOrNot($source, $destinationFolder, $uploadDirAbs)
|
||||
{
|
||||
if ($destinationFolder != 'mingled') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Option is set for mingled, but this does not neccessarily means we should store "mingled".
|
||||
// - because the mingled option only applies to upload folder, the rest is stored in separate cache folder
|
||||
// So, return true, if $source is located in upload folder
|
||||
return (strpos($source, $uploadDirAbs) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if source is inside in document root
|
||||
* Note: This function relies on the existence of both.
|
||||
*
|
||||
* @return true if windows; false if not.
|
||||
*/
|
||||
public static function sourceIsInsideDocRoot($source, $docRoot){
|
||||
|
||||
$normalizedSource = realpath($source);
|
||||
$normalizedDocRoot = realpath($docRoot);
|
||||
|
||||
return strpos($normalizedSource, $normalizedDocRoot) === 0;
|
||||
}
|
||||
|
||||
public static function getSource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Append ".webp" to path or replace extension with "webp", depending on what is appropriate.
|
||||
*
|
||||
* If destination-folder is set to mingled and destination-extension is set to "set" and
|
||||
* the path is inside upload folder, the appropriate thing is to SET the extension.
|
||||
* Otherwise, it is to APPEND.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $destinationFolder
|
||||
* @param string $destinationExt
|
||||
* @param boolean $inUploadFolder
|
||||
*/
|
||||
public static function appendOrSetExtension($path, $destinationFolder, $destinationExt, $inUploadFolder)
|
||||
{
|
||||
if (($destinationFolder == 'mingled') && ($destinationExt == 'set') && $inUploadFolder) {
|
||||
return preg_replace('/\\.(jpe?g|png)$/i', '', $path) . '.webp';
|
||||
} else {
|
||||
return $path . '.webp';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get destination path corresponding to the source path given (and some configurations)
|
||||
*
|
||||
* If for example Operation mode is set to "mingled" and extension is set to "Append .webp",
|
||||
* the result of finding the destination path that corresponds to "/path/to/logo.jpg" will be "/path/to/logo.jpg.webp".
|
||||
*
|
||||
* @param string $source Path to source file
|
||||
* @param string $destinationFolder 'mingled' or 'separate'
|
||||
* @param string $destinationExt Extension ('append' or 'set')
|
||||
* @param string $webExpressContentDirAbs
|
||||
* @param string $uploadDirAbs
|
||||
* @param boolean $useDocRootForStructuringCacheDir
|
||||
* @param ImageRoots $imageRoots An image roots object
|
||||
*
|
||||
* @return string|false Returns path to destination corresponding to source, or false on failure
|
||||
*/
|
||||
public static function getDestination(
|
||||
$source,
|
||||
$destinationFolder,
|
||||
$destinationExt,
|
||||
$webExpressContentDirAbs,
|
||||
$uploadDirAbs,
|
||||
$useDocRootForStructuringCacheDir,
|
||||
$imageRoots)
|
||||
{
|
||||
// At this point, everything has already been checked for sanity. But for good meassure, lets
|
||||
// check the most important parts again. This is after all a public method.
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
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, $destinationFolder, $uploadDirAbs)) {
|
||||
$destination = self::appendOrSetExtension($source, $destinationFolder, $destinationExt, true);
|
||||
} else {
|
||||
|
||||
if ($useDocRootForStructuringCacheDir) {
|
||||
// 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, $destinationFolder, $destinationExt, 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, $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 . '<br>' . $closestExistingResolved . '<br>' . $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', <<<APACHE
|
||||
<IfModule mod_authz_core.c>
|
||||
Require all denied
|
||||
</IfModule>
|
||||
<IfModule !mod_authz_core.c>
|
||||
Order deny,allow
|
||||
Deny from all
|
||||
</IfModule>
|
||||
APACHE
|
||||
);
|
||||
@chmod($logDir . '/.htaccess', 0664);
|
||||
}
|
||||
return is_dir($logDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the log file corresponding to a conversion.
|
||||
*
|
||||
* @param string $source Path to the source file that was converted
|
||||
* @param string $logDir The folder where log files are kept
|
||||
* @param string $text Content of the log file
|
||||
* @param string $msgTop A message that is printed before the conversion log (containing version info)
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static function saveLog($source, $logDir, $text, $msgTop)
|
||||
{
|
||||
|
||||
if (!file_exists($logDir)) {
|
||||
self::createLogDir($logDir);
|
||||
}
|
||||
|
||||
$text = preg_replace('#' . preg_quote($_SERVER["DOCUMENT_ROOT"]) . '#', '[doc-root]', $text);
|
||||
|
||||
// TODO: Put version number somewhere else. Ie \WebPExpress\VersionNumber::version
|
||||
$text = 'WebP Express 0.25.9. ' . $msgTop . ', ' . date("Y-m-d H:i:s") . "\n\r\n\r" . $text;
|
||||
|
||||
$logFile = self::getLogFilename($source, $logDir);
|
||||
|
||||
if ($logFile === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$logFolder = @dirname($logFile);
|
||||
if (!@file_exists($logFolder)) {
|
||||
mkdir($logFolder, 0777, true);
|
||||
}
|
||||
if (@file_exists($logFolder)) {
|
||||
file_put_contents($logFile, $text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger an actual conversion with webp-convert.
|
||||
*
|
||||
* PS: To convert with a specific converter, set it in the $converter param.
|
||||
*
|
||||
* @param string $source Full path to the source file that was converted.
|
||||
* @param string $destination Full path to the destination file (may exist or not).
|
||||
* @param array $convertOptions Conversion options.
|
||||
* @param string $logDir The folder where log files are kept or null for no logging
|
||||
* @param string $converter (optional) Set it to convert with a specific converter.
|
||||
*/
|
||||
public static function convert($source, $destination, $convertOptions, $logDir = null, $converter = null) {
|
||||
include_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
// At this point, everything has already been checked for sanity. But for good meassure, lets
|
||||
// check the most important parts again. This is after all a public method.
|
||||
// ------------------------------------------------------------------
|
||||
try {
|
||||
|
||||
// Check that source path is sane, exists, is a file and is inside document root
|
||||
// -------------------------------------------------------
|
||||
|
||||
// First check if file exists before doing any other validations
|
||||
if (!file_exists($source)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'msg' => 'Source file does not exist: ' . $source,
|
||||
'log' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$source = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
|
||||
|
||||
|
||||
// Check that destination path is sane and is inside document root
|
||||
// -------------------------------------------------------
|
||||
$destination = SanityCheck::absPathIsInDocRoot($destination);
|
||||
$destination = SanityCheck::pregMatch('#\.webp$#', $destination, 'Destination does not end with .webp');
|
||||
|
||||
|
||||
// Check that log path is sane and inside document root
|
||||
// -------------------------------------------------------
|
||||
if (!is_null($logDir)) {
|
||||
$logDir = SanityCheck::absPathIsInDocRoot($logDir);
|
||||
}
|
||||
|
||||
|
||||
// PS: No need to check $logMsgTop. Log files are markdown and stored as ".md". They can do no harm.
|
||||
|
||||
} catch (SanityException $e) {
|
||||
return [
|
||||
'success' => false,
|
||||
'msg' => $e->getMessage(),
|
||||
'log' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$success = false;
|
||||
$msg = '';
|
||||
$logger = new BufferLogger();
|
||||
try {
|
||||
if (!is_null($converter)) {
|
||||
//if (isset($convertOptions['converter'])) {
|
||||
//print_r($convertOptions);exit;
|
||||
$logger->logLn('Converter set to: ' . $converter);
|
||||
$logger->logLn('');
|
||||
$converter = ConverterFactory::makeConverter($converter, $source, $destination, $convertOptions, $logger);
|
||||
$converter->doConvert();
|
||||
} else {
|
||||
//error_log('options:' . print_r(json_encode($convertOptions,JSON_PRETTY_PRINT), true));
|
||||
WebPConvert::convert($source, $destination, $convertOptions, $logger);
|
||||
}
|
||||
$success = true;
|
||||
} catch (\WebpConvert\Exceptions\WebPConvertException $e) {
|
||||
$msg = $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
//$msg = 'An exception was thrown!';
|
||||
$msg = $e->getMessage();
|
||||
} catch (\Throwable $e) {
|
||||
//Executed only in PHP 7 and 8, will not match in PHP 5
|
||||
$msg = $e->getMessage();
|
||||
}
|
||||
|
||||
if (!is_null($logDir)) {
|
||||
self::saveLog($source, $logDir, $logger->getMarkDown("\n\r"), 'Conversion triggered using bulk conversion');
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => $success,
|
||||
'msg' => $msg,
|
||||
'log' => $logger->getMarkDown("\n"),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve a converted file (if it does not already exist, a conversion is triggered - all handled in webp-convert).
|
||||
*
|
||||
*/
|
||||
public static function serveConverted($source, $destination, $serveOptions, $logDir = null, $logMsgTop = '')
|
||||
{
|
||||
include_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
// At this point, everything has already been checked for sanity. But for good meassure, lets
|
||||
// check again. This is after all a public method.
|
||||
// ---------------------------------------------
|
||||
try {
|
||||
|
||||
// Check that source path is sane, exists, is a file.
|
||||
// -------------------------------------------------------
|
||||
//$source = SanityCheck::absPathExistsAndIsFileInDocRoot($source);
|
||||
$source = SanityCheck::absPathExistsAndIsFile($source);
|
||||
|
||||
|
||||
// Check that destination path is sane
|
||||
// -------------------------------------------------------
|
||||
//$destination = SanityCheck::absPathIsInDocRoot($destination);
|
||||
$destination = SanityCheck::absPath($destination);
|
||||
$destination = SanityCheck::pregMatch('#\.webp$#', $destination, 'Destination does not end with .webp');
|
||||
|
||||
|
||||
// Check that log path is sane
|
||||
// -------------------------------------------------------
|
||||
//$logDir = SanityCheck::absPathIsInDocRoot($logDir);
|
||||
if ($logDir != null) {
|
||||
$logDir = SanityCheck::absPath($logDir);
|
||||
}
|
||||
|
||||
// PS: No need to check $logMsgTop. Log files are markdown and stored as ".md". They can do no harm.
|
||||
|
||||
} catch (SanityException $e) {
|
||||
$msg = $e->getMessage();
|
||||
echo $msg;
|
||||
header('X-WebP-Express-Error: ' . $msg, true);
|
||||
// TODO: error_log() ?
|
||||
exit;
|
||||
}
|
||||
|
||||
$convertLogger = new BufferLogger();
|
||||
WebPConvert::serveConverted($source, $destination, $serveOptions, null, $convertLogger);
|
||||
if (!is_null($logDir)) {
|
||||
$convertLog = $convertLogger->getMarkDown("\n\r");
|
||||
if ($convertLog != '') {
|
||||
self::saveLog($source, $logDir, $convertLog, $logMsgTop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
48
lib/classes/ConvertLog.php
Normal file
48
lib/classes/ConvertLog.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\ConvertHelperIndependent;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
class ConvertLog
|
||||
{
|
||||
public static function processAjaxViewLog()
|
||||
{
|
||||
if (!check_ajax_referer('webpexpress-ajax-view-log-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();
|
||||
}
|
||||
|
||||
// We need to be absolute certain that this feature cannot be misused.
|
||||
// - so disabling until I get the time...
|
||||
|
||||
$msg = 'This feature is on the road map...';
|
||||
echo json_encode($msg, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
|
||||
/*
|
||||
$source = sanitize_text_field($_POST['source']);
|
||||
$logFile = ConvertHelperIndependent::getLogFilename($source, Paths::getLogDirAbs());
|
||||
$msg = 'Log file: <i>' . $logFile . '</i><br><br><hr>';
|
||||
|
||||
if (!file_exists($logFile)) {
|
||||
$msg .= '<b>No log file found on that location</b>';
|
||||
|
||||
} else {
|
||||
$log = file_get_contents($logFile);
|
||||
if ($log === false) {
|
||||
$msg .= '<b>Could not read log file</b>';
|
||||
} else {
|
||||
$msg .= nl2br($log);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//$log = $source;
|
||||
//file_get_contents
|
||||
|
||||
echo json_encode($msg, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
|
||||
*/
|
||||
wp_die();
|
||||
}
|
||||
|
||||
}
|
||||
287
lib/classes/ConvertersHelper.php
Normal file
287
lib/classes/ConvertersHelper.php
Normal file
@ -0,0 +1,287 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class ConvertersHelper
|
||||
{
|
||||
public static $defaultConverters = [
|
||||
['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' => '',
|
||||
]],
|
||||
['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'];
|
||||
}
|
||||
|
||||
}
|
||||
208
lib/classes/Destination.php
Normal file
208
lib/classes/Destination.php
Normal file
@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This class is made to not be dependent on Wordpress functions and must be kept like that.
|
||||
It is used by webp-on-demand.php, which does not register an auto loader. It is also used for bulk conversion.
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
class Destination
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean Whether or not the destination corresponding to a given source should be stored in the same folder
|
||||
* or the separate folder (in wp-content/webp-express)
|
||||
*/
|
||||
private static function storeMingledOrNot($source, $mingled, $uploadDirAbs)
|
||||
{
|
||||
if ($mingled == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Option is set for mingled, but this does not neccessarily means we should store "mingled".
|
||||
// - because the mingled option only applies to upload folder, the rest is stored in separate cache folder
|
||||
// So, return true, if $source is located in upload folder
|
||||
return (strpos($source, $uploadDirAbs) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append ".webp" to path or replace extension with "webp", depending on what is appropriate.
|
||||
*
|
||||
* If destination-folder is set to mingled and destination-extension is set to "set" and
|
||||
* the path is inside upload folder, the appropriate thing is to SET the extension.
|
||||
* Otherwise, it is to APPEND.
|
||||
*
|
||||
* @param string $path
|
||||
* @param boolean $mingled Mingled setting (notice that mingled only applies to uploads)
|
||||
* @param string $replaceExt If file extension should be replaced with ".webp". If false, ".webp" is appended.
|
||||
* @param boolean $inUploadFolder
|
||||
*/
|
||||
public static function appendOrSetExtension($path, $mingled, $replaceExt, $inUploadFolder)
|
||||
{
|
||||
if ($mingled && $replaceExt && $inUploadFolder) {
|
||||
return preg_replace('/\\.(jpe?g|png)$/i', '', $path) . '.webp';
|
||||
} else {
|
||||
return $path . '.webp';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get destination path corresponding to the source path given (and some configurations)
|
||||
*
|
||||
* If for example Operation mode is set to "mingled" and extension is set to "Append .webp",
|
||||
* the result of finding the destination path that corresponds to "/path/to/logo.jpg" will be "/path/to/logo.jpg.webp".
|
||||
*
|
||||
* The imageRoots are tried in order.
|
||||
* This means that ie "uploads" is preferred over "wp-content" even though the source resides in both (when uploads is inside wp-content)
|
||||
* So destination is ie [..]/wp-content/webp-express/uploads/[..]", rather than same but with "wp-content"
|
||||
*
|
||||
* @param string $source Path to source file
|
||||
* @param string $webExpressContentDirAbs
|
||||
* @param string $uploadDirAbs
|
||||
* @param DestinationOptions $destinationOptions
|
||||
* @param ImageRoots $imageRoots An image roots object
|
||||
*
|
||||
* @return string|false Returns path to destination corresponding to source, or false on failure
|
||||
*/
|
||||
public static function getDestinationPathCorrespondingToSource(
|
||||
$source,
|
||||
$webExpressContentDirAbs,
|
||||
$uploadDirAbs,
|
||||
$destinationOptions,
|
||||
$imageRoots
|
||||
) {
|
||||
// At this point, everything has already been checked for sanity. But for good meassure, lets
|
||||
// check the most important parts again. This is after all a public method.
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
$mingled = $destinationOptions->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;
|
||||
}
|
||||
|
||||
}
|
||||
42
lib/classes/DestinationOptions.php
Normal file
42
lib/classes/DestinationOptions.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class DestinationOptions
|
||||
{
|
||||
|
||||
public $mingled;
|
||||
public $useDocRoot;
|
||||
public $replaceExt;
|
||||
public $scope;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $imageRootDef assoc array containing "id", "url" and either "abs-path", "rel-path" or both.
|
||||
*/
|
||||
public function __construct($mingled, $useDocRoot, $replaceExt, $scope)
|
||||
{
|
||||
$this->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']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
229
lib/classes/DestinationUrl.php
Normal file
229
lib/classes/DestinationUrl.php
Normal file
@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/**
|
||||
* This class is not used yet! - and not finished!
|
||||
* It is the beginning of a refactor, where code is to be moved from AlterHtmlHelper to here
|
||||
*/
|
||||
class DestinationUrl
|
||||
{
|
||||
|
||||
/**
|
||||
* Gets relative path between a base url and another.
|
||||
* Returns false if the url isn't a subpath
|
||||
*
|
||||
* @param $imageUrl (ie "http://example.com/wp-content/image.jpg")
|
||||
* @param $baseUrl (ie "http://example.com/wp-content")
|
||||
* @return path or false (ie "/image.jpg")
|
||||
*/
|
||||
public static function getRelUrlPath($imageUrl, $baseUrl)
|
||||
{
|
||||
$baseUrlComponents = parse_url($baseUrl);
|
||||
/* ie:
|
||||
(
|
||||
[scheme] => 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);
|
||||
}*/
|
||||
|
||||
}
|
||||
100
lib/classes/DismissableGlobalMessages.php
Normal file
100
lib/classes/DismissableGlobalMessages.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
use \WebPExpress\State;
|
||||
use \WebPExpress\Messenger;
|
||||
|
||||
class DismissableGlobalMessages
|
||||
{
|
||||
|
||||
/**
|
||||
* Add dismissible message.
|
||||
*
|
||||
* @param string $id An identifier, ie "suggest_enable_pngs"
|
||||
*/
|
||||
public static function addDismissableMessage($id)
|
||||
{
|
||||
$dismissableGlobalMessageIds = State::getState('dismissableGlobalMessageIds', []);
|
||||
|
||||
// Ensure we do not add a message that is already there
|
||||
if (in_array($id, $dismissableGlobalMessageIds)) {
|
||||
return;
|
||||
}
|
||||
$dismissableGlobalMessageIds[] = $id;
|
||||
State::setState('dismissableGlobalMessageIds', $dismissableGlobalMessageIds);
|
||||
}
|
||||
|
||||
public static function printDismissableMessage($level, $msg, $id, $buttons)
|
||||
{
|
||||
$msg .= '<br><br>';
|
||||
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 .= '<button type="button" class="button ' .
|
||||
(($i == 0) ? 'button-primary' : '') .
|
||||
'" onclick="' . $javascript . '" ' .
|
||||
'style="display:inline-block; margin-top:20px; margin-right:20px; ' . (($i > 0) ? 'float:right;' : '') .
|
||||
'">' . $button['text'] . '</button>';
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
86
lib/classes/DismissableMessages.php
Normal file
86
lib/classes/DismissableMessages.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
use \WebPExpress\State;
|
||||
use \WebPExpress\Messenger;
|
||||
|
||||
class DismissableMessages
|
||||
{
|
||||
|
||||
/**
|
||||
* Add dismissible message.
|
||||
*
|
||||
* @param string $id An identifier, ie "suggest_enable_pngs"
|
||||
*/
|
||||
public static function addDismissableMessage($id)
|
||||
{
|
||||
$dismissableMessageIds = State::getState('dismissableMessageIds', []);
|
||||
|
||||
// Ensure we do not add a message that is already there
|
||||
if (in_array($id, $dismissableMessageIds)) {
|
||||
return;
|
||||
}
|
||||
$dismissableMessageIds[] = $id;
|
||||
State::setState('dismissableMessageIds', $dismissableMessageIds);
|
||||
}
|
||||
|
||||
public static function printDismissableMessage($level, $msg, $id, $gotItText = '')
|
||||
{
|
||||
if ($gotItText != '') {
|
||||
$javascript = "jQuery(this).closest('div.notice').slideUp();";
|
||||
//$javascript = "console.log(jQuery(this).closest('div.notice'));";
|
||||
$javascript .= "jQuery.post(ajaxurl, {'action': 'webpexpress_dismiss_message', 'id': '" . $id . "'});";
|
||||
|
||||
$msg .= '<button type="button" class="button button-primary" onclick="' . $javascript . '" style="display:block; margin-top:20px">' . $gotItText . '</button>';
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
113
lib/classes/EwwwTools.php
Normal file
113
lib/classes/EwwwTools.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
// This class does NOT, and MAY NOT rely on Wordpress functions (it is used in WebPOnDemand)
|
||||
class EwwwTools
|
||||
{
|
||||
/**
|
||||
* Mark ewww api keys as non functional in config.json
|
||||
*
|
||||
* @return boolean If it went well.
|
||||
*/
|
||||
private static function markApiKeysAsNonFunctionalInConfig($apiKeysToMarkAsNonFunctional, $configDir)
|
||||
{
|
||||
$config = FileHelper::loadJSONOptions($configDir . '/config.json');
|
||||
if ($config === false) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($config['converters'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($config['converters'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($config['converters'] as &$c) {
|
||||
if (!isset($c['converter'])) {
|
||||
continue;
|
||||
}
|
||||
if ($c['converter'] == 'ewww') {
|
||||
if (!isset($c['non-functional-api-keys'])) {
|
||||
$c['non-functional-api-keys'] = [];
|
||||
}
|
||||
$c['non-functional-api-keys'] = array_unique(
|
||||
array_merge($c['non-functional-api-keys'], $apiKeysToMarkAsNonFunctional)
|
||||
);
|
||||
|
||||
// Do we have an api-key-2 which is not "blacklisted"?
|
||||
$haveBackupKey = (isset($c['options']['api-key-2']) && !empty($c['options']['api-key-2']));
|
||||
$switchToBackupKey = $haveBackupKey && (!in_array($c['options']['api-key-2'], $c['non-functional-api-keys']));
|
||||
|
||||
if ($switchToBackupKey) {
|
||||
$temp = $c['options']['api-key'];
|
||||
$c['options']['api-key'] = $c['options']['api-key-2'];
|
||||
$c['options']['api-key-2'] = $temp;
|
||||
} else {
|
||||
// deactivate converter, we must then.
|
||||
$c['deactivated'] = true;
|
||||
$c['working'] = false;
|
||||
}
|
||||
|
||||
//$successfulWrite = Config::saveConfigurationFileAndWodOptions($config);
|
||||
$successfulWrite = FileHelper::saveJSONOptions($configDir . '/config.json', $config);
|
||||
return $successfulWrite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove ewww in wod-options.json
|
||||
*
|
||||
* @return boolean If it went well.
|
||||
*/
|
||||
private static function removeEwwwFromWodOptions($apiKeysToMarkAsNonFunctional, $configDir)
|
||||
{
|
||||
$wodOptions = FileHelper::loadJSONOptions($configDir . '/wod-options.json');
|
||||
if ($config === false) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($wodOptions['webp-convert']['convert']['converters'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($wodOptions['webp-convert']['convert']['converters'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($wodOptions['webp-convert']['convert']['converters'] as $i => $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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
395
lib/classes/FileHelper.php
Normal file
395
lib/classes/FileHelper.php
Normal file
@ -0,0 +1,395 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class FileHelper
|
||||
{
|
||||
|
||||
public static function fileExists($filename) {
|
||||
return @file_exists($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file permission of a file (integer). Only get the last part, ie 0644
|
||||
* If failure, it returns false
|
||||
*/
|
||||
public static function filePerm($filename) {
|
||||
if (!self::fileExists($filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// fileperms can still fail. In that case, it returns false
|
||||
$perm = @fileperms($filename);
|
||||
if ($perm === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return octdec(substr(decoct($perm), -4));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get file permission of a file (integer). Only get the last part, ie 0644
|
||||
* If failure, it returns $fallback
|
||||
*/
|
||||
public static function filePermWithFallback($filename, $fallback) {
|
||||
$perm = self::filePerm($filename);
|
||||
if ($perm === false) {
|
||||
return $fallback;
|
||||
}
|
||||
return $perm;
|
||||
}
|
||||
|
||||
public static function humanReadableFilePerm($mode) {
|
||||
return substr(decoct($mode), -4);
|
||||
}
|
||||
|
||||
public static function humanReadableFilePermOfFile($filename) {
|
||||
return self::humanReadableFilePerm(self::filePerm($filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* As the return value of the PHP function isn't reliable,
|
||||
* we have our own chmod.
|
||||
*/
|
||||
public static function chmod($filename, $mode) {
|
||||
// In case someone carelessly passed the result of a filePerm call, which was false:
|
||||
if ($mode === false) {
|
||||
return false;
|
||||
}
|
||||
$existingPermission = self::filePerm($filename);
|
||||
if ($mode === $existingPermission) {
|
||||
return true;
|
||||
}
|
||||
if (@chmod($filename, $mode)) {
|
||||
// in some cases chmod returns true, even though it did not succeed!
|
||||
// - so we test if our operation had the desired effect.
|
||||
if (self::filePerm($filename) !== $mode) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function chmod_r($dir, $dirPerm = null, $filePerm = null, $uid = null, $gid = null, $regexFileMatchPattern = null, $regexDirMatchPattern = null) {
|
||||
if (!@file_exists($dir) || (!@is_dir($dir))) {
|
||||
return;
|
||||
}
|
||||
$fileIterator = new \FilesystemIterator($dir);
|
||||
|
||||
while ($fileIterator->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);
|
||||
}
|
||||
|
||||
}
|
||||
436
lib/classes/HTAccess.php
Normal file
436
lib/classes/HTAccess.php
Normal file
@ -0,0 +1,436 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\HTAccessRules;
|
||||
use \WebPExpress\Paths;
|
||||
use \WebPExpress\State;
|
||||
|
||||
class HTAccess
|
||||
{
|
||||
|
||||
public static function inlineInstructions($instructions, $marker)
|
||||
{
|
||||
if ($marker == 'WebP Express') {
|
||||
return [];
|
||||
} else {
|
||||
return $instructions;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Must be parsed ie "wp-content", "index", etc. Not real dirs
|
||||
*/
|
||||
public static function addToActiveHTAccessDirsArray($dirId)
|
||||
{
|
||||
$activeHtaccessDirs = State::getState('active-htaccess-dirs', []);
|
||||
if (!in_array($dirId, $activeHtaccessDirs)) {
|
||||
$activeHtaccessDirs[] = $dirId;
|
||||
State::setState('active-htaccess-dirs', array_values($activeHtaccessDirs));
|
||||
}
|
||||
}
|
||||
|
||||
public static function removeFromActiveHTAccessDirsArray($dirId)
|
||||
{
|
||||
$activeHtaccessDirs = State::getState('active-htaccess-dirs', []);
|
||||
if (in_array($dirId, $activeHtaccessDirs)) {
|
||||
$activeHtaccessDirs = array_diff($activeHtaccessDirs, [$dirId]);
|
||||
State::setState('active-htaccess-dirs', array_values($activeHtaccessDirs));
|
||||
}
|
||||
}
|
||||
|
||||
public static function isInActiveHTAccessDirsArray($dirId)
|
||||
{
|
||||
$activeHtaccessDirs = State::getState('active-htaccess-dirs', []);
|
||||
return (in_array($dirId, $activeHtaccessDirs));
|
||||
}
|
||||
|
||||
public static function hasRecordOfSavingHTAccessToDir($dir) {
|
||||
$dirId = Paths::getAbsDirId($dir);
|
||||
if ($dirId !== false) {
|
||||
return self::isInActiveHTAccessDirsArray($dirId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|false Rules, or false if no rules found or file does not exist.
|
||||
*/
|
||||
public static function extractWebPExpressRulesFromHTAccess($filename) {
|
||||
if (FileHelper::fileExists($filename)) {
|
||||
$content = FileHelper::loadFile($filename);
|
||||
if ($content === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pos1 = strpos($content, '# BEGIN WebP Express');
|
||||
if ($pos1 === false) {
|
||||
return false;
|
||||
}
|
||||
$pos2 = strrpos($content, '# END WebP Express');
|
||||
if ($pos2 === false) {
|
||||
return false;
|
||||
}
|
||||
return substr($content, $pos1, $pos2 - $pos1);
|
||||
} else {
|
||||
// the .htaccess isn't even there. So there are no rules.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sneak peak into .htaccess to see if we have rules in it
|
||||
* This may not be possible (it requires read permission)
|
||||
* Return true, false, or null if we just can't tell
|
||||
*/
|
||||
public static function haveWeRulesInThisHTAccess($filename) {
|
||||
if (FileHelper::fileExists($filename)) {
|
||||
$content = FileHelper::loadFile($filename);
|
||||
if ($content === false) {
|
||||
return null;
|
||||
}
|
||||
$weRules = (self::extractWebPExpressRulesFromHTAccess($filename));
|
||||
if ($weRules === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (strpos($weRules, '<IfModule ') !== false);
|
||||
} else {
|
||||
// the .htaccess isn't even there. So there are no rules.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function haveWeRulesInThisHTAccessBestGuess($filename)
|
||||
{
|
||||
// First try to sneak peak. May return null if it cannot be determined.
|
||||
$result = self::haveWeRulesInThisHTAccess($filename);
|
||||
if ($result === true) {
|
||||
return true;
|
||||
}
|
||||
if ($result === null) {
|
||||
// We were not allowed to sneak-peak.
|
||||
// Well, good thing that we stored successful .htaccess write locations ;)
|
||||
// If we recorded a successful write, then we assume there are still rules there
|
||||
// If we did not, we assume there are no rules there
|
||||
$dir = FileHelper::dirName($filename);
|
||||
return self::hasRecordOfSavingHTAccessToDir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getRootsWithWebPExpressRulesIn()
|
||||
{
|
||||
$allIds = Paths::getImageRootIds();
|
||||
$allIds[] = 'cache';
|
||||
$result = [];
|
||||
foreach ($allIds as $imageRootId) {
|
||||
$filename = Paths::getAbsDirById($imageRootId) . '/.htaccess';
|
||||
if (self::haveWeRulesInThisHTAccessBestGuess($filename)) {
|
||||
$result[] = $imageRootId;
|
||||
}
|
||||
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function saveHTAccessRulesToFile($filename, $rules, $createIfMissing = false) {
|
||||
if (!@file_exists($filename)) {
|
||||
if (!$createIfMissing) {
|
||||
return false;
|
||||
}
|
||||
// insert_with_markers will create file if it doesn't exist, so we can continue...
|
||||
}
|
||||
|
||||
$existingFilePermission = null;
|
||||
$existingDirPermission = null;
|
||||
|
||||
// Try to make .htaccess writable if its not
|
||||
if (@file_exists($filename)) {
|
||||
if (!@is_writable($filename)) {
|
||||
$existingFilePermission = FileHelper::filePerm($filename);
|
||||
@chmod($filename, 0664); // chmod may fail, we know...
|
||||
}
|
||||
} else {
|
||||
$dir = FileHelper::dirName($filename);
|
||||
if (!@is_writable($dir)) {
|
||||
$existingDirPermission = FileHelper::filePerm($dir);
|
||||
@chmod($dir, 0775);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add rules to .htaccess */
|
||||
if (!function_exists('insert_with_markers')) {
|
||||
require_once ABSPATH . 'wp-admin/includes/misc.php';
|
||||
}
|
||||
|
||||
// Convert to array, because string version has bugs in Wordpress 4.3
|
||||
$rules = explode("\n", $rules);
|
||||
|
||||
add_filter('insert_with_markers_inline_instructions', array('\WebPExpress\HTAccess', 'inlineInstructions'), 10, 2);
|
||||
|
||||
$success = insert_with_markers($filename, 'WebP Express', $rules);
|
||||
|
||||
// Revert file or dir permissions
|
||||
if (!is_null($existingFilePermission)) {
|
||||
@chmod($filename, $existingFilePermission);
|
||||
}
|
||||
if (!is_null($existingDirPermission)) {
|
||||
@chmod($dir, $existingDirPermission);
|
||||
}
|
||||
|
||||
if ($success) {
|
||||
State::setState('htaccess-rules-saved-at-some-point', true);
|
||||
|
||||
//$containsRules = (strpos(implode('',$rules), '# Redirect images to webp-on-demand.php') != false);
|
||||
$containsRules = (strpos(implode('',$rules), '<IfModule mod_rewrite.c>') !== 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 = '<br>';
|
||||
$links .= '<a href="/' . $webpExpressRoot . '/test/test.jpg?debug&time=' . time() . '" target="_blank">Convert test image (show debug)</a><br>';
|
||||
$links .= '<a href="/' . $webpExpressRoot . '/test/test.jpg?' . time() . '" target="_blank">Convert test image</a><br>';
|
||||
}
|
||||
// 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 .= '<p>Rewrite rules were saved to the following files:</p>';
|
||||
foreach ($successfullWrites as $rootId) {
|
||||
$rootIdName = $rootId;
|
||||
if ($rootIdName == 'cache') {
|
||||
$rootIdName = 'webp folder';
|
||||
}
|
||||
$msg .= '<i>' . Paths::getAbsDirById($rootId) . '/.htaccess</i> (' . $rootIdName . ')<br>';
|
||||
}
|
||||
}
|
||||
|
||||
if (count($successfulDeactivations) > 0) {
|
||||
$msg .= '<p>Rewrite rules were removed from the following files:</p>';
|
||||
foreach ($successfulDeactivations as $rootId) {
|
||||
$rootIdName = $rootId;
|
||||
if ($rootIdName == 'cache') {
|
||||
$rootIdName = 'webp folder';
|
||||
}
|
||||
$msg .= '<i>' . Paths::getAbsDirById($rootId) . '/.htaccess</i> (' . $rootIdName . ')<br>';
|
||||
}
|
||||
}
|
||||
|
||||
if ($msg != '') {
|
||||
Messenger::addMessage(
|
||||
($success ? 'success' : 'info'),
|
||||
$msg
|
||||
);
|
||||
}
|
||||
|
||||
if (count($failedWrites) > 0) {
|
||||
$msg = '<p>Failed writing rewrite rules to the following files:</p>';
|
||||
foreach ($failedWrites as $rootId) {
|
||||
$msg .= '<i>' . Paths::getAbsDirById($rootId) . '/.htaccess</i> (' . $rootId . ')<br>';
|
||||
}
|
||||
$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 = '<p>Failed deleting unused rewrite rules in the following files:</p>';
|
||||
foreach ($failedDeactivations as $rootId) {
|
||||
$msg .= '<i>' . Paths::getAbsDirById($rootId) . '/.htaccess</i> (' . $rootId . ')<br>';
|
||||
}
|
||||
$msg .= 'You need to change the file permissions to allow WebP Express to remove the rules or ' .
|
||||
'remove them manually';
|
||||
Messenger::addMessage('error', $msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
187
lib/classes/HTAccessCapabilityTestRunner.php
Normal file
187
lib/classes/HTAccessCapabilityTestRunner.php
Normal file
@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/*
|
||||
This functionality will be moved to a separate project.
|
||||
|
||||
Btw:
|
||||
Seems someone else got similar idea:
|
||||
http://christian.roy.name/blog/detecting-modrewrite-using-php
|
||||
*/
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
use \HtaccessCapabilityTester\HtaccessCapabilityTester;
|
||||
|
||||
include_once WEBPEXPRESS_PLUGIN_DIR . '/vendor/autoload.php';
|
||||
|
||||
class HTAccessCapabilityTestRunner
|
||||
{
|
||||
|
||||
public static $cachedResults;
|
||||
|
||||
/**
|
||||
* Tests if a test script responds with "pong"
|
||||
*/
|
||||
private static function canRunPingPongTestScript($url)
|
||||
{
|
||||
$response = wp_remote_get($url, ['timeout' => 10]);
|
||||
//echo '<pre>' . print_r($response, true) . '</pre>';
|
||||
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'
|
||||
<FilesMatch "(webp-on-demand\.php|webp-realizer\.php|ping\.php|ping\.txt)$">
|
||||
<IfModule !mod_authz_core.c>
|
||||
Order deny,allow
|
||||
Allow from all
|
||||
</IfModule>
|
||||
<IfModule mod_authz_core.c>
|
||||
Require all granted
|
||||
</IfModule>
|
||||
</FilesMatch>
|
||||
EOD;
|
||||
return $hct->crashTest($rules, 'grant-all');
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
//error_log('test: ' . $testName . ':' . (($testResult === true) ? 'ok' : ($testResult === false ? 'failed' : 'hm')));
|
||||
|
||||
throw new \Exception('Unknown test:' . $testName);
|
||||
}
|
||||
|
||||
|
||||
public static function modRewriteWorking()
|
||||
{
|
||||
return self::runOrGetCached('modRewriteWorking');
|
||||
}
|
||||
|
||||
public static function htaccessEnabled()
|
||||
{
|
||||
return self::runOrGetCached('htaccessEnabled');
|
||||
}
|
||||
|
||||
public static function modHeadersLoaded()
|
||||
{
|
||||
return self::runOrGetCached('modHeadersLoaded');
|
||||
}
|
||||
|
||||
public static function modHeaderWorking()
|
||||
{
|
||||
return self::runOrGetCached('modHeaderWorking');
|
||||
}
|
||||
|
||||
public static function passThroughEnvWorking()
|
||||
{
|
||||
return self::runOrGetCached('passThroughEnvWorking');
|
||||
}
|
||||
|
||||
public static function passThroughHeaderWorking()
|
||||
{
|
||||
return self::runOrGetCached('passThroughHeaderWorking');
|
||||
}
|
||||
|
||||
public static function grantAllAllowed()
|
||||
{
|
||||
return self::runOrGetCached('grantAllAllowed');
|
||||
}
|
||||
|
||||
public static function canRunTestScriptInWOD()
|
||||
{
|
||||
return self::runOrGetCached('canRunTestScriptInWOD');
|
||||
}
|
||||
|
||||
public static function canRunTestScriptInWOD2()
|
||||
{
|
||||
return self::runOrGetCached('canRunTestScriptInWOD2');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
1200
lib/classes/HTAccessRules.php
Normal file
1200
lib/classes/HTAccessRules.php
Normal file
File diff suppressed because it is too large
Load Diff
42
lib/classes/HandleDeleteFileHook.php
Normal file
42
lib/classes/HandleDeleteFileHook.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
use \WebPExpress\Convert;
|
||||
use \WebPExpress\Mime;
|
||||
use \WebPExpress\SanityCheck;
|
||||
|
||||
class HandleDeleteFileHook
|
||||
{
|
||||
|
||||
/**
|
||||
* hook: wp_delete_file
|
||||
*/
|
||||
public static function deleteAssociatedWebP($filename)
|
||||
{
|
||||
try {
|
||||
$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($filename);
|
||||
|
||||
$mimeTypes = [
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
];
|
||||
if (!Mime::isOneOfTheseImageMimeTypes($filename, $mimeTypes)) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
$config = Config::loadConfigAndFix();
|
||||
$destination = Convert::getDestination($filename, $config);
|
||||
if (@file_exists($destination)) {
|
||||
if (@unlink($destination)) {
|
||||
Convert::updateBiggerThanOriginalMark($filename, $destination, $config);
|
||||
} else {
|
||||
error_log('WebP Express failed deleting webp:' . $destination);
|
||||
}
|
||||
}
|
||||
} catch (SanityException $e) {
|
||||
// fail silently. (maybe we should write to debug log instead?)
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
||||
90
lib/classes/HandleUploadHooks.php
Normal file
90
lib/classes/HandleUploadHooks.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\Convert;
|
||||
use \WebPExpress\Mime;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
|
||||
class HandleUploadHooks
|
||||
{
|
||||
|
||||
private static $config;
|
||||
|
||||
/**
|
||||
* Convert if:
|
||||
* - Option has been enabled
|
||||
* - We are not in "No conversion" mode
|
||||
* - The mime type is one of the ones the user has activated (in config)
|
||||
*/
|
||||
private static function convertIf($filename)
|
||||
{
|
||||
if (!isset(self::$config)) {
|
||||
self::$config = Config::loadConfigAndFix();
|
||||
}
|
||||
|
||||
$config = &self::$config;
|
||||
|
||||
if (!$config['convert-on-upload']) {
|
||||
return;
|
||||
}
|
||||
if ($config['operation-mode'] == 'no-conversion') {
|
||||
return;
|
||||
}
|
||||
|
||||
//$mimeType = getimagesize($filename)['mime'];
|
||||
|
||||
$allowedMimeTypes = [];
|
||||
$imageTypes = $config['image-types'];
|
||||
if ($imageTypes & 1) {
|
||||
$allowedMimeTypes[] = 'image/jpeg';
|
||||
$allowedMimeTypes[] = 'image/jpg'; /* don't think "image/jpg" is necessary, but just in case */
|
||||
}
|
||||
if ($imageTypes & 2) {
|
||||
$allowedMimeTypes[] = 'image/png';
|
||||
}
|
||||
|
||||
if (!in_array(Mime::getMimeTypeOfMedia($filename), $allowedMimeTypes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Convert::convertFile($filename, $config);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* hook: handle_upload
|
||||
* $filename is ie "/var/www/webp-express-tests/we0/wordpress/uploads-moved/image4-10-150x150.jpg"
|
||||
*/
|
||||
public static function handleUpload($filearray, $overrides = false, $ignore = false)
|
||||
{
|
||||
if (isset($filearray['file'])) {
|
||||
try {
|
||||
$filename = SanityCheck::absPathExistsAndIsFileInDocRoot($filearray['file']);
|
||||
self::convertIf($filename);
|
||||
} catch (SanityException $e) {
|
||||
// fail silently. (maybe we should write to debug log instead?)
|
||||
}
|
||||
}
|
||||
return $filearray;
|
||||
}
|
||||
|
||||
/**
|
||||
* hook: image_make_intermediate_size
|
||||
* $filename is ie "/var/www/webp-express-tests/we0/wordpress/uploads-moved/image4-10-150x150.jpg"
|
||||
*/
|
||||
public static function handleMakeIntermediateSize($filename)
|
||||
{
|
||||
if (!is_null($filename)) {
|
||||
try {
|
||||
$filenameToConvert = SanityCheck::absPathExistsAndIsFileInDocRoot($filename);
|
||||
self::convertIf($filenameToConvert);
|
||||
} catch (SanityException $e) {
|
||||
// fail silently. (maybe we should write to debug log instead?)
|
||||
}
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
}
|
||||
53
lib/classes/ImageRoot.php
Normal file
53
lib/classes/ImageRoot.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\PathHelper;
|
||||
|
||||
class ImageRoot
|
||||
{
|
||||
public $id;
|
||||
private $imageRootDef;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $imageRootDef assoc array containing "id", "url" and either "abs-path", "rel-path" or both.
|
||||
*/
|
||||
public function __construct($imageRootDef)
|
||||
{
|
||||
$this->imageRootDef = $imageRootDef;
|
||||
$this->id = $imageRootDef['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get / calculate abs path.
|
||||
*
|
||||
* If "rel-path" is set and document root is available, the abs path will be calculated from the relative path.
|
||||
* Otherwise the "abs-path" is returned.
|
||||
* @throws Exception In case rel-path is not
|
||||
*/
|
||||
public function getAbsPath()
|
||||
{
|
||||
$def = $this->imageRootDef;
|
||||
if (isset($def['rel-path']) && PathHelper::isDocRootAvailable()) {
|
||||
return rtrim($_SERVER["DOCUMENT_ROOT"], '/') . '/' . $def['rel-path'];
|
||||
} elseif (isset($def['abs-path'])) {
|
||||
return $def['abs-path'];
|
||||
} else {
|
||||
if (!isset($def['rel-path'])) {
|
||||
throw new \Exception(
|
||||
'Image root definition in config file is must either have a "rel-path" or "abs-path" property defined. ' .
|
||||
'Probably your system setup has changed. Please re-save WebP Express options and regenerate .htaccess'
|
||||
);
|
||||
} else {
|
||||
throw new \Exception(
|
||||
'Image root definition in config file is defined by "rel-path". However, DOCUMENT_ROOT is unavailable so we ' .
|
||||
'cannot use that (as the rel-path is relative to that. ' .
|
||||
'Probably your system setup has changed. Please re-save WebP Express options and regenerate .htaccess'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
52
lib/classes/ImageRoots.php
Normal file
52
lib/classes/ImageRoots.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\ImageRoot;
|
||||
|
||||
class ImageRoots
|
||||
{
|
||||
private $imageRootsDef;
|
||||
private $imageRoots;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $imageRoots Array representation of image roots
|
||||
*/
|
||||
public function __construct($imageRootsDef)
|
||||
{
|
||||
$this->imageRootsDef = $imageRootsDef;
|
||||
|
||||
$this->imageRoots = [];
|
||||
foreach ($imageRootsDef as $i => $def)
|
||||
{
|
||||
$this->imageRoots[] = new ImageRoot($def);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image root by id.
|
||||
*
|
||||
* @return \WebPExpress\ImageRoot An image root object
|
||||
*/
|
||||
public function byId($id)
|
||||
{
|
||||
foreach ($this->imageRoots as $i => $imageRoot) {
|
||||
if ($imageRoot->id == $id) {
|
||||
return $imageRoot;
|
||||
}
|
||||
}
|
||||
throw new \Exception('Image root not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image roots array
|
||||
*
|
||||
* @return array An array of ImageRoot objects
|
||||
*/
|
||||
public function getArray()
|
||||
{
|
||||
return $this->imageRoots;
|
||||
}
|
||||
}
|
||||
60
lib/classes/KeepEwwwSubscriptionAlive.php
Normal file
60
lib/classes/KeepEwwwSubscriptionAlive.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\Messenger;
|
||||
use \WebPExpress\State;
|
||||
use \WebPConvert\Converters\Ewww;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class KeepEwwwSubscriptionAlive
|
||||
{
|
||||
public static function keepAlive($config = null) {
|
||||
include_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
if (is_null($config)) {
|
||||
$config = Config::loadConfigAndFix(false); // false, because we do not need to test if quality detection is working
|
||||
}
|
||||
|
||||
$ewww = Config::getConverterByName($config, 'ewww');
|
||||
if (!isset($ewww['options']['key'])) {
|
||||
return;
|
||||
}
|
||||
if (!$ewww['working']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ewwwConvertResult = Ewww::keepSubscriptionAlive(__DIR__ . '/../../test/very-small.jpg', $ewww['options']['key']);
|
||||
if ($ewwwConvertResult === true) {
|
||||
Messenger::addMessage(
|
||||
'info',
|
||||
'Successfully optimized regular jpg with <i>ewww</i> converter in order to keep the subscription alive'
|
||||
);
|
||||
State::setState('last-ewww-optimize', time());
|
||||
} else {
|
||||
Messenger::addMessage(
|
||||
'warning',
|
||||
'Failed optimizing regular jpg with <i>ewww</i> converter in order to keep the subscription alive'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function keepAliveIfItIsTime($config = null) {
|
||||
|
||||
$timeSinseLastSuccesfullOptimize = time() - State::getState('last-ewww-optimize', 0);
|
||||
if ($timeSinseLastSuccesfullOptimize > 3 * 30 * 24 * 60 * 60) {
|
||||
|
||||
$timeSinseLastOptimizeAttempt = time() - State::getState('last-ewww-optimize-attempt', 0);
|
||||
if ($timeSinseLastOptimizeAttempt > 14 * 24 * 60 * 60) {
|
||||
State::setState('last-ewww-optimize-attempt', time());
|
||||
self::keepAlive($config);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
99
lib/classes/LogPurge.php
Normal file
99
lib/classes/LogPurge.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class LogPurge
|
||||
{
|
||||
|
||||
/**
|
||||
* - Removes cache dir
|
||||
* - Removes all files with ".webp" extension in upload dir (if set to mingled)
|
||||
*/
|
||||
public static function purge()
|
||||
{
|
||||
DismissableMessages::dismissMessage('0.14.0/suggest-wipe-because-lossless');
|
||||
|
||||
$filter = [
|
||||
'only-png' => $onlyPng,
|
||||
'only-with-corresponding-original' => false
|
||||
];
|
||||
|
||||
$numDeleted = 0;
|
||||
$numFailed = 0;
|
||||
|
||||
$dir = Paths::getLogDirAbs();
|
||||
list($numDeleted, $numFailed) = self::purgeLogFilesInDir($dir);
|
||||
FileHelper::removeEmptySubFolders($dir);
|
||||
|
||||
return [
|
||||
'delete-count' => $numDeleted,
|
||||
'fail-count' => $numFailed
|
||||
];
|
||||
|
||||
//$successInRemovingCacheDir = FileHelper::rrmdir(Paths::getCacheDirAbs());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Purge log files in a dir
|
||||
*
|
||||
* @return [num files deleted, num files failed to delete]
|
||||
*/
|
||||
private static function purgeLogFilesInDir($dir)
|
||||
{
|
||||
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::purgeLogFilesInDir($dir . "/" . $filename);
|
||||
$numFilesDeleted += $r1;
|
||||
$numFilesFailedDeleting += $r2;
|
||||
} else {
|
||||
|
||||
// its a file
|
||||
// Run through filters, which each may set "skipThis" to true
|
||||
|
||||
$skipThis = false;
|
||||
|
||||
// filter: It must have ".md" extension
|
||||
if (!$skipThis && !preg_match('#\.md$#', $filename)) {
|
||||
$skipThis = true;
|
||||
}
|
||||
|
||||
if (!$skipThis) {
|
||||
if (@unlink($dir . "/" . $filename)) {
|
||||
$numFilesDeleted++;
|
||||
} else {
|
||||
$numFilesFailedDeleting++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$fileIterator->next();
|
||||
}
|
||||
return [$numFilesDeleted, $numFilesFailedDeleting];
|
||||
}
|
||||
|
||||
public static function processAjaxPurgeLog()
|
||||
{
|
||||
|
||||
if (!check_ajax_referer('webpexpress-ajax-purge-log-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();
|
||||
}
|
||||
$result = self::purge($config);
|
||||
echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
96
lib/classes/Messenger.php
Normal file
96
lib/classes/Messenger.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
use \WebPExpress\State;
|
||||
|
||||
class Messenger
|
||||
{
|
||||
private static $printedStyles = false;
|
||||
|
||||
/**
|
||||
* @param string $level (info | success | warning | error)
|
||||
* @param string $msg the message (not translated)
|
||||
*
|
||||
* Hm... we should add some sprintf-like support
|
||||
* $msg = sprintf(__( 'You are on a very old version of PHP (%s). WebP Express may not work as intended.', 'webp-express' ), phpversion());
|
||||
*/
|
||||
public static function addMessage($level, $msg) {
|
||||
//error_log('add message:' . $msg);
|
||||
|
||||
Option::updateOption('webp-express-messages-pending', true, true); // We want this option to be autoloaded
|
||||
$pendingMessages = State::getState('pendingMessages', []);
|
||||
|
||||
// Ensure we do not add a message that is already pending.
|
||||
foreach ($pendingMessages as $i => $entry) {
|
||||
if ($entry['message'] == $msg) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
$pendingMessages[] = ['level' => $level, 'message' => $msg];
|
||||
State::setState('pendingMessages', $pendingMessages);
|
||||
}
|
||||
|
||||
public static function printMessage($level, $msg) {
|
||||
if (!(self::$printedStyles)) {
|
||||
global $wp_version;
|
||||
if (floatval(substr($wp_version, 0, 3)) < 4.1) {
|
||||
// Actually, I don't know precisely what version the styles were introduced.
|
||||
// They are there in 4.1. They are not there in 4.0
|
||||
self::printMessageStylesForOldWordpress();
|
||||
}
|
||||
self::$printedStyles = true;
|
||||
}
|
||||
|
||||
//$msg = __( $msg, 'webp-express'); // uncommented. We should add some sprintf-like functionality before making the plugin translatable
|
||||
printf(
|
||||
'<div class="%1$s"><div style="margin:10px 0">%2$s</div></div>',
|
||||
//esc_attr('notice notice-' . $level . ' is-dismissible'),
|
||||
esc_attr('notice notice-' . $level),
|
||||
$msg
|
||||
);
|
||||
}
|
||||
|
||||
private static function printMessageStylesForOldWordpress() {
|
||||
?>
|
||||
<style>
|
||||
/* In Older Wordpress (ie 4.0), .notice is not declared */
|
||||
.notice {
|
||||
background: #fff;
|
||||
border-left: 4px solid #fff;
|
||||
-webkit-box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
|
||||
box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
|
||||
margin: 10px 15px 2px 2px;
|
||||
padding: 1px 12px;
|
||||
}
|
||||
.notice-error {
|
||||
border-left-color: #dc3232;
|
||||
}
|
||||
.notice-success {esc_attr('notice notice-' . $level . ' is-dismissible'),
|
||||
border-left-color: #46b450;
|
||||
}
|
||||
.notice-info {
|
||||
border-left-color: #00a0d2;
|
||||
}
|
||||
.notice-warning {
|
||||
border-left-color: #ffb900;
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function printPendingMessages() {
|
||||
|
||||
$messages = State::getState('pendingMessages', []);
|
||||
|
||||
foreach ($messages as $message) {
|
||||
self::printMessage($message['level'], $message['message']);
|
||||
}
|
||||
|
||||
State::setState('pendingMessages', []);
|
||||
|
||||
Option::updateOption('webp-express-messages-pending', false, true);
|
||||
}
|
||||
|
||||
}
|
||||
55
lib/classes/Mime.php
Normal file
55
lib/classes/Mime.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\Convert;
|
||||
|
||||
class Mime
|
||||
{
|
||||
|
||||
public static function getMimeTypeOfMedia($filename)
|
||||
{
|
||||
// ensure filename is not empty, as wp_get_image_mime() goes fatal if it is
|
||||
if ($filename === '') {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
// First try the Wordpress function if available (it was introduced in 4.7.1)
|
||||
if (function_exists('wp_get_image_mime')) {
|
||||
|
||||
// PS: wp_get_image_mime tries exif_imagetype and getimagesize and returns false if no methods are available
|
||||
$mimeType = wp_get_image_mime($filename);
|
||||
if ($mimeType !== false) {
|
||||
return $mimeType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Try mime_content_type
|
||||
if (function_exists('mime_content_type')) {
|
||||
$mimeType = mime_content_type($filename);
|
||||
if ($mimeType !== false) {
|
||||
return $mimeType;
|
||||
}
|
||||
}
|
||||
|
||||
if (function_exists('wp_check_filetype')) { // introduced in 2.0.4
|
||||
// Try wordpress method, which simply uses the file extension and a map
|
||||
$mimeType = wp_check_filetype($filename)['type'];
|
||||
if ($mimeType !== false) {
|
||||
return $mimeType;
|
||||
}
|
||||
}
|
||||
|
||||
// Don't say we didn't try!
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
public static function isOneOfTheseImageMimeTypes($filename, $imageMimeTypes)
|
||||
{
|
||||
$detectedMimeType = self::getMimeTypeOfMedia($filename);
|
||||
return in_array($detectedMimeType, $imageMimeTypes);
|
||||
}
|
||||
|
||||
}
|
||||
36
lib/classes/Multisite.php
Normal file
36
lib/classes/Multisite.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class Multisite
|
||||
{
|
||||
public static $networkActive;
|
||||
|
||||
/*
|
||||
Needed because is_plugin_active_for_network() does not return true right after network activation
|
||||
*/
|
||||
public static function overrideIsNetworkActivated($networkActive)
|
||||
{
|
||||
self::$networkActive = $networkActive;
|
||||
}
|
||||
|
||||
public static function isNetworkActivated()
|
||||
{
|
||||
if (!is_null(self::$networkActive)) {
|
||||
return self::$networkActive;
|
||||
}
|
||||
if (!self::isMultisite()) {
|
||||
return false;
|
||||
}
|
||||
if (!function_exists( 'is_plugin_active_for_network')) {
|
||||
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
|
||||
}
|
||||
return is_plugin_active_for_network('webp-express/webp-express.php');
|
||||
}
|
||||
|
||||
public static function isMultisite()
|
||||
{
|
||||
return is_multisite();
|
||||
}
|
||||
|
||||
}
|
||||
39
lib/classes/Option.php
Normal file
39
lib/classes/Option.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Multisite;
|
||||
|
||||
class Option
|
||||
{
|
||||
|
||||
public static function getOption($optionName, $default = false)
|
||||
{
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
return get_site_option($optionName, $default);
|
||||
} else {
|
||||
return get_option($optionName, $default);
|
||||
}
|
||||
}
|
||||
|
||||
public static function updateOption($optionName, $value, $autoload = null)
|
||||
{
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
//error_log('update option (network):' . $optionName . ':' . $value);
|
||||
return update_site_option($optionName, $value);
|
||||
} else {
|
||||
//error_log('update option:' . $optionName . ':' . $value);
|
||||
return update_option($optionName, $value, $autoload);
|
||||
}
|
||||
}
|
||||
|
||||
public static function deleteOption($optionName)
|
||||
{
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
return delete_site_option($optionName);
|
||||
} else {
|
||||
return delete_option($optionName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
20
lib/classes/OptionsPage.php
Normal file
20
lib/classes/OptionsPage.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class OptionsPage
|
||||
{
|
||||
|
||||
// callback (registred in AdminUi)
|
||||
public static function display() {
|
||||
include WEBPEXPRESS_PLUGIN_DIR . '/lib/options/page.php';
|
||||
}
|
||||
|
||||
public static function enqueueScripts() {
|
||||
include WEBPEXPRESS_PLUGIN_DIR . '/lib/options/enqueue_scripts.php';
|
||||
}
|
||||
}
|
||||
16
lib/classes/OptionsPageHooks.php
Normal file
16
lib/classes/OptionsPageHooks.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class OptionsPageHooks
|
||||
{
|
||||
|
||||
// callback for 'admin_post_webpexpress_settings_submit' (registred in AdminInit::addHooks)
|
||||
public static function submitHandler() {
|
||||
include WEBPEXPRESS_PLUGIN_DIR . '/lib/options/submit.php';
|
||||
}
|
||||
}
|
||||
481
lib/classes/PathHelper.php
Normal file
481
lib/classes/PathHelper.php
Normal file
@ -0,0 +1,481 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class PathHelper
|
||||
{
|
||||
|
||||
public static function isDocRootAvailable() {
|
||||
|
||||
// BTW:
|
||||
// Note that DOCUMENT_ROOT does not end with trailing slash on old litespeed servers:
|
||||
// https://www.litespeedtech.com/support/forum/threads/document_root-trailing-slash.5304/
|
||||
|
||||
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
|
||||
return false;
|
||||
}
|
||||
if ($_SERVER['DOCUMENT_ROOT'] == '') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a path exists as is resolvable (will be unless it is outside open_basedir)
|
||||
*
|
||||
* @param string $absPath The path to test (must be absolute. symlinks allowed)
|
||||
* @return boolean The result
|
||||
*/
|
||||
public static function pathExistsAndIsResolvable($absPath) {
|
||||
if (!@realpath($absPath)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if document root is available, exists and symlinks are resolvable (resolved path is within open basedir)
|
||||
*
|
||||
* @return boolean The result
|
||||
*/
|
||||
public static function isDocRootAvailableAndResolvable() {
|
||||
return (
|
||||
self::isDocRootAvailable() &&
|
||||
self::pathExistsAndIsResolvable($_SERVER['DOCUMENT_ROOT'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the rewrite rules are using the absolute dir, the rewrite rules does not work if that dir
|
||||
* is outside document root. This poses a problem if some part of the document root has been symlinked.
|
||||
*
|
||||
* This method "unresolves" the document root part of a dir.
|
||||
* That is: It takes an absolute url, looks to see if it begins with the resolved document root.
|
||||
* In case it does, it replaces the resolved document root with the unresolved document root.
|
||||
*
|
||||
* Unfortunately we can only unresolve when document root is available and resolvable.
|
||||
* - which is sad, because the image-roots was introduced in order to get it to work on setups
|
||||
*/
|
||||
public static function fixAbsPathToUseUnresolvedDocRoot($absPath) {
|
||||
if (self::isDocRootAvailableAndResolvable()) {
|
||||
if (strpos($absPath, realpath($_SERVER['DOCUMENT_ROOT'])) === 0) {
|
||||
return $_SERVER['DOCUMENT_ROOT'] . substr($absPath, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
|
||||
}
|
||||
}
|
||||
return $absPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out if path is below - or equal to a path.
|
||||
*
|
||||
* "/var/www" below/equal to "/var"? : Yes
|
||||
* "/var/www" below/equal to "/var/www"? : Yes
|
||||
* "/var/www2" below/equal to "/var/www"? : No
|
||||
*/
|
||||
/*
|
||||
public static function isPathBelowOrEqualToPath($path1, $path2)
|
||||
{
|
||||
return (strpos($path1 . '/', $path2 . '/') === 0);
|
||||
//$rel = self::getRelDir($path2, $path1);
|
||||
//return (substr($rel, 0, 3) != '../');
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Calculate relative path from document root to a given absolute path (must exist and be resolvable) - if possible AND
|
||||
* if it can be done without directory traversal.
|
||||
*
|
||||
* The function is designed with the usual folders in mind (index, uploads, wp-content, plugins), which all presumably
|
||||
* exists and are within open_basedir.
|
||||
*
|
||||
* @param string $dir An absolute path (may contain symlinks). The path must exist and be resolvable.
|
||||
* @throws \Exception If it is not possible to get such path (ie if doc-root is unavailable or the dir is outside doc-root)
|
||||
* @return string Relative path to document root or empty string if document root is unavailable
|
||||
*/
|
||||
public static function getRelPathFromDocRootToDirNoDirectoryTraversalAllowed($dir)
|
||||
{
|
||||
if (!self::isDocRootAvailable()) {
|
||||
throw new \Exception('Cannot calculate relative path from document root to dir, as document root is not available');
|
||||
}
|
||||
|
||||
// First try unresolved.
|
||||
// This will even work when ie wp-content is symlinked to somewhere outside document root, while the symlink itself is within document root)
|
||||
$relPath = self::getRelDir($_SERVER['DOCUMENT_ROOT'], $dir);
|
||||
if (strpos($relPath, '../') !== 0) { // Check if relPath starts with "../" (if it does, we cannot use it)
|
||||
return $relPath;
|
||||
}
|
||||
|
||||
if (self::isDocRootAvailableAndResolvable()) {
|
||||
if (self::pathExistsAndIsResolvable($dir)) {
|
||||
// Try with both resolved
|
||||
$relPath = self::getRelDir(realpath($_SERVER['DOCUMENT_ROOT']), realpath($dir));
|
||||
if (strpos($relPath, '../') !== 0) {
|
||||
return $relPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Try with just document root resolved
|
||||
$relPath = self::getRelDir(realpath($_SERVER['DOCUMENT_ROOT']), $dir);
|
||||
if (strpos($relPath, '../') !== 0) {
|
||||
return $relPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (self::pathExistsAndIsResolvable($dir)) {
|
||||
// Try with dir resolved
|
||||
$relPath = self::getRelDir($_SERVER['DOCUMENT_ROOT'], realpath($dir));
|
||||
if (strpos($relPath, '../') !== 0) {
|
||||
return $relPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Problem:
|
||||
// - dir is already resolved (ie: /disk/the-content)
|
||||
// - document root is ie. /var/www/website/wordpress
|
||||
// - the unresolved symlink is ie. /var/www/website/wordpress/wp-content
|
||||
// - we do not know what the unresolved symlink is
|
||||
// The result should be "wp-content". But how do we get to that result?
|
||||
// I guess we must check out all folders below document root to see if anyone resolves to dir
|
||||
// we could start out trying usual suspects such as "wp-content" and "wp-content/uploads"
|
||||
//foreach (glob($dir . DIRECTORY_SEPARATOR . $filePattern) as $filename)
|
||||
/*
|
||||
$iter = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($_SERVER['DOCUMENT_ROOT'], \RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::SELF_FIRST,
|
||||
\RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
|
||||
);
|
||||
|
||||
foreach ($iter as $path => $dirObj) {
|
||||
if ($dirObj->isDir()) {
|
||||
if (realpath($path) == $dir) {
|
||||
//return $path;
|
||||
$relPath = self::getRelDir(realpath($_SERVER['DOCUMENT_ROOT']), $path);
|
||||
if (strpos($relPath, '../') !== 0) {
|
||||
return $relPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
// Ok, the above works - but when subfolders to the symlink is referenced. Ie referencing uploads when wp-content is symlinked
|
||||
// - dir is already resolved (ie: /disk/the-content/uploads)
|
||||
// - document root is ie. /var/www/website/wordpress
|
||||
// - the unresolved symlink is ie. /var/www/website/wordpress/wp-content/uploads
|
||||
// - we do not know what the unresolved symlink is
|
||||
// The result should be "wp-content/uploads". But how do we get to that result?
|
||||
|
||||
// What if we collect all symlinks below document root in a assoc array?
|
||||
// ['/disk/the-content' => 'wp-content']
|
||||
// Input is: '/disk/the-content/uploads'
|
||||
// 1. We check the symlinks and substitute. We get: 'wp-content/uploads'.
|
||||
// 2. We test if realpath($_SERVER['DOCUMENT_ROOT'] . '/' . 'wp-content/uploads') equals input.
|
||||
// It seems I have a solution!
|
||||
// - I shall continue work soon! - for a 0.15.1 release (test instance #26)
|
||||
// PS: cache the result of the symlinks in docroot collector.
|
||||
|
||||
throw new \Exception(
|
||||
'Cannot get relative path from document root to dir without resolving to directory traversal. ' .
|
||||
'It seems the dir is not below document root'
|
||||
);
|
||||
|
||||
/*
|
||||
if (!self::pathExistsAndIsResolvable($dir)) {
|
||||
throw new \Exception('Cannot calculate relative path from document root to dir. The path given is not resolvable (realpath fails)');
|
||||
}
|
||||
|
||||
|
||||
// Check if relPath starts with "../"
|
||||
if (strpos($relPath, '../') === 0) {
|
||||
|
||||
// Unresolved failed. Try with document root resolved
|
||||
$relPath = self::getRelDir(realpath($_SERVER['DOCUMENT_ROOT']), $dir);
|
||||
|
||||
if (strpos($relPath, '../') === 0) {
|
||||
|
||||
// Try with both resolved
|
||||
$relPath = self::getRelDir($dir, $dir);
|
||||
throw new \Exception('Cannot calculate relative path from document root to dir. The path given is not within document root');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return $relPath;
|
||||
} else {
|
||||
// We cannot get the resolved doc-root.
|
||||
// This might be ok as long as the (resolved) path we are examining begins with the configured doc-root.
|
||||
$relPath = self::getRelDir($_SERVER['DOCUMENT_ROOT'], $dir);
|
||||
|
||||
// Check if relPath starts with "../" (it may not)
|
||||
if (strpos($relPath, '../') === 0) {
|
||||
|
||||
// Well, that did not work. We can try the resolved path instead.
|
||||
if (!self::pathExistsAndIsResolvable($dir)) {
|
||||
throw new \Exception('Cannot calculate relative path from document root to dir. The path given is not resolvable (realpath fails)');
|
||||
}
|
||||
|
||||
$relPath = self::getRelDir($_SERVER['DOCUMENT_ROOT'], realpath($dir));
|
||||
if (strpos($relPath, '../') === 0) {
|
||||
|
||||
// That failed too.
|
||||
// Either it is in fact outside document root or it is because of a special setup.
|
||||
throw new \Exception(
|
||||
'Cannot calculate relative path from document root to dir. Either the path given is not within the configured document root or ' .
|
||||
'it is because of a special setup. The document root is outside open_basedir. If it is also symlinked, but the other Wordpress paths ' .
|
||||
'are not using that same symlink, it will not be possible to calculate the relative path.'
|
||||
);
|
||||
}
|
||||
}
|
||||
return $relPath;
|
||||
}*/
|
||||
}
|
||||
|
||||
public static function canCalculateRelPathFromDocRootToDir($dir)
|
||||
{
|
||||
try {
|
||||
$relPath = self::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed($dir);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find closest existing folder with symlinks expandend, using realpath.
|
||||
*
|
||||
* Note that if the input or the closest existing folder is outside open_basedir, no folder will
|
||||
* be found and an empty string will be returned.
|
||||
*
|
||||
* @return string closest existing path or empty string if none found (due to open_basedir restriction)
|
||||
*/
|
||||
public static function findClosestExistingFolderSymLinksExpanded($input) {
|
||||
|
||||
// The strategy is to first try the supplied directory. If it fails, try the parent, etc.
|
||||
$dir = $input;
|
||||
|
||||
// We count the levels up to avoid infinite loop - as good practice. It ought not to get that far
|
||||
$levelsUp = 0;
|
||||
|
||||
while ($levelsUp < 100) {
|
||||
// We suppress warning because we are aware that we might get a
|
||||
// open_basedir restriction warning.
|
||||
$realPathResult = @realpath($dir);
|
||||
if ($realPathResult !== false) {
|
||||
return $realPathResult;
|
||||
}
|
||||
// Stop at root. This will happen if the original path is outside basedir.
|
||||
if (($dir == '/') || (strlen($dir) < 4)) {
|
||||
return '';
|
||||
}
|
||||
// Peal off one directory
|
||||
$dir = @dirname($dir);
|
||||
$levelsUp++;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Look if filepath is within a dir path (both by string matching and by using realpath, see notes).
|
||||
*
|
||||
* Note that the naive string match does not resolve '..'. You might want to call ::canonicalize first.
|
||||
* Note that the realpath match requires: 1. that the dir exist and is within open_basedir
|
||||
* 2. that the closest existing folder within filepath is within open_basedir
|
||||
*
|
||||
* @param string $filePath Path to file. It may be non-existing.
|
||||
* @param string $dirPath Path to dir. It must exist and be within open_basedir in order for the realpath match to execute.
|
||||
*/
|
||||
public static function isFilePathWithinDirPath($filePath, $dirPath)
|
||||
{
|
||||
// See if $filePath begins with $dirPath + '/'.
|
||||
if (strpos($filePath, $dirPath . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strpos(self::canonicalize($filePath), self::canonicalize($dirPath) . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Also try with symlinks expanded.
|
||||
// As symlinks can only be retrieved with realpath and realpath fails with non-existing paths,
|
||||
// we settle with checking if closest existing folder in the filepath is within the dir.
|
||||
// If that is the case, then surely, the complete filepath is also within the dir.
|
||||
// Note however that it might be that the closest existing folder is not within the dir, while the
|
||||
// file would be (if it existed)
|
||||
// For WebP Express, we are pretty sure that the dirs we are checking against (uploads folder,
|
||||
// wp-content, plugins folder) exists. So getting the closest existing folder should be sufficient.
|
||||
// but could it be that these are outside open_basedir on some setups? Perhaps on a few systems.
|
||||
if (self::pathExistsAndIsResolvable($dirPath)) {
|
||||
$closestExistingDirOfFile = PathHelper::findClosestExistingFolderSymLinksExpanded($filePath);
|
||||
if (strpos($closestExistingDirOfFile, realpath($dirPath) . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look if path is within a dir path. Also tries expanding symlinks
|
||||
*
|
||||
* @param string $path Path to examine. It may be non-existing.
|
||||
* @param string $dirPath Path to dir. It must exist in order for symlinks to be expanded.
|
||||
*/
|
||||
public static function isPathWithinExistingDirPath($path, $dirPath)
|
||||
{
|
||||
if ($path == $dirPath) {
|
||||
return true;
|
||||
}
|
||||
// See if $filePath begins with $dirPath + '/'.
|
||||
if (strpos($path, $dirPath . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Also try with symlinks expanded (see comments in ::isFilePathWithinDirPath())
|
||||
$closestExistingDir = PathHelper::findClosestExistingFolderSymLinksExpanded($path);
|
||||
if (strpos($closestExistingDir . '/', $dirPath . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function frontslasher($str)
|
||||
{
|
||||
// TODO: replace backslash with frontslash
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace double slash with single slash. ie '/var//www/' => '/var/www/'
|
||||
* This allows you to lazely concatenate paths with '/' and then call this method to clean up afterwards.
|
||||
* Also removes triple slash etc.
|
||||
*/
|
||||
public static function fixDoubleSlash($str)
|
||||
{
|
||||
return preg_replace('/\/\/+/', '/', $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove trailing slash, if any
|
||||
*/
|
||||
public static function untrailSlash($str)
|
||||
{
|
||||
return rtrim($str, '/');
|
||||
//return preg_replace('/\/$/', '', $str);
|
||||
}
|
||||
|
||||
public static function backslashesToForwardSlashes($path) {
|
||||
return str_replace( "\\", '/', $path);
|
||||
}
|
||||
|
||||
// Canonicalize a path by resolving '../' and './'. It also replaces backslashes with forward slash
|
||||
// Got it from a comment here: http://php.net/manual/en/function.realpath.php
|
||||
// But fixed it (it could not handle './../')
|
||||
public static function canonicalize($path) {
|
||||
|
||||
$parts = explode('/', $path);
|
||||
|
||||
// Remove parts containing just '.' (and the empty holes afterwards)
|
||||
$parts = array_values(array_filter($parts, function($var) {
|
||||
return ($var != '.');
|
||||
}));
|
||||
|
||||
// Remove parts containing '..' and the preceding
|
||||
$keys = array_keys($parts, '..');
|
||||
foreach($keys as $keypos => $key) {
|
||||
array_splice($parts, $key - ($keypos * 2 + 1), 2);
|
||||
}
|
||||
return implode('/', $parts);
|
||||
}
|
||||
|
||||
public static function dirname($path) {
|
||||
return self::canonicalize($path . '/..');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base name of a path (the last component of a path - ie the filename).
|
||||
*
|
||||
* This function operates natively on the string and is not locale aware.
|
||||
* It only works with "/" path separators.
|
||||
*
|
||||
* @return string the last component of a path
|
||||
*/
|
||||
public static function basename($path) {
|
||||
$parts = explode('/', $path);
|
||||
return array_pop($parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns absolute path from a relative path and root
|
||||
* The result is canonicalized (dots and double-dots are resolved)
|
||||
*
|
||||
* @param $path Absolute path or relative path
|
||||
* @param $root What the path is relative to, if its relative
|
||||
*/
|
||||
public static function relPathToAbsPath($path, $root)
|
||||
{
|
||||
return self::canonicalize(self::fixDoubleSlash($root . '/' . $path));
|
||||
}
|
||||
|
||||
/**
|
||||
* isAbsPath
|
||||
* If path starts with '/', it is considered an absolute path (no Windows support)
|
||||
*
|
||||
* @param $path Path to inspect
|
||||
*/
|
||||
public static function isAbsPath($path)
|
||||
{
|
||||
return (substr($path, 0, 1) == '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns absolute path from a path which can either be absolute or relative to second argument.
|
||||
* If path starts with '/', it is considered an absolute path.
|
||||
* The result is canonicalized (dots and double-dots are resolved)
|
||||
*
|
||||
* @param $path Absolute path or relative path
|
||||
* @param $root What the path is relative to, if its relative
|
||||
*/
|
||||
public static function pathToAbsPath($path, $root)
|
||||
{
|
||||
if (self::isAbsPath($path)) {
|
||||
// path is already absolute
|
||||
return $path;
|
||||
} else {
|
||||
return self::relPathToAbsPath($path, $root);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get relative path between two absolute paths
|
||||
* Examples:
|
||||
* from '/var/www' to 'var/ddd'. Result: '../ddd'
|
||||
* from '/var/www' to 'var/www/images'. Result: 'images'
|
||||
* from '/var/www' to 'var/www'. Result: '.'
|
||||
*/
|
||||
public static function getRelDir($fromPath, $toPath)
|
||||
{
|
||||
$fromDirParts = explode('/', str_replace('\\', '/', self::canonicalize(self::untrailSlash($fromPath))));
|
||||
$toDirParts = explode('/', str_replace('\\', '/', self::canonicalize(self::untrailSlash($toPath))));
|
||||
$i = 0;
|
||||
while (($i < count($fromDirParts)) && ($i < count($toDirParts)) && ($fromDirParts[$i] == $toDirParts[$i])) {
|
||||
$i++;
|
||||
}
|
||||
$rel = "";
|
||||
for ($j = $i; $j < count($fromDirParts); $j++) {
|
||||
$rel .= "../";
|
||||
}
|
||||
|
||||
for ($j = $i; $j < count($toDirParts); $j++) {
|
||||
$rel .= $toDirParts[$j];
|
||||
if ($j < count($toDirParts)-1) {
|
||||
$rel .= '/';
|
||||
}
|
||||
}
|
||||
if ($rel == '') {
|
||||
$rel = '.';
|
||||
}
|
||||
return $rel;
|
||||
}
|
||||
|
||||
}
|
||||
879
lib/classes/Paths.php
Normal file
879
lib/classes/Paths.php
Normal file
@ -0,0 +1,879 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\Multisite;
|
||||
use \WebPExpress\PathHelper;
|
||||
|
||||
class Paths
|
||||
{
|
||||
public static function areAllImageRootsWithinDocRoot() {
|
||||
if (!PathHelper::isDocRootAvailable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$roots = self::getImageRootIds();
|
||||
foreach ($roots as $dirId) {
|
||||
$dir = self::getAbsDirById($dirId);
|
||||
if (!PathHelper::canCalculateRelPathFromDocRootToDir($dir)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we can use document root for calculating relative paths (which may not contain "/.." directory traversal)
|
||||
*
|
||||
* Note that this method allows document root to be outside open_basedir as long as document root is
|
||||
* non-empty AND it is possible to calculate relative paths to all image roots (including "index").
|
||||
* Here is a case when a relative CAN be calculated:
|
||||
* - Document root is configured to "/var/www/website" - which is also the absolute file path.
|
||||
* - open_basedir is set to "/var/www/website/wordpress"
|
||||
* - uploads is in "/var/www/website/wordpress/wp-content/uploads" (within open_basedir, as it should)
|
||||
* - "/wp-uploads" symlinks to "/var/www/website/wordpress")
|
||||
* - Wordpress has been configured to use "/wp-uploads" path for uploads.
|
||||
*
|
||||
* What happens?
|
||||
* First, it is tested if the configured upload path ("/wp-uploads") begins with the configured document root ("/var/www/website").
|
||||
* This fails.
|
||||
* Next, it is tested if the uploads path can be resolved. It can, as it is within the open_basedir.
|
||||
* Next, it is tested if the *resolved* the uploads path begins with the configured document root.
|
||||
* As "/var/www/website/wordpress/wp-content/uploads" begins with "/var/www/website", we have a match.
|
||||
* The relative path can be calculated to be "wordpress/wp-content/uploads".
|
||||
* Later, when the relative path is used, it will be used as $docRoot + "/" + $relPath, which
|
||||
* will be "/var/www/website/wordpress/wp-content/uploads". All is well.
|
||||
*
|
||||
* Here is a case where it CAN NOT be calculated:
|
||||
* - Document root is configured to "/the-website", which symlinks to "/var/www/website"
|
||||
* - open_basedir is set to "/var/www/website/wordpress"
|
||||
* - uploads is in "/var/www/website/wordpress/wp-content/uploads" and wordpress is configured to use that upload path.
|
||||
*
|
||||
* What happens?
|
||||
* First, it is tested if the configured upload path begins with the configured document root
|
||||
* "/var/www/website/wordpress/wp-content/uploads" does not begin with "/the-website", so it fails.
|
||||
* Next, it is tested if the *resolved* the uploads path begins with the configured document root.
|
||||
* The resolved uploads path is the same as the configured so it also fails.
|
||||
* Next, it is tested if Document root can be resolved. It can not, as the resolved path is not within open_basedir.
|
||||
* If it could, it would have been tested if the resolved path begins with the resolved document root and we would have
|
||||
* gotten a yes, and the relative path would have been "wordpress/wp-content/uploads" and it would work.
|
||||
* However: Document root could not be resolved and we could not get a result.
|
||||
* To sum the scenario up:
|
||||
* If document root is configured to a symlink which cannot be resolved then it will only be possible to get relative paths
|
||||
* when all other configured paths begins are relative to that symlink.
|
||||
*/
|
||||
public static function canUseDocRootForRelPaths() {
|
||||
if (!PathHelper::isDocRootAvailable()) {
|
||||
return false;
|
||||
}
|
||||
return self::areAllImageRootsWithinDocRoot();
|
||||
}
|
||||
|
||||
public static function canCalculateRelPathFromDocRootToDir($absPath) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we can use document root for structuring the cache dir.
|
||||
*
|
||||
* In order to structure the images by doc-root, WebP Express needs all images to be within document root.
|
||||
* Does WebP Express in addition to this need to be able to resolve document root?
|
||||
* Short answer is yes.
|
||||
* The long answer is available as a comment inside ConvertHelperIndependent::getDestination()
|
||||
*
|
||||
*/
|
||||
public static function canUseDocRootForStructuringCacheDir() {
|
||||
return (PathHelper::isDocRootAvailableAndResolvable() && self::canUseDocRootForRelPaths());
|
||||
}
|
||||
|
||||
public static function docRootStatusText()
|
||||
{
|
||||
if (!PathHelper::isDocRootAvailable()) {
|
||||
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
|
||||
return 'Unavailable (DOCUMENT_ROOT is not set in the global $_SERVER var)';
|
||||
}
|
||||
if ($_SERVER['DOCUMENT_ROOT'] == '') {
|
||||
return 'Unavailable (empty string)';
|
||||
}
|
||||
return 'Unavailable';
|
||||
}
|
||||
|
||||
$imageRootsWithin = self::canUseDocRootForRelPaths();
|
||||
if (!PathHelper::isDocRootAvailableAndResolvable()) {
|
||||
$status = 'Available, but either non-existing or not within open_basedir.' .
|
||||
($imageRootsWithin ? '' : ' And not all image roots are within that document root.');
|
||||
} elseif (!$imageRootsWithin) {
|
||||
$status = 'Available, but not all image roots are within that document root.';
|
||||
} else {
|
||||
$status = 'Available and its "realpath" is available too.';
|
||||
}
|
||||
if (self::canUseDocRootForStructuringCacheDir()) {
|
||||
$status .= ' Can be used for structuring cache dir.';
|
||||
} else {
|
||||
$status .= ' Cannot be used for structuring cache dir.';
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
public static function getAbsDirId($absDir) {
|
||||
switch ($absDir) {
|
||||
case self::getContentDirAbs():
|
||||
return 'wp-content';
|
||||
case self::getIndexDirAbs():
|
||||
return 'index';
|
||||
case self::getHomeDirAbs():
|
||||
return 'home';
|
||||
case self::getPluginDirAbs():
|
||||
return 'plugins';
|
||||
case self::getUploadDirAbs():
|
||||
return 'uploads';
|
||||
case self::getThemesDirAbs():
|
||||
return 'themes';
|
||||
case self::getCacheDirAbs():
|
||||
return 'cache';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getAbsDirById($dirId) {
|
||||
switch ($dirId) {
|
||||
case 'wp-content':
|
||||
return self::getContentDirAbs();
|
||||
case 'index':
|
||||
return self::getIndexDirAbs();
|
||||
case 'home':
|
||||
// "home" is still needed (used in PluginDeactivate.php)
|
||||
return self::getHomeDirAbs();
|
||||
case 'plugins':
|
||||
return self::getPluginDirAbs();
|
||||
case 'uploads':
|
||||
return self::getUploadDirAbs();
|
||||
case 'themes':
|
||||
return self::getThemesDirAbs();
|
||||
case 'cache':
|
||||
return self::getCacheDirAbs();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ids for folders where SOURCE images may reside
|
||||
*/
|
||||
public static function getImageRootIds() {
|
||||
return ['uploads', 'themes', 'plugins', 'wp-content', 'index'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find which rootId a path belongs to.
|
||||
*
|
||||
* Note: If the root ids passed are ordered the way getImageRootIds() returns them, the root id
|
||||
* returned will be the "deepest"
|
||||
*/
|
||||
public static function findImageRootOfPath($path, $rootIdsToSearch) {
|
||||
foreach ($rootIdsToSearch as $rootId) {
|
||||
if (PathHelper::isPathWithinExistingDirPath($path, self::getAbsDirById($rootId))) {
|
||||
return $rootId;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getImageRootsDefForSelectedIds($ids) {
|
||||
$canUseDocRootForRelPaths = self::canUseDocRootForRelPaths();
|
||||
|
||||
$mapping = [];
|
||||
foreach ($ids as $rootId) {
|
||||
$obj = [
|
||||
'id' => $rootId,
|
||||
];
|
||||
$absPath = self::getAbsDirById($rootId);
|
||||
if ($canUseDocRootForRelPaths) {
|
||||
$obj['rel-path'] = PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed($absPath);
|
||||
} else {
|
||||
$obj['abs-path'] = $absPath;
|
||||
}
|
||||
$obj['url'] = self::getUrlById($rootId);
|
||||
$mapping[] = $obj;
|
||||
}
|
||||
return $mapping;
|
||||
}
|
||||
|
||||
public static function getImageRootsDef()
|
||||
{
|
||||
return self::getImageRootsDefForSelectedIds(self::getImageRootIds());
|
||||
}
|
||||
|
||||
public static function filterOutSubRoots($rootIds)
|
||||
{
|
||||
// Get dirs of enabled roots
|
||||
$dirs = [];
|
||||
foreach ($rootIds as $rootId) {
|
||||
$dirs[] = self::getAbsDirById($rootId);
|
||||
}
|
||||
|
||||
// Filter out dirs which are below other dirs
|
||||
$dirsToSkip = [];
|
||||
foreach ($dirs as $dirToExamine) {
|
||||
foreach ($dirs as $dirToCompareAgainst) {
|
||||
if ($dirToExamine == $dirToCompareAgainst) {
|
||||
continue;
|
||||
}
|
||||
if (self::isDirInsideDir($dirToExamine, $dirToCompareAgainst)) {
|
||||
$dirsToSkip[] = $dirToExamine;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$dirs = array_diff($dirs, $dirsToSkip);
|
||||
|
||||
// back to ids
|
||||
$result = [];
|
||||
foreach ($dirs as $dir) {
|
||||
$result[] = self::getAbsDirId($dir);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function createDirIfMissing($dir)
|
||||
{
|
||||
if (!@file_exists($dir)) {
|
||||
// We use the wp_mkdir_p, because it takes care of setting folder
|
||||
// permissions to that of parent, and handles creating deep structures too
|
||||
wp_mkdir_p($dir);
|
||||
}
|
||||
return file_exists($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find out if $dir1 is inside - or equal to - $dir2
|
||||
*/
|
||||
public static function isDirInsideDir($dir1, $dir2)
|
||||
{
|
||||
$rel = PathHelper::getRelDir($dir2, $dir1);
|
||||
return (substr($rel, 0, 3) != '../');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return absolute dir.
|
||||
*
|
||||
* - Path is canonicalized (without resolving symlinks)
|
||||
* - trailing dash is removed - we don't use that around here.
|
||||
*
|
||||
* We do not resolve symlinks anymore. Information was lost that way.
|
||||
* And in some cases we needed the unresolved path - for example in the .htaccess.
|
||||
*/
|
||||
public static function getAbsDir($dir)
|
||||
{
|
||||
$dir = PathHelper::canonicalize($dir);
|
||||
return rtrim($dir, '/');
|
||||
/*
|
||||
$result = realpath($dir);
|
||||
if ($result === false) {
|
||||
$dir = PathHelper::canonicalize($dir);
|
||||
} else {
|
||||
$dir = $result;
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
// ------------ Home Dir -------------
|
||||
|
||||
// PS: Home dir is not the same as index dir.
|
||||
// For example, if Wordpress folder has been moved (method 2), the home dir could be below.
|
||||
public static function getHomeDirAbs()
|
||||
{
|
||||
if (!function_exists('get_home_path')) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
return self::getAbsDir(get_home_path());
|
||||
}
|
||||
|
||||
// ------------ Index Dir (WP root dir) -------------
|
||||
// (The Wordpress installation dir- where index.php and wp-load.php resides)
|
||||
|
||||
public static function getIndexDirAbs()
|
||||
{
|
||||
// We used to return self::getAbsDir(ABSPATH), which used realpath.
|
||||
// It has been changed now, as it seems we do not need realpath for ABSPATH, as it is defined
|
||||
// (in wp-load.php) as dirname(__FILE__) . "/" and according to this link, __FILE__ returns resolved paths:
|
||||
// https://stackoverflow.com/questions/3221771/how-do-you-get-php-symlinks-and-file-to-work-together-nicely
|
||||
// AND a user reported an open_basedir restriction problem thrown by realpath($_SERVER['DOCUMENT_ROOT']),
|
||||
// due to symlinking and opendir restriction (see #322)
|
||||
|
||||
return rtrim(ABSPATH, '/');
|
||||
|
||||
// TODO: read up on this, regarding realpath:
|
||||
// https://github.com/twigphp/Twig/issues/2707
|
||||
|
||||
}
|
||||
|
||||
// ------------ .htaccess dir -------------
|
||||
// (directory containing the relevant .htaccess)
|
||||
// (see https://github.com/rosell-dk/webp-express/issues/36)
|
||||
|
||||
|
||||
|
||||
public static function canWriteHTAccessRulesHere($dirName) {
|
||||
return FileHelper::canEditOrCreateFileHere($dirName . '/.htaccess');
|
||||
}
|
||||
|
||||
public static function canWriteHTAccessRulesInDir($dirId) {
|
||||
return self::canWriteHTAccessRulesHere(self::getAbsDirById($dirId));
|
||||
}
|
||||
|
||||
public static function returnFirstWritableHTAccessDir($dirs)
|
||||
{
|
||||
foreach ($dirs as $dir) {
|
||||
if (self::canWriteHTAccessRulesHere($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------ Content Dir (the "WP" content dir) -------------
|
||||
|
||||
public static function getContentDirAbs()
|
||||
{
|
||||
return self::getAbsDir(WP_CONTENT_DIR);
|
||||
}
|
||||
public static function getContentDirRel()
|
||||
{
|
||||
return PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(self::getContentDirAbs());
|
||||
}
|
||||
public static function getContentDirRelToPluginDir()
|
||||
{
|
||||
return PathHelper::getRelDir(self::getPluginDirAbs(), self::getContentDirAbs());
|
||||
}
|
||||
public static function getContentDirRelToWebPExpressPluginDir()
|
||||
{
|
||||
return PathHelper::getRelDir(self::getWebPExpressPluginDirAbs(), self::getContentDirAbs());
|
||||
}
|
||||
|
||||
|
||||
public static function isWPContentDirMoved()
|
||||
{
|
||||
return (self::getContentDirAbs() != (ABSPATH . 'wp-content'));
|
||||
}
|
||||
|
||||
public static function isWPContentDirMovedOutOfAbsPath()
|
||||
{
|
||||
return !(self::isDirInsideDir(self::getContentDirAbs(), ABSPATH));
|
||||
}
|
||||
|
||||
// ------------ Themes Dir -------------
|
||||
|
||||
public static function getThemesDirAbs()
|
||||
{
|
||||
return self::getContentDirAbs() . '/themes';
|
||||
}
|
||||
|
||||
// ------------ WebPExpress Content Dir -------------
|
||||
// (the "webp-express" directory inside wp-content)
|
||||
|
||||
public static function getWebPExpressContentDirAbs()
|
||||
{
|
||||
return self::getContentDirAbs() . '/webp-express';
|
||||
}
|
||||
|
||||
public static function getWebPExpressContentDirRel()
|
||||
{
|
||||
return PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(self::getWebPExpressContentDirAbs());
|
||||
}
|
||||
|
||||
public static function createContentDirIfMissing()
|
||||
{
|
||||
return self::createDirIfMissing(self::getWebPExpressContentDirAbs());
|
||||
}
|
||||
|
||||
// ------------ Upload Dir -------------
|
||||
public static function getUploadDirAbs()
|
||||
{
|
||||
$upload_dir = wp_upload_dir(null, false);
|
||||
return self::getAbsDir($upload_dir['basedir']);
|
||||
}
|
||||
public static function getUploadDirRel()
|
||||
{
|
||||
return PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(self::getUploadDirAbs());
|
||||
}
|
||||
|
||||
/*
|
||||
public static function getUploadDirAbs()
|
||||
{
|
||||
if ( defined( 'UPLOADS' ) ) {
|
||||
return ABSPATH . rtrim(UPLOADS, '/');
|
||||
} else {
|
||||
return self::getContentDirAbs() . '/uploads';
|
||||
}
|
||||
}*/
|
||||
|
||||
public static function isUploadDirMovedOutOfWPContentDir()
|
||||
{
|
||||
return !(self::isDirInsideDir(self::getUploadDirAbs(), self::getContentDirAbs()));
|
||||
}
|
||||
|
||||
public static function isUploadDirMovedOutOfAbsPath()
|
||||
{
|
||||
return !(self::isDirInsideDir(self::getUploadDirAbs(), ABSPATH));
|
||||
}
|
||||
|
||||
// ------------ Config Dir -------------
|
||||
|
||||
public static function getConfigDirAbs()
|
||||
{
|
||||
return self::getWebPExpressContentDirAbs() . '/config';
|
||||
}
|
||||
|
||||
public static function getConfigDirRel()
|
||||
{
|
||||
return PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(self::getConfigDirAbs());
|
||||
}
|
||||
|
||||
public static function createConfigDirIfMissing()
|
||||
{
|
||||
$configDir = self::getConfigDirAbs();
|
||||
// Using code from Wordfence bootstrap.php...
|
||||
// Why not simply use wp_mkdir_p ? - it sets the permissions to same as parent. Isn't that better?
|
||||
// or perhaps not... - Because we need write permissions in the config dir.
|
||||
if (!is_dir($configDir)) {
|
||||
@mkdir($configDir, 0775);
|
||||
@chmod($configDir, 0775);
|
||||
@file_put_contents(rtrim($configDir . '/') . '/.htaccess', <<<APACHE
|
||||
<IfModule mod_authz_core.c>
|
||||
Require all denied
|
||||
</IfModule>
|
||||
<IfModule !mod_authz_core.c>
|
||||
Order deny,allow
|
||||
Deny from all
|
||||
</IfModule>
|
||||
APACHE
|
||||
);
|
||||
@chmod($configDir . '/.htaccess', 0664);
|
||||
}
|
||||
return is_dir($configDir);
|
||||
}
|
||||
|
||||
public static function getConfigFileName()
|
||||
{
|
||||
return self::getConfigDirAbs() . '/config.json';
|
||||
}
|
||||
|
||||
public static function getWodOptionsFileName()
|
||||
{
|
||||
return self::getConfigDirAbs() . '/wod-options.json';
|
||||
}
|
||||
|
||||
// ------------ Cache Dir -------------
|
||||
|
||||
public static function getCacheDirAbs()
|
||||
{
|
||||
return self::getWebPExpressContentDirAbs() . '/webp-images';
|
||||
}
|
||||
|
||||
public static function getCacheDirRelToDocRoot()
|
||||
{
|
||||
return PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(self::getCacheDirAbs());
|
||||
}
|
||||
|
||||
public static function getCacheDirForImageRoot($destinationFolder, $destinationStructure, $imageRootId)
|
||||
{
|
||||
if (($destinationFolder == 'mingled') && ($imageRootId == 'uploads')) {
|
||||
return self::getUploadDirAbs();
|
||||
}
|
||||
|
||||
if ($destinationStructure == 'doc-root') {
|
||||
$relPath = PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(
|
||||
self::getAbsDirById($imageRootId)
|
||||
);
|
||||
return self::getCacheDirAbs() . '/doc-root/' . $relPath;
|
||||
} else {
|
||||
return self::getCacheDirAbs() . '/' . $imageRootId;
|
||||
}
|
||||
}
|
||||
|
||||
public static function createCacheDirIfMissing()
|
||||
{
|
||||
return self::createDirIfMissing(self::getCacheDirAbs());
|
||||
}
|
||||
|
||||
// ------------ Log Dir -------------
|
||||
|
||||
public static function getLogDirAbs()
|
||||
{
|
||||
return self::getWebPExpressContentDirAbs() . '/log';
|
||||
}
|
||||
|
||||
// ------------ Bigger-than-source dir -------------
|
||||
|
||||
public static function getBiggerThanSourceDirAbs()
|
||||
{
|
||||
return self::getWebPExpressContentDirAbs() . '/webp-images-bigger-than-source';
|
||||
}
|
||||
|
||||
// ------------ Plugin Dir (all plugins) -------------
|
||||
|
||||
public static function getPluginDirAbs()
|
||||
{
|
||||
return self::getAbsDir(WP_PLUGIN_DIR);
|
||||
}
|
||||
|
||||
|
||||
public static function isPluginDirMovedOutOfAbsPath()
|
||||
{
|
||||
return !(self::isDirInsideDir(self::getPluginDirAbs(), ABSPATH));
|
||||
}
|
||||
|
||||
public static function isPluginDirMovedOutOfWpContent()
|
||||
{
|
||||
return !(self::isDirInsideDir(self::getPluginDirAbs(), self::getContentDirAbs()));
|
||||
}
|
||||
|
||||
// ------------ WebP Express Plugin Dir -------------
|
||||
|
||||
public static function getWebPExpressPluginDirAbs()
|
||||
{
|
||||
return self::getAbsDir(WEBPEXPRESS_PLUGIN_DIR);
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
// --------- Url paths ----------
|
||||
// ------------------------------------
|
||||
|
||||
/**
|
||||
* Get url path (relative to domain) from absolute url.
|
||||
* Ie: "http://example.com/blog" => "blog"
|
||||
* Btw: By "url path" we shall always mean relative to domain
|
||||
* By "url" we shall always mean complete URL (with domain and everything)
|
||||
* (or at least something that starts with it...)
|
||||
*
|
||||
* Also note that in this library, we never returns trailing or leading slashes.
|
||||
*/
|
||||
public static function getUrlPathFromUrl($url)
|
||||
{
|
||||
$parsed = parse_url($url);
|
||||
if (!isset($parsed['path'])) {
|
||||
return '';
|
||||
}
|
||||
if (is_null($parsed['path'])) {
|
||||
return '';
|
||||
}
|
||||
$path = untrailingslashit($parsed['path']);
|
||||
return ltrim($path, '/\\');
|
||||
}
|
||||
|
||||
public static function getUrlById($dirId) {
|
||||
switch ($dirId) {
|
||||
case 'wp-content':
|
||||
return self::getContentUrl();
|
||||
case 'index':
|
||||
return self::getHomeUrl();
|
||||
case 'home':
|
||||
return self::getHomeUrl();
|
||||
case 'plugins':
|
||||
return self::getPluginsUrl();
|
||||
case 'uploads':
|
||||
return self::getUploadUrl();
|
||||
case 'themes':
|
||||
return self::getThemesUrl();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get destination root url and path, provided rootId and some configuration options
|
||||
*
|
||||
* This method kind of establishes the overall structure of the cache dir.
|
||||
* (but not quite, as the logic is also in ConverterHelperIndependent::getDestination).
|
||||
*
|
||||
* @param string $rootId
|
||||
* @param DestinationOptions $destinationOptions
|
||||
*
|
||||
* @return array url and abs-path of destination root
|
||||
*/
|
||||
public static function destinationRoot($rootId, $destinationOptions)
|
||||
{
|
||||
if (($destinationOptions->mingled) && ($rootId == 'uploads')) {
|
||||
return [
|
||||
'url' => self::getUrlById('uploads'),
|
||||
'abs-path' => self::getUploadDirAbs()
|
||||
];
|
||||
} else {
|
||||
|
||||
// Its within these bases:
|
||||
$destUrl = self::getUrlById('wp-content') . '/webp-express/webp-images';
|
||||
$destPath = self::getAbsDirById('wp-content') . '/webp-express/webp-images';
|
||||
|
||||
if (($destinationOptions->useDocRoot) && self::canUseDocRootForStructuringCacheDir()) {
|
||||
$relPathFromDocRootToSourceImageRoot = PathHelper::getRelPathFromDocRootToDirNoDirectoryTraversalAllowed(
|
||||
self::getAbsDirById($rootId)
|
||||
);
|
||||
return [
|
||||
'url' => $destUrl . '/doc-root/' . $relPathFromDocRootToSourceImageRoot,
|
||||
'abs-path' => $destPath . '/doc-root/' . $relPathFromDocRootToSourceImageRoot
|
||||
];
|
||||
} else {
|
||||
$extraPath = '';
|
||||
if (is_multisite() && (get_current_blog_id() != 1)) {
|
||||
$extraPath = '/sites/' . get_current_blog_id(); // #510
|
||||
}
|
||||
return [
|
||||
'url' => $destUrl . '/' . $rootId . $extraPath,
|
||||
'abs-path' => $destPath . '/' . $rootId . $extraPath
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getRootAndRelPathForDestination($destinationPath, $imageRoots) {
|
||||
foreach ($imageRoots->getArray() as $i => $imageRoot) {
|
||||
$rootPath = $imageRoot->getAbsPath();
|
||||
if (strpos($destinationPath, realpath($rootPath)) !== false) {
|
||||
$relPath = substr($destinationPath, strlen(realpath($rootPath)) + 1);
|
||||
return [$imageRoot->id, $relPath];
|
||||
}
|
||||
}
|
||||
return ['', ''];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// PST:
|
||||
// appendOrSetExtension() have been copied from ConvertHelperIndependent.
|
||||
// TODO: I should complete the move ASAP.
|
||||
|
||||
/**
|
||||
* Append ".webp" to path or replace extension with "webp", depending on what is appropriate.
|
||||
*
|
||||
* If destination-folder is set to mingled and destination-extension is set to "set" and
|
||||
* the path is inside upload folder, the appropriate thing is to SET the extension.
|
||||
* Otherwise, it is to APPEND.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $destinationFolder
|
||||
* @param string $destinationExt
|
||||
* @param boolean $inUploadFolder
|
||||
*/
|
||||
public static function appendOrSetExtension($path, $destinationFolder, $destinationExt, $inUploadFolder)
|
||||
{
|
||||
if (($destinationFolder == 'mingled') && ($destinationExt == 'set') && $inUploadFolder) {
|
||||
return preg_replace('/\\.(jpe?g|png)$/i', '', $path) . '.webp';
|
||||
} else {
|
||||
return $path . '.webp';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get destination root url and path, provided rootId and some configuration options
|
||||
*
|
||||
* This method kind of establishes the overall structure of the cache dir.
|
||||
* (but not quite, as the logic is also in ConverterHelperIndependent::getDestination).
|
||||
*
|
||||
* @param string $rootId
|
||||
* @param string $relPath
|
||||
* @param string $destinationFolder ("mingled" or "separate")
|
||||
* @param string $destinationExt ('append' or 'set')
|
||||
* @param string $destinationStructure ("doc-root" or "image-roots")
|
||||
*
|
||||
* @return array url and abs-path of destination
|
||||
*/
|
||||
/*
|
||||
public static function destinationPath($rootId, $relPath, $destinationFolder, $destinationExt, $destinationStructure) {
|
||||
|
||||
// TODO: Current logic will not do!
|
||||
// We must use ConvertHelper::getDestination for the abs path.
|
||||
// And we must use logic from AlterHtmlHelper to get the URL
|
||||
// Perhaps this method must be abandonned
|
||||
|
||||
$root = self::destinationRoot($rootId, $destinationFolder, $destinationStructure);
|
||||
$inUploadFolder = ($rootId == 'upload');
|
||||
$relPath = ConvertHelperIndependent::appendOrSetExtension($relPath, $destinationFolder, $destinationExt, $inUploadFolder);
|
||||
|
||||
return [
|
||||
'abs-path' => $root['abs-path'] . '/' . $relPath,
|
||||
'url' => $root['url'] . '/' . $relPath,
|
||||
];
|
||||
}
|
||||
|
||||
public static function destinationPathConvenience($rootId, $relPath, $config) {
|
||||
return self::destinationPath(
|
||||
$rootId,
|
||||
$relPath,
|
||||
$config['destination-folder'],
|
||||
$config['destination-extension'],
|
||||
$config['destination-structure']
|
||||
);
|
||||
}*/
|
||||
|
||||
public static function getDestinationPathCorrespondingToSource($source, $destinationOptions) {
|
||||
return Destination::getDestinationPathCorrespondingToSource(
|
||||
$source,
|
||||
Paths::getWebPExpressContentDirAbs(),
|
||||
Paths::getUploadDirAbs(),
|
||||
$destinationOptions,
|
||||
new ImageRoots(self::getImageRootsDef())
|
||||
);
|
||||
}
|
||||
|
||||
public static function getUrlPathById($dirId) {
|
||||
return self::getUrlPathFromUrl(self::getUrlById($dirId));
|
||||
}
|
||||
|
||||
public static function getHostNameOfUrl($url) {
|
||||
$urlComponents = parse_url($url);
|
||||
/* ie:
|
||||
(
|
||||
[scheme] => http
|
||||
[host] => we0
|
||||
[path] => /wordpress/uploads-moved
|
||||
)*/
|
||||
|
||||
if (!isset($urlComponents['host'])) {
|
||||
return '';
|
||||
} else {
|
||||
return $urlComponents['host'];
|
||||
}
|
||||
}
|
||||
|
||||
// Get complete home url (no trailing slash). Ie: "http://example.com/blog"
|
||||
public static function getHomeUrl()
|
||||
{
|
||||
if (!function_exists('home_url')) {
|
||||
// silence is golden?
|
||||
// bad joke. Need to handle this...
|
||||
}
|
||||
return untrailingslashit(home_url());
|
||||
}
|
||||
|
||||
/** Get home url, relative to domain. Ie "" or "blog"
|
||||
* If home url is for example http://example.com/blog/, the result is "blog"
|
||||
*/
|
||||
public static function getHomeUrlPath()
|
||||
{
|
||||
return self::getUrlPathFromUrl(self::getHomeUrl());
|
||||
}
|
||||
|
||||
|
||||
public static function getUploadUrl()
|
||||
{
|
||||
$uploadDir = wp_upload_dir(null, false);
|
||||
return untrailingslashit($uploadDir['baseurl']);
|
||||
}
|
||||
|
||||
public static function getUploadUrlPath()
|
||||
{
|
||||
return self::getUrlPathFromUrl(self::getUploadUrl());
|
||||
}
|
||||
|
||||
public static function getContentUrl()
|
||||
{
|
||||
return untrailingslashit(content_url());
|
||||
}
|
||||
|
||||
public static function getContentUrlPath()
|
||||
{
|
||||
return self::getUrlPathFromUrl(self::getContentUrl());
|
||||
}
|
||||
|
||||
public static function getThemesUrl()
|
||||
{
|
||||
return self::getContentUrl() . '/themes';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Url to plugins (base)
|
||||
*/
|
||||
public static function getPluginsUrl()
|
||||
{
|
||||
return untrailingslashit(plugins_url());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Url to WebP Express plugin (this is in fact an incomplete URL, you need to append ie '/webp-on-demand.php' to get a full URL)
|
||||
*/
|
||||
public static function getWebPExpressPluginUrl()
|
||||
{
|
||||
return untrailingslashit(plugins_url('', WEBPEXPRESS_PLUGIN));
|
||||
}
|
||||
|
||||
public static function getWebPExpressPluginUrlPath()
|
||||
{
|
||||
return self::getUrlPathFromUrl(self::getWebPExpressPluginUrl());
|
||||
}
|
||||
|
||||
public static function getWodFolderUrlPath()
|
||||
{
|
||||
return
|
||||
self::getWebPExpressPluginUrlPath() .
|
||||
'/wod';
|
||||
}
|
||||
|
||||
public static function getWod2FolderUrlPath()
|
||||
{
|
||||
return
|
||||
self::getWebPExpressPluginUrlPath() .
|
||||
'/wod2';
|
||||
}
|
||||
|
||||
public static function getWodUrlPath()
|
||||
{
|
||||
return
|
||||
self::getWodFolderUrlPath() .
|
||||
'/webp-on-demand.php';
|
||||
}
|
||||
|
||||
public static function getWod2UrlPath()
|
||||
{
|
||||
return
|
||||
self::getWod2FolderUrlPath() .
|
||||
'/webp-on-demand.php';
|
||||
}
|
||||
|
||||
public static function getWebPRealizerUrlPath()
|
||||
{
|
||||
return
|
||||
self::getWodFolderUrlPath() .
|
||||
'/webp-realizer.php';
|
||||
}
|
||||
|
||||
public static function getWebPRealizer2UrlPath()
|
||||
{
|
||||
return
|
||||
self::getWod2FolderUrlPath() .
|
||||
'/webp-realizer.php';
|
||||
}
|
||||
|
||||
public static function getWebServiceUrl()
|
||||
{
|
||||
//return self::getWebPExpressPluginUrl() . '/wpc.php';
|
||||
//return self::getHomeUrl() . '/webp-express-server';
|
||||
return self::getHomeUrl() . '/webp-express-web-service';
|
||||
}
|
||||
|
||||
public static function getUrlsAndPathsForTheJavascript()
|
||||
{
|
||||
return [
|
||||
'urls' => [
|
||||
'webpExpressRoot' => self::getWebPExpressPluginUrlPath(),
|
||||
'content' => self::getContentUrlPath(),
|
||||
],
|
||||
'filePaths' => [
|
||||
'webpExpressRoot' => self::getWebPExpressPluginDirAbs(),
|
||||
'destinationRoot' => self::getCacheDirAbs(),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function getSettingsUrl()
|
||||
{
|
||||
if (!function_exists('admin_url')) {
|
||||
require_once ABSPATH . 'wp-includes/link-template.php';
|
||||
}
|
||||
if (Multisite::isNetworkActivated()) {
|
||||
// network_admin_url is also defined in link-template.php.
|
||||
return network_admin_url('settings.php?page=webp_express_settings_page');
|
||||
} else {
|
||||
return admin_url('options-general.php?page=webp_express_settings_page');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
122
lib/classes/PlatformInfo.php
Normal file
122
lib/classes/PlatformInfo.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class PlatformInfo
|
||||
{
|
||||
|
||||
public static function isMicrosoftIis()
|
||||
{
|
||||
$server = strtolower($_SERVER['SERVER_SOFTWARE']);
|
||||
return ( strpos( $server, 'microsoft-iis') !== false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Apache handles the PHP requests (Note that duel setups are possible and ie Nginx could be handling the image requests).
|
||||
*/
|
||||
public static function isApache()
|
||||
{
|
||||
return (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false);
|
||||
}
|
||||
|
||||
public static function isLiteSpeed()
|
||||
{
|
||||
$server = strtolower($_SERVER['SERVER_SOFTWARE']);
|
||||
return ( strpos( $server, 'litespeed') !== false );
|
||||
}
|
||||
|
||||
public static function isNginx()
|
||||
{
|
||||
return (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') !== false);
|
||||
}
|
||||
|
||||
public static function isApacheOrLiteSpeed()
|
||||
{
|
||||
return self::isApache() || self::isLiteSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an Apache module is available.
|
||||
*
|
||||
* If apache_get_modules() exists, it is used. That function is however only available in mod_php installs.
|
||||
* Otherwise the Wordpress function "apache_mod_loaded" is tried, which examines phpinfo() output.
|
||||
* However, it seems there is no module output on php-fpm setups.
|
||||
* So on php-fpm, we cannot come with an answer.
|
||||
* https://stackoverflow.com/questions/9021425/how-to-check-if-mod-rewrite-is-enabled-in-php
|
||||
*
|
||||
* @param string $mod Name of module - ie "mod_rewrite"
|
||||
* @return boolean|null Return if module is available, or null if indeterminate
|
||||
*/
|
||||
public static function gotApacheModule($mod)
|
||||
{
|
||||
if (function_exists('apache_get_modules')) {
|
||||
return in_array($mod, apache_get_modules());
|
||||
}
|
||||
|
||||
// Revert to Wordpress method, which examines output from phpinfo as well
|
||||
if (function_exists('apache_mod_loaded')) {
|
||||
$result = apache_mod_loaded($mod, null);
|
||||
|
||||
// If we got a real result, return it.
|
||||
if ($result != null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// We could run shell_exec("apachectl -l"), as suggested here:
|
||||
// https://stackoverflow.com/questions/9021425/how-to-check-if-mod-rewrite-is-enabled-in-php
|
||||
// But it does not seem to return all modules in my php-fpm setup.
|
||||
|
||||
// Currently we got no more tools in this function...
|
||||
// you might want to take a look at the "htaccess_capability_tester" library...
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* It is not always possible to determine if apache has a given module...
|
||||
* We shall not fool anyone into thinking otherwise by providing a "got" method like Wordpress does...
|
||||
*/
|
||||
public static function definitelyGotApacheModule($mod)
|
||||
{
|
||||
return (self::gotApacheModule($mod) === true);
|
||||
}
|
||||
|
||||
public static function definitelyNotGotApacheModule($mod)
|
||||
{
|
||||
return (self::gotApacheModule($mod) === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if mod_rewrite or IIS rewrite is available.
|
||||
*
|
||||
* @return boolean|null Return bool if it can be determined, or null if not
|
||||
*/
|
||||
public static function gotRewriteModule()
|
||||
{
|
||||
$gotModRewrite = self::gotApacheModule('mod_rewrite');
|
||||
if (!is_null($gotModRewrite)) {
|
||||
return $gotModRewrite;
|
||||
}
|
||||
|
||||
// Got the IIS check here: https://stackoverflow.com/a/21249745/842756
|
||||
// but have not tested it...
|
||||
if (isset($_SERVER['IIS_UrlRewriteModule'])) {
|
||||
return true;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static function definitelyNotGotModRewrite()
|
||||
{
|
||||
return self::definitelyNotGotApacheModule('mod_rewrite');
|
||||
}
|
||||
|
||||
public static function definitelyGotModEnv()
|
||||
{
|
||||
return self::definitelyGotApacheModule('mod_env');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
111
lib/classes/PluginActivate.php
Normal file
111
lib/classes/PluginActivate.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\HTAccess;
|
||||
use \WebPExpress\Messenger;
|
||||
use \WebPExpress\Multisite;
|
||||
use \WebPExpress\Paths;
|
||||
use \WebPExpress\PlatformInfo;
|
||||
use \WebPExpress\State;
|
||||
|
||||
class PluginActivate
|
||||
{
|
||||
// callback for 'register_activation_hook' (registred in AdminInit)
|
||||
public static function activate($network_active) {
|
||||
|
||||
Multisite::overrideIsNetworkActivated($network_active);
|
||||
|
||||
// Test if plugin is activated for the first time or reactivated
|
||||
if (State::getState('configured', false)) {
|
||||
self::reactivate();
|
||||
} else {
|
||||
self::activateFirstTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static function reactivate()
|
||||
{
|
||||
$config = Config::loadConfigAndFix(false); // false, because we do not need to test if quality detection is working
|
||||
|
||||
if ($config === false) {
|
||||
Messenger::addMessage(
|
||||
'error',
|
||||
'The config file seems to have gone missing. You will need to reconfigure WebP Express ' .
|
||||
'<a href="' . Paths::getSettingsUrl() . '">(here)</a>.'
|
||||
);
|
||||
} else {
|
||||
$rulesResult = HTAccess::saveRules($config, false);
|
||||
|
||||
$rulesSaveSuccess = $rulesResult[0];
|
||||
if ($rulesSaveSuccess) {
|
||||
Messenger::addMessage(
|
||||
'success',
|
||||
'WebP Express re-activated successfully.<br>' .
|
||||
'The image redirections are in effect again.<br><br>' .
|
||||
'Just a quick reminder: If you at some point change the upload directory or move Wordpress, ' .
|
||||
'the <i>.htaccess</i> files will need to be regenerated.<br>' .
|
||||
'You do that by re-saving the settings ' .
|
||||
'<a href="' . Paths::getSettingsUrl() . '">(here)</a>'
|
||||
);
|
||||
} else {
|
||||
Messenger::addMessage(
|
||||
'warning',
|
||||
'WebP Express could not regenerate the rewrite rules<br>' .
|
||||
'You need to change some permissions. Head to the ' .
|
||||
'<a href="' . Paths::getSettingsUrl() . '">settings page</a> ' .
|
||||
'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 <a href="https://github.com/rosell-dk/webp-express/pull/213">should work on Windows now</a>, 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.<br>' .
|
||||
'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.<br>' .
|
||||
'Btw: your server is: ' . $_SERVER['SERVER_SOFTWARE']
|
||||
);
|
||||
}
|
||||
|
||||
// Welcome!
|
||||
// -------------------------------
|
||||
Messenger::addMessage(
|
||||
'info',
|
||||
'WebP Express was installed successfully. To start using it, you must ' .
|
||||
'<a href="' . Paths::getSettingsUrl() . '">configure it here</a>.'
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
36
lib/classes/PluginDeactivate.php
Normal file
36
lib/classes/PluginDeactivate.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class PluginDeactivate
|
||||
{
|
||||
// The hook was registred in AdminInit
|
||||
public static function deactivate() {
|
||||
|
||||
list($success, $failures, $successes) = HTAccess::deactivateHTAccessRules();
|
||||
|
||||
if ($success) {
|
||||
// Oh, it would be nice to be able to add a goodbye message here...
|
||||
// But well, that cannot be done here.
|
||||
} else {
|
||||
// Oh no. We failed removing the rules
|
||||
$msg = "<b>Sorry, can't let you disable WebP Express!</b><br>" .
|
||||
'There are rewrite rules in the <i>.htaccess</i> that could not be removed. If these are not removed, it would break all images.<br>' .
|
||||
'Please make your <i>.htaccess</i> writable and then try to disable WebPExpress again.<br>Alternatively, remove the rules manually in your <i>.htaccess</i> file and try disabling again.' .
|
||||
'<br>It concerns the following files:<br>';
|
||||
|
||||
|
||||
foreach ($failures as $rootId) {
|
||||
$msg .= '- ' . Paths::getAbsDirById($rootId) . '/.htaccess<br>';
|
||||
}
|
||||
|
||||
Messenger::addMessage(
|
||||
'error',
|
||||
$msg
|
||||
);
|
||||
|
||||
wp_redirect(admin_url('options-general.php?page=webp_express_settings_page'));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
lib/classes/PluginPageScript.php
Normal file
26
lib/classes/PluginPageScript.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class PluginPageScript
|
||||
{
|
||||
// The hook was registred in AdminInit
|
||||
public static function enqueueScripts() {
|
||||
$ver = '1'; // note: Minimum 1
|
||||
$jsDir = 'js/0.16.0'; // We change dir when it is critical that no-one gets the cached version (there is a plugin that strips version strings out there...)
|
||||
|
||||
if (!function_exists('webp_express_add_inline_script')) {
|
||||
function webp_express_add_inline_script($id, $script, $position) {
|
||||
if (function_exists('wp_add_inline_script')) {
|
||||
// wp_add_inline_script is available from Wordpress 4.5
|
||||
wp_add_inline_script($id, $script, $position);
|
||||
} else {
|
||||
echo '<script>' . $script . '</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');
|
||||
}
|
||||
}
|
||||
33
lib/classes/PluginUninstall.php
Normal file
33
lib/classes/PluginUninstall.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\FileHelper;
|
||||
use \WebPExpress\Option;
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class PluginUninstall
|
||||
{
|
||||
// The hook was registred in AdminInit
|
||||
public static function uninstall() {
|
||||
|
||||
$optionsToDelete = [
|
||||
'webp-express-messages-pending',
|
||||
'webp-express-action-pending',
|
||||
'webp-express-state',
|
||||
'webp-express-version',
|
||||
'webp-express-activation-error',
|
||||
'webp-express-migration-version'
|
||||
];
|
||||
foreach ($optionsToDelete as $i => $optionName) {
|
||||
Option::deleteOption($optionName);
|
||||
}
|
||||
|
||||
// remove content dir (config plus images plus htaccess-tests)
|
||||
FileHelper::rrmdir(Paths::getWebPExpressContentDirAbs());
|
||||
}
|
||||
}
|
||||
31
lib/classes/Sanitize.php
Normal file
31
lib/classes/Sanitize.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class Sanitize
|
||||
{
|
||||
|
||||
/**
|
||||
* The NUL character is a demon, because it can be used to bypass other tests
|
||||
* See https://st-g.de/2011/04/doing-filename-checks-securely-in-PHP.
|
||||
*
|
||||
* @param string $string string remove NUL characters in
|
||||
*/
|
||||
public static function removeNUL($string)
|
||||
{
|
||||
return str_replace(chr(0), '', $string);
|
||||
}
|
||||
|
||||
public static function removeStreamWrappers($string)
|
||||
{
|
||||
return preg_replace('#^\\w+://#', '', $string);
|
||||
}
|
||||
|
||||
public static function path($string)
|
||||
{
|
||||
$string = self::removeNUL($string);
|
||||
$string = self::removeStreamWrappers($string);
|
||||
return $string;
|
||||
}
|
||||
|
||||
}
|
||||
412
lib/classes/SanityCheck.php
Normal file
412
lib/classes/SanityCheck.php
Normal file
@ -0,0 +1,412 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\PathHelper;
|
||||
use \WebPExpress\Sanitize;
|
||||
use \WebPExpress\SanityException;
|
||||
|
||||
class SanityCheck
|
||||
{
|
||||
|
||||
private static function fail($errorMsg, $input)
|
||||
{
|
||||
// sanitize input before calling error_log(), it might be sent to file, mail, syslog etc.
|
||||
//error_log($errorMsg . '. input:' . Sanitize::removeNUL($input) . 'backtrace: ' . print_r(debug_backtrace(), true));
|
||||
error_log($errorMsg . '. input:' . Sanitize::removeNUL($input));
|
||||
|
||||
//error_log(get_magic_quotes_gpc() ? 'on' :'off');
|
||||
throw new SanityException($errorMsg); // . '. Check debug.log for details (and make sure debugging is enabled)'
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $input string to test for NUL char
|
||||
*/
|
||||
public static function mustBeString($input, $errorMsg = 'String expected')
|
||||
{
|
||||
if (gettype($input) !== 'string') {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* The NUL character is a demon, because it can be used to bypass other tests
|
||||
* See https://st-g.de/2011/04/doing-filename-checks-securely-in-PHP.
|
||||
*
|
||||
* @param string $input string to test for NUL char
|
||||
*/
|
||||
public static function noNUL($input, $errorMsg = 'NUL character is not allowed')
|
||||
{
|
||||
self::mustBeString($input);
|
||||
if (strpos($input, chr(0)) !== false) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent control chararters (#00 - #20).
|
||||
*
|
||||
* This prevents line feed, new line, tab, charater return, tab, ets.
|
||||
* https://www.rapidtables.com/code/text/ascii-table.html
|
||||
*
|
||||
* @param string $input string to test for control characters
|
||||
*/
|
||||
public static function noControlChars($input, $errorMsg = 'Control characters are not allowed')
|
||||
{
|
||||
self::mustBeString($input);
|
||||
self::noNUL($input);
|
||||
if (preg_match('#[\x{0}-\x{1f}]#', $input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mixed $input something that may not be empty
|
||||
*/
|
||||
public static function notEmpty($input, $errorMsg = 'Must be non-empty')
|
||||
{
|
||||
if (empty($input)) {
|
||||
self::fail($errorMsg, '');
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function noDirectoryTraversal($input, $errorMsg = 'Directory traversal is not allowed')
|
||||
{
|
||||
self::mustBeString($input);
|
||||
self::noControlChars($input);
|
||||
if (preg_match('#\.\.\/#', $input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function noStreamWrappers($input, $errorMsg = 'Stream wrappers are not allowed')
|
||||
{
|
||||
self::mustBeString($input);
|
||||
self::noControlChars($input);
|
||||
|
||||
// Prevent stream wrappers ("phar://", "php://" and the like)
|
||||
// https://www.php.net/manual/en/wrappers.phar.php
|
||||
if (preg_match('#^\\w+://#', Sanitize::removeNUL($input))) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function pathDirectoryTraversalAllowed($input)
|
||||
{
|
||||
self::notEmpty($input);
|
||||
self::mustBeString($input);
|
||||
self::noControlChars($input);
|
||||
self::noStreamWrappers($input);
|
||||
|
||||
// PS: The following sanitize has no effect, as we have just tested that there are no NUL and
|
||||
// no stream wrappers. It is here to avoid false positives on coderisk.com
|
||||
$input = Sanitize::path($input);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function pathWithoutDirectoryTraversal($input)
|
||||
{
|
||||
self::pathDirectoryTraversalAllowed($input);
|
||||
self::noDirectoryTraversal($input);
|
||||
$input = Sanitize::path($input);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function path($input)
|
||||
{
|
||||
return self::pathWithoutDirectoryTraversal($input);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Beware: This does not take symlinks into account.
|
||||
* I should make one that does. Until then, you should probably not call this method from outside this class
|
||||
*/
|
||||
private static function pathBeginsWith($input, $beginsWith, $errorMsg = 'Path is outside allowed path')
|
||||
{
|
||||
self::path($input);
|
||||
if (!(strpos($input, $beginsWith) === 0)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
private static function pathBeginsWithSymLinksExpanded($input, $beginsWith, $errorMsg = 'Path is outside allowed path') {
|
||||
$closestExistingFolder = PathHelper::findClosestExistingFolderSymLinksExpanded($input);
|
||||
self::pathBeginsWith($closestExistingFolder, $beginsWith, $errorMsg);
|
||||
}
|
||||
|
||||
private static function absPathMicrosoftStyle($input, $errorMsg = 'Not an fully qualified Windows path')
|
||||
{
|
||||
// On microsoft we allow [drive letter]:\
|
||||
if (!preg_match("#^[A-Z]:\\\\|/#", $input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
private static function isOnMicrosoft()
|
||||
{
|
||||
if (isset($_SERVER['SERVER_SOFTWARE'])) {
|
||||
if (strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'microsoft') !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
switch (PHP_OS) {
|
||||
case "WINNT":
|
||||
case "WIN32":
|
||||
case "INTERIX":
|
||||
case "UWIN":
|
||||
case "UWIN-W7":
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function absPath($input, $errorMsg = 'Not an absolute path')
|
||||
{
|
||||
// first make sure there are no nasty things like control chars, phar wrappers, etc.
|
||||
// - and no directory traversal either.
|
||||
self::path($input);
|
||||
|
||||
// For non-windows, we require that an absolute path begins with "/"
|
||||
// On windows, we also accept that a path starts with a drive letter, ie "C:\"
|
||||
if ((strpos($input, '/') !== 0)) {
|
||||
if (self::isOnMicrosoft()) {
|
||||
self::absPathMicrosoftStyle($input);
|
||||
} else {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function absPathInOneOfTheseRoots()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Look if filepath is within a dir path.
|
||||
* Also tries expanding symlinks
|
||||
*
|
||||
* @param string $filePath Path to file. It may be non-existing.
|
||||
* @param string $dirPath Path to dir. It must exist in order for symlinks to be expanded.
|
||||
*/
|
||||
private static function isFilePathWithinExistingDirPath($filePath, $dirPath)
|
||||
{
|
||||
// sanity-check input. It must be a valid absolute filepath. It is allowed to be non-existing
|
||||
self::absPath($filePath);
|
||||
|
||||
// sanity-check dir and that it exists.
|
||||
self::absPathExistsAndIsDir($dirPath);
|
||||
|
||||
return PathHelper::isFilePathWithinDirPath($filePath, $dirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look if filepath is within multiple dir paths.
|
||||
* Also tries expanding symlinks
|
||||
*
|
||||
* @param string $input Path to file. It may be non-existing.
|
||||
* @param array $roots Allowed root dirs. Note that they must exist in order for symlinks to be expanded.
|
||||
*/
|
||||
public static function filePathWithinOneOfTheseRoots($input, $roots, $errorMsg = 'The path is outside allowed roots.')
|
||||
{
|
||||
self::absPath($input);
|
||||
|
||||
foreach ($roots as $root) {
|
||||
if (self::isFilePathWithinExistingDirPath($input, $root)) {
|
||||
return $input;
|
||||
}
|
||||
}
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
|
||||
/*
|
||||
public static function sourcePath($input, $errorMsg = 'The source path is outside allowed roots. It is only allowed to convert images that resides in: home dir, content path, upload dir and plugin dir.')
|
||||
{
|
||||
$validPaths = [
|
||||
Paths::getHomeDirAbs(),
|
||||
Paths::getIndexDirAbs(),
|
||||
Paths::getContentDirAbs(),
|
||||
Paths::getUploadDirAbs(),
|
||||
Paths::getPluginDirAbs()
|
||||
];
|
||||
return self::filePathWithinOneOfTheseRoots($input, $validPaths, $errorMsg);
|
||||
}
|
||||
|
||||
public static function destinationPath($input, $errorMsg = 'The destination path is outside allowed roots. The webps may only be stored in the upload folder and in the folder that WebP Express stores converted images in')
|
||||
{
|
||||
self::absPath($input);
|
||||
|
||||
// Webp Express only store converted images in upload folder and in its "webp-images" folder
|
||||
// Check that destination path is within one of these.
|
||||
$validPaths = [
|
||||
'/var/www/webp-express-tests/we1'
|
||||
//Paths::getUploadDirAbs(),
|
||||
//Paths::getWebPExpressContentDirRel() . '/webp-images'
|
||||
];
|
||||
return self::filePathWithinOneOfTheseRoots($input, $validPaths, $errorMsg);
|
||||
}*/
|
||||
|
||||
|
||||
/**
|
||||
* Test that path is an absolute path and it is in document root.
|
||||
*
|
||||
* If DOCUMENT_ROOT is not available, then only the absPath check will be done.
|
||||
*
|
||||
* TODO: Instead of this method, we shoud check
|
||||
*
|
||||
*
|
||||
* It is acceptable if the absolute path does not exist
|
||||
*/
|
||||
public static function absPathIsInDocRoot($input, $errorMsg = 'Path is outside document root')
|
||||
{
|
||||
self::absPath($input);
|
||||
|
||||
if (!isset($_SERVER["DOCUMENT_ROOT"])) {
|
||||
return $input;
|
||||
}
|
||||
if ($_SERVER["DOCUMENT_ROOT"] == '') {
|
||||
return $input;
|
||||
}
|
||||
|
||||
$docRoot = self::absPath($_SERVER["DOCUMENT_ROOT"]);
|
||||
$docRoot = rtrim($docRoot, '/');
|
||||
|
||||
try {
|
||||
$docRoot = self::absPathExistsAndIsDir($docRoot);
|
||||
} catch (SanityException $e) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
// Use realpath to expand symbolic links and check if it exists
|
||||
$docRootSymLinksExpanded = @realpath($docRoot);
|
||||
if ($docRootSymLinksExpanded === false) {
|
||||
// probably outside open basedir restriction.
|
||||
//$errorMsg = 'Cannot resolve document root';
|
||||
//self::fail($errorMsg, $input);
|
||||
|
||||
// Cannot resolve document root, so cannot test if in document root
|
||||
return $input;
|
||||
}
|
||||
|
||||
// See if $filePath begins with the realpath of the $docRoot + '/'. If it does, we are done and OK!
|
||||
// (pull #429)
|
||||
if (strpos($input, $docRootSymLinksExpanded . '/') === 0) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
$docRootSymLinksExpanded = rtrim($docRootSymLinksExpanded, '\\/');
|
||||
$docRootSymLinksExpanded = self::absPathExists($docRootSymLinksExpanded, 'Document root does not exist!');
|
||||
$docRootSymLinksExpanded = self::absPathExistsAndIsDir($docRootSymLinksExpanded, 'Document root is not a directory!');
|
||||
|
||||
$directorySeparator = self::isOnMicrosoft() ? '\\' : '/';
|
||||
$errorMsg = 'Path is outside resolved document root (' . $docRootSymLinksExpanded . ')';
|
||||
self::pathBeginsWithSymLinksExpanded($input, $docRootSymLinksExpanded . $directorySeparator, $errorMsg);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function absPathExists($input, $errorMsg = 'Path does not exist or it is outside restricted basedir')
|
||||
{
|
||||
self::absPath($input);
|
||||
if (@!file_exists($input)) {
|
||||
// TODO: We might be able to detect if the problem is that the path does not exist or if the problem
|
||||
// is that it is outside restricted basedir.
|
||||
// ie by creating an error handler or inspecting the php ini "open_basedir" setting
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function absPathExistsAndIsDir(
|
||||
$input,
|
||||
$errorMsg = 'Path points to a file (it should point to a directory)'
|
||||
) {
|
||||
self::absPathExists($input, 'Directory does not exist or is outside restricted basedir');
|
||||
if (!is_dir($input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function absPathExistsAndIsFile(
|
||||
$input,
|
||||
$errorMsg = 'Path points to a directory (it should not do that)'
|
||||
) {
|
||||
self::absPathExists($input, 'File does not exist or is outside restricted basedir');
|
||||
if (@is_dir($input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function absPathExistsAndIsFileInDocRoot($input)
|
||||
{
|
||||
self::absPathExistsAndIsFile($input);
|
||||
self::absPathIsInDocRoot($input);
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function absPathExistsAndIsNotDir(
|
||||
$input,
|
||||
$errorMsg = 'Path points to a directory (it should point to a file)'
|
||||
) {
|
||||
self::absPathExistsAndIsFile($input, $errorMsg);
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
public static function pregMatch($pattern, $input, $errorMsg = 'Does not match expected pattern')
|
||||
{
|
||||
self::noNUL($input);
|
||||
self::mustBeString($input);
|
||||
if (!preg_match($pattern, $input)) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function isJSONArray($input, $errorMsg = 'Not a JSON array')
|
||||
{
|
||||
self::noNUL($input);
|
||||
self::mustBeString($input);
|
||||
self::notEmpty($input);
|
||||
if ((strpos($input, '[') !== 0) || (!is_array(json_decode($input)))) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
public static function isJSONObject($input, $errorMsg = 'Not a JSON object')
|
||||
{
|
||||
self::noNUL($input);
|
||||
self::mustBeString($input);
|
||||
self::notEmpty($input);
|
||||
if ((strpos($input, '{') !== 0) || (!is_object(json_decode($input)))) {
|
||||
self::fail($errorMsg, $input);
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
}
|
||||
7
lib/classes/SanityException.php
Normal file
7
lib/classes/SanityException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class SanityException extends \Exception
|
||||
{
|
||||
}
|
||||
118
lib/classes/SelfTest.php
Normal file
118
lib/classes/SelfTest.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class SelfTest
|
||||
{
|
||||
|
||||
private static $next;
|
||||
|
||||
public static function allInfo()
|
||||
{
|
||||
self::$next = 'done';
|
||||
$config = Config::loadConfigAndFix(false);
|
||||
return SelfTestHelper::allInfo($config);
|
||||
}
|
||||
|
||||
|
||||
public static function systemInfo()
|
||||
{
|
||||
self::$next = 'configInfo';
|
||||
return SelfTestHelper::systemInfo();
|
||||
}
|
||||
|
||||
public static function configInfo()
|
||||
{
|
||||
self::$next = 'capabilityTests';
|
||||
$config = Config::loadConfigAndFix(false);
|
||||
return SelfTestHelper::configInfo($config);
|
||||
}
|
||||
|
||||
public static function capabilityTests()
|
||||
{
|
||||
self::$next = 'done';
|
||||
$config = Config::loadConfigAndFix(false);
|
||||
return SelfTestHelper::capabilityTests($config);
|
||||
}
|
||||
|
||||
public static function redirectToExisting()
|
||||
{
|
||||
self::$next = 'done';
|
||||
list ($success, $result) = SelfTestRedirectToExisting::runTest();
|
||||
return $result;
|
||||
/*
|
||||
$result = [];
|
||||
$result[] = '# Redirection tests';
|
||||
$modRewriteWorking = HTAccessCapabilityTestRunner::modRewriteWorking();
|
||||
$modHeaderWorking = HTAccessCapabilityTestRunner::modHeaderWorking();
|
||||
|
||||
if (($modRewriteWorking === false) && ($modHeaderWorking)) {
|
||||
//$result[] = 'mod_rewrite is not working';
|
||||
|
||||
if (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') !== false) {
|
||||
|
||||
$result[] = 'You are on Nginx and the rules that WebP Express stores in the .htaccess files does not ' .
|
||||
'have any effect. '
|
||||
|
||||
}
|
||||
// if (stripos($_SERVER["SERVER_SOFTWARE"], 'apache') !== false && stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') === false) {
|
||||
|
||||
}
|
||||
|
||||
return [$result, 'done'];*/
|
||||
}
|
||||
|
||||
public static function redirectToConverter()
|
||||
{
|
||||
self::$next = 'done';
|
||||
list ($success, $result) = SelfTestRedirectToConverter::runTest();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function redirectToWebPRealizer()
|
||||
{
|
||||
self::$next = 'done';
|
||||
list ($success, $result) = SelfTestRedirectToWebPRealizer::runTest();
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public static function processAjax()
|
||||
{
|
||||
if (!check_ajax_referer('webpexpress-ajax-self-test-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();
|
||||
}
|
||||
|
||||
// Check input
|
||||
// --------------
|
||||
try {
|
||||
// Check "testId"
|
||||
$checking = '"testId" argument';
|
||||
Validate::postHasKey('testId');
|
||||
|
||||
$testId = sanitize_text_field(stripslashes($_POST['testId']));
|
||||
|
||||
} catch (Exception $e) {
|
||||
wp_send_json_error('Validation failed for ' . $checking . ': '. $e->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();
|
||||
}
|
||||
|
||||
}
|
||||
792
lib/classes/SelfTestHelper.php
Normal file
792
lib/classes/SelfTestHelper.php
Normal file
@ -0,0 +1,792 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Paths;
|
||||
|
||||
class SelfTestHelper
|
||||
{
|
||||
|
||||
public static function deleteFilesInDir($dir, $filePattern = "*")
|
||||
{
|
||||
foreach (glob($dir . DIRECTORY_SEPARATOR . $filePattern) as $filename) {
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove files in dir and the dir. Does not remove files recursively.
|
||||
*/
|
||||
public static function deleteDir($dir)
|
||||
{
|
||||
if (@file_exists($dir)) {
|
||||
self::deleteFilesInDir($dir);
|
||||
rmdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function deleteTestImagesInFolder($rootId)
|
||||
{
|
||||
$testDir = Paths::getAbsDirById($rootId) . '/webp-express-test-images';
|
||||
self::deleteDir($testDir);
|
||||
}
|
||||
|
||||
public static function cleanUpTestImages($rootId, $config)
|
||||
{
|
||||
// Clean up test images in source folder
|
||||
self::deleteTestImagesInFolder($rootId);
|
||||
|
||||
// Clean up dummy webp images in cache folder for the root
|
||||
$cacheDirForRoot = Paths::getCacheDirForImageRoot(
|
||||
$config['destination-folder'],
|
||||
$config['destination-structure'],
|
||||
$rootId
|
||||
);
|
||||
|
||||
$testDir = $cacheDirForRoot . '/webp-express-test-images';
|
||||
self::deleteDir($testDir);
|
||||
}
|
||||
|
||||
public static function copyFile($source, $destination)
|
||||
{
|
||||
$log = [];
|
||||
if (@copy($source, $destination)) {
|
||||
return [true, $log];
|
||||
} else {
|
||||
$log[] = 'Failed to copy *' . $source . '* to *' . $destination . '*';
|
||||
if (!@file_exists($source)) {
|
||||
$log[] = 'The source file was not found';
|
||||
} else {
|
||||
if (!@file_exists(dirname($destination))) {
|
||||
$log[] = 'The destination folder does not exist!';
|
||||
} else {
|
||||
$log[] = 'This is probably a permission issue. Check that your webserver has permission to ' .
|
||||
'write files in the directory (*' . dirname($destination) . '*)';
|
||||
}
|
||||
}
|
||||
return [false, $log];
|
||||
}
|
||||
}
|
||||
|
||||
public static function randomDigitsAndLetters($length)
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
public static function copyTestImageToRoot($rootId, $imageType = 'jpeg')
|
||||
{
|
||||
// TODO: Copy to a subfolder instead
|
||||
// TODO: Use smaller jpeg / pngs please.
|
||||
$log = [];
|
||||
switch ($imageType) {
|
||||
case 'jpeg':
|
||||
$fileNameToCopy = 'very-small.jpg';
|
||||
break;
|
||||
case 'png':
|
||||
$fileNameToCopy = 'test.png';
|
||||
break;
|
||||
}
|
||||
$testSource = Paths::getPluginDirAbs() . '/webp-express/test/' . $fileNameToCopy;
|
||||
$filenameOfDestination = self::randomDigitsAndLetters(6) . '.' . strtoupper($imageType);
|
||||
//$filenameOfDestination = self::randomDigitsAndLetters(6) . '.' . $imageType;
|
||||
$log[] = 'Copying ' . strtoupper($imageType) . ' to ' . $rootId . ' folder (*webp-express-test-images/' . $filenameOfDestination . '*)';
|
||||
|
||||
$destDir = Paths::getAbsDirById($rootId) . '/webp-express-test-images';
|
||||
$destination = $destDir . '/' . $filenameOfDestination;
|
||||
|
||||
if (!@file_exists($destDir)) {
|
||||
if (!@mkdir($destDir)) {
|
||||
$log[count($log) - 1] .= '. FAILED';
|
||||
$log[] = 'Failed to create folder for test images: ' . $destDir;
|
||||
return [$log, false, ''];
|
||||
}
|
||||
}
|
||||
|
||||
list($success, $errors) = self::copyFile($testSource, $destination);
|
||||
if (!$success) {
|
||||
$log[count($log) - 1] .= '. FAILED';
|
||||
$log = array_merge($log, $errors);
|
||||
return [$log, false, ''];
|
||||
} else {
|
||||
$log[count($log) - 1] .= '. ok!';
|
||||
$log[] = 'We now have a ' . $imageType . ' stored here:';
|
||||
$log[] = '*' . $destination . '*';
|
||||
}
|
||||
return [$log, true, $filenameOfDestination];
|
||||
}
|
||||
|
||||
public static function copyTestImageToUploadFolder($imageType = 'jpeg')
|
||||
{
|
||||
return self::copyTestImageToRoot('uploads', $imageType);
|
||||
}
|
||||
|
||||
public static function copyDummyWebPToCacheFolder($rootId, $destinationFolder, $destinationExtension, $destinationStructure, $sourceFileName, $imageType = 'jpeg')
|
||||
{
|
||||
$log = [];
|
||||
$dummyWebP = Paths::getPluginDirAbs() . '/webp-express/test/test.jpg.webp';
|
||||
|
||||
$log[] = 'Copying dummy webp to the cache root for ' . $rootId;
|
||||
$destDir = Paths::getCacheDirForImageRoot($destinationFolder, $destinationStructure, $rootId);
|
||||
if (!file_exists($destDir)) {
|
||||
$log[] = 'The folder did not exist. Creating folder at: ' . $destinationFolder;
|
||||
if (!mkdir($destDir, 0777, true)) {
|
||||
$log[] = 'Failed creating folder!';
|
||||
return [$log, false, ''];
|
||||
}
|
||||
}
|
||||
$destDir .= '/webp-express-test-images';
|
||||
if (!file_exists($destDir)) {
|
||||
if (!mkdir($destDir, 0755, false)) {
|
||||
$log[] = 'Failed creating the folder for the test images:';
|
||||
$log[] = $destDir;
|
||||
$log[] = 'To run this test, you must grant write permissions';
|
||||
return [$log, false, ''];
|
||||
}
|
||||
}
|
||||
|
||||
$filenameOfDestination = ConvertHelperIndependent::appendOrSetExtension(
|
||||
$sourceFileName,
|
||||
$destinationFolder,
|
||||
$destinationExtension,
|
||||
($rootId == 'uploads')
|
||||
);
|
||||
|
||||
//$filenameOfDestination = $destinationFileNameNoExt . ($destinationExtension == 'append' ? '.' . $imageType : '') . '.webp';
|
||||
$destination = $destDir . '/' . $filenameOfDestination;
|
||||
|
||||
list($success, $errors) = self::copyFile($dummyWebP, $destination);
|
||||
if (!$success) {
|
||||
$log[count($log) - 1] .= '. FAILED';
|
||||
$log = array_merge($log, $errors);
|
||||
return [$log, false, ''];
|
||||
} else {
|
||||
$log[count($log) - 1] .= '. ok!';
|
||||
$log[] = 'We now have a webp file stored here:';
|
||||
$log[] = '*' . $destination . '*';
|
||||
$log[] = '';
|
||||
}
|
||||
return [$log, true, $destination];
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform HTTP request.
|
||||
*
|
||||
* @param string $requestUrl URL
|
||||
* @param array $args Args to pass to wp_remote_get. Note however that "redirection" is set to 0
|
||||
* @param int $maxRedirects For internal use
|
||||
* @return array The result
|
||||
* $success (boolean): If we got a 200 response in the end (after max 2 redirects)
|
||||
* $log (array) : Message log
|
||||
* $results : Array of results from wp_remote_get. If no redirection occured, it will only contain one item.
|
||||
*
|
||||
*/
|
||||
public static function remoteGet($requestUrl, $args = [], $maxRedirects = 2)
|
||||
{
|
||||
$log = [];
|
||||
$args['redirection'] = 0;
|
||||
|
||||
if (defined('WP_DEBUG') && WP_DEBUG ) {
|
||||
// Prevent errors with unverified certificates (#379)
|
||||
$args['sslverify'] = false;
|
||||
}
|
||||
|
||||
$log[] = 'Request URL: ' . $requestUrl;
|
||||
|
||||
$results = [];
|
||||
$wpResult = wp_remote_get($requestUrl, $args);
|
||||
if (is_wp_error($wpResult)) {
|
||||
$log[] = 'The remote request errored';
|
||||
$log[] = $wpResult->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;
|
||||
}
|
||||
}
|
||||
115
lib/classes/SelfTestRedirectAbstract.php
Normal file
115
lib/classes/SelfTestRedirectAbstract.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
abstract class SelfTestRedirectAbstract
|
||||
{
|
||||
protected $config;
|
||||
|
||||
public function __construct($config) {
|
||||
$this->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];
|
||||
}
|
||||
|
||||
}
|
||||
239
lib/classes/SelfTestRedirectToConverter.php
Normal file
239
lib/classes/SelfTestRedirectToConverter.php
Normal file
@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class SelfTestRedirectToConverter extends SelfTestRedirectAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Run test for either jpeg or png
|
||||
*
|
||||
* @param string $rootId (ie "uploads" or "themes")
|
||||
* @param string $imageType ("jpeg" or "png")
|
||||
* @return array [$success, $log, $createdTestFiles]
|
||||
*/
|
||||
protected function runTestForImageType($rootId, $imageType)
|
||||
{
|
||||
$log = [];
|
||||
$createdTestFiles = false;
|
||||
$noWarningsYet = true;
|
||||
|
||||
$htaccessFile = Paths::getAbsDirById($rootId) . '/.htaccess';
|
||||
if (!FileHelper::fileExists($htaccessFile)) {
|
||||
$log[] = '**Warning: There is no .htaccess file in the ' . $rootId . ' folder!**{: .warn} (did you save settings yet?)';
|
||||
$noWarningsYet = false;
|
||||
} elseif (!HTAccess::haveWeRulesInThisHTAccess($htaccessFile)) {
|
||||
$log[] = '**Warning: There are no WebP Express rules in the .htaccess file in the ' . $rootId . ' folder!**{: .warn}';
|
||||
$noWarningsYet = false;
|
||||
}
|
||||
|
||||
// Copy test image (jpeg)
|
||||
list($subResult, $success, $sourceFileName) = SelfTestHelper::copyTestImageToRoot($rootId, $imageType);
|
||||
$log = array_merge($log, $subResult);
|
||||
if (!$success) {
|
||||
$log[] = 'The test cannot be completed';
|
||||
return [false, $log, $createdTestFiles];
|
||||
}
|
||||
$createdTestFiles = true;
|
||||
|
||||
$requestUrl = Paths::getUrlById($rootId) . '/webp-express-test-images/' . $sourceFileName;
|
||||
|
||||
$log[] = '### Lets check that browsers supporting webp gets a freshly converted WEBP ' .
|
||||
'when the ' . $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[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();
|
||||
}
|
||||
|
||||
}
|
||||
250
lib/classes/SelfTestRedirectToExisting.php
Normal file
250
lib/classes/SelfTestRedirectToExisting.php
Normal file
@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class SelfTestRedirectToExisting extends SelfTestRedirectAbstract
|
||||
{
|
||||
/**
|
||||
* Run test for either jpeg or png
|
||||
*
|
||||
* @param string $rootId (ie "uploads" or "themes")
|
||||
* @param string $imageType ("jpeg" or "png")
|
||||
* @return array [$success, $log, $createdTestFiles]
|
||||
*/
|
||||
protected function runTestForImageType($rootId, $imageType)
|
||||
{
|
||||
$log = [];
|
||||
$createdTestFiles = false;
|
||||
$noWarningsYet = true;
|
||||
|
||||
$log[] = '### Copying files for testing';
|
||||
|
||||
// Copy test image
|
||||
list($subResult, $success, $sourceFileName) = SelfTestHelper::copyTestImageToRoot($rootId, $imageType);
|
||||
$log = array_merge($log, $subResult);
|
||||
if (!$success) {
|
||||
$log[] = 'The test cannot be completed';
|
||||
return [false, $log, $createdTestFiles];
|
||||
}
|
||||
$createdTestFiles = true;
|
||||
|
||||
$log[] = '';
|
||||
|
||||
// Copy dummy webp
|
||||
list($subResult, $success, $destinationFile) = SelfTestHelper::copyDummyWebPToCacheFolder(
|
||||
$rootId,
|
||||
$this->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[] = "```<IfModule mod_mime.c>\n AddType image/webp .webp\n</IfModule>```";
|
||||
|
||||
$log[] = '### .htaccess status';
|
||||
$log = array_merge($log, SelfTestHelper::htaccessInfo($this->config, true));
|
||||
}
|
||||
|
||||
$log[] = 'The test **FAILED**{: .error}.';
|
||||
} else {
|
||||
$log[] = 'Additionally, the content-length reveals that we did not get the webp ' .
|
||||
'(we know that the file we put is exactly 6964 bytes). ' .
|
||||
'So we can conclude that the rewrite did not happen';
|
||||
$log[] = 'The test **FAILED**{: .error}.';
|
||||
$log[] = '#### Diagnosing rewrites';
|
||||
$log = array_merge($log, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers));
|
||||
}
|
||||
} else {
|
||||
$log[] = 'In addition, we did not get a *content-length* header either.' .
|
||||
$log[] = 'It seems we can conclude that the rewrite did not happen.';
|
||||
$log[] = 'The test **FAILED**{: .error}.';
|
||||
$log[] = '#### Diagnosing rewrites';
|
||||
$log = array_merge($log, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers));
|
||||
}
|
||||
|
||||
return [false, $log, $createdTestFiles];
|
||||
}
|
||||
|
||||
if (isset($headers['x-webp-convert-log'])) {
|
||||
$log[] = 'Bummer. Although we did get a webp, we did not get it as a result of a direct ' .
|
||||
'redirection. This webp was returned by the PHP script. Although this works, it takes more ' .
|
||||
'resources to ignite the PHP engine for each image request than redirecting directly to the image.';
|
||||
$log[] = 'The test FAILED.';
|
||||
|
||||
$log[] = 'It seems something went wrong with the redirection.';
|
||||
$log[] = '#### Diagnosing redirects';
|
||||
$log = array_merge($log, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers));
|
||||
|
||||
return [false, $log, $createdTestFiles];
|
||||
} else {
|
||||
$log[] = 'Alrighty. We got a webp. Just what we wanted. **Great!**{: .ok}';
|
||||
}
|
||||
|
||||
if (!SelfTestHelper::hasVaryAcceptHeader($headers)) {
|
||||
$log = array_merge($log, SelfTestHelper::diagnoseNoVaryHeader($rootId, 'existing'));
|
||||
$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';
|
||||
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, 'existing'));
|
||||
$noWarningsYet = false;
|
||||
}
|
||||
|
||||
return [$noWarningsYet, $log, $createdTestFiles];
|
||||
}
|
||||
|
||||
protected function getSuccessMessage()
|
||||
{
|
||||
return 'Everything **seems to work**{: .ok} as it should. ' .
|
||||
'However, a couple of things were not tested (it is on the TODO). ' .
|
||||
'TODO 1: If one image type is disabled, check that it does not redirect to webp (unless redirection to converter is set up). ' .
|
||||
'TODO 2: Test that redirection to webp only is triggered when the webp exists. ';
|
||||
}
|
||||
|
||||
public function startupTests()
|
||||
{
|
||||
$log[] = '# Testing redirection to existing webp';
|
||||
if (!$this->config['redirect-to-existing-in-htaccess']) {
|
||||
$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 SelfTestRedirectToExisting($config);
|
||||
return $me->startTest();
|
||||
}
|
||||
}
|
||||
257
lib/classes/SelfTestRedirectToWebPRealizer.php
Normal file
257
lib/classes/SelfTestRedirectToWebPRealizer.php
Normal file
@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
use \WebPExpress\Option;
|
||||
|
||||
|
||||
class SelfTestRedirectToWebPRealizer extends SelfTestRedirectAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Run test for either jpeg or png
|
||||
*
|
||||
* @param string $rootId (ie "uploads" or "themes")
|
||||
* @param string $imageType ("jpeg" or "png")
|
||||
* @return array [$success, $log, $createdTestFiles]
|
||||
*/
|
||||
protected function runTestForImageType($rootId, $imageType)
|
||||
{
|
||||
$log = [];
|
||||
$createdTestFiles = false;
|
||||
$noWarningsYet = true;
|
||||
|
||||
// Copy test image
|
||||
list($subResult, $success, $sourceFileName) = SelfTestHelper::copyTestImageToRoot($rootId, $imageType);
|
||||
$log = array_merge($log, $subResult);
|
||||
if (!$success) {
|
||||
$log[] = 'The test cannot be completed';
|
||||
return [false, $log, $createdTestFiles];
|
||||
}
|
||||
$createdTestFiles = true;
|
||||
|
||||
//$requestUrl = Paths::getUploadUrl() . '/' . $sourceFileName;
|
||||
|
||||
// Hacky, I know.
|
||||
// AlterHtmlHelper was not meant to be used like this, but it is the only place where we currently
|
||||
// have logic for finding destination url from source url.
|
||||
|
||||
//$sourceUrl = Paths::getUploadUrl() . '/' . $sourceFileName;
|
||||
$sourceUrl = Paths::getUrlById($rootId) . '/webp-express-test-images/' . $sourceFileName;
|
||||
|
||||
AlterHtmlHelper::$options = json_decode(Option::getOption('webp-express-alter-html-options', null), true);
|
||||
AlterHtmlHelper::$options['only-for-webps-that-exists'] = false;
|
||||
|
||||
// TODO: Check that AlterHtmlHelper::$options['scope'] is not empty
|
||||
// - it has been seen to happen
|
||||
|
||||
$requestUrl = AlterHtmlHelper::getWebPUrlInImageRoot(
|
||||
$sourceUrl,
|
||||
$rootId,
|
||||
Paths::getUrlById($rootId),
|
||||
Paths::getAbsDirById($rootId)
|
||||
);
|
||||
|
||||
if ($requestUrl === false) {
|
||||
// PS: this has happened due to AlterHtmlHelper::$options['scope'] being empty...
|
||||
|
||||
$log[] = 'Hm, strange. The source URL does not seem to be in the base root';
|
||||
$log[] = 'Source URL:' . $sourceUrl;
|
||||
//$log[] = 'Root ID:' . $rootId;
|
||||
$log[] = 'Root Url:' . Paths::getUrlById($rootId);
|
||||
$log[] = 'Request Url:' . $requestUrl;
|
||||
$log[] = 'parsed url:' . print_r(parse_url($sourceUrl), true);
|
||||
$log[] = 'parsed url:' . print_r(parse_url(Paths::getUrlById($rootId)), true);
|
||||
$log[] = 'scope:' . print_r(AlterHtmlHelper::$options['scope'], true);
|
||||
$log[] = 'cached options:' . print_r(AlterHtmlHelper::$options, true);
|
||||
$log[] = 'cached options: ' . print_r(Option::getOption('webp-express-alter-html-options', 'not there!'), true);
|
||||
}
|
||||
|
||||
|
||||
$log[] = '### Lets check that browsers supporting webp gets a freshly converted WEBP ' .
|
||||
'when a non-existing WEBP is requested, which has a corresponding source';
|
||||
$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[count($log) - 1] .= '. FAILED';
|
||||
//$log[] = '*' . $requestUrl . '*';
|
||||
|
||||
$log[] = 'The test **failed**{: .error}';
|
||||
|
||||
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));
|
||||
return [false, $log, $createdTestFiles];
|
||||
//$log[] = 'or that there is an .htaccess file in the ';
|
||||
}
|
||||
// $log[] = print_r($results[0]['response']['code'], true);
|
||||
}
|
||||
|
||||
$log[] = 'Why did it fail? It could either be that the redirection rule did not trigger ' .
|
||||
'or it could be that the PHP script could not locate a source image corresponding to the destination URL. ' .
|
||||
'Currently, this analysis cannot dertermine which was the case and it cannot be helpful ' .
|
||||
'if the latter is the case (sorry!). However, if the redirection rules are the problem, here is some info:';
|
||||
|
||||
$log[] = '### Diagnosing redirection problems (presuming it is the redirection to the script that is failing)';
|
||||
$log = array_merge($log, SelfTestHelper::diagnoseFailedRewrite($this->config, $headers));
|
||||
|
||||
|
||||
//$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];
|
||||
}
|
||||
|
||||
$log[] = '**Alrighty**{: .ok}. We got a webp.';
|
||||
if (isset($headers['x-webp-convert-log'])) {
|
||||
$log[] = 'The "x-webp-convert-log" headers reveals we got the webp from the PHP script. **Great!**{: .ok}';
|
||||
} else {
|
||||
$log[] = 'Interestingly, there are no "x-webp-convert-log" headers even though ' .
|
||||
'the PHP script always produces such. Could it be you have some weird setup that eats these headers?';
|
||||
}
|
||||
|
||||
if (SelfTestHelper::hasVaryAcceptHeader($headers)) {
|
||||
$log[] = 'All is however not super-duper:';
|
||||
|
||||
$log[] = '**Notice: We received a Vary:Accept header. ' .
|
||||
'That header need not to be set. Actually, it is a little bit bad for performance ' .
|
||||
'as proxies are currently doing a bad job maintaining several caches (in many cases they simply do not)**{: .warn}';
|
||||
$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[] = '';
|
||||
|
||||
return [$noWarningsYet, $log, $createdTestFiles];
|
||||
}
|
||||
|
||||
/*
|
||||
private static function doRunTest($this->config)
|
||||
{
|
||||
$log = [];
|
||||
$log[] = '# Testing redirection to converter';
|
||||
|
||||
$createdTestFiles = false;
|
||||
if (!file_exists(Paths::getConfigFileName())) {
|
||||
$log[] = 'Hold on. You need to save options before you can run this test. There is no config file yet.';
|
||||
return [true, $log, $createdTestFiles];
|
||||
}
|
||||
|
||||
|
||||
if ($this->config['image-types'] == 0) {
|
||||
$log[] = 'No image types have been activated, nothing to test';
|
||||
return [true, $log, $createdTestFiles];
|
||||
}
|
||||
|
||||
if ($this->config['image-types'] & 1) {
|
||||
list($success, $subResult, $createdTestFiles) = self::runTestForImageType($this->config, 'jpeg');
|
||||
$log = array_merge($log, $subResult);
|
||||
|
||||
if ($success) {
|
||||
if ($this->config['image-types'] & 2) {
|
||||
$log[] = '### Performing same tests for PNG';
|
||||
list($success, $subResult, $createdTestFiles2) = self::runTestForImageType($this->config, 'png');
|
||||
$createdTestFiles = $createdTestFiles || $createdTestFiles2;
|
||||
if ($success) {
|
||||
//$log[count($log) - 1] .= '. **ok**{: .ok}';
|
||||
$log[] .= 'All tests passed for PNG as well.';
|
||||
$log[] = '(I shall spare you for the report, which is almost identical to the one above)';
|
||||
} else {
|
||||
$log = array_merge($log, $subResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
list($success, $subResult, $createdTestFiles) = self::runTestForImageType($this->config, 'png');
|
||||
$log = array_merge($log, $subResult);
|
||||
}
|
||||
|
||||
if ($success) {
|
||||
$log[] = '### Conclusion';
|
||||
$log[] = 'Everything **seems to work**{: .ok} as it should. ' .
|
||||
'However, notice that this test only tested an image which was placed in the *uploads* folder. ' .
|
||||
'The rest of the image roots (such as theme images) have not been tested (it is on the TODO). ' .
|
||||
'Also on the TODO: If one image type is disabled, check that it does not redirect to the conversion script. ' .
|
||||
'These things probably work, though.';
|
||||
}
|
||||
|
||||
|
||||
return [true, $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 "WebP Realizer" functionality';
|
||||
if (!$this->config['enable-redirection-to-webp-realizer']) {
|
||||
$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 SelfTestRedirectToWebPRealizer($config);
|
||||
return $me->startTest();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
45
lib/classes/State.php
Normal file
45
lib/classes/State.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Option;
|
||||
|
||||
/**
|
||||
* Store state in db
|
||||
* We are using update_option WITHOUT autoloading.
|
||||
* So this class is not intended for storing stuff that is needed on every page load.
|
||||
* For such things, use update_option / get_option directly
|
||||
*/
|
||||
|
||||
|
||||
class State
|
||||
{
|
||||
|
||||
public static function getStateObj() {
|
||||
// TODO: cache
|
||||
$json = Option::getOption('webp-express-state', '[]');
|
||||
return json_decode($json, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return state by key. Returns supplied default if key doesn't exist, or state object is corrupt
|
||||
*/
|
||||
public static function getState($key, $default = null) {
|
||||
$obj = self::getStateObj();
|
||||
if ($obj != false) {
|
||||
if (isset($obj[$key])) {
|
||||
return $obj[$key];
|
||||
}
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
public static function setState($key, $value) {
|
||||
$currentStateObj = self::getStateObj();
|
||||
$currentStateObj[$key] = $value;
|
||||
$json = json_encode($currentStateObj, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
|
||||
|
||||
// Store in db. No autoloading.
|
||||
Option::updateOption('webp-express-state', $json, false);
|
||||
}
|
||||
}
|
||||
152
lib/classes/TestRun.php
Normal file
152
lib/classes/TestRun.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\Config;
|
||||
use \WebPExpress\ConvertersHelper;
|
||||
use \WebPExpress\Paths;
|
||||
use \WebPExpress\FileHelper;
|
||||
|
||||
use \WebPConvert\Convert\ConverterFactory;
|
||||
use \WebPConvert\Convert\Helpers\JpegQualityDetector;
|
||||
|
||||
include_once WEBPEXPRESS_PLUGIN_DIR . '/vendor/autoload.php';
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class TestRun
|
||||
{
|
||||
|
||||
|
||||
public static $converterStatus = null; // to cache the result
|
||||
|
||||
private static $warnings;
|
||||
|
||||
public static function warningHandler($errno, $errstr, $errfile, $errline, $errcontext = null)
|
||||
{
|
||||
$errorTypes = [
|
||||
E_WARNING => "Warning",
|
||||
E_NOTICE => "Notice",
|
||||
E_STRICT => "Strict Notice",
|
||||
E_DEPRECATED => "Deprecated",
|
||||
E_USER_DEPRECATED => "User Deprecated",
|
||||
];
|
||||
|
||||
if (isset($errorTypes[$errno])) {
|
||||
$errType = $errorTypes[$errno];
|
||||
} else {
|
||||
$errType = "Warning ($errno)";
|
||||
}
|
||||
|
||||
$msg = $errType . ': ' . $errstr . ' in ' . $errfile . ', line ' . $errline;
|
||||
self::$warnings[] = $msg;
|
||||
|
||||
// suppress!
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Get a test result object OR false, if tests cannot be made.
|
||||
*
|
||||
* @return object|false
|
||||
*/
|
||||
public static function getConverterStatus() {
|
||||
//return false;
|
||||
|
||||
// Is result cached?
|
||||
if (isset(self::$converterStatus)) {
|
||||
return self::$converterStatus;
|
||||
}
|
||||
$source = Paths::getWebPExpressPluginDirAbs() . '/test/small-q61.jpg';
|
||||
$destination = Paths::getUploadDirAbs() . '/webp-express-test-conversion.webp';
|
||||
if (!FileHelper::canCreateFile($destination)) {
|
||||
$destination = Paths::getContentDirAbs() . '/webp-express-test-conversion.webp';
|
||||
}
|
||||
if (!FileHelper::canCreateFile($destination)) {
|
||||
self::$converterStatus = false; // // cache the result
|
||||
return false;
|
||||
}
|
||||
$workingConverters = [];
|
||||
$errors = [];
|
||||
self::$warnings = [];
|
||||
|
||||
// We need wod options.
|
||||
// But we cannot simply use loadWodOptions - because that would leave out the deactivated
|
||||
// converters. And we need to test all converters - even the deactivated ones.
|
||||
// So we load config, set "deactivated" to false, and generate Wod options from the config
|
||||
$config = Config::loadConfigAndFix();
|
||||
|
||||
// set deactivated to false on all converters
|
||||
foreach($config['converters'] as &$converter) {
|
||||
$converter['deactivated'] = false;
|
||||
}
|
||||
|
||||
$options = Config::generateWodOptionsFromConfigObj($config);
|
||||
$options['converters'] = ConvertersHelper::normalize($options['webp-convert']['convert']['converters']);
|
||||
|
||||
$previousErrorHandler = set_error_handler(
|
||||
array('\WebPExpress\TestRun', "warningHandler"),
|
||||
E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE
|
||||
);
|
||||
|
||||
$warnings = [];
|
||||
//echo '<pre>' . print_r($options, true) . '</pre>';
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
lib/classes/Validate.php
Normal file
27
lib/classes/Validate.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\ConvertersHelper;
|
||||
use \WebPExpress\ValidateException;
|
||||
use \WebPExpress\SanityCheck;
|
||||
|
||||
class Validate
|
||||
{
|
||||
|
||||
public static function postHasKey($key)
|
||||
{
|
||||
if (!isset($_POST[$key])) {
|
||||
throw new ValidateException('Expected parameter in POST missing: ' . $key);
|
||||
}
|
||||
}
|
||||
|
||||
public static function isConverterId($converterId, $errorMsg = 'Not a valid converter id')
|
||||
{
|
||||
SanityCheck::pregMatch('#^[a-z]+$#', $converterId, $errorMsg);
|
||||
if (!in_array($converterId, ConvertersHelper::getDefaultConverterNames())) {
|
||||
throw new ValidateException($errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
7
lib/classes/ValidateException.php
Normal file
7
lib/classes/ValidateException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
class ValidateException extends \Exception
|
||||
{
|
||||
}
|
||||
659
lib/classes/WCFMApi.php
Normal file
659
lib/classes/WCFMApi.php
Normal file
@ -0,0 +1,659 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPConvert\Convert\Converters\Stack;
|
||||
use \WebPConvert\WebPConvert;
|
||||
use \ImageMimeTypeGuesser\ImageMimeTypeGuesser;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class WCFMApi
|
||||
{
|
||||
private static function doProcessRequest() {
|
||||
if (!check_ajax_referer('webpexpress-wcfm-nonce', 'nonce', false)) {
|
||||
throw new \Exception('The security nonce has expired. You need to reload (press F5) and try again)');
|
||||
}
|
||||
Validate::postHasKey('command');
|
||||
$command = sanitize_text_field(stripslashes($_POST['command']));
|
||||
|
||||
switch ($command) {
|
||||
/*
|
||||
case 'get-tree':
|
||||
$result = self::processGetTree();
|
||||
break;*/
|
||||
case 'get-folder':
|
||||
$result = self::processGetFolder();
|
||||
break;
|
||||
case 'conversion-settings':
|
||||
$result = self::processConversionSettings();
|
||||
break;
|
||||
case 'info':
|
||||
$result = self::processInfo();
|
||||
break;
|
||||
case 'convert':
|
||||
$result = self::processConvert();
|
||||
break;
|
||||
case 'delete-converted':
|
||||
$result = self::processDeleteConverted();
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('Unknown command');
|
||||
}
|
||||
if (!isset($result)) {
|
||||
throw new \Exception('Command: ' . $command . ' gave no result');
|
||||
}
|
||||
|
||||
$json = wp_json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
if ($json === false) {
|
||||
// TODO: We can do better error handling than this!
|
||||
throw new \Exception('Failed encoding result to JSON');
|
||||
} else {
|
||||
echo $json;
|
||||
}
|
||||
wp_die();
|
||||
}
|
||||
|
||||
public static function processRequest() {
|
||||
try {
|
||||
self::doProcessRequest();
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
wp_send_json_error($e->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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
54
lib/classes/WCFMPage.php
Normal file
54
lib/classes/WCFMPage.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
use \WebPConvert\WebPConvert;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
class WCFMPage
|
||||
{
|
||||
|
||||
// callback (registred in AdminUi)
|
||||
public static function display() {
|
||||
echo '<div id="wcfmintro">' .
|
||||
'<h1>WebP Express Conversion Browser</h1>' .
|
||||
'</div>';
|
||||
|
||||
echo '<div id="webpconvert-filemanager" style="position:relative; min-height:400px">loading</div>';
|
||||
//include WEBPEXPRESS_PLUGIN_DIR . '/lib/options/page.php';
|
||||
|
||||
/* require_once __DIR__ . "/../../vendor/autoload.php";
|
||||
// print_r(WebPConvert::getConverterOptionDefinitions('png', false, true));
|
||||
echo '<pre>' .
|
||||
print_r(
|
||||
json_encode(
|
||||
WebPConvert::getConverterOptionDefinitions('png', false, true),
|
||||
JSON_PRETTY_PRINT
|
||||
),
|
||||
true
|
||||
) . '</pre>';*/
|
||||
}
|
||||
|
||||
/* 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 '<scr' . 'ipt>window.webpExpressWCFMNonce = "' . $wcfmNonce . '";</scr' . 'ipt>';
|
||||
|
||||
echo '<scr' . 'ipt src="' . $baseUrl . '/wcfm-options.js?25"></scr' . 'ipt>';
|
||||
//echo '<scr' . 'ipt type="module" src="' . $baseUrl . '/vendor.js?1"></scr' . 'ipt>';
|
||||
|
||||
echo '<scr' . 'ipt type="module" src="' . $baseUrl . '/index.be5d792e.js"></scr' . 'ipt>';
|
||||
echo '<link rel="stylesheet" href="' . $baseUrl . '/index.0c25b0fb.css">';
|
||||
}
|
||||
|
||||
}
|
||||
36
lib/classes/WPHttpRequester.php
Normal file
36
lib/classes/WPHttpRequester.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \HtaccessCapabilityTester\HttpRequesterInterface;
|
||||
use \HtaccessCapabilityTester\HttpResponse;
|
||||
|
||||
class WPHttpRequester implements HttpRequesterInterface
|
||||
{
|
||||
/**
|
||||
* Make a HTTP request to a URL.
|
||||
*
|
||||
* @param string $url The URL to make the HTTP request to
|
||||
*
|
||||
* @return HttpResponse A HttpResponse object, which simply contains body, status code
|
||||
* and response headers
|
||||
*/
|
||||
public function makeHTTPRequest($url) {
|
||||
$response = wp_remote_get($url, ['timeout' => 10]);
|
||||
//echo '<pre>' . print_r($response, true) . '</pre>';
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
285
lib/classes/WebPOnDemand.php
Normal file
285
lib/classes/WebPOnDemand.php
Normal file
@ -0,0 +1,285 @@
|
||||
<?php
|
||||
/*
|
||||
This class is used by wod/webp-on-demand.php, which does not do a Wordpress bootstrap, but does register an autoloader for
|
||||
the WebPExpress classes.
|
||||
|
||||
Calling Wordpress functions will FAIL. Make sure not to do that in either this class or the helpers.
|
||||
*/
|
||||
//error_reporting(E_ALL);
|
||||
//ini_set('display_errors', 1);
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\ConvertHelperIndependent;
|
||||
use \WebPExpress\Sanitize;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
use \WebPExpress\ValidateException;
|
||||
use \WebPExpress\Validate;
|
||||
use \WebPExpress\WodConfigLoader;
|
||||
use WebPConvert\Loggers\EchoLogger;
|
||||
|
||||
class WebPOnDemand extends WodConfigLoader
|
||||
{
|
||||
private static function getSourceDocRoot() {
|
||||
|
||||
|
||||
//echo 't:' . $_GET['test'];exit;
|
||||
// Check if it is in an environment variable
|
||||
$source = self::getEnvPassedInRewriteRule('REQFN');
|
||||
if ($source !== false) {
|
||||
self::$checking = 'source (passed through env)';
|
||||
return SanityCheck::absPathExistsAndIsFile($source);
|
||||
}
|
||||
|
||||
// Check if it is in header (but only if .htaccess was configured to send in header)
|
||||
if (isset($wodOptions['base-htaccess-on-these-capability-tests'])) {
|
||||
$capTests = $wodOptions['base-htaccess-on-these-capability-tests'];
|
||||
$passThroughHeaderDefinitelyUnavailable = ($capTests['passThroughHeaderWorking'] === false);
|
||||
$passThrougEnvVarDefinitelyAvailable =($capTests['passThroughEnvWorking'] === true);
|
||||
// This determines if .htaccess was configured to send in querystring
|
||||
$headerMagicAddedInHtaccess = ((!$passThrougEnvVarDefinitelyAvailable) && (!$passThroughHeaderDefinitelyUnavailable));
|
||||
} else {
|
||||
$headerMagicAddedInHtaccess = true; // pretend its true
|
||||
}
|
||||
if ($headerMagicAddedInHtaccess && (isset($_SERVER['HTTP_REQFN']))) {
|
||||
self::$checking = 'source (passed through request header)';
|
||||
return SanityCheck::absPathExistsAndIsFile($_SERVER['HTTP_REQFN']);
|
||||
}
|
||||
|
||||
if (!isset(self::$docRoot)) {
|
||||
//$source = self::getEnvPassedInRewriteRule('REQFN');
|
||||
if (isset($_GET['root-id']) && isset($_GET['xsource-rel-to-root-id'])) {
|
||||
$xsrcRelToRootId = SanityCheck::noControlChars($_GET['xsource-rel-to-root-id']);
|
||||
$srcRelToRootId = SanityCheck::pathWithoutDirectoryTraversal(substr($xsrcRelToRootId, 1));
|
||||
//echo $srcRelToRootId; exit;
|
||||
|
||||
$rootId = SanityCheck::noControlChars($_GET['root-id']);
|
||||
SanityCheck::pregMatch('#^[a-z]+$#', $rootId, 'Not a valid root-id');
|
||||
|
||||
$source = self::getRootPathById($rootId) . '/' . $srcRelToRootId;
|
||||
return SanityCheck::absPathExistsAndIsFile($source);
|
||||
}
|
||||
}
|
||||
|
||||
// Check querystring (relative path to docRoot) - when docRoot is available
|
||||
if (isset(self::$docRoot) && isset($_GET['xsource-rel'])) {
|
||||
self::$checking = 'source (passed as relative path, through querystring)';
|
||||
$xsrcRel = SanityCheck::noControlChars($_GET['xsource-rel']);
|
||||
$srcRel = SanityCheck::pathWithoutDirectoryTraversal(substr($xsrcRel, 1));
|
||||
return SanityCheck::absPathExistsAndIsFile(self::$docRoot . '/' . $srcRel);
|
||||
}
|
||||
|
||||
// Check querystring (relative path to plugin) - when docRoot is unavailable
|
||||
/*TODO
|
||||
if (!isset(self::$docRoot) && isset($_GET['xsource-rel-to-plugin-dir'])) {
|
||||
self::$checking = 'source (passed as relative path to plugin dir, through querystring)';
|
||||
$xsrcRelPlugin = SanityCheck::noControlChars($_GET['xsource-rel-to-plugin-dir']);
|
||||
$srcRelPlugin = SanityCheck::pathWithoutDirectoryTraversal(substr($xsrcRelPlugin, 1));
|
||||
return SanityCheck::absPathExistsAndIsFile(self::$docRoot . '/' . $srcRel);
|
||||
}*/
|
||||
|
||||
|
||||
// Check querystring (full path)
|
||||
// - But only on Nginx (our Apache .htaccess rules never passes absolute url)
|
||||
if (
|
||||
(self::isNginxHandlingImages()) &&
|
||||
(isset($_GET['source']) || isset($_GET['xsource']))
|
||||
) {
|
||||
self::$checking = 'source (passed as absolute path on nginx)';
|
||||
if (isset($_GET['source'])) {
|
||||
$source = SanityCheck::absPathExistsAndIsFile($_GET['source']);
|
||||
} else {
|
||||
$xsrc = SanityCheck::noControlChars($_GET['xsource']);
|
||||
return SanityCheck::absPathExistsAndIsFile(substr($xsrc, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort is to use $_SERVER['REQUEST_URI'], well knowing that it does not give the
|
||||
// correct result in all setups (ie "folder method 1")
|
||||
if (isset(self::$docRoot)) {
|
||||
self::$checking = 'source (retrieved by the request_uri server var)';
|
||||
$srcRel = SanityCheck::pathWithoutDirectoryTraversal(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
||||
return SanityCheck::absPathExistsAndIsFile(self::$docRoot . $srcRel);
|
||||
}
|
||||
}
|
||||
|
||||
private static function getSourceNoDocRoot()
|
||||
{
|
||||
$dirIdOfHtaccess = self::getEnvPassedInRewriteRule('WE_HTACCESS_ID');
|
||||
if ($dirIdOfHtaccess === false) {
|
||||
$dirIdOfHtaccess = SanityCheck::noControlChars($_GET['htaccess-id']);
|
||||
}
|
||||
|
||||
if (!in_array($dirIdOfHtaccess, ['uploads', 'themes', 'wp-content', 'plugins', 'index'])) {
|
||||
throw new \Exception('invalid htaccess directory id argument.');
|
||||
}
|
||||
|
||||
// First try ENV
|
||||
$sourceRelHtaccess = self::getEnvPassedInRewriteRule('WE_SOURCE_REL_HTACCESS');
|
||||
|
||||
// Otherwise use query-string
|
||||
if ($sourceRelHtaccess === false) {
|
||||
if (isset($_GET['xsource-rel-htaccess'])) {
|
||||
$x = SanityCheck::noControlChars($_GET['xsource-rel-htaccess']);
|
||||
$sourceRelHtaccess = SanityCheck::pathWithoutDirectoryTraversal(substr($x, 1));
|
||||
} else {
|
||||
throw new \Exception('Argument for source path is missing');
|
||||
}
|
||||
}
|
||||
|
||||
$sourceRelHtaccess = SanityCheck::pathWithoutDirectoryTraversal($sourceRelHtaccess);
|
||||
|
||||
|
||||
$imageRoots = self::getImageRootsDef();
|
||||
|
||||
$source = $imageRoots->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 '<pre>' . print_r($wodOptions, true) . '</pre>'; 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 . '<br>' . $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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
276
lib/classes/WebPRealizer.php
Normal file
276
lib/classes/WebPRealizer.php
Normal file
@ -0,0 +1,276 @@
|
||||
<?php
|
||||
/*
|
||||
This class is used by wod/webp-realizer.php, which does not do a Wordpress bootstrap, but does register an autoloader for
|
||||
the WebPExpress classes.
|
||||
|
||||
Calling Wordpress functions will FAIL. Make sure not to do that in either this class or the helpers.
|
||||
*/
|
||||
//error_reporting(E_ALL);
|
||||
//ini_set('display_errors', 1);
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPExpress\ConvertHelperIndependent;
|
||||
use \WebPExpress\Sanitize;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
use \WebPExpress\ValidateException;
|
||||
use \WebPExpress\Validate;
|
||||
use \WebPExpress\WodConfigLoader;
|
||||
|
||||
class WebPRealizer extends WodConfigLoader
|
||||
{
|
||||
private static function getDestinationDocRoot() {
|
||||
$docRoot = self::$docRoot;
|
||||
|
||||
// Check if it is in an environment variable
|
||||
$destRel = self::getEnvPassedInRewriteRule('DESTINATIONREL');
|
||||
if ($destRel !== false) {
|
||||
return SanityCheck::absPath($docRoot . '/' . $destRel);
|
||||
}
|
||||
|
||||
// Check querystring (relative path)
|
||||
if (isset($_GET['xdestination-rel'])) {
|
||||
$xdestRel = SanityCheck::noControlChars($_GET['xdestination-rel']);
|
||||
$destRel = SanityCheck::pathWithoutDirectoryTraversal(substr($xdestRel, 1));
|
||||
$destination = SanityCheck::absPath($docRoot . '/' . $destRel);
|
||||
return SanityCheck::absPathIsInDocRoot($destination);
|
||||
}
|
||||
|
||||
// Check querystring (full path)
|
||||
// - But only on Nginx (our Apache .htaccess rules never passes absolute url)
|
||||
if (self::isNginxHandlingImages()) {
|
||||
if (isset($_GET['destination'])) {
|
||||
return SanityCheck::absPathIsInDocRoot($_GET['destination']);
|
||||
}
|
||||
if (isset($_GET['xdestination'])) {
|
||||
$xdest = SanityCheck::noControlChars($_GET['xdestination']);
|
||||
return SanityCheck::absPathIsInDocRoot(substr($xdest, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort is to use $_SERVER['REQUEST_URI'], well knowing that it does not give the
|
||||
// correct result in all setups (ie "folder method 1").
|
||||
// On nginx, it can even return the path to webp-realizer.php. TODO: Handle that better than now
|
||||
$destRel = SanityCheck::pathWithoutDirectoryTraversal(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
||||
if ($destRel) {
|
||||
if (preg_match('#webp-realizer\.php$#', $destRel)) {
|
||||
throw new \Exception(
|
||||
'webp-realizer.php need to know the file path and cannot simply use $_SERVER["REQUEST_URI"] ' .
|
||||
'as that points to itself rather than the image requested. ' .
|
||||
'On Nginx, please add: "&xdestination=x$request_filename" to the URL in the rules in the nginx config ' .
|
||||
'(sorry, the parameter was missing in the rules in the README for a while, but it is back)'
|
||||
);
|
||||
}
|
||||
}
|
||||
$destination = SanityCheck::absPath($docRoot . $destRel);
|
||||
return SanityCheck::absPathIsInDocRoot($destination);
|
||||
}
|
||||
|
||||
private static function getDestinationNoDocRoot() {
|
||||
|
||||
$dirIdOfHtaccess = self::getEnvPassedInRewriteRule('WE_HTACCESS_ID');
|
||||
if ($dirIdOfHtaccess === false) {
|
||||
$dirIdOfHtaccess = SanityCheck::noControlChars($_GET['htaccess-id']);
|
||||
}
|
||||
|
||||
if (!in_array($dirIdOfHtaccess, ['uploads', 'cache'])) {
|
||||
throw new \Exception('invalid htaccess directory id argument. It must be either "uploads" or "cache".');
|
||||
}
|
||||
|
||||
|
||||
// First try ENV
|
||||
$destinationRelHtaccess = self::getEnvPassedInRewriteRule('WE_DESTINATION_REL_HTACCESS');
|
||||
|
||||
// Otherwise use query-string
|
||||
if ($destinationRelHtaccess === false) {
|
||||
if (isset($_GET['xdestination-rel-htaccess'])) {
|
||||
$x = SanityCheck::noControlChars($_GET['xdestination-rel-htaccess']);
|
||||
$destinationRelHtaccess = SanityCheck::pathWithoutDirectoryTraversal(substr($x, 1));
|
||||
} else {
|
||||
throw new \Exception('Argument for destination path is missing');
|
||||
}
|
||||
}
|
||||
|
||||
$destinationRelHtaccess = SanityCheck::pathWithoutDirectoryTraversal($destinationRelHtaccess);
|
||||
|
||||
$imageRoots = self::getImageRootsDef();
|
||||
if ($dirIdOfHtaccess == 'uploads') {
|
||||
return $imageRoots->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 '<pre>' . print_r($wodOptions, true) . '</pre>'; 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 '<h3>destination:</h3> ' . $destination . '<h3>source:</h3>' . $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:<br><i>' . $destination . '</i>';
|
||||
}
|
||||
//$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());
|
||||
}
|
||||
}
|
||||
}
|
||||
252
lib/classes/WodConfigLoader.php
Normal file
252
lib/classes/WodConfigLoader.php
Normal file
@ -0,0 +1,252 @@
|
||||
<?php
|
||||
/*
|
||||
This class is used by wod/webp-on-demand.php, which does not do a Wordpress bootstrap, but does register an autoloader for
|
||||
the WebPExpress classes.
|
||||
|
||||
Calling Wordpress functions will FAIL. Make sure not to do that in either this class or the helpers.
|
||||
*/
|
||||
//error_reporting(E_ALL);
|
||||
//ini_set('display_errors', 1);
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
use \WebPConvert\Convert\Converters\Ewww;
|
||||
|
||||
use \WebPExpress\ImageRoots;
|
||||
use \WebPExpress\Sanitize;
|
||||
use \WebPExpress\SanityCheck;
|
||||
use \WebPExpress\SanityException;
|
||||
use \WebPExpress\ValidateException;
|
||||
use \WebPExpress\Validate;
|
||||
|
||||
class WodConfigLoader
|
||||
{
|
||||
|
||||
protected static $docRoot;
|
||||
protected static $checking;
|
||||
protected static $wodOptions;
|
||||
protected static $options;
|
||||
protected static $usingDocRoot;
|
||||
protected static $webExpressContentDirAbs;
|
||||
|
||||
public static function exitWithError($msg) {
|
||||
header('X-WebP-Express-Error: ' . $msg, true);
|
||||
echo $msg;
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Apache handles the PHP requests (Note that duel setups are possible and ie Nginx could be handling the image requests).
|
||||
*/
|
||||
public static function isApache()
|
||||
{
|
||||
return (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false);
|
||||
}
|
||||
|
||||
protected static function isNginxHandlingImages()
|
||||
{
|
||||
if (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') !== false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// On WP Engine, SERVER_SOFTWARE is "Apache", but images are handled by NGINX.
|
||||
if (isset($_SERVER['WPENGINE_ACCOUNT'])) {
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function preventDirectAccess($filename)
|
||||
{
|
||||
// Protect against directly accessing webp-on-demand.php
|
||||
// Only protect on Apache. We know for sure that the method is not reliable on nginx.
|
||||
// We have not tested on litespeed yet, so we dare not.
|
||||
if (self::isApache() && (!self::isNginxHandlingImages())) {
|
||||
if (strpos($_SERVER['REQUEST_URI'], $filename) !== false) {
|
||||
self::exitWithError(
|
||||
'It seems you are visiting this file (plugins/webp-express/wod/' . $filename . ') directly. We do not allow this.'
|
||||
);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable set with mod_rewrite module
|
||||
* Return false if the environment variable isn't found
|
||||
*/
|
||||
protected static function getEnvPassedInRewriteRule($envName) {
|
||||
// Envirenment variables passed through the REWRITE module have "REWRITE_" as a prefix (in Apache, not Litespeed, if I recall correctly)
|
||||
// Multiple iterations causes multiple REWRITE_ prefixes, and we get many environment variables set.
|
||||
// Multiple iterations causes multiple REWRITE_ prefixes, and we get many environment variables set.
|
||||
// We simply look for an environment variable that ends with what we are looking for.
|
||||
// (so make sure to make it unique)
|
||||
$len = strlen($envName);
|
||||
foreach ($_SERVER as $key => $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'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
18
lib/debug.php
Normal file
18
lib/debug.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
/*
|
||||
This file is actually not included, only when debugging activation errors, I include it manually.
|
||||
Haven't used it in a quite a while...
|
||||
*/
|
||||
|
||||
function webpexpress_activated() {
|
||||
update_option( 'webp-express-activation-error', ob_get_contents() );
|
||||
}
|
||||
add_action( 'activated_plugin', 'webpexpress_activated' );
|
||||
if (!empty(get_option('webp-express-activation-error'))) {
|
||||
add_filter( 'admin_footer_text', function() {
|
||||
return 'Activation error:' . get_option('webp-express-activation-error');
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
$msgId = '0.19.0/meet-ffmpeg-a-working-conversion-method';
|
||||
|
||||
DismissableGlobalMessages::printDismissableMessage(
|
||||
'success',
|
||||
'Great news!<br><br> 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'],
|
||||
]
|
||||
);
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
$msgId = '0.19.0/meet-ffmpeg-better-than-ewww';
|
||||
|
||||
DismissableGlobalMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.19.0 introduced a new conversion method: ffmpeg. ' .
|
||||
'You may consider moving it above ewww, as ffmpeg supports the "Auto" WebP encoding option ' .
|
||||
'(encoding to both lossy and lossless and then selecting the smallest)',
|
||||
$msgId,
|
||||
[
|
||||
['text' => 'Take me to the settings', 'redirect-to-settings' => true],
|
||||
['text' => 'Dismiss'],
|
||||
]
|
||||
);
|
||||
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
$msgId = '0.19.0/meet-ffmpeg-better-than-gd';
|
||||
|
||||
DismissableGlobalMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.19.0 introduced a new conversion method: ffmpeg. ' .
|
||||
'You may consider moving it above Gd, as it is slightly better. ',
|
||||
$msgId,
|
||||
[
|
||||
['text' => 'Take me to the settings', 'redirect-to-settings' => true],
|
||||
['text' => 'Dismiss'],
|
||||
]
|
||||
);
|
||||
39
lib/dismissable-messages/0.14.0/say-hello-to-vips.php
Normal file
39
lib/dismissable-messages/0.14.0/say-hello-to-vips.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
use \WebPExpress\DismissableMessages;
|
||||
use \WebPExpress\State;
|
||||
use \WebPExpress\TestRun;
|
||||
|
||||
/*
|
||||
$testResult = TestRun::getConverterStatus();
|
||||
if ($testResult !== false) {
|
||||
$workingConvertersIds = $testResult['workingConverters'];
|
||||
} else {
|
||||
$workingConvertersIds = [];
|
||||
}
|
||||
*/
|
||||
|
||||
$workingConvertersIds = State::getState('workingConverterIds', []);
|
||||
|
||||
if (in_array('vips', $workingConvertersIds)) {
|
||||
if (in_array('cwebp', $workingConvertersIds)) {
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'<p>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.</p>',
|
||||
'0.14.0/say-hello-to-vips',
|
||||
'Got it!'
|
||||
);
|
||||
} else {
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'<p>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.' .
|
||||
'</p>',
|
||||
'0.14.0/say-hello-to-vips',
|
||||
'Got it!'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// show message?
|
||||
}
|
||||
10
lib/dismissable-messages/0.14.0/suggest-enable-pngs.php
Normal file
10
lib/dismissable-messages/0.14.0/suggest-enable-pngs.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use \WebPExpress\DismissableMessages;
|
||||
|
||||
// introduced in 0.14.0 (migrate 9)
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.14 handles PNG to WebP conversions quite well. Perhaps it is time to enable PNGs? ',
|
||||
'0.14.0/suggest-enable-pngs',
|
||||
'Got it!'
|
||||
);
|
||||
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
use \WebPExpress\DismissableMessages;
|
||||
use \WebPExpress\State;
|
||||
use \WebPExpress\TestRun;
|
||||
|
||||
$convertersSupportingEncodingAuto = ['cwebp', 'vips', 'imagick', 'imagemagick', 'gmagick', 'graphicsmagick'];
|
||||
|
||||
$workingConvertersIds = State::getState('workingConverterIds', []);
|
||||
$workingAndActiveConverterIds = State::getState('workingAndActiveConverterIds', []);
|
||||
|
||||
$firstActiveAndWorkingConverterId = (isset($workingAndActiveConverterIds[0]) ? $workingAndActiveConverterIds[0] : '');
|
||||
|
||||
if (in_array($firstActiveAndWorkingConverterId, $convertersSupportingEncodingAuto)) {
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'<p>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.</p>' .
|
||||
'<p>Once satisfied, dont forget to ' .
|
||||
'wipe your existing converted files (there is a "Delete converted files" button for that here on this page).</p>',
|
||||
'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',
|
||||
'<p>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.</p>' .
|
||||
'<p>Once satisfied, dont forget to wipe your existing converted files (there is a "Delete converted files" ' .
|
||||
'button for that here on this page)</p>' .
|
||||
'<p>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.</p>',
|
||||
'0.14.0/suggest-wipe-because-lossless',
|
||||
'Got it!'
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace WebPExpress;
|
||||
|
||||
// introduced in 0.14.0 (migrate 9)
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.15 introduced a new "scope" setting which determines which folders that WebP Express ' .
|
||||
'operates in. It has been set to "All content" in order not to change behaviour. ' .
|
||||
'However, I would usually recommend limitting scope to "Uploads and Themes".',
|
||||
'0.15.0/new-scope-setting-content',
|
||||
'Got it!'
|
||||
);
|
||||
13
lib/dismissable-messages/0.15.0/new-scope-setting-index.php
Normal file
13
lib/dismissable-messages/0.15.0/new-scope-setting-index.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
// introduced in 0.14.0 (migrate 9)
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.15 introduced a new "scope" setting which determines which folders that WebP Express ' .
|
||||
'operates in. It has been set to work on "index" (any images in the whole install, including wp-adimn) ' .
|
||||
'in order not to change the behaviour. However, I would usually recommend a more limitted scope, ie. "Uploads and Themes".',
|
||||
'0.15.0/new-scope-setting-index',
|
||||
'Got it!'
|
||||
);
|
||||
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'WebP Express 0.15 introduced a new "scope" setting which determines which folders that WebP Express ' .
|
||||
'operates in. The migration script did not set it to include "uploads" because it seemed that it ' .
|
||||
'would not be possible to write the .htaccess rules there.',
|
||||
'0.15.0/new-scope-setting-no-uploads',
|
||||
'Got it!'
|
||||
);
|
||||
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/*
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'error',
|
||||
'Sorry, due to a bug, the combination of having destination folder set to "mingled" and ' .
|
||||
'File extension set to "Set to .webp" ' .
|
||||
'does not currently work. Please change the settings. ' .
|
||||
'I shall fix this soon!',
|
||||
'0.15.1/problems-with-mingled-set',
|
||||
'Got it!'
|
||||
);
|
||||
*/
|
||||
19
lib/dismissable-messages/0.16.0/nginx-link-to-faq.php
Normal file
19
lib/dismissable-messages/0.16.0/nginx-link-to-faq.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
/*echo '<p>You are running on NGINX. WebP Express works well on NGINX, however this UI is not streamlined NGINX yet. </p>' .
|
||||
'<p><b>You should head over to the </b>' .
|
||||
'<a href="https://wordpress.org/plugins/webp-express/#i%20am%20on%20nginx%20or%20openresty" target="_blank"><b>NGINX section in the FAQ</b></a>' .
|
||||
'<b> to learn how to use WebP Express on NGINX</b></p>';*/
|
||||
|
||||
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'warning',
|
||||
'<p>You are running on NGINX. WebP Express works well on NGINX, however this UI is not streamlined NGINX yet. </p>' .
|
||||
'<p><b>You should head over to the </b>' .
|
||||
'<a href="https://wordpress.org/plugins/webp-express/#i%20am%20on%20nginx%20or%20openresty" target="_blank"><b>NGINX section in the FAQ</b></a>' .
|
||||
'<b> to learn how to use WebP Express on NGINX</b></p>',
|
||||
'0.16.0/nginx-link-to-faq',
|
||||
'Got it!'
|
||||
);
|
||||
34
lib/dismissable-messages/0.23.0/elementor.php
Normal file
34
lib/dismissable-messages/0.23.0/elementor.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace WebPExpress;
|
||||
|
||||
$elementorActivated = in_array('elementor/elementor.php', get_option('active_plugins', []));
|
||||
$showMessage = false;
|
||||
if ($elementorActivated) {
|
||||
try {
|
||||
// The following is wrapped in a try statement because it depends on Elementor classes which might be subject to change
|
||||
if (\Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_css_loading' ) === false) {
|
||||
$showMessage = true;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Well, just bad luck.
|
||||
}
|
||||
}
|
||||
|
||||
if ($showMessage) {
|
||||
DismissableMessages::printDismissableMessage(
|
||||
'info',
|
||||
'<p>' .
|
||||
'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 <i>Elementor > Settings > Experiments</i> and activate "Improved CSS Loading". ' .
|
||||
'Note: This requires that Alter HTML is configured to "Replace image URLs". ' .
|
||||
'For more information, <a target="_blank" href="https://wordpress.org/support/topic/background-images-not-working-as-webp-elementor/#post-15060686">' .
|
||||
'head over here</a>' .
|
||||
'</p>',
|
||||
'0.23.0/elementor',
|
||||
'Got it!'
|
||||
);
|
||||
} else {
|
||||
DismissableMessages::dismissMessage('0.23.0/elementor');
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user