Skip to content

Instantly share code, notes, and snippets.

@abranhe
Created August 27, 2020 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save abranhe/84ba4ff7e0f1b28a56cb70f060a2dce1 to your computer and use it in GitHub Desktop.
Save abranhe/84ba4ff7e0f1b28a56cb70f060a2dce1 to your computer and use it in GitHub Desktop.
WordPress Minifier
<?php
/**
* W3C Fix and HTML minifier
* Nobody knows how this work, so don't touch it!!!
*
* https://wordpress.stackexchange.com/a/227896/129134
* https://stackoverflow.com/a/41148695
*/
add_action('wp_loaded', 'outputBufferStart');
add_action('shutdown', 'outputBufferEnd');
function outputBufferStart()
{
ob_start("minify");
}
function outputBufferEnd()
{
ob_get_clean();
}
/**
* Minify HTML output.
*/
function minify($buffer)
{
if (!is_admin() && !(defined('DOING_AJAX') && DOING_AJAX)) {
// Remove type from Javascript and CSS.
$buffer = preg_replace("%[ ]type=[\'\"]text\/(javascript|css)[\'\"]%", '', $buffer);
// Add alt attribute to images where not exists.
$buffer = preg_replace('%(<img(?!.*?alt=([\'"]).*?\2)[^>]*?)(/?>)%', '$1 alt="" $3', $buffer);
$buffer = preg_replace('%\s+alt="%', ' alt="', $buffer);
// Clear <head> tag.
$buffer = preg_replace_callback('/(?=<head(.*?)>)(.*?)(?<=<\/head>)/s', 'cleanHTML', $buffer);
// Clear <body>.
$buffer = preg_replace_callback('/(?=<body(.*?)>)(.*?)(?<=<\/body>)/s', 'cleanHTML', $buffer);
// Clear <scripts>
$buffer = preg_replace_callback('/(?=<script(.*?)>)(.*?)(?<=<\/script>)/s', 'cleanJavascript', $buffer);
// Clear <styles>
$buffer = preg_replace_callback('/(?=<style(.*?)>)(.*?)(?<=<\/style>)/s', 'cleanCSS', $buffer);
// Clean between <head> and <body> tags.
$buffer = preg_replace("%</head>([\s\t\n\r]+)<body%", '</head><body', $buffer);
// Clean between body and <html> tags.
$buffer = preg_replace("%</body>([\s\t\n\r]+)</html>%", '</body></html>', $buffer);
// Clean between <html> and <head>
$buffer = preg_replace("%<html(.*?)>([\s\t\n\r]+)<head%", '<html$1><head', $buffer);
}
return $buffer;
}
/**
* Clean HTML.
*/
function cleanHTML($matches)
{
return preg_replace([
'/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s',
'/\>[^\S ]+/s',
'/[^\S ]+\</s',
'/\>\s+\</',
], ['', '>', '<', '><'], $matches[2]);
}
/**
* Clean scripts.
*/
function cleanJavascript($matches)
{
return preg_replace([
'@\/\*(.*?)\*\/@s',
'@((^|\t|\s|\r)\/{2,}.+?(\n|$))@s',
'@(\}(\n|\s+)else(\n|\s+)\{)@s',
'@((\)\{)|(\)(\n|\s+)\{))@s',
'@(\}(\n+|\t+|\s+)else\sif(\s+|)\()@s',
'@(if|for|while|switch|function)\(@s',
'@\s+(\={1,3}|\:)\s+@s',
'@\$\((.*?)\)@s',
'@(if|while)\s\((.*?)\)\s\{@s',
'@function\s\(\s+\)\s{@s',
'@(\n{2,})@s',
'@([\r\n\s\t]+)(,)@s',
'@([\r\n\s\t]+)?([;,{}()]+)([\r\n\s\t]+)?@',
], [
"\n",
"\n",
'}else{',
'){',
'}else if(',
"$1(",
" $1 ",
'$' . "($1)",
"$1 ($2) {",
'function(){',
"\n",
',',
"$2",
], $matches[2]);
}
/**
* Clean styles.
*/
function cleanCSS($matches)
{
return preg_replace([
'/([.#]?)([a-zA-Z0-9,_-]|\)|\])([\s|\t|\n|\r]+)?{([\s|\t|\n|\r]+)(.*?)([\s|\t|\n|\r]+)}([\s|\t|\n|\r]+)/s',
'/([0-9a-zA-Z]+)([;,])([\s|\t|\n|\r]+)?/s',
'@([\r\n\s\t]+)?([;:,{}()]+)([\r\n\s\t]+)?@',
], [
'$1$2{$5} ',
'$1$2',
"$2",
], $matches[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment