| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 2 | 3 | |
| 3 | 4 | include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-rewrite-rules-abstract.php'; |
| 4 | 5 | |
| 5 | -class Apache extends Rewrite_Rules_Abstract { | |
| 6 | +class Quickwebp_Apache extends Quickwebp_Rewrite_Rules_Abstract { | |
| 6 | 7 | |
| 7 | 8 | /** |
| 8 | 9 | * Get the path to the file. |
| 9 | 10 | */ |
| @@ -16,40 +17,41 @@ | ||
| 16 | 17 | /** |
| 17 | 18 | * Get unfiltered new contents to write into the file. |
| 18 | 19 | */ |
| 19 | 20 | protected function get_raw_new_contents() { |
| 20 | - $extensions = 'jpg|jpeg|jpe|png'; | |
| 21 | - $home_root = wp_parse_url( home_url( '/' ) ); | |
| 22 | - $home_root = $home_root['path']; | |
| 21 | + $home_root = wp_parse_url( home_url( '/' ) ); | |
| 22 | + $home_root = $home_root['path']; | |
| 23 | 23 | |
| 24 | - return trim( ' | |
| 25 | -# BEGIN ' . $this->tag_name . ' | |
| 26 | -<IfModule mod_setenvif.c> | |
| 27 | - # Vary: Accept for all the requests to jpeg and png. | |
| 28 | - SetEnvIf Request_URI "\.(' . $extensions . ')$" REQUEST_image | |
| 29 | -</IfModule> | |
| 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>"; | |
| 30 | 28 | |
| 31 | -<IfModule mod_rewrite.c> | |
| 32 | - RewriteEngine On | |
| 33 | - RewriteBase ' . $home_root . ' | |
| 29 | + $content .= "\n<IfModule mod_rewrite.c>"; | |
| 30 | + $content .= "\n\tRewriteEngine On"; | |
| 31 | + $content .= "\n\tRewriteBase $home_root"; | |
| 34 | 32 | |
| 35 | - # Check if browser supports WebP images. | |
| 36 | - RewriteCond %{HTTP_ACCEPT} image/webp | |
| 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]"; | |
| 37 | 42 | |
| 38 | - # Check if WebP replacement image exists. | |
| 39 | - RewriteCond %{REQUEST_FILENAME}.webp -f | |
| 43 | + $content .= "\n</IfModule>"; | |
| 40 | 44 | |
| 41 | - # Serve WebP image instead. | |
| 42 | - RewriteRule (.+)\.(' . $extensions . ')$ $1.$2.webp [T=image/webp,NC] | |
| 43 | -</IfModule> | |
| 45 | + $content .= "\n<IfModule mod_headers.c>"; | |
| 46 | + $content .= "\n\tHeader append Vary Accept env=REQUEST_image"; | |
| 47 | + $content .= "\n</IfModule>"; | |
| 44 | 48 | |
| 45 | -<IfModule mod_headers.c> | |
| 46 | - Header append Vary Accept env=REQUEST_image | |
| 47 | -</IfModule> | |
| 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; | |
| 48 | 54 | |
| 49 | -<IfModule mod_mime.c> | |
| 50 | - AddType image/webp .webp | |
| 51 | -</IfModule> | |
| 52 | -# END ' . $this->tag_name .'' ); | |
| 55 | + return trim( $content ); | |
| 53 | 56 | } |
| 54 | - | |
| 55 | -} | |
| 57 | +} | |