Created
October 9, 2012 14:04
-
-
Save simplistik/3859017 to your computer and use it in GitHub Desktop.
Wordpress: Compile LESS & Wordpress with lessphp
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 | |
/** | |
* Compile a LESS file in Wordpress with lessphp while preserving the comment meta. | |
* @param string $less_fname LESS input file path | |
* @param string $css_fname CSS output file path | |
*/ | |
function auto_compile_less($less_fname, $css_fname) | |
{ | |
// WP 3.4 Setup | |
$theme_meta = array(); | |
if ( wp_get_theme()->get('Name') ) | |
$theme_meta[] = 'Theme Name: ' . wp_get_theme()->get('Name'); | |
if ( wp_get_theme()->get('ThemeURI') ) | |
$theme_meta[] = 'Theme URI: ' . wp_get_theme()->get('ThemeURI'); | |
if ( wp_get_theme()->get('Description') ) | |
$theme_meta[] = 'Description: ' . wp_get_theme()->get('Description'); | |
if ( wp_get_theme()->get('Version') ) | |
$theme_meta[] = 'Version: ' . wp_get_theme()->get('Version'); | |
if ( wp_get_theme()->get('Author') ) | |
$theme_meta[] = 'Author: ' . wp_get_theme()->get('Author'); | |
if ( wp_get_theme()->get('AuthorURI') ) | |
$theme_meta[] = 'Author URI: ' . wp_get_theme()->get('AuthorURI'); | |
$theme_data = "/*\n" . implode("\n", $theme_meta) . "\n*/\n"; | |
$cache_fname = $less_fname . '.cache'; | |
if ( file_exists($cache_fname) ): | |
$cache = unserialize(file_get_contents($cache_fname)); | |
else: | |
$cache = $inputFile; | |
endif; | |
$less = new lessc; | |
$new_cache = $less->cachedCompile($cache); | |
if (!is_array($cache) || $new_cache['updated'] > $cache['updated']): | |
file_put_contents($cache_fname, serialize($new_cache)); | |
file_put_contents($css_fname, $theme_data . $new_cache['compiled']); | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment