| @@ -1,123 +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 | - /** | |
| 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 | - { | |
| 6 | + private $exclude = array('<!-- ngg_resource_manager_marker -->'); | |
| 7 | + | |
| 8 | + public function read($options) { | |
| 32 | 9 | // Remove the HTML comments? |
| 33 | 10 | $this->keepcomments = (bool) $options['keepcomments']; |
| 34 | - | |
| 35 | - // Filter to force xhtml. | |
| 11 | + | |
| 12 | + // filter to force xhtml | |
| 36 | 13 | $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 ); | |
| 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); | |
| 43 | 20 | } |
| 44 | - | |
| 45 | - // Nothing else for HTML! | |
| 21 | + | |
| 22 | + // Nothing else for HTML | |
| 46 | 23 | return true; |
| 47 | 24 | } |
| 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 ) { | |
| 25 | + | |
| 26 | + //Joins and optimizes CSS | |
| 27 | + public function minify() { | |
| 28 | + $noptimizeHTML = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content ); | |
| 29 | + if ($noptimizeHTML) | |
| 58 | 30 | 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 ); | |
| 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 | + } | |
| 66 | 39 | } |
| 67 | - } | |
| 68 | 40 | |
| 69 | - // Noptimize. | |
| 70 | - $this->content = $this->hide_noptimize( $this->content ); | |
| 41 | + // noptimize me | |
| 42 | + $this->content = $this->hide_noptimize($this->content); | |
| 71 | 43 | |
| 72 | - // Preparing options for Minify_HTML. | |
| 73 | - $options = array( 'keepComments' => $this->keepcomments ); | |
| 74 | - if ( $this->forcexhtml ) { | |
| 75 | - $options['xhtml'] = true; | |
| 76 | - } | |
| 44 | + // Minify html | |
| 45 | + $options = array('keepComments' => $this->keepcomments); | |
| 46 | + if ($this->forcexhtml) { | |
| 47 | + $options['xhtml'] = true; | |
| 48 | + } | |
| 77 | 49 | |
| 78 | - $tmp_content = Minify_HTML::minify( $this->content, $options ); | |
| 79 | - if ( ! empty( $tmp_content ) ) { | |
| 80 | - $this->content = $tmp_content; | |
| 81 | - unset( $tmp_content ); | |
| 82 | - } | |
| 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 | + } | |
| 83 | 57 | |
| 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 ); | |
| 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 | + } | |
| 92 | 67 | } |
| 93 | - } | |
| 94 | 68 | |
| 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 ); | |
| 69 | + return true; | |
| 99 | 70 | } |
| 100 | - | |
| 101 | - return true; | |
| 71 | + | |
| 72 | + // Didn't minify :( | |
| 73 | + return false; | |
| 102 | 74 | } |
| 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 | - { | |
| 75 | + | |
| 76 | + // Does nothing | |
| 77 | + public function cache() { | |
| 78 | + //No cache for HTML | |
| 111 | 79 | return true; |
| 112 | 80 | } |
| 113 | - | |
| 114 | - /** | |
| 115 | - * Returns the HTML markup. | |
| 116 | - * | |
| 117 | - * @return string | |
| 118 | - */ | |
| 119 | - public function getcontent() | |
| 120 | - { | |
| 81 | + | |
| 82 | + //Returns the content | |
| 83 | + public function getcontent() { | |
| 121 | 84 | return $this->content; |
| 122 | 85 | } |
| 123 | 86 | } |