PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Webp / RewriteRules / Nginx.php

Nginx.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2, at classes/Webp/RewriteRules/Nginx.php

71 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Webp\RewriteRules;
5
6 use Imagify\WriteFile\AbstractNginxDirConfFile;
7
8 /**
9 * Add and remove rewrite rules to the imagify.conf file to display WebP images on the site.
10 *
11 * @since 1.9
12 */
13 class Nginx extends AbstractNginxDirConfFile {
14
15 /**
16 * Name of the tag used as block delemiter.
17 *
18 * @var string
19 * @since 1.9
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 *
28 * @return string
29 */
30 protected function get_raw_new_contents() {
31 $extensions = $this->get_extensions_pattern() . '|avif';
32 $home_root = wp_parse_url( home_url( '/' ) );
33 $home_root = $home_root['path'];
34
35 return trim( '
36 location ~* ^(' . $home_root . '.+)\.(' . $extensions . ')$ {
37 add_header Vary Accept;
38
39 set $canavif 1;
40
41 if ($http_accept !~* "avif"){
42 set $canavif 0;
43 }
44
45 if (!-f $request_filename.avif) {
46 set $canavif 0;
47
48 }
49 if ($canavif = 1){
50 rewrite ^(.*) $1.avif;
51 break;
52 }
53
54 set $canwebp 1;
55
56 if ($http_accept !~* "webp"){
57 set $canwebp 0;
58 }
59
60 if (!-f $request_filename.webp) {
61 set $canwebp 0;
62
63 }
64 if ($canwebp = 1){
65 rewrite ^(.*) $1.webp;
66 break;
67 }
68 }' );
69 }
70 }
71