PluginProbe
Autoptimize / 3.1.4
Autoptimize v3.1.4
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
← All changes | classes/autoptimizeHTML.php +124 -66 2.3.13.1.4 View file →
@@ -1,92 +1,150 @@
1 1 <?php
2 -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2 +/**
3 + * Handles minifying HTML markup.
4 + */
3 5
4 -class autoptimizeHTML extends autoptimizeBase {
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 + */
5 17 private $keepcomments = false;
6 - private $exclude = array('<!-- ngg_resource_manager_marker -->');
7 -
8 - public function read($options) {
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 + {
9 46 // Remove the HTML comments?
10 47 $this->keepcomments = (bool) $options['keepcomments'];
11 -
12 - // filter to force xhtml
48 +
49 + // Filter to force xhtml.
13 50 $this->forcexhtml = (bool) apply_filters( 'autoptimize_filter_html_forcexhtml', false );
14 -
15 - // filter to add strings to be excluded from HTML minification
16 - $excludeHTML = apply_filters( 'autoptimize_filter_html_exclude','' );
17 - if ($excludeHTML!=="") {
18 - $exclHTMLArr = array_filter(array_map('trim',explode(",",$excludeHTML)));
19 - $this->exclude = array_merge($exclHTMLArr,$this->exclude);
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 );
20 60 }
21 -
22 - // Nothing else for HTML
61 +
62 + // Nothing else for HTML!
23 63 return true;
24 64 }
25 -
26 - //Joins and optimizes CSS
27 - public function minify() {
28 - $noptimizeHTML = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
29 - if ($noptimizeHTML)
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 ) {
30 75 return false;
31 -
32 - if(class_exists('Minify_HTML')) {
33 - // wrap the to-be-excluded strings in noptimize tags
34 - foreach ($this->exclude as $exclString) {
35 - if (strpos($this->content,$exclString)!==false) {
36 - $replString="<!--noptimize-->".$exclString."<!--/noptimize-->";
37 - $this->content=str_replace($exclString,$replString,$this->content);
38 - }
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 );
39 83 }
84 + }
40 85
41 - // noptimize me
42 - $this->content = $this->hide_noptimize($this->content);
86 + // Noptimize.
87 + $this->content = $this->hide_noptimize( $this->content );
43 88
44 - // Minify html
45 - $options = array('keepComments' => $this->keepcomments);
46 - if ($this->forcexhtml) {
47 - $options['xhtml'] = true;
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';
48 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 + }
49 104
50 - if (@is_callable(array("Minify_HTML","minify"))) {
51 - $tmp_content = Minify_HTML::minify($this->content,$options);
52 - if (!empty($tmp_content)) {
53 - $this->content = $tmp_content;
54 - unset($tmp_content);
55 - }
56 - }
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 + }
57 110
58 - // restore noptimize
59 - $this->content = $this->restore_noptimize($this->content);
60 -
61 - // remove the noptimize-wrapper from around the excluded strings
62 - foreach ($this->exclude as $exclString) {
63 - $replString="<!--noptimize-->".$exclString."<!--/noptimize-->";
64 - if (strpos($this->content,$replString)!==false) {
65 - $this->content=str_replace($replString,$exclString,$this->content);
66 - }
67 - }
111 + // Restore noptimize.
112 + $this->content = $this->restore_noptimize( $this->content );
68 113
69 - // revslider data attribs somehow suffer from HTML optimization, this fixes that
70 - if ( class_exists('RevSlider') && apply_filters('autoptimize_filter_html_dataattrib_cleanup', false) ) {
71 - $this->content = preg_replace('#\n(data-.*$)\n#Um',' $1 ', $this->content);
72 - $this->content = preg_replace('#<[^>]*(=\"[^"\'<>\s]*\")(\w)#','$1 $2', $this->content);
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 );
73 119 }
120 + }
74 121
75 - return true;
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 );
76 126 }
77 -
78 - // Didn't minify :(
79 - return false;
127 +
128 + return true;
80 129 }
81 -
82 - // Does nothing
83 - public function cache() {
84 - //No cache for HTML
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 + {
85 138 return true;
86 139 }
87 -
88 - //Returns the content
89 - public function getcontent() {
140 +
141 + /**
142 + * Returns the HTML markup.
143 + *
144 + * @return string
145 + */
146 + public function getcontent()
147 + {
90 148 return $this->content;
91 149 }
92 150 }