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