PluginProbe
Autoptimize / 2.6.2
Autoptimize v2.6.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
autoptimize / classes / autoptimizeHTML.php

autoptimizeHTML.php in Autoptimize 2.6.2, at classes/autoptimizeHTML.php

127 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles minifying HTML markup.
4 */
5
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 */
17 private $keepcomments = false;
18
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 }
126 }
127