PluginProbe
Autoptimize / 2.4.0
Autoptimize v2.4.0
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.4.0, at classes/autoptimizeHTML.php

124 lines 3.4 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 /**
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 }
123 }
124