| 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 |
|
| 7 |
class Quickwebp_IIS extends Quickwebp_Rewrite_Rules_Abstract { |
| 8 |
|
| 9 |
/** |
| 10 |
* Get the path to the file. |
| 11 |
*/ |
| 12 |
protected function get_file_path() { |
| 13 |
$file_path = $this->get_site_root() . 'web.config'; |
| 14 |
|
| 15 |
return $file_path; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get unfiltered new contents to write into the file. |
| 20 |
*/ |
| 21 |
protected function get_raw_new_contents() { |
| 22 |
$extensions = 'jpg|jpeg|jpe|png'; |
| 23 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 24 |
$home_root = $home_root['path']; |
| 25 |
|
| 26 |
$content = '# BEGIN ' . $this->tag_name . "\n"; |
| 27 |
$content .= '<!-- @parent /configuration/system.webServer/rewrite/rules -->'; |
| 28 |
|
| 29 |
// Serve AVIF first |
| 30 |
$content .= "\n<rule name=\"" . esc_attr( $this->tag_name ) . " AVIF\">"; |
| 31 |
$content .= "\n\t<match url=\"^(" . $home_root . ".+)\\.(" . $extensions . ")$\" ignoreCase=\"true\" />"; |
| 32 |
$content .= "\n\t<conditions logicalGrouping=\"MatchAll\">"; |
| 33 |
$content .= "\n\t\t<add input=\"{HTTP_ACCEPT}\" pattern=\"image/avif\" ignoreCase=\"false\" />"; |
| 34 |
$content .= "\n\t\t<add input=\"{DOCUMENT_ROOT}/{R:1}{R:2}.avif\" matchType=\"IsFile\" />"; |
| 35 |
$content .= "\n\t</conditions>"; |
| 36 |
$content .= "\n\t<action type=\"Rewrite\" url=\"{R:1}{R:2}.avif\" logRewrittenUrl=\"true\" />"; |
| 37 |
$content .= "\n\t<serverVariables>"; |
| 38 |
$content .= "\n\t\t<set name=\"ACCEPTS_IMAGE\" value=\"true\" />"; |
| 39 |
$content .= "\n\t</serverVariables>"; |
| 40 |
$content .= "\n</rule>"; |
| 41 |
|
| 42 |
// Serve WebP if AVIF isn't available |
| 43 |
$content .= "\n\n<rule name=\"" . esc_attr( $this->tag_name ) . " WebP\">"; |
| 44 |
$content .= "\n\t<match url=\"^(" . $home_root . ".+)\\.(" . $extensions . ")$\" ignoreCase=\"true\" />"; |
| 45 |
$content .= "\n\t<conditions logicalGrouping=\"MatchAll\">"; |
| 46 |
$content .= "\n\t\t<add input=\"{HTTP_ACCEPT}\" pattern=\"image/webp\" ignoreCase=\"false\" />"; |
| 47 |
$content .= "\n\t\t<add input=\"{DOCUMENT_ROOT}/{R:1}{R:2}.webp\" matchType=\"IsFile\" />"; |
| 48 |
$content .= "\n\t</conditions>"; |
| 49 |
$content .= "\n\t<action type=\"Rewrite\" url=\"{R:1}{R:2}.webp\" logRewrittenUrl=\"true\" />"; |
| 50 |
$content .= "\n\t<serverVariables>"; |
| 51 |
$content .= "\n\t\t<set name=\"ACCEPTS_IMAGE\" value=\"true\" />"; |
| 52 |
$content .= "\n\t</serverVariables>"; |
| 53 |
$content .= "\n</rule>"; |
| 54 |
|
| 55 |
$content .= "\n\n<!-- @parent /configuration/system.webServer/rewrite/outboundRules -->"; |
| 56 |
$content .= "\n<rule preCondition=\"IsModernImage\" name=\"" . esc_attr( $this->tag_name ) . " Vary\">"; |
| 57 |
$content .= "\n\t<match serverVariable=\"RESPONSE_Vary\" pattern=\".*\" />"; |
| 58 |
$content .= "\n\t<action type=\"Rewrite\" value=\"Accept\" />"; |
| 59 |
$content .= "\n</rule>"; |
| 60 |
|
| 61 |
$content .= "\n<preConditions name=\"" . esc_attr( $this->tag_name ) . " Preconditions\">"; |
| 62 |
$content .= "\n\t<preCondition name=\"IsModernImage\">"; |
| 63 |
$content .= "\n\t\t<add input=\"{ACCEPTS_IMAGE}\" pattern=\"true\" ignoreCase=\"false\" />"; |
| 64 |
$content .= "\n\t</preCondition>"; |
| 65 |
$content .= "\n</preConditions>"; |
| 66 |
|
| 67 |
$content .= "\n\n<!-- @parent /configuration/system.webServer -->"; |
| 68 |
$content .= "\n<staticContent name=\"" . esc_attr( $this->tag_name ) . " Mime\">"; |
| 69 |
$content .= "\n\t<mimeMap fileExtension=\".webp\" mimeType=\"image/webp\" />"; |
| 70 |
$content .= "\n\t<mimeMap fileExtension=\".avif\" mimeType=\"image/avif\" />"; |
| 71 |
$content .= "\n</staticContent>"; |
| 72 |
|
| 73 |
$content .= "\n# END " . $this->tag_name; |
| 74 |
|
| 75 |
return trim( $content ); |
| 76 |
} |
| 77 |
} |
| 78 |
|