Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jpdevries/dc100bfba892b7c499d8 to your computer and use it in GitHub Desktop.
Save jpdevries/dc100bfba892b7c499d8 to your computer and use it in GitHub Desktop.
<?php
/**
*
* Plugin fires OnWebPagePrerender system event, loads Minify HTML by Mr Clay and minifies the requested Resource HTML inc. inline css and js.
*
* If used in conjunction with StatCache the minified HTML is cached and served as a flat file via IIS Rewrite.
*
* CREDITS
*
* http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins
* http://forums.modx.com/thread/69932/full-page-caching---is-it-achievable
* http://ttyl.modx.com/thread/69839/using-modx--gt-cachemanager--gt-refresh-with-custom-components#dis-post-391711
* http://forums.modx.com/thread/29643/tidy-plugin-fetch-the-document-output
*
* DEPENDENCIES
*
* https://github.com/mrclay/minify - set to use version bundled with MODX core (/manager/min) with MODX_MANAGER_PATH
*
* AUTHOR
*
* chris@cubewebworks.co.uk
*
**/
switch ($modx->event->name) { // switch not required, plugin only fires on one event OnWebPagePrerender
case 'OnWebPagePrerender':
$output = $modx->resource->_output;
$initial_bytes = strlen($output);
// $minify_lib_path = $modx->config['base_path'] . 'min/'; // this is for manually installed version of min
$minify_lib_path = $modx->config['manager_path'] . 'min/'; // this is for min version bundled with MODX
if (!class_exists('Minify_HTML')) {
// Two lines below to register Minify Loader are only applicable if using minify v2.1.7
// require("$minify_lib_path/lib/Minify/Loader.php");
// Minify_Loader::register();
ini_set('include_path', ini_get('include_path') . ":$minify_lib_path/lib"); // required? test without
require_once("$minify_lib_path/lib/Minify/HTML.php");
require_once("$minify_lib_path/lib/Minify/CSS.php");
require_once("$minify_lib_path/lib/JSMin.php");
// also include comment preserver because of error without /*[13-Aug-2015 17:41:17 UTC] PHP Fatal error: require_once(): Failed opening required 'C:/path/to/hosts/domain.co.uk/httpdocs/min//lib/CommentPreserver.php' (include_path='.;.\includes;.\pear:C:/path/to/hosts/domain.co.uk/httpdocs/min//lib')*/
require_once("$minify_lib_path/lib/Minify/CommentPreserver.php");
}
// call minify HTML function and minify inline CSS and JS
$output = Minify_HTML::minify($output, array(
'cssMinifier' => array(
'Minify_CSS',
'minify'
),
'jsMinifier' => array(
'JSMin',
'minify'
)
));
// calculate savings
$final_bytes = strlen($output);
$saved_bytes = ($initial_bytes - $final_bytes);
$savings = round((($initial_bytes - $final_bytes) / $initial_bytes * 100), 3);
// tag bytes saved comment to end out output
$output .= PHP_EOL . "<!-- [HTML Minification] Uncompressed: $initial_bytes bytes; Compressed: $final_bytes bytes; $saved_bytes bytes ($savings%) saved. -->";
$modx->resource->_output = $output;
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment