-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify-html.php
executable file
·101 lines (89 loc) · 2.8 KB
/
minify-html.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use WyriHaximus\HtmlCompress\Factory;
/**
* Class MinifyHtmlPlugin
* @package Grav\Plugin
*/
class MinifyHtmlPlugin extends Plugin
{
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) return;
// Check if plugin is enabled
if ($this->config['plugins.minify-html.enabled']) {
// Enable the main event we are interested in
$this->enable([
'onOutputGenerated' => ['onOutputGenerated', 0]
]);
}
}
/**
* On Page Content Raw Hook
*/
public function onOutputGenerated()
{
// Check if the page type is HTML
if ($this->grav['page']->templateFormat() === 'html') {
// If Minify HTML cache option is enabled and if page exist continue
// Else only compress the page
if ($this->config['plugins.minify-html.cache'] and !is_null($this->grav['page']->route())) {
$cache = $this->grav['cache'];
$cache_id = md5('minify-html' . $this->grav['page']->id());
$compressedHtmlCache = $cache->fetch($cache_id);
// If the page is not already cached compress the output then cache it
// Else return the precached page
if ($compressedHtmlCache === false) {
$compressedHtml = $this->compressHtml();
$cache->save($cache_id, $compressedHtml);
} else {
$compressedHtml = $compressedHtmlCache;
}
} else {
$compressedHtml = $this->compressHtml();
}
// Return the compressed HTML
$this->grav->output = $compressedHtml;
}
}
/**
* Compress HTML output
*
* @return string HTML output compressed
*/
private function compressHtml()
{
require_once(__DIR__ . '/vendor/autoload.php');
// HTML input (not compressed)
$sourceHtml = $this->grav->output;
// Compression mode
$mode = $this->config['plugins.minify-html.mode'];
// Instantiate the compressor
if ($mode == 'default') $compressor = Factory::construct();
elseif ($mode == 'fastest') $compressor = Factory::constructFastest();
elseif ($mode == 'smallest') $compressor = Factory::constructSmallest();
// HTML output (compressed)
return $compressor->compress($sourceHtml);
}
}