HTML.php
6.1 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
declare( strict_types=1 );
namespace WP_Rocket\Engine\Optimization\DelayJS;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Optimization\DynamicLists\DataManager;
use WP_Rocket\Engine\Optimization\RegexTrait;
class HTML {
use RegexTrait;
/**
* Plugin options instance.
*
* @var Options_Data
*/
protected $options;
/**
* DataManager instance
*
* @var DataManager
*/
private $data_manager;
/**
* Array of excluded patterns from delay JS
*
* @since 3.9
*
* @var array
*/
protected $excluded = [];
/**
* Creates an instance of HTML.
*
* @param Options_Data $options Plugin options instance.
* @param DataManager $data_manager DataManager instance.
*/
public function __construct( Options_Data $options, DataManager $data_manager ) {
$this->options = $options;
$this->data_manager = $data_manager;
}
/**
* Adjust HTML to have delay js structure.
*
* @since 3.9 Updated to use exclusions list instead of inclusions list.
* @since 3.7
*
* @param string $html Buffer html for the page.
*
* @return string
*/
public function delay_js( $html ): string {
if ( ! $this->is_allowed() ) {
return $html;
}
$this->set_exclusions();
$this->excluded = array_merge( $this->excluded, $this->options->get( 'delay_js_exclusions', [] ) );
/**
* Filters the delay JS exclusions array
*
* @since 3.9
*
* @param array $excluded Array of excluded patterns.
*/
$this->excluded = (array) apply_filters( 'rocket_delay_js_exclusions', $this->excluded );
$this->excluded = array_map(
function ( $value ) {
if ( ! is_string( $value ) ) {
$value = (string) $value;
}
return str_replace(
[ '+', '?ver', '#' ],
[ '\+', '\?ver', '\#' ],
$value
);
},
$this->excluded
);
return $this->parse( $html );
}
/**
* Checks if is allowed to Delay JS.
*
* @since 3.7
*
* @return bool
*/
public function is_allowed(): bool {
if ( rocket_bypass() ) {
return false;
}
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
return false;
}
if ( is_rocket_post_excluded_option( 'delay_js' ) ) {
return false;
}
return (bool) $this->options->get( 'delay_js', 0 );
}
/**
* Gets Javascript to redirect IE visitors to the uncached page
*
* @since 3.9
*
* @return string
*/
public function get_ie_fallback(): string {
return 'if(navigator.userAgent.match(/MSIE|Internet Explorer/i)||navigator.userAgent.match(/Trident\/7\..*?rv:11/i)){var href=document.location.href;if(!href.match(/[?&]nowprocket/)){if(href.indexOf("?")==-1){if(href.indexOf("#")==-1){document.location.href=href+"?nowprocket=1"}else{document.location.href=href.replace("#","?nowprocket=1#")}}else{if(href.indexOf("#")==-1){document.location.href=href+"&nowprocket=1"}else{document.location.href=href.replace("#","&nowprocket=1#")}}}}';
}
/**
* Parse the html and add/remove attributes from specific scripts.
*
* @since 3.7
*
* @param string $html Buffer html for the page.
*
* @return string
*/
private function parse( $html ): string {
if ( empty( $html ) ) {
return $html;
}
$result = $this->replace_xmp_tags( $html );
$replaced_html = preg_replace_callback(
'/<\s*script\s*(?<attr>[^>]*?)?>(?<content>.*?)?<\s*\/\s*script\s*>/ims',
[
$this,
'replace_scripts',
],
$result
);
if ( empty( $replaced_html ) ) {
return $html;
}
return $this->restore_xmp_tags( $replaced_html );
}
/**
* Callback method for preg_replace_callback that is used to adjust attributes for scripts.
*
* @since 3.9 Use exclusions list & fake type attribute.
* @since 3.7
*
* @param array $matches Matches array for scripts regex.
*
* @return string
*/
public function replace_scripts( $matches ): string {
foreach ( $this->excluded as $pattern ) {
if ( preg_match( "#{$pattern}#i", $matches[0] ) ) {
return $matches[0];
}
}
$matches['attr'] = trim( $matches['attr'] );
$delay_js = $matches[0];
if ( ! empty( $matches['attr'] ) ) {
if (
strpos( $matches['attr'], 'type=' ) !== false
&&
! preg_match( '/type\s*=\s*["\'](?:text|application)\/(?:(?:x\-)?javascript|ecmascript|jscript)["\']|type\s*=\s*["\'](?:module)[ "\']/i', $matches['attr'] )
) {
return $matches[0];
}
$delay_attr = preg_replace( '/type=(["\'])(.*?)\1/i', 'data-rocket-$0', $matches['attr'], 1 );
if ( null !== $delay_attr ) {
$delay_js = preg_replace( '#' . preg_quote( $matches['attr'], '#' ) . '#i', $delay_attr, $matches[0], 1 );
}
}
return preg_replace( '/<script/i', '<script type="rocketlazyloadscript"', $delay_js, 1 );
}
/**
* Move meta charset to head if not found to the top of page content.
*
* @since 3.9.4
*
* @param string $html Html content.
*
* @return string
*/
public function move_meta_charset_to_head( $html ): string {
$meta_pattern = "#<meta[^h]*(http-equiv[^=]*=[^\'\"]*[\'\" ]Content-Type[\'\"][ ]*[^>]*|)(charset[^=]*=[ ]*[\'\" ]*[^\'\"> ][^\'\">]+[^\'\"> ][\'\" ]*|charset[^=]*=*[^\'\"> ][^\'\">]+[^\'\"> ])([^>]*|)>(?=.*</head>)#Usmi";
if ( ! preg_match( $meta_pattern, $html, $matches ) ) {
return $html;
}
$replaced_html = preg_replace( "$meta_pattern", '', $html );
if ( empty( $replaced_html ) ) {
return $html;
}
if ( preg_match( '/<head\b/i', $replaced_html ) ) {
$replaced_html = preg_replace( '/(<head\b[^>]*?>)/i', "\${1}${matches[0]}", $replaced_html, 1 );
if ( empty( $replaced_html ) ) {
return $html;
}
return $replaced_html;
}
if ( preg_match( '/<html\b/i', $replaced_html ) ) {
$replaced_html = preg_replace( '/(<html\b[^>]*?>)/i', "\${1}${matches[0]}", $replaced_html, 1 );
if ( empty( $replaced_html ) ) {
return $html;
}
return $replaced_html;
}
$replaced_html = preg_replace( '/(<\w+)/', "${matches[0]}\${1}", $replaced_html, 1 );
if ( empty( $replaced_html ) ) {
return $html;
}
return $replaced_html;
}
/**
* Get exclusions
*
* @return void
*/
private function set_exclusions() {
$lists = $this->data_manager->get_lists();
$this->excluded = isset( $lists->delay_js_exclusions ) ? $lists->delay_js_exclusions : [];
}
}