PluginProbe
Autoptimize / 3.1.12
Autoptimize v3.1.12
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeHTML.php

autoptimizeHTML.php in Autoptimize 3.1.12, at classes/autoptimizeHTML.php

151 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles minifying HTML markup.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class autoptimizeHTML extends autoptimizeBase
11 {
12 /**
13 * Whether HTML comments are kept.
14 *
15 * @var bool
16 */
17 private $keepcomments = false;
18
19 /**
20 * Whether inline CSS/ JS is minified.
21 *
22 * @var bool
23 */
24 private $minify_inline = false;
25
26 /**
27 * Whether to force xhtml compatibility.
28 *
29 * @var bool
30 */
31 private $forcexhtml = false;
32
33 /**
34 * Things to exclude from being minifed.
35 *
36 * @var array
37 */
38 private $exclude = array(
39 '<!-- ngg_resource_manager_marker -->',
40 '<!--noindex-->',
41 '<!--/noindex-->',
42 );
43
44 public function read( $options )
45 {
46 // Remove the HTML comments?
47 $this->keepcomments = (bool) $options['keepcomments'];
48
49 // Filter to force xhtml.
50 $this->forcexhtml = (bool) apply_filters( 'autoptimize_filter_html_forcexhtml', false );
51
52 // minify inline JS/ CSS.
53 $this->minify_inline = (bool) apply_filters( 'autoptimize_html_minify_inline_js_css', $options['minify_inline'] );
54
55 // Filterable strings to be excluded from HTML minification.
56 $exclude = apply_filters( 'autoptimize_filter_html_exclude', '' );
57 if ( '' !== $exclude ) {
58 $exclude_arr = array_filter( array_map( 'trim', explode( ',', $exclude ) ) );
59 $this->exclude = array_merge( $exclude_arr, $this->exclude );
60 }
61
62 // Nothing else for HTML!
63 return true;
64 }
65
66 /**
67 * Minifies HTML.
68 *
69 * @return bool
70 */
71 public function minify()
72 {
73 $noptimize = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
74 if ( $noptimize ) {
75 return false;
76 }
77
78 // Wrap the to-be-excluded strings in noptimize tags.
79 foreach ( $this->exclude as $str ) {
80 if ( false !== strpos( $this->content, $str ) ) {
81 $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
82 $this->content = str_replace( $str, $replacement, $this->content );
83 }
84 }
85
86 // Noptimize.
87 $this->content = $this->hide_noptimize( $this->content );
88
89 // Preparing options for Minify_HTML.
90 $options = array( 'keepComments' => $this->keepcomments );
91 if ( $this->forcexhtml ) {
92 $options['xhtml'] = true;
93 }
94
95 // Optionally minify inline JS & CSS.
96 if ( $this->minify_inline ) {
97 if ( false != autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && false != autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_js_optimize' ) && true !== apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ) && false === strpos( $this->content, 'text/template' ) ) {
98 $options['jsMinifier'] = 'JSMin::minify';
99 }
100 if ( false != autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && false != autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_css_optimize' ) && true !== apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ) ) {
101 $options['cssMinifier'] = 'autoptimizeCSSmin::minify';
102 }
103 }
104
105 $tmp_content = AO_Minify_HTML::minify( $this->content, $options );
106 if ( ! empty( $tmp_content ) ) {
107 $this->content = $tmp_content;
108 unset( $tmp_content );
109 }
110
111 // Restore noptimize.
112 $this->content = $this->restore_noptimize( $this->content );
113
114 // Remove the noptimize-wrapper from around the excluded strings.
115 foreach ( $this->exclude as $str ) {
116 $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
117 if ( false !== strpos( $this->content, $replacement ) ) {
118 $this->content = str_replace( $replacement, $str, $this->content );
119 }
120 }
121
122 // Revslider data attribs somehow suffer from HTML optimization, this fixes that!
123 if ( class_exists( 'RevSlider' ) && apply_filters( 'autoptimize_filter_html_dataattrib_cleanup', false ) ) {
124 $this->content = preg_replace( '#\n(data-.*$)\n#Um', ' $1 ', $this->content );
125 $this->content = preg_replace( '#<[^>]*(=\"[^"\'<>\s]*\")(\w)#', '$1 $2', $this->content );
126 }
127
128 return true;
129 }
130
131 /**
132 * Doesn't do much in case of HTML (no cache in css/js sense there)
133 *
134 * @return true
135 */
136 public function cache()
137 {
138 return true;
139 }
140
141 /**
142 * Returns the HTML markup.
143 *
144 * @return string
145 */
146 public function getcontent()
147 {
148 return $this->content;
149 }
150 }
151