/
minify-html.php
354 lines (304 loc) · 10.7 KB
/
minify-html.php
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php //HTML縮小化用
/**
* Cocoon WordPress Theme
* @author: yhira
* @link: https://wp-cocoon.com/
* @license: http://www.gnu.org/licenses/gpl-2.0.html GPL v2 or later
*/
if ( !defined( 'ABSPATH' ) ) exit;
//HTMLソースコードの縮小化
if ( !function_exists( 'code_minify_call_back' ) ):
function code_minify_call_back($html) {
global $post;
//_v($post);
if (is_admin() || is_feed() || !$post) {
return $html;
}
//何故かa開始タグがpタグでラップされるのを修正
$html = preg_replace('{<p>(<a[^>]+?>)</p>}i', "$1", $html);
//何故かa終了タグがpタグでラップされるのを修正
$html = preg_replace('{<p>(</a>)</p>}i', "$1", $html);
//HTMLの縮小化
if (is_html_minify_enable()) {
$html = minify_html($html);
}
///////////////////////////////////////////
// Lazy Load
///////////////////////////////////////////
//WordPress5.5で追加されたLazy Loadが有効でないときはCocoonのLazy Loadを利用する
if (!is_wp_lazy_load_valid()) {
$html = convert_all_lazy_load_tag($html);
}
//数式表示ショートコードの除外
if (is_formula_enable() && is_math_shortcode_exist()) {
$esc_shortcode = preg_quote(MATH_SHORTCODE, '#');
$html = preg_replace('#<p[^>]*?>'.$esc_shortcode.'</p>|'.$esc_shortcode.'#', '', $html);
}
//「Warning: Attribute aria-required is unnecessary for elements that have attribute required.」対策
$html = str_replace('aria-required="true" required>', 'aria-required="true">', $html);
$html = str_replace('aria-required="true" required="required">', 'aria-required="true">', $html);
///////////////////////////////////////
// HTML5エラー除外
///////////////////////////////////////
//Alt属性がないIMGタグにalt=""を追加する
$html = preg_replace('/<img((?![^>]*alt=)[^>]*)>/i', '<img alt${1}>', $html);
//wpForoのHTML5エラー
if (is_wpforo_exist()) {
$html = str_replace(' id="wpf-widget-recent-replies"', '', $html);
}
//BuddyPressのHTML5エラー
if (is_buddypress_exist()) {
$html = str_replace('<label for="bp-login-widget-rememberme">', '<label>', $html);
}
//JavaScriptの縮小化
if (is_amp() && is_js_minify_enable()) {
$pattern = '{<script[^>]*?>(.*?)</script>}is';
$subject = $html;
$res = preg_match_all($pattern, $subject, $m);
if ($res && isset($m[1])) {
foreach ($m[1] as $match) {
if (empty($match)) {
continue;
}
$html = str_replace($match, minify_js($match), $html);
}
}
}
//タブボックスのタブ変換
$html = preg_replace('#<div class="(.*?)blank-box bb-tab bb-(.+?)".*?>#', '$0<div class="bb-label"><span class="fa"></span></div>', $html);
//Font Awesome5変換
$html = change_fa($html);
return apply_filters('code_minify_call_back', $html);
}
endif;
//縮小化して良いページかどうか
if ( !function_exists( 'is_minify_page' ) ):
function is_minify_page(){
if (is_admin()) return false;
if (includes_wp_admin_in_request_uri()) return false;
if (includes_wp_cron_php_in_request_uri()) return false;
if (includes_service_worker_js_in_http_referer()) return false;
if (is_server_request_post()) return false;
if (is_server_request_uri_backup_download_php()) return false;
if (is_robots_txt_page()) return false;
if (is_analytics_access_php_page()) return false;
if (is_feed()) return false;
return true;
}
endif;
//最終HTML取得開始
add_action('get_header', 'code_minify_buffer_start', 99999999);
add_action('get_template_part_tmp/amp-header', 'code_minify_buffer_start', 99999999);//AMP
if ( !function_exists( 'code_minify_buffer_start' ) ):
function code_minify_buffer_start() {
ob_start('code_minify_call_back');
}
endif;
//最終HTML取得終了
add_action('shutdown', 'code_minify_buffer_end');
if ( !function_exists( 'code_minify_buffer_end' ) ):
function code_minify_buffer_end() {
if (ob_get_length()){
ob_end_flush();
}
}
endif;
///////////////////////////////////////
// 出力フィルタリングフック
///////////////////////////////////////
// wp_head 出力フィルタリング・ハンドラ追加
add_action( 'wp_head', 'wp_head_buffer_start', 1 );
add_action( 'wp_head', 'wp_head_buffer_end', 99999999 );
// wp_footer 出力フィルタリング・ハンドラ追加
add_action( 'wp_footer', 'wp_footer_buffer_start', 1 );
add_action( 'wp_footer', 'wp_footer_buffer_end', 99999999 );
///////////////////////////////////////
// バッファリング開始
///////////////////////////////////////
if ( !function_exists( 'wp_head_buffer_start' ) ):
function wp_head_buffer_start() {
ob_start( 'wp_head_minify' );
}
endif;
if ( !function_exists( 'wp_footer_buffer_start' ) ):
function wp_footer_buffer_start() {
ob_start( 'wp_footer_minify' );
}
endif;
///////////////////////////////////////
// バッファリング終了
///////////////////////////////////////
if ( !function_exists( 'wp_head_buffer_end' ) ):
function wp_head_buffer_end() {
if (ob_get_length()) ob_end_flush();
}
endif;
if ( !function_exists( 'wp_footer_buffer_end' ) ):
function wp_footer_buffer_end() {
if (ob_get_length()) ob_end_flush();
}
endif;
///////////////////////////////////////
// フィルター
///////////////////////////////////////
if ( !function_exists( 'wp_head_minify' ) ):
function wp_head_minify($html) {
if (is_admin()) {
return $html;
}
//ヘッダーコードのCSS縮小化
if (is_css_minify_enable()) {
$html = tag_code_to_minify_css($html);
}
//ヘッダーコードのJS縮小化
if (is_js_minify_enable()) {
$html = tag_code_to_minify_js($html);
}
//WordPressが出力する type='text/javascript'を削除
$html = str_replace(" type='text/javascript'", '', $html);
$html = str_replace(' type="text/javascript"', '', $html);
//WordPressが出力する type='text/css'を削除
$html = str_replace(" type='text/css'", '', $html);
$html = str_replace(' type="text/css"', '', $html);
//_v($html);
return apply_filters('wp_head_minify', $html);
}
endif;
if ( !function_exists( 'wp_footer_minify' ) ):
function wp_footer_minify($html) {
if (is_admin()) {
return $html;
}
//フッターコードのCSS縮小化
if (is_css_minify_enable()) {
$html = tag_code_to_minify_css($html);
}
//フッターコードのJS縮小化
if (is_js_minify_enable()) {
$html = tag_code_to_minify_js($html);
}
//WordPressが出力する type='text/javascript'を削除
$html = str_replace(" type='text/javascript'", '', $html);
$html = str_replace(' type="text/javascript"', '', $html);
return apply_filters('wp_footer_minify', $html);
}
endif;
//リストにマッチするか
if ( !function_exists( 'has_match_list_text' ) ):
function has_match_list_text($text, $list){
$excludes = list_text_to_array($list);
foreach ($excludes as $exclude_str) {
if (strpos($text, $exclude_str) !== false) {
return true;
}
}
}
endif;
//imgタグをLazy Load用の画像タグに変換
if ( !function_exists( 'convert_lazy_load_tag' ) ):
function convert_lazy_load_tag($the_content, $media){
//AMP・アクセス解析ページでは実行しない
if (is_amp() || is_analytics_access_php_page() || is_feed() || is_admin()) {
return $the_content;
}
$is_img = ($media == 'img');
$is_iframe = !$is_img;
if ($is_iframe) {
//YouTube高速化がある場合はiframeは処理しない
if (includes_string($the_content, ' data-iframe=')) {
return $the_content;
}
}
$pattern = '{<'.$media.'.+?>}is';
if ($is_iframe) {
$pattern = '{<iframe.+?>.*?</iframe>}is';
}
//imgタグをamp-imgタグに変更する
$res = preg_match_all($pattern, $the_content, $m);
if ($res) {//画像タグがある場合
//_v($m);
//置換するタグ格納用
$img_tags = array();
foreach ($m[0] as $match) {
//文字列が1024バイト以上の場合はスキップ
if (strlen($match) > 1024) {
continue;
}
//URLが含まれていない場合はスキップ
if (!preg_match(URL_REG, $match)) {
continue;
}
//重複置換を避ける
if (in_array($match, $img_tags, true)) {
continue;
}
//タグマネージャ用のものは避ける
if (includes_string($match, '<iframe src="https://www.googletagmanager.com/ns.html?id=')) {
continue;
}
//置換するタグを格納してく
$img_tags[] = $match;
///////////////////////////////////////////
// 除外設定
///////////////////////////////////////////
if (
//サイトロゴ
includes_string($match, 'header-site-logo-image')
// //アイキャッチ
// || includes_string($match, ' eye-catch-image ')
//Jetpackの統計グラフ
|| (is_user_logged_in() && includes_string($match, 'admin-bar-hours-scale'))
) {
continue;
}
//除外リストにマッチする文字列はLazy Loadしない
$exclude_list = get_lazy_load_exclude_list();
if ($exclude_list && has_match_list_text($match, $exclude_list)) {
continue;
}
//変数の初期化
$src_attr = null;
$url = null;
$tag = $match;
//Lazy Load:画像URLの入れ替え
$search = '{ src=["\'](.+?)["\']}i';
//$replace = ' src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src="$1"';
$replace = ' data-src="$1"';
$tag = preg_replace($search, $replace, $tag);
//$tag = convert_src_to_data_src($tag);
//クラスの変更
//挿入するクラス
$classes = 'lozad lozad-'.$media;
$chrome_lazy = 'loading="lazy"';
if (preg_match('/class=/i', $tag)) {
$search = '{class=["\'](.+?)["\']}i';
$replace = 'class="$1 '.$classes.'" '.$chrome_lazy;
$tag = preg_replace($search, $replace, $tag);
} else {
$search = '<'.$media;
$replace = '<'.$media.' class="'.$classes.'" '.$chrome_lazy;
$tag = str_replace($search, $replace, $tag);
}
//srcsetの変換宇
$tag = str_replace(' srcset=', ' data-srcset=', $tag);
//noscriptタグの追加
$tag = $tag.'<noscript>'.$match.'</noscript>';
//imgタグをLazy Load対応に置換
$the_content = preg_replace('{'.preg_quote($match).'(?!<noscript>)}', $tag , $the_content);
}
}
return apply_filters('convert_lazy_load_tag', $the_content, $media);
}
endif;
//全てのLazy Load置換処理の実行
if ( !function_exists( 'convert_all_lazy_load_tag' ) ):
function convert_all_lazy_load_tag($html){
if (is_lazy_load_enable() && !is_login_page()) {
//画像の変換
$html = convert_lazy_load_tag($html, 'img');
//iframeの変換
$html = convert_lazy_load_tag($html, 'iframe');
}
return $html;
}
endif;