/** * SCSSify 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 .scss files if (!preg_match('/\\.scss(\\.php)?$/', preg_replace('/\\?.*$/', '', $src))) { return $src; } // get file path from $src if (!strstr($src, '?')) { $src .= '?'; } // prevent non-existent index warning when using list() & explode() // vars to pass into the compiler - default @themeurl var for image urls etc... $this->add_vars(array('theme-url' => '"' . get_template_directory_uri() . '"')); // Lets get the paths we need $scss_directory = str_replace(get_template_directory_uri() . "/", "", $src); $scss_directory = substr($scss_directory, 0, strrpos($scss_directory, '/')); $scss_directory = get_template_directory() . "/{$scss_directory}"; $scss_filename = substr(basename($src), 0, strrpos(basename($src), '?')); $css_filename = str_replace("scss", "css", $scss_filename); $css_directory_uri = wp_upload_dir()['baseurl'] . "/wp-scss-cache"; $this->set_scss_directory($scss_directory); $this->set_handle($handle); $this->set_src_path("{$scss_directory}/{$scss_filename}"); $this->add_vars(apply_filters('scss_vars', $this->get_vars(), $handle)); $scss_is_changed = $this->scss_is_changed(); $is_changed = $scss_is_changed['changed']; $hash = $scss_is_changed['hash']; $this->add_vars(array('color-map' => array("blue" => "green", "yellow" => "purple"))); // Don't recompile if the neither the vars nor the source have changed if (!$is_changed && !WP_DEBUG) { return "{$css_directory_uri}/{$css_filename}"; } // Do recompile if either the vars or soure have changed try { $scss = new \Leafo\ScssPhp\Compiler(); $scss->setVariables($this->vars); $scss->addImportPath($scss_directory); $compiled_css = $scss->compile(file_get_contents("{$scss_directory}/{$scss_filename}")); $this->save_parsed_css($this->get_css_directory() . "/{$css_filename}", $compiled_css); } catch (Exception $ex) { wp_die($ex->getMessage()); } return "{$css_directory_uri}/{$css_filename}?ver={$hash}"; }
/** * Compile SCSS into CSS. * * @param string|array $source_filename * @param string $target_filename * @param array $variables (optional) * @parsm bool $minify (optional) * @return bool */ public static function compileSCSS($source_filename, $target_filename, $variables = array(), $minify = false) { // Get the cleaned and concatenated content. $content = self::concatCSS($source_filename, $target_filename); // Compile! try { $scss_compiler = new \Leafo\ScssPhp\Compiler(); $scss_compiler->setFormatter($minify ? '\\Leafo\\ScssPhp\\Formatter\\Crunched' : '\\Leafo\\ScssPhp\\Formatter\\Expanded'); $scss_compiler->setImportPaths(array(dirname(is_array($source_filename) ? array_first($source_filename) : $source_filename))); if ($variables) { $scss_compiler->setVariables($variables); } $charset = strpos($content, '@charset') === false ? '@charset "UTF-8";' . "\n" : ''; $content = $charset . $scss_compiler->compile($content) . "\n"; $result = true; } catch (\Exception $e) { $content = '/*' . "\n" . 'Error while compiling SCSS:' . "\n" . $e->getMessage() . "\n" . '*/' . "\n"; $result = false; } // Save the result to the target file. Storage::write($target_filename, $content); return $result; }
/** * * @param type $variables * @param type $functions * @return \Leafo\ScssPhp\Server|boolean */ function get_scss_parser($variables, $functions) { if (!(include_once SCSSPHP_INC . 'scss.inc.php')) { trigger_error('Unable to process .scss file -- SCSSPHP not configured correctly on your server. Check the SCSSPHP_INC setting in settings/package_settings.php.'); return false; } $scss = new Leafo\ScssPhp\Compiler(); $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed'); $scss->setVariables($variables); foreach ($functions as $name => $func) { $scss->registerFunction($name, $func); } return new \Leafo\ScssPhp\Server('.', '.', $scss); }