Created
November 9, 2009 19:37
-
-
Save westonruter/230197 to your computer and use it in GitHub Desktop.
lessphp for WordPress: filter for stylesheet URLs so that if there is a corresponding file with a .less extension and that file is newer than this .css file, then lessphp is invoked to rebuild the stylesheet: the theme comment header is preserved.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter stylesheet URLs so that if there is a corresponding file with | |
* a .less extension and that file is newer than this .css file, then lessphp | |
* <http://leafo.net/lessphp/> is invoked to rebuild the stylesheet: the | |
* comment header is preserved in the LESS output. Requires that lessphp be | |
* located in <TEMPLATEPATH/lessphp/lessc.inc.php> This filter may be invoked | |
* in header.php as follows: | |
* <link rel="stylesheet" | |
* href="<?php echo apply_filters('style_loader_src', get_stylesheet_uri(), null); ?>" | |
* type="text/css" | |
* media="screen" /> | |
* Filter also includes filemtime() in query string so that far-future expires | |
* may be employed. | |
*/ | |
function less_filter_style_loader_src($src, $handle = null){ | |
$parsedSrc = parse_url($src); | |
//Process this file if the src's host is the same as this host (or if it's not specified) | |
if(!@$parsedSrc['host'] || @$parsedSrc['host'] == parse_url(get_option('siteurl'), PHP_URL_HOST)){ | |
//Get the paths to the .css and .less files | |
$cssPath = trailingslashit(ABSPATH) . $parsedSrc['path']; | |
$lessPath = preg_replace('/\.css$/i', '.less', $cssPath); | |
//Only process the file if a corresponding .less file exists and if the | |
// .css file does not or it is older than the .less file | |
if($lessPath && file_exists($lessPath) && | |
(!file_exists($cssPath) || filemtime($lessPath) > filemtime($cssPath)) && | |
include(TEMPLATEPATH . '/lessphp/lessc.inc.php') | |
){ | |
try { | |
$lc = new lessc(); | |
$lc->importDir = TEMPLATEPATH . '/'; | |
$lessCode = file_get_contents($lessPath); | |
$cssCode = $lc->parse($lessCode); | |
//Get the comment header out of the source .less file to preserve it | |
$header = ''; | |
if(preg_match('{(^(?:\s*/\*.+?\*/\s*)+)}s', $lessCode, $matches)) | |
$header = $matches[1]; | |
//Add a warning to the header to instruct people to not modify it | |
// directly since it is dynamically generated by lessphp. | |
$header .= "\n/*\n"; | |
$header .= wordwrap("WARNING! This file is automatically generated from '" . basename($lessPath) . "' by lessphp." | |
. "To make modifications to the stylesheet, modify that file simply reload" | |
. "the page to be automatically regenerated by WordPress (requires that lessphp be available: http://github.com/cloudhead/less)", 80, "\n"); //\r\n | |
$header .= "\n*/\n"; | |
//Write out new .css file | |
if(!@file_put_contents($cssPath, $header . $cssCode)) | |
throw new Exception("Unable to write to " . basename($cssPath)); | |
} | |
catch(Exception $e){ | |
//Construct an error message to display in the page | |
$error = "<p style='color:red; padding:0.5em; border:solid 1px red; background:white; position:fixed; top:0; left:0;'>"; | |
$error .= "<b><a href='http://github.com/leafo/lessphp' target='_blank'>lessphp</a> error in "; | |
$error .= "<a href='" . esc_attr(preg_replace('/.css$/', '.less', $src)) . "'>"; | |
$error .= basename($lessPath) . "</a></b>: " . esc_attr($e->getMessage()) . "</p>"; | |
add_action( | |
'wp_footer', | |
create_function( | |
'', | |
"print " . var_export($error, true) . ";" | |
) | |
); | |
} | |
} | |
//Append the filemtime to the CSS file so that far-future expires can be set | |
$src = $parsedSrc['path'] . '?' . @filemtime($cssPath); | |
} | |
return $src; | |
} | |
if(!is_admin()) | |
add_filter('style_loader_src', 'less_filter_style_loader_src', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment