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