/** * Lessify the stylesheet and return the href of the compiled file * * @param string $src Source URL of the file to be parsed * @param string $handle An identifier for the file used to create the file name in the cache * @return string URL of the compiled stylesheet */ public function parse_stylesheet($src, $handle) { // we only want to handle .less files if (!preg_match('/\\.less(\\.php)?$/', preg_replace('/\\?.*$/', '', $src))) { return $src; } // get file path from $src if (!strstr($src, '?')) { $src .= '?'; } // prevent non-existent index warning when using list() & explode() // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work $src_scheme = parse_url($src, PHP_URL_SCHEME); $wp_content_url_scheme = parse_url(WP_CONTENT_URL, PHP_URL_SCHEME); if ($src_scheme != $wp_content_url_scheme) { $src = set_url_scheme($src, $wp_content_url_scheme); } list($less_path, $query_string) = explode('?', str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $src)); // output css file name $css_path = trailingslashit($this->get_cache_dir()) . "{$handle}.css"; // automatically regenerate files if source's modified time has changed or vars have changed try { // initialise the parser $less = new lessc(); // load the cache $cache_path = "{$css_path}.cache"; if (file_exists($cache_path)) { $cache = unserialize(file_get_contents($cache_path)); } // vars to pass into the compiler - default @themeurl var for image urls etc... $this->vars['themeurl'] = '~"' . get_stylesheet_directory_uri() . '"'; $this->vars['lessurl'] = '~"' . dirname($src) . '"'; $this->vars = apply_filters('less_vars', $this->vars, $handle); // If the cache or root path in it are invalid then regenerate if (empty($cache) || empty($cache['less']['root']) || !file_exists($cache['less']['root'])) { $cache = array('vars' => $this->vars, 'less' => $less_path); } // less config $less->setFormatter(apply_filters('less_compression', $this->compression)); $less->setPreserveComments(apply_filters('less_preserve_comments', $this->preserve_comments)); $less->setVariables($this->vars); // add directories to scan for imports $import_dirs = apply_filters('less_import_dirs', $this->import_dirs); if (!empty($import_dirs)) { foreach ((array) $import_dirs as $dir) { $less->addImportDir($dir); } } // register and unregister functions foreach ($this->registered_functions as $name => $callable) { $less->registerFunction($name, $callable); } foreach ($this->unregistered_functions as $name) { $less->unregisterFunction($name); } // allow devs to mess around with the less object configuration do_action_ref_array('lessc', array(&$less)); // $less->cachedCompile only checks for changed file modification times // if using the theme customiser (changed variables not files) then force a compile if ($this->vars !== $cache['vars']) { $force = true; } else { $force = false; } $less_cache = $less->cachedCompile($cache['less'], apply_filters('less_force_compile', $force)); if (empty($cache) || empty($cache['less']['updated']) || $less_cache['updated'] > $cache['less']['updated'] || $this->vars !== $cache['vars']) { file_put_contents($cache_path, serialize(array('vars' => $this->vars, 'less' => $less_cache))); file_put_contents($css_path, $less_cache['compiled']); } } catch (exception $ex) { wp_die($ex->getMessage()); } // restore query string it had if any $url = trailingslashit($this->get_cache_dir(false)) . "{$handle}.css" . (!empty($query_string) ? "?{$query_string}" : ''); // restore original url scheme $url = set_url_scheme($url, $src_scheme); return add_query_arg('ver', $less_cache['updated'], $url); }
/** * Lessify the stylesheet and return the href of the compiled file * * @param string $src Source URL of the file to be parsed * @param string $handle An identifier for the file used to create the file name in the cache * @return string URL of the compiled stylesheet */ public function parse_stylesheet($src, $handle) { // we only want to handle .less files if (!preg_match('/\\.less(\\.php)?$/', preg_replace('/\\?.*$/', '', $src))) { return $src; } // get file path from $src if (!strstr($src, '?')) { $src .= '?'; } // prevent non-existent index warning when using list() & explode() // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work $src_scheme = parse_url($src, PHP_URL_SCHEME); $wp_content_url_scheme = parse_url(WP_CONTENT_URL, PHP_URL_SCHEME); if ($src_scheme != $wp_content_url_scheme) { $src = set_url_scheme($src, $wp_content_url_scheme); } list($less_path, $query_string) = explode('?', str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $src)); $cache = $this->get_cached_file_data($handle); // vars to pass into the compiler - default @themeurl var for image urls etc... $this->vars['themeurl'] = '~"' . get_stylesheet_directory_uri() . '"'; $this->vars['lessurl'] = '~"' . dirname($src) . '"'; $this->vars = apply_filters('less_vars', $this->vars, $handle); // The overall "version" of the LESS file is all it's vars, src etc. $less_version = md5(serialize(array($this->vars, $src))); /** * Give the ability to disable always compiling the LESS with lessc() * and instead just use the $vars and $version of the LESS file to * dictate whether the LESS should be (re)generated. * * This means we don't need to run everything through the lessc() compiler * on every page load. The tradeoff is making a change in a LESS file will not * necessarily cause a (re)generation, one would need to bump the $ver param * on wp_enqueue_script() to cause that. */ if (!get_option('wp_less_always_compile_less', true)) { if (!empty($cache['version']) && $cache['version'] === $less_version) { // restore query string it had if any $url = $cache['url'] . (!empty($query_string) ? "?{$query_string}" : ''); $url = set_url_scheme($url, $src_scheme); return add_query_arg('ver', $less_version, $url); } } // automatically regenerate files if source's modified time has changed or vars have changed try { // initialise the parser if (!class_exists('lessc')) { return $url; //wp_die( 'the lessphp library is missing, aborting, run composer update' ); } // If the cache or root path in it are invalid then regenerate if (empty($cache) || empty($cache['less']['root']) || !file_exists($cache['less']['root'])) { $cache = array('vars' => $this->vars, 'less' => $less_path); } if (empty($cache['url'])) { $cache['url'] = trailingslashit($this->get_cache_dir(false)) . "{$handle}.css"; } // less config $less = new lessc(); $less->setFormatter(apply_filters('less_compression', $this->compression)); $less->setPreserveComments(apply_filters('less_preserve_comments', $this->preserve_comments)); $less->setVariables($this->vars); // add directories to scan for imports $import_dirs = apply_filters('less_import_dirs', $this->import_dirs); if (!empty($import_dirs)) { foreach ((array) $import_dirs as $dir) { $less->addImportDir($dir); } } // register and unregister functions foreach ($this->registered_functions as $name => $callable) { $less->registerFunction($name, $callable); } foreach ($this->unregistered_functions as $name) { $less->unregisterFunction($name); } // allow devs to mess around with the less object configuration do_action_ref_array('lessc', array(&$less)); // $less->cachedCompile only checks for changed file modification times // if using the theme customiser (changed variables not files) then force a compile if ($this->vars !== $cache['vars']) { $force = true; } else { $force = false; } $force = apply_filters('less_force_compile', $force); $less_cache = $less->cachedCompile($cache['less'], $force); // if they have the same values but differing order, they wont match //sort( $cache['less'] ); //sort( $less_cache ); if (empty($cache) || empty($cache['less']['updated']) || md5($less_cache['compiled']) !== md5($cache['less']['compiled']) || $this->vars !== $cache['vars']) { // output css file name $css_path = trailingslashit($this->get_cache_dir()) . "{$handle}.css"; $cache = array('vars' => $this->vars, 'url' => trailingslashit($this->get_cache_dir(false)) . "{$handle}.css", 'version' => $less_version, 'less' => null); /** * If the option to not have LESS always compiled is set, * then we dont store the whole less_cache in the options table as it's * not needed because we only do a comparison based off $vars and $src * (which includes the $ver param). * * This saves space on the options table for high performance environments. */ if (get_option('wp_less_always_compile_less', true)) { $cache['less'] = $less_cache; } $payload = '<strong>Rebuilt stylesheet with handle: "' . $handle . '"</strong><br>'; if ($this->vars != $cache['vars']) { $payload .= '<em>Variables changed</em>'; $difference = array_merge(array_diff_assoc($cache['vars'], $this->vars), array_diff_assoc($this->vars, $cache['vars'])); $payload .= '<pre>' . print_r($difference, true) . '</pre>'; } else { if (empty($cache) || empty($cache['less']['updated'])) { $payload .= '<em>Empty cache or empty last update time</em>'; } else { if ($less_cache['updated'] > $cache['less']['updated']) { $payload .= '<em>Update times different</em>'; } else { $payload .= '<em><strong>Unknown! Contact the developers poste haste!!!!!!!</em><strong></em>'; } } } $payload .= '<br>src: <code>"' . $src . '"</code> css path: <code>"' . $css_path . '"</code> and cache path: <code>"' . $cache_path . '"</code> and scheme <code>"' . $src_scheme . '"</code>'; $this->add_message(array('time' => time(), 'payload' => $payload)); $this->save_parsed_css($css_path, $less_cache['compiled']); $this->update_cached_file_data($handle, $cache); } } catch (exception $ex) { $this->add_message(array('time' => time(), 'payload' => '<strong>Lessphp failure</strong> ' . $ex->GetMessage())); wp_die(wp_strip_all_tags($ex->getMessage())); } // restore query string it had if any $url = $cache['url'] . (!empty($query_string) ? "?{$query_string}" : ''); // restore original url scheme $url = set_url_scheme($url, $src_scheme); if (get_option('wp_less_always_compile_less', true)) { return add_query_arg('ver', $less_cache['updated'], $url); } return add_query_arg('ver', $less_version, $url); }