PluginProbe
Autoptimize / 1.6.5
Autoptimize v1.6.5
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 +52 -120 2.5.11.6.5 View file →
@@ -1,126 +1,58 @@
1 1 <?php
2 -/**
3 - * Handles minifying HTML markup.
4 - */
5 2
6 -if ( ! defined( 'ABSPATH' ) ) {
7 - exit;
8 -}
9 -
10 3 class autoptimizeHTML extends autoptimizeBase
11 4 {
12 - /**
13 - * Whether HTML comments are kept.
14 - *
15 - * @var bool
16 - */
17 - private $keepcomments = false;
5 + private $keepcomments = false;
6 +
7 + //Does nothing
8 + public function read($options)
9 + {
10 + //Remove the HTML comments?
11 + $this->keepcomments = (bool) $options['keepcomments'];
12 +
13 + //Nothing to read for HTML
14 + return true;
15 + }
16 +
17 + //Joins and optimizes CSS
18 + public function minify()
19 + {
20 + if(class_exists('Minify_HTML'))
21 + {
22 + // Minify html
23 + // but don't remove comment-blocks needed by WP Super Cache (& W3 Total Cache)
24 + if ( ($this->keepcomments===false) && (preg_match( '/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $this->content ))) {
25 + $this->content = preg_replace('#(<!--mclude .*<!--/mclude-->)#ise','\'%%MFUNC%%\'.base64_encode("$0").\'%%MFUNC%%\'', $this->content);
26 + $this->content = preg_replace('#(<!--mfunc.*<!--/mfunc-->)#ise','\'%%MFUNC%%\'.base64_encode("$1").\'%%MFUNC%%\'', $this->content);
27 + $this->content = preg_replace('#(<!--dynamic-cached-content-->.*<!--/dynamic-cached-content-->)#ise','\'%%MFUNC%%\'.base64_encode("$0").\'%%MFUNC%%\'', $this->content);
28 + $restore_mfuncs=true;
29 + }
30 +
31 + $options = array('keepComments' => $this->keepcomments);
32 + $this->content = Minify_HTML::minify($this->content,$options);
33 +
18 34
19 - /** @var bool */
20 - private $forcexhtml = false;
21 -
22 - /**
23 - * Things to exclude from being minifed.
24 - *
25 - * @var array
26 - */
27 - private $exclude = array(
28 - '<!-- ngg_resource_manager_marker -->',
29 - '<!--noindex-->',
30 - '<!--/noindex-->',
31 - );
32 -
33 - public function read( $options )
34 - {
35 - // Remove the HTML comments?
36 - $this->keepcomments = (bool) $options['keepcomments'];
37 -
38 - // Filter to force xhtml.
39 - $this->forcexhtml = (bool) apply_filters( 'autoptimize_filter_html_forcexhtml', false );
40 -
41 - // Filterable strings to be excluded from HTML minification.
42 - $exclude = apply_filters( 'autoptimize_filter_html_exclude', '' );
43 - if ( '' !== $exclude ) {
44 - $exclude_arr = array_filter( array_map( 'trim', explode( ',', $exclude ) ) );
45 - $this->exclude = array_merge( $exclude_arr, $this->exclude );
46 - }
47 -
48 - // Nothing else for HTML!
49 - return true;
50 - }
51 -
52 - /**
53 - * Minifies HTML.
54 - *
55 - * @return bool
56 - */
57 - public function minify()
58 - {
59 - $noptimize = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
60 - if ( $noptimize ) {
61 - return false;
62 - }
63 -
64 - // Wrap the to-be-excluded strings in noptimize tags.
65 - foreach ( $this->exclude as $str ) {
66 - if ( false !== strpos( $this->content, $str ) ) {
67 - $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
68 - $this->content = str_replace( $str, $replacement, $this->content );
69 - }
70 - }
71 -
72 - // Noptimize.
73 - $this->content = $this->hide_noptimize( $this->content );
74 -
75 - // Preparing options for Minify_HTML.
76 - $options = array( 'keepComments' => $this->keepcomments );
77 - if ( $this->forcexhtml ) {
78 - $options['xhtml'] = true;
79 - }
80 -
81 - $tmp_content = Minify_HTML::minify( $this->content, $options );
82 - if ( ! empty( $tmp_content ) ) {
83 - $this->content = $tmp_content;
84 - unset( $tmp_content );
85 - }
86 -
87 - // Restore noptimize.
88 - $this->content = $this->restore_noptimize( $this->content );
89 -
90 - // Remove the noptimize-wrapper from around the excluded strings.
91 - foreach ( $this->exclude as $str ) {
92 - $replacement = '<!--noptimize-->' . $str . '<!--/noptimize-->';
93 - if ( false !== strpos( $this->content, $replacement ) ) {
94 - $this->content = str_replace( $replacement, $str, $this->content );
95 - }
96 - }
97 -
98 - // Revslider data attribs somehow suffer from HTML optimization, this fixes that!
99 - if ( class_exists( 'RevSlider' ) && apply_filters( 'autoptimize_filter_html_dataattrib_cleanup', false ) ) {
100 - $this->content = preg_replace( '#\n(data-.*$)\n#Um', ' $1 ', $this->content );
101 - $this->content = preg_replace( '#<[^>]*(=\"[^"\'<>\s]*\")(\w)#', '$1 $2', $this->content );
102 - }
103 -
104 - return true;
105 - }
106 -
107 - /**
108 - * Doesn't do much in case of HTML (no cache in css/js sense there)
109 - *
110 - * @return true
111 - */
112 - public function cache()
113 - {
114 - return true;
115 - }
116 -
117 - /**
118 - * Returns the HTML markup.
119 - *
120 - * @return string
121 - */
122 - public function getcontent()
123 - {
124 - return $this->content;
125 - }
35 + if (isset($restore_mfuncs)) {
36 + $this->content = preg_replace('#%%MFUNC%%(.*)%%MFUNC%%#sie','stripslashes(base64_decode("$1"))',$this->content);
37 + }
38 +
39 + return true;
40 + }
41 +
42 + //Didn't minify :(
43 + return false;
44 + }
45 +
46 + //Does nothing
47 + public function cache()
48 + {
49 + //No cache for HTML
50 + return true;
51 + }
52 +
53 + //Returns the content
54 + public function getcontent()
55 + {
56 + return $this->content;
57 + }
126 58 }