IEConditionalSubscriber.php
3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace WP_Rocket\Engine\Optimization;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Handles IE conditionals comments in the HTML to prevent their content from being processed during optimization.
*
* @since 3.6 Changes template tag to be an HTML comment.
* @since 3.1
*/
class IEConditionalSubscriber implements Subscriber_Interface {
/**
* Stores IE conditionals.
*
* @since 3.1
*
* @var array
*/
private $conditionals = [];
/**
* HTML IE conditional pattern.
*
* @since 3.6.2
*
* @var string
*/
const IE_PATTERN = '/<!--\[if[^\]]*?\]>.*?<!\[endif\]-->/is';
/**
* HTML IE conditional template tag.
*
* @since 3.6.2
*
* @var string
*/
const WP_ROCKET_CONDITIONAL = '<!--{{WP_ROCKET_CONDITIONAL}}-->';
/**
* Return an array of events that this subscriber listens to.
*
* @since 3.1
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [
[ 'extract_ie_conditionals', 1 ],
[ 'inject_ie_conditionals', 34 ],
],
];
}
/**
* Extracts IE conditionals tags and replace them with placeholders.
*
* @since 3.1
*
* @param string $html HTML content.
*
* @return string
*/
public function extract_ie_conditionals( $html ) {
preg_match_all( self::IE_PATTERN, $html, $conditionals_match );
if ( ! $conditionals_match ) {
return $html;
}
foreach ( $conditionals_match[0] as $conditional ) {
$this->conditionals[] = $conditional;
}
return preg_replace( self::IE_PATTERN, self::WP_ROCKET_CONDITIONAL, $html );
}
/**
* Replaces WP Rocket placeholders with IE conditional tags.
*
* @since 3.1
*
* @param string $html HTML content.
*
* @return string
*/
public function inject_ie_conditionals( $html ) {
if ( ! $this->has_conditional_tag( $html ) ) {
return $html;
}
foreach ( $this->conditionals as $conditional ) {
// Prevent scripts containing things like "\\s" to be striped of a backslash when put back in content.
if ( preg_match( '@^(?<opening><!--\[if[^\]]*?\]>\s*?(?:<!-->)?\s*<script(?:\s[^>]*?>))\s*(?<content>.*?)\s*(?<closing></script>\s*(?:<!--)?\s*?<!\[endif\]-->)$@is', $conditional, $matches ) ) {
$conditional = $matches['opening'] . preg_replace( '#(?<!\\\\)(\\$|\\\\)#', '\\\\$1', $matches['content'] ) . $matches['closing'];
}
$html = $this->replace_conditional_tag( $html, $conditional );
}
return $html;
}
/**
* Checks if the template tag for the IE conditional exists in the given HTML string.
*
* @since 3.6.2
*
* @param string $html HTML content.
*
* @return bool true if at least one exists; else false.
*/
private function has_conditional_tag( $html ) {
return ( false !== strpos( $html, self::WP_ROCKET_CONDITIONAL ) );
}
/**
* Replaces the template tag with the original IE conditional HTML.
*
* @since 3.6.2
*
* @param string $html HTML content.
* @param string $original Original IE conditional HTML.
*
* @return string
*/
private function replace_conditional_tag( $html, $original ) {
$template_tag_position = strpos( $html, self::WP_ROCKET_CONDITIONAL );
return substr_replace(
$html,
$original,
$template_tag_position,
strlen( self::WP_ROCKET_CONDITIONAL )
);
}
}