PluginProbe
Autoptimize / 1.8.2
Autoptimize v1.8.2
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 +51 -119 2.6.11.8.2 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 -}
3 +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
9 4
10 -class autoptimizeHTML extends autoptimizeBase
11 -{
12 - /**
13 - * Whether HTML comments are kept.
14 - *
15 - * @var bool
16 - */
17 - private $keepcomments = false;
5 +class autoptimizeHTML extends autoptimizeBase {
6 + private $keepcomments = false;
7 +
8 + //Does nothing
9 + public function read($options)
10 + {
11 + //Remove the HTML comments?
12 + $this->keepcomments = (bool) $options['keepcomments'];
13 +
14 + //Nothing to read for HTML
15 + return true;
16 + }
17 +
18 + //Joins and optimizes CSS
19 + public function minify()
20 + {
21 + if(class_exists('Minify_HTML'))
22 + {
23 + // noptimize me
24 + $this->content = $this->hide_noptimize($this->content);
18 25
19 - /** @var bool */
20 - private $forcexhtml = false;
26 + // Minify html
27 + $options = array('keepComments' => $this->keepcomments);
21 28
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 - );
29 + if (is_callable(array(new Minify_HTML,"minify"))) {
30 + $tmp_content = Minify_HTML::minify($this->content,$options);
31 + if (!empty($tmp_content)) {
32 + $this->content = $tmp_content;
33 + unset($tmp_content);
34 + }
35 + }
32 36
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 - }
37 + // restore noptimize
38 + $this->content = $this->restore_noptimize($this->content);
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 }