| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-rewrite-rules-abstract.php'; |
| 5 |
|
| 6 |
class Quickwebp_Apache extends Quickwebp_Rewrite_Rules_Abstract { |
| 7 |
|
| 8 |
/** |
| 9 |
* Get the path to the file. |
| 10 |
*/ |
| 11 |
protected function get_file_path() { |
| 12 |
$file_path = $this->get_site_root() . '.htaccess'; |
| 13 |
|
| 14 |
return $file_path; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get unfiltered new contents to write into the file. |
| 19 |
*/ |
| 20 |
protected function get_raw_new_contents() { |
| 21 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 22 |
$home_root = $home_root['path']; |
| 23 |
|
| 24 |
$content = '# BEGIN ' . $this->tag_name . "\n"; |
| 25 |
$content .= "<IfModule mod_setenvif.c>"; |
| 26 |
$content .= "\n\tSetEnvIf Request_URI \"\\.(jpg|jpeg|jpe|png)$\" REQUEST_image"; |
| 27 |
$content .= "\n</IfModule>"; |
| 28 |
|
| 29 |
$content .= "\n<IfModule mod_rewrite.c>"; |
| 30 |
$content .= "\n\tRewriteEngine On"; |
| 31 |
$content .= "\n\tRewriteBase $home_root"; |
| 32 |
|
| 33 |
// Serve AVIF if browser supports it and file exists |
| 34 |
$content .= "\n\tRewriteCond %{HTTP_ACCEPT} image/avif"; |
| 35 |
$content .= "\n\tRewriteCond %{REQUEST_FILENAME}.avif -f"; |
| 36 |
$content .= "\n\tRewriteRule (.+)\\.(jpg|jpeg|jpe|png)$ $1.$2.avif [T=image/avif,NC,E=REQUEST_image:avif,L]"; |
| 37 |
|
| 38 |
// Otherwise, serve WebP if browser supports it and file exists |
| 39 |
$content .= "\n\tRewriteCond %{HTTP_ACCEPT} image/webp"; |
| 40 |
$content .= "\n\tRewriteCond %{REQUEST_FILENAME}.webp -f"; |
| 41 |
$content .= "\n\tRewriteRule (.+)\\.(jpg|jpeg|jpe|png)$ $1.$2.webp [T=image/webp,NC,E=REQUEST_image:webp,L]"; |
| 42 |
|
| 43 |
$content .= "\n</IfModule>"; |
| 44 |
|
| 45 |
$content .= "\n<IfModule mod_headers.c>"; |
| 46 |
$content .= "\n\tHeader append Vary Accept env=REQUEST_image"; |
| 47 |
$content .= "\n</IfModule>"; |
| 48 |
|
| 49 |
$content .= "\n<IfModule mod_mime.c>"; |
| 50 |
$content .= "\n\tAddType image/webp .webp"; |
| 51 |
$content .= "\n\tAddType image/avif .avif"; |
| 52 |
$content .= "\n</IfModule>"; |
| 53 |
$content .= "\n# END " . $this->tag_name; |
| 54 |
|
| 55 |
return trim( $content ); |
| 56 |
} |
| 57 |
} |
| 58 |
|