smourph
3/29/2016 - 3:43 PM

Autocompile Less to CSS -- lessc.inc.php v0.4.0 (http://leafo.net/lessphp/)

Autocompile Less to CSS -- lessc.inc.php v0.4.0 (http://leafo.net/lessphp/)

const CUSTOM_CSSFIX_FILE = '/css/euroserum-fix.css';
const CUSTOM_LESSFIX_FILE = '/less/euroserum-fix.less';
const LESSC_INC = '/inc/lessc.inc.php';

/**
 * Autocompile Less to CSS
 */
if (is_admin()) {
    add_action('init', 'autoCompileLess');
}
function autoCompileLess()
{
    // include lessc.inc
    require_once(get_stylesheet_directory() . LESSC_INC);

    // input and output location
    $inputFile = get_stylesheet_directory() . CUSTOM_LESSFIX_FILE;
    $outputFile = get_stylesheet_directory() . CUSTOM_CSSFIX_FILE;

    // load the cache
    $cacheFile = $inputFile . ".cache";

    if (file_exists($cacheFile)) {
        $cache = unserialize(file_get_contents($cacheFile));
    } else {
        $cache = $inputFile;
    }

    $less = new lessc;
    // preserve comments
    $less->setPreserveComments(true);
    // create a new cache object, and compile
    $newCache = $less->cachedCompile($cache);

    // output a LESS file, and cache file only if it has been modified since last compile
    if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
        file_put_contents($cacheFile, serialize($newCache));
        file_put_contents($outputFile, $newCache['compiled']);
    }
}