| 1 |
<?php |
| 2 |
namespace Imagify\Webp\RewriteRules; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Add and remove rewrite rules to the web.config file to display webp images on the site. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class IIS extends \Imagify\WriteFile\AbstractIISDirConfFile { |
| 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 |
* @source https://github.com/igrigorik/webp-detect/blob/master/iis.config |
| 29 |
* @author Grégory Viguier |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
protected function get_raw_new_contents() { |
| 34 |
$extensions = $this->get_extensions_pattern(); |
| 35 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 36 |
$home_root = $home_root['path']; |
| 37 |
|
| 38 |
return trim( ' |
| 39 |
<!-- @parent /configuration/system.webServer/rewrite/rules --> |
| 40 |
<rule name="' . esc_attr( static::TAG_NAME ) . ' 2"> |
| 41 |
<match url="^(' . $home_root . '.+)\.(' . $extensions . ')$" ignoreCase="true" /> |
| 42 |
<conditions logicalGrouping="MatchAll"> |
| 43 |
<add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" /> |
| 44 |
<add input="{DOCUMENT_ROOT}/{R:1}{R:2}.webp" matchType="IsFile" /> |
| 45 |
</conditions> |
| 46 |
<action type="Rewrite" url="{R:1}{R:2}.webp" logRewrittenUrl="true" /> |
| 47 |
<serverVariables> |
| 48 |
<set name="ACCEPTS_WEBP" value="true" /> |
| 49 |
</serverVariables> |
| 50 |
</rule> |
| 51 |
|
| 52 |
<!-- @parent /configuration/system.webServer/rewrite/outboundRules --> |
| 53 |
<rule preCondition="IsWebp" name="' . esc_attr( static::TAG_NAME ) . ' 3"> |
| 54 |
<match serverVariable="RESPONSE_Vary" pattern=".*" /> |
| 55 |
<action type="Rewrite" value="Accept"/> |
| 56 |
</rule> |
| 57 |
<preConditions name="' . esc_attr( static::TAG_NAME ) . ' 4"> |
| 58 |
<preCondition name="IsWebp"> |
| 59 |
<add input="{ACCEPTS_WEBP}" pattern="true" ignoreCase="false" /> |
| 60 |
</preCondition> |
| 61 |
</preConditions>' ); |
| 62 |
} |
| 63 |
} |
| 64 |
|