| 1 |
<?php |
| 2 |
namespace Imagify\Webp\RewriteRules; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Add and remove rewrite rules to the imagify.conf file to display webp images on the site. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class Nginx extends \Imagify\WriteFile\AbstractNginxDirConfFile { |
| 13 |
|
| 14 |
/** |
| 15 |
* Name of the tag used as block delemiter. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
* @since 1.9 |
| 19 |
* @author Grégory Viguier |
| 20 |
*/ |
| 21 |
const TAG_NAME = 'Imagify: rewrite rules for webp'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Get unfiltered new contents to write into the file. |
| 25 |
* |
| 26 |
* @since 1.9 |
| 27 |
* @access protected |
| 28 |
* @author Grégory Viguier |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
protected function get_raw_new_contents() { |
| 33 |
$extensions = $this->get_extensions_pattern(); |
| 34 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 35 |
$home_root = $home_root['path']; |
| 36 |
|
| 37 |
return trim( ' |
| 38 |
location ~* ^(' . $home_root . '.+)\.(' . $extensions . ')$ { |
| 39 |
add_header Vary Accept; |
| 40 |
|
| 41 |
if ($http_accept ~* "webp"){ |
| 42 |
set $imwebp A; |
| 43 |
} |
| 44 |
if (-f $request_filename.webp) { |
| 45 |
set $imwebp "${imwebp}B"; |
| 46 |
} |
| 47 |
if ($imwebp = AB) { |
| 48 |
rewrite ^(.*) $1.webp final; |
| 49 |
} |
| 50 |
}' ); |
| 51 |
} |
| 52 |
} |
| 53 |
|