| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
class autoptimizeHTML extends autoptimizeBase { |
| 5 |
private $keepcomments = false; |
| 6 |
private $exclude = array('<!-- ngg_resource_manager_marker -->', '<!--noindex-->', '<!--/noindex-->'); |
| 7 |
|
| 8 |
public function read($options) { |
| 9 |
// Remove the HTML comments? |
| 10 |
$this->keepcomments = (bool) $options['keepcomments']; |
| 11 |
|
| 12 |
// filter to force xhtml |
| 13 |
$this->forcexhtml = (bool) apply_filters( 'autoptimize_filter_html_forcexhtml', false ); |
| 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); |
| 20 |
} |
| 21 |
|
| 22 |
// Nothing else for HTML |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
//Joins and optimizes CSS |
| 27 |
public function minify() { |
| 28 |
$noptimizeHTML = apply_filters( 'autoptimize_filter_html_noptimize', false, $this->content ); |
| 29 |
if ($noptimizeHTML) |
| 30 |
return false; |
| 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 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// noptimize me |
| 42 |
$this->content = $this->hide_noptimize($this->content); |
| 43 |
|
| 44 |
// Minify html |
| 45 |
$options = array('keepComments' => $this->keepcomments); |
| 46 |
if ($this->forcexhtml) { |
| 47 |
$options['xhtml'] = true; |
| 48 |
} |
| 49 |
|
| 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 |
} |
| 57 |
|
| 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 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// revslider data attribs somehow suffer from HTML optimization, this fixes that |
| 70 |
if ( class_exists('RevSlider') && apply_filters('autoptimize_filter_html_dataattrib_cleanup', false) ) { |
| 71 |
$this->content = preg_replace('#\n(data-.*$)\n#Um',' $1 ', $this->content); |
| 72 |
$this->content = preg_replace('#<[^>]*(=\"[^"\'<>\s]*\")(\w)#','$1 $2', $this->content); |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
// Didn't minify :( |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// Does nothing |
| 83 |
public function cache() { |
| 84 |
//No cache for HTML |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
//Returns the content |
| 89 |
public function getcontent() { |
| 90 |
return $this->content; |
| 91 |
} |
| 92 |
} |
| 93 |
|