PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
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 +114 -66 2.3.03.0.3 View file →
@@ -1,92 +1,140 @@
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 to force xhtml compatibility.
21 + *
22 + * @var bool
23 + */
24 + private $forcexhtml = false;
25 +
26 + /**
27 + * Things to exclude from being minifed.
28 + *
29 + * @var array
30 + */
31 + private $exclude = array(
32 + '<!-- ngg_resource_manager_marker -->',
33 + '<!--noindex-->',
34 + '<!--/noindex-->',
35 + );
36 +
37 + public function read( $options )
38 + {
9 39 // Remove the HTML comments?
10 40 $this->keepcomments = (bool) $options['keepcomments'];
11 -
12 - // filter to force xhtml
41 +
42 + // Filter to force xhtml.
13 43 $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);
44 +
45 + // Filterable strings to be excluded from HTML minification.
46 + $exclude = apply_filters( 'autoptimize_filter_html_exclude', '' );
47 + if ( '' !== $exclude ) {
48 + $exclude_arr = array_filter( array_map( 'trim', explode( ',', $exclude ) ) );
49 + $this->exclude = array_merge( $exclude_arr, $this->exclude );
20 50 }
21 -
22 - // Nothing else for HTML
51 +
52 + // Nothing else for HTML!
23 53 return true;
24 54 }
25 -
26 - //Joins and optimizes CSS
27 - public function minify() {
28 - $noptimizeHTML = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
29 - if ($noptimizeHTML)
55 +
56 + /**
57 + * Minifies HTML.
58 + *
59 + * @return bool
60 + */
61 + public function minify()
62 + {
63 + $noptimize = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
64 + if ( $noptimize ) {
30 65 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 - }
66 + }
67 +
68 + // Wrap the to-be-excluded strings in noptimize tags.
69 + foreach ( $this->exclude as $str ) {
70 + if ( false !== strpos( $this->content, $str ) ) {
71 + $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
72 + $this->content = str_replace( $str, $replacement, $this->content );
39 73 }
74 + }
40 75
41 - // noptimize me
42 - $this->content = $this->hide_noptimize($this->content);
76 + // Noptimize.
77 + $this->content = $this->hide_noptimize( $this->content );
43 78
44 - // Minify html
45 - $options = array('keepComments' => $this->keepcomments);
46 - if ($this->forcexhtml) {
47 - $options['xhtml'] = true;
79 + // Preparing options for Minify_HTML.
80 + $options = array( 'keepComments' => $this->keepcomments );
81 + if ( $this->forcexhtml ) {
82 + $options['xhtml'] = true;
83 + }
84 +
85 + // Optionally minify inline JS & CSS.
86 + if ( apply_filters( 'autoptimize_html_minify_inline_js_css', false ) ) {
87 + 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' ) ) {
88 + $options['jsMinifier'] = 'JSMin::minify';
48 89 }
90 + 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 ) ) {
91 + $options['cssMinifier'] = 'autoptimizeCSSmin::minify';
92 + }
93 + }
49 94
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 - }
95 + $tmp_content = AO_Minify_HTML::minify( $this->content, $options );
96 + if ( ! empty( $tmp_content ) ) {
97 + $this->content = $tmp_content;
98 + unset( $tmp_content );
99 + }
57 100
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 - }
101 + // Restore noptimize.
102 + $this->content = $this->restore_noptimize( $this->content );
68 103
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);
104 + // Remove the noptimize-wrapper from around the excluded strings.
105 + foreach ( $this->exclude as $str ) {
106 + $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
107 + if ( false !== strpos( $this->content, $replacement ) ) {
108 + $this->content = str_replace( $replacement, $str, $this->content );
73 109 }
110 + }
74 111
75 - return true;
112 + // Revslider data attribs somehow suffer from HTML optimization, this fixes that!
113 + if ( class_exists( 'RevSlider' ) && apply_filters( 'autoptimize_filter_html_dataattrib_cleanup', false ) ) {
114 + $this->content = preg_replace( '#\n(data-.*$)\n#Um', ' $1 ', $this->content );
115 + $this->content = preg_replace( '#<[^>]*(=\"[^"\'<>\s]*\")(\w)#', '$1 $2', $this->content );
76 116 }
77 -
78 - // Didn't minify :(
79 - return false;
117 +
118 + return true;
80 119 }
81 -
82 - // Does nothing
83 - public function cache() {
84 - //No cache for HTML
120 +
121 + /**
122 + * Doesn't do much in case of HTML (no cache in css/js sense there)
123 + *
124 + * @return true
125 + */
126 + public function cache()
127 + {
85 128 return true;
86 129 }
87 -
88 - //Returns the content
89 - public function getcontent() {
130 +
131 + /**
132 + * Returns the HTML markup.
133 + *
134 + * @return string
135 + */
136 + public function getcontent()
137 + {
90 138 return $this->content;
91 139 }
92 140 }