PluginProbe
Autoptimize / 2.1.2
Autoptimize v2.1.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 +62 -102 2.6.12.1.2 View file →
@@ -1,126 +1,86 @@
1 1 <?php
2 -/**
3 - * Handles minifying HTML markup.
4 - */
2 +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5 3
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 - */
4 +class autoptimizeHTML extends autoptimizeBase {
17 5 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 - {
6 + private $exclude = array('<!-- ngg_resource_manager_marker -->');
7 +
8 + public function read($options) {
35 9 // Remove the HTML comments?
36 10 $this->keepcomments = (bool) $options['keepcomments'];
37 -
38 - // Filter to force xhtml.
11 +
12 + // filter to force xhtml
39 13 $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 );
14 +
15 + // filter to add strings to be excluded from HTML minification
16 + $excludeHTML = apply_filters( 'autoptimize_filter_html_exclude','' );
17 + if ($excludeHTML!=="") {
18 + $exclHTMLArr = array_filter(array_map('trim',explode(",",$excludeHTML)));
19 + $this->exclude = array_merge($exclHTMLArr,$this->exclude);
46 20 }
47 -
48 - // Nothing else for HTML!
21 +
22 + // Nothing else for HTML
49 23 return true;
50 24 }
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 ) {
25 +
26 + //Joins and optimizes CSS
27 + public function minify() {
28 + $noptimizeHTML = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content );
29 + if ($noptimizeHTML)
61 30 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 );
31 +
32 + if(class_exists('Minify_HTML')) {
33 + // wrap the to-be-excluded strings in noptimize tags
34 + foreach ($this->exclude as $exclString) {
35 + if (strpos($this->content,$exclString)!==false) {
36 + $replString="<!--noptimize-->".$exclString."<!--/noptimize-->";
37 + $this->content=str_replace($exclString,$replString,$this->content);
38 + }
69 39 }
70 - }
71 40
72 - // Noptimize.
73 - $this->content = $this->hide_noptimize( $this->content );
41 + // noptimize me
42 + $this->content = $this->hide_noptimize($this->content);
74 43
75 - // Preparing options for Minify_HTML.
76 - $options = array( 'keepComments' => $this->keepcomments );
77 - if ( $this->forcexhtml ) {
78 - $options['xhtml'] = true;
79 - }
44 + // Minify html
45 + $options = array('keepComments' => $this->keepcomments);
46 + if ($this->forcexhtml) {
47 + $options['xhtml'] = true;
48 + }
80 49
81 - $tmp_content = Minify_HTML::minify( $this->content, $options );
82 - if ( ! empty( $tmp_content ) ) {
83 - $this->content = $tmp_content;
84 - unset( $tmp_content );
85 - }
50 + if (@is_callable(array("Minify_HTML","minify"))) {
51 + $tmp_content = Minify_HTML::minify($this->content,$options);
52 + if (!empty($tmp_content)) {
53 + $this->content = $tmp_content;
54 + unset($tmp_content);
55 + }
56 + }
86 57
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 );
58 + // restore noptimize
59 + $this->content = $this->restore_noptimize($this->content);
60 +
61 + // remove the noptimize-wrapper from around the excluded strings
62 + foreach ($this->exclude as $exclString) {
63 + $replString="<!--noptimize-->".$exclString."<!--/noptimize-->";
64 + if (strpos($this->content,$replString)!==false) {
65 + $this->content=str_replace($replString,$exclString,$this->content);
66 + }
95 67 }
96 - }
97 68
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 );
69 + return true;
102 70 }
103 -
104 - return true;
71 +
72 + // Didn't minify :(
73 + return false;
105 74 }
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 - {
75 +
76 + // Does nothing
77 + public function cache() {
78 + //No cache for HTML
114 79 return true;
115 80 }
116 -
117 - /**
118 - * Returns the HTML markup.
119 - *
120 - * @return string
121 - */
122 - public function getcontent()
123 - {
81 +
82 + //Returns the content
83 + public function getcontent() {
124 84 return $this->content;
125 85 }
126 86 }