| 1 |
<?php |
| 2 |
|
| 3 |
class autoptimizeYUI |
| 4 |
{ |
| 5 |
static function available() |
| 6 |
{ |
| 7 |
//If we can run apps |
| 8 |
if(function_exists('shell_exec') && is_callable('shell_exec')) |
| 9 |
{ |
| 10 |
//And if YUI is there for us |
| 11 |
if(file_exists('/usr/bin/java') && file_exists(WP_PLUGIN_DIR.'/autoptimize/yui/yuicompressor.jar')) |
| 12 |
{ |
| 13 |
//And we have a dir in where to work |
| 14 |
if(is_writable(WP_PLUGIN_DIR.'/autoptimize/yui/')) |
| 15 |
{ |
| 16 |
//Then we're available |
| 17 |
return true; |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
//We can't use YUI :( |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
static function compress($type,$code) |
| 26 |
{ |
| 27 |
//Check for supported types |
| 28 |
if(!in_array($type,array('js','css'))) |
| 29 |
return false; |
| 30 |
|
| 31 |
//Write temp file |
| 32 |
$file = tempnam(WP_PLUGIN_DIR.'/autoptimize/yui/',$type); |
| 33 |
file_put_contents($file,$code); |
| 34 |
|
| 35 |
//Call YUI |
| 36 |
$yuipath = escapeshellarg(WP_PLUGIN_DIR.'/autoptimize/yui/yuicompressor.jar'); |
| 37 |
$code = shell_exec('/usr/bin/java -jar '.$yuipath.' --type '.$type.' '.escapeshellarg($file)); |
| 38 |
|
| 39 |
//Delete temp file |
| 40 |
unlink($file); |
| 41 |
|
| 42 |
//Give the code! |
| 43 |
return $code; |
| 44 |
} |
| 45 |
} |
| 46 |
|