public static function Get($less_files, $parser_options = array(), $modify_vars = array()) { if (isset($parser_options['cache_dir'])) { Less_Cache::$cache_dir = $parser_options['cache_dir']; }if (empty(Less_Cache::$cache_dir)) { throw new Exception('cache_dir not set'); }if (isset($parser_options['prefix'])) { Less_Cache::$prefix = $parser_options['prefix']; }if (isset($parser_options['prefix_vars'])) { Less_Cache::$prefix_vars = $parser_options['prefix_vars']; }if (empty(Less_Cache::$prefix_vars)) { throw new Exception('prefix_vars not set'); }self::CheckCacheDir(); $less_files = (array) $less_files; if (!empty($modify_vars)) { $lessvars = Less_Parser::serializeVars($modify_vars); $vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1($lessvars) . '.less'; if (!file_exists($vars_file)) { file_put_contents($vars_file, $lessvars); }$less_files+=array($vars_file => '/'); }$hash = md5(json_encode($less_files)); $list_file = Less_Cache::$cache_dir . $hash . '.list'; if (!isset($parser_options['use_cache']) || $parser_options['use_cache'] === true) { if (file_exists($list_file)) { self::ListFiles($list_file, $list, $cached_name); $compiled_name = self::CompiledName($list); if (!$cached_name || $cached_name === $compiled_name) { $output_file = self::OutputFile($compiled_name, $parser_options); if ($output_file && file_exists($output_file)) { @touch($list_file); return basename($output_file); } } } }$compiled = self::Cache($less_files, $parser_options); if (!$compiled) { return false; }$compiled_name = self::CompiledName($less_files); $output_file = self::OutputFile($compiled_name, $parser_options); $list = $less_files; $list[] = $compiled_name; $cache = implode("\n", $list); file_put_contents($list_file, $cache); $compiled = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $compiled); file_put_contents($output_file, $compiled); self::CleanCache(); return basename($output_file); }
/** * Save and reuse the results of compiled less files. * The first call to Get() will generate css and save it. * Subsequent calls to Get() with the same arguments will return the same css filename * * @param array $less_files Array of .less files to compile * @param array $parser_options Array of compiler options * @param array $modify_vars Array of variables * @return string Name of the css file */ public static function Get($less_files, $parser_options = array(), $modify_vars = array()) { //check $cache_dir if (isset($parser_options['cache_dir'])) { Less_Cache::$cache_dir = $parser_options['cache_dir']; } if (empty(Less_Cache::$cache_dir)) { throw new Exception('cache_dir not set'); } if (isset($parser_options['prefix'])) { Less_Cache::$prefix = $parser_options['prefix']; } if (empty(Less_Cache::$prefix)) { throw new Exception('prefix not set'); } if (isset($parser_options['prefix_vars'])) { Less_Cache::$prefix_vars = $parser_options['prefix_vars']; } if (empty(Less_Cache::$prefix_vars)) { throw new Exception('prefix_vars not set'); } self::CheckCacheDir(); $less_files = (array) $less_files; //create a file for variables if (!empty($modify_vars)) { $lessvars = Less_Parser::serializeVars($modify_vars); $vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1($lessvars) . '.less'; if (!file_exists($vars_file)) { file_put_contents($vars_file, $lessvars); } $less_files += array($vars_file => '/'); } // generate name for compiled css file $hash = md5(json_encode($less_files)); $list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list'; // check cached content if (!isset($parser_options['use_cache']) || $parser_options['use_cache'] === true) { if (file_exists($list_file)) { self::ListFiles($list_file, $list, $cached_name); $compiled_name = self::CompiledName($list, $hash); // if $cached_name is the same as the $compiled name, don't regenerate if (!$cached_name || $cached_name === $compiled_name) { $output_file = self::OutputFile($compiled_name, $parser_options); if ($output_file && file_exists($output_file)) { @touch($list_file); return basename($output_file); // for backwards compatibility, we just return the name of the file } } } } $compiled = self::Cache($less_files, $parser_options); if (!$compiled) { return false; } $compiled_name = self::CompiledName($less_files, $hash); $output_file = self::OutputFile($compiled_name, $parser_options); //save the file list $list = $less_files; $list[] = $compiled_name; $cache = implode("\n", $list); file_put_contents($list_file, $cache); //save the css file_put_contents($output_file, $compiled); //clean up self::CleanCache(); return basename($output_file); }
/** * Save and reuse the results of compiled less files. * The first call to Get() will generate css and save it. * Subsequent calls to Get() with the same arguments will return the same css filename * * @param array $less_files Array of .less files to compile * @param array $parser_options Array of compiler options * @param array $modify_vars Array of variables * @return string Name of the css file */ public static function Get($less_files, $parser_options = array(), $modify_vars = array()) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; //check $cache_dir if (isset($parser_options['cache_dir'])) { Less_Cache::$cache_dir = $parser_options['cache_dir']; } if (empty(Less_Cache::$cache_dir)) { throw new Exception('cache_dir not set'); } if (isset($parser_options['prefix'])) { Less_Cache::$prefix = $parser_options['prefix']; } if (empty(Less_Cache::$prefix)) { throw new Exception('prefix not set'); } if (isset($parser_options['prefix_vars'])) { Less_Cache::$prefix_vars = $parser_options['prefix_vars']; } if (empty(Less_Cache::$prefix_vars)) { throw new Exception('prefix_vars not set'); } self::CheckCacheDir(); $less_files = (array) $less_files; //create a file for variables if (!empty($modify_vars)) { $lessvars = Less_Parser::serializeVars($modify_vars); $vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1($lessvars) . '.less'; if (!file_exists($vars_file)) { $wp_filesystem->put_contents($vars_file, $lessvars, FS_CHMOD_FILE); } $less_files += array($vars_file => '/'); } // generate name for compiled css file $hash = md5(json_encode($less_files)); $list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list'; // check cached content if (!isset($parser_options['use_cache']) || $parser_options['use_cache'] === true) { if (file_exists($list_file)) { self::ListFiles($list_file, $list, $cached_name); $compiled_name = self::CompiledName($list); // if $cached_name != $compiled_name, we know we need to recompile if (!$cached_name || $cached_name === $compiled_name) { $output_file = self::OutputFile($compiled_name, $parser_options); if ($output_file && file_exists($output_file)) { @touch($list_file); return wp_basename($output_file); // for backwards compatibility, we just return the name of the file } } } } $compiled = self::Cache($less_files, $parser_options); if (!$compiled) { return false; } $compiled_name = self::CompiledName($less_files); $output_file = self::OutputFile($compiled_name, $parser_options); //save the file list $list = $less_files; $list[] = $compiled_name; $cache = implode("\n", $list); $wp_filesystem->put_contents($list_file, $cache, FS_CHMOD_FILE); //save the css $wp_filesystem->put_contents($output_file, $compiled, FS_CHMOD_FILE); //clean up self::CleanCache(); return wp_basename($output_file); }