-
Notifications
You must be signed in to change notification settings - Fork 1
/
kirby-html-minifier.php
46 lines (37 loc) · 1.13 KB
/
kirby-html-minifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace KirbyMinifier;
use c;
use str;
use Kirby\Component\Response;
use TinyHtmlMinifier;
use Minify_HTML;
use TinyMinify;
require __DIR__ . DS . 'tiny-html-minifier' . DS . 'tiny-html-minifier.php';
class Minifier extends Response {
public function make($response) {
if($this->isBlacklisted($response->format())) {
return $response;
}
$html = parent::make( $response );
if(empty($html)) return '';
if(!c::get('plugin.html.minifier.active', true)) return $html;
if(!$this->minifierAllowed()) return $html;
return TinyMinify::html($html, c::get('plugin.html.minifier.options', []));
}
public function minifierAllowed() {
$parents = c::get('plugin.html.minifier.blacklist', []);
$parents = (is_string($parents)) ? [$parents] : $parents;
$page_id = page()->id();
foreach($parents as $parent) {
if(str::startsWith($page_id, $parent . '/') || $parent == $page_id) {
return false;
}
}
return true;
}
private function isBlacklisted($format, $blacklist = ['js', 'css'])
{
return in_array($format, $blacklist);
}
}
$kirby->set('component', 'response', 'KirbyMinifier\Minifier');