| 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_Nginx 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() . 'conf/quickwebp.conf'; |
| 13 |
|
| 14 |
return $file_path; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get unfiltered new contents to write into the file. |
| 19 |
* |
| 20 |
* @since 1.9 |
| 21 |
* @access protected |
| 22 |
* @author Grégory Viguier |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
protected function get_raw_new_contents() { |
| 27 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 28 |
$home_root = $home_root['path']; |
| 29 |
|
| 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; |
| 58 |
|
| 59 |
return trim( $content ); |
| 60 |
} |
| 61 |
} |
| 62 |
|