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
|
<?php /** * Handles minifying HTML markup. */
if ( ! defined( 'ABSPATH' ) ) { exit; }
class autoptimizeHTML extends autoptimizeBase { /** * Whether HTML comments are kept. * * @var bool */ private $keepcomments = false;
/** * Whether to force xhtml compatibility. * * @var bool */ private $forcexhtml = false;
/** * Things to exclude from being minifed. * * @var array */ private $exclude = array( '<!-- ngg_resource_manager_marker -->', '<!--noindex-->', '<!--/noindex-->', );
public function read( $options ) { // Remove the HTML comments? $this->keepcomments = (bool) $options['keepcomments'];
// Filter to force xhtml. $this->forcexhtml = (bool) apply_filters( 'autoptimize_filter_html_forcexhtml', false );
// Filterable strings to be excluded from HTML minification. $exclude = apply_filters( 'autoptimize_filter_html_exclude', '' ); if ( '' !== $exclude ) { $exclude_arr = array_filter( array_map( 'trim', explode( ',', $exclude ) ) ); $this->exclude = array_merge( $exclude_arr, $this->exclude ); }
// Nothing else for HTML! return true; }
/** * Minifies HTML. * * @return bool */ public function minify() { $noptimize = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content ); if ( $noptimize ) { return false; }
// Wrap the to-be-excluded strings in noptimize tags. foreach ( $this->exclude as $str ) { if ( false !== strpos( $this->content, $str ) ) { $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->'; $this->content = str_replace( $str, $replacement, $this->content ); } }
// Noptimize. $this->content = $this->hide_noptimize( $this->content );
// Preparing options for Minify_HTML. $options = array( 'keepComments' => $this->keepcomments ); if ( $this->forcexhtml ) { $options['xhtml'] = true; }
$tmp_content = Minify_HTML::minify( $this->content, $options ); if ( ! empty( $tmp_content ) ) { $this->content = $tmp_content; unset( $tmp_content ); }
// Restore noptimize. $this->content = $this->restore_noptimize( $this->content );
// Remove the noptimize-wrapper from around the excluded strings. foreach ( $this->exclude as $str ) { $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->'; if ( false !== strpos( $this->content, $replacement ) ) { $this->content = str_replace( $replacement, $str, $this->content ); } }
// Revslider data attribs somehow suffer from HTML optimization, this fixes that! if ( class_exists( 'RevSlider' ) && apply_filters( 'autoptimize_filter_html_dataattrib_cleanup', false ) ) { $this->content = preg_replace( '#\n(data-.*$)\n#Um', ' $1 ', $this->content ); $this->content = preg_replace( '#<[^>]*(=\"[^"\'<>\s]*\")(\w)#', '$1 $2', $this->content ); }
return true; }
/** * Doesn't do much in case of HTML (no cache in css/js sense there) * * @return true */ public function cache() { return true; }
/** * Returns the HTML markup. * * @return string */ public function getcontent() { return $this->content; } }
|