| @@ -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 Nginx extends Rewrite_Rules_Abstract { | |
| 6 | +class Quickwebp_Nginx extends Quickwebp_Rewrite_Rules_Abstract { | |
| 6 | 7 | |
| 7 | 8 | /** |
| 8 | 9 | * Get the path to the file. |
| 9 | 10 | */ |
| @@ -22,28 +23,39 @@ | ||
| 22 | 23 | * |
| 23 | 24 | * @return string |
| 24 | 25 | */ |
| 25 | 26 | protected function get_raw_new_contents() { |
| 26 | - $extensions = 'jpg|jpeg|jpe|png'; | |
| 27 | - $home_root = wp_parse_url( home_url( '/' ) ); | |
| 28 | - $home_root = $home_root['path']; | |
| 27 | + $home_root = wp_parse_url( home_url( '/' ) ); | |
| 28 | + $home_root = $home_root['path']; | |
| 29 | 29 | |
| 30 | - return trim( ' | |
| 31 | -# BEGIN ' . $this->tag_name . ' | |
| 32 | -location ~* ^(' . $home_root . '.+)\.(' . $extensions . ')$ { | |
| 33 | - add_header Vary Accept; | |
| 30 | + $content = "# BEGIN " . $this->tag_name . "\n"; | |
| 31 | + $content .= "location ~* ^($home_root.+)\\.(jpg|jpeg|jpe|png)$ {"; | |
| 32 | + $content .= "\n\tadd_header Vary Accept;"; | |
| 33 | + | |
| 34 | + // Check for AVIF support and file existence | |
| 35 | + $content .= "\n\n\tif (\$http_accept ~* \"avif\") {"; | |
| 36 | + $content .= "\n\t\tset \$imavif A;"; | |
| 37 | + $content .= "\n\t}"; | |
| 38 | + $content .= "\n\tif (-f \$request_filename.avif) {"; | |
| 39 | + $content .= "\n\t\tset \$imavif \"\${imavif}B\";"; | |
| 40 | + $content .= "\n\t}"; | |
| 41 | + $content .= "\n\tif (\$imavif = AB) {"; | |
| 42 | + $content .= "\n\t\trewrite ^(.*) \$1.avif break;"; | |
| 43 | + $content .= "\n\t}"; | |
| 44 | + | |
| 45 | + // Check for WebP support and file existence | |
| 46 | + $content .= "\n\n\tif (\$http_accept ~* \"webp\") {"; | |
| 47 | + $content .= "\n\t\tset \$imwebp A;"; | |
| 48 | + $content .= "\n\t}"; | |
| 49 | + $content .= "\n\tif (-f \$request_filename.webp) {"; | |
| 50 | + $content .= "\n\t\tset \$imwebp \"\${imwebp}B\";"; | |
| 51 | + $content .= "\n\t}"; | |
| 52 | + $content .= "\n\tif (\$imwebp = AB) {"; | |
| 53 | + $content .= "\n\t\trewrite ^(.*) \$1.webp break;"; | |
| 54 | + $content .= "\n\t}"; | |
| 55 | + | |
| 56 | + $content .= "\n}"; | |
| 57 | + $content .= "\n# END " . $this->tag_name; | |
| 34 | 58 | |
| 35 | - if ($http_accept ~* "webp"){ | |
| 36 | - set $imwebp A; | |
| 59 | + return trim( $content ); | |
| 37 | 60 | } |
| 38 | - if (-f $request_filename.webp) { | |
| 39 | - set $imwebp "${imwebp}B"; | |
| 40 | - } | |
| 41 | - if ($imwebp = AB) { | |
| 42 | - rewrite ^(.*) $1.webp; | |
| 43 | - } | |
| 44 | 61 | } |
| 45 | -# END ' . $this->tag_name . ''); | |
| 46 | - } | |
| 47 | - | |
| 48 | - | |
| 49 | -} | |