Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scssPHP 1.5.2 & update deprecations #195

Merged
merged 12 commits into from
Jun 16, 2021
33 changes: 13 additions & 20 deletions class/class-wp-scss.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,28 @@
use ScssPhp\ScssPhp\Compiler;

class Wp_Scss {
/**
* Compiling preferences properites
*
* @var string
* @access private
*/
private $scss_dir, $css_dir, $compile_method, $scssc, $compile_errors, $sourcemaps, $cache;

/**
* Set values for Wp_Scss::properties
*
* @param string scss_dir - path to source directory for scss files
* @param string css_dir - path to output directory for css files
* @param string method - type of compile (compressed, expanded, etc)
* @param string compile_method - type of compile (compressed or expanded)
*
* @var object scssc - instantiate the compiling object.
*
* @var array compile_errors - catches errors from compile
*/
public function __construct ($scss_dir, $css_dir, $compile_method, $sourcemaps) {

$this->scss_dir = $scss_dir;
$this->css_dir = $css_dir;
$this->compile_method = $compile_method;
$this->compile_errors = array();
$this->scssc = new Compiler();

$this->cache = WPSCSS_PLUGIN_DIR . '/cache/';

$this->scssc->setFormatter( $compile_method );
$this->scssc->setOutputStyle( $compile_method );
$this->scssc->setImportPaths( $this->scss_dir );

$this->sourcemaps = $sourcemaps;
Expand Down Expand Up @@ -89,7 +81,7 @@ public function get_compile_errors() {
* @access public
*/
public function compile() {

$input_files = array();

// Loop through directory and get .scss file that do not start with '_'
Expand All @@ -105,7 +97,7 @@ public function compile() {
$outputName = preg_replace("/\.[^$]*/", ".css", $scss_file);
$output = $this->css_dir . $outputName;

$this->compiler($input, $output, $this);
$this->compiler($input, $output);
}

if (count($this->compile_errors) < 1) {
Expand Down Expand Up @@ -141,38 +133,39 @@ public function compile() {
* Puts error in 'compile_errors' property
* @access public
*/
private function compiler($in, $out, $instance) {
private function compiler($in, $out) {

if (!file_exists($this->cache)) {
mkdir($this->cache, 0644);
}
if (is_writable($this->cache)) {
try {
$map = basename($out) . '.map';
$this->scssc->setSourceMap(constant('ScssPhp\ScssPhp\Compiler::' . $instance->sourcemaps));
$this->scssc->setSourceMap(constant('ScssPhp\ScssPhp\Compiler::' . $this->sourcemaps));
$this->scssc->setSourceMapOptions(array(
'sourceMapWriteTo' => $instance->css_dir . $map, // absolute path to a file to write the map to
'sourceMapWriteTo' => $this->css_dir . $map, // absolute path to a file to write the map to
'sourceMapURL' => $map, // url of the map
'sourceMapBasepath' => rtrim(ABSPATH, '/'), // base path for filename normalization
'sourceRoot' => home_url('/'), // This value is prepended to the individual entries in the 'source' field.
));

$css = $this->scssc->compile(file_get_contents($in), $in);
$compilationResult = $this->scssc->compileString(file_get_contents($in), $in);
$css = $compilationResult->getCss();

file_put_contents($this->cache . basename($out), $css);
} catch (Exception $e) {
$errors = array (
'file' => basename($in),
'message' => $e->getMessage(),
);
array_push($instance->compile_errors, $errors);
array_push($this->compile_errors, $errors);
}
} else {
$errors = array (
'file' => $this->cache,
'message' => "File Permission Error, permission denied. Please make the cache directory writable."
);
array_push($instance->compile_errors, $errors);
array_push($this->compile_errors, $errors);
}
}

Expand Down Expand Up @@ -283,6 +276,6 @@ public function enqueue_files($base_folder_path, $css_folder) {

public function set_variables(array $variables) {

$this->scssc->setVariables($variables);
$this->scssc->addVariables(array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables));
}
} // End Wp_Scss Class
8 changes: 2 additions & 6 deletions options.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,8 @@ public function page_init()
'name' => 'compiling_options',
'type' => apply_filters( 'wp_scss_compiling_modes',
array(
'ScssPhp\ScssPhp\Formatter\Expanded' => 'Expanded',
'ScssPhp\ScssPhp\Formatter\Nested' => 'Nested',
'ScssPhp\ScssPhp\Formatter\Compressed' => 'Compressed',
'ScssPhp\ScssPhp\Formatter\Compact' => 'Compact',
'ScssPhp\ScssPhp\Formatter\Crunched' => 'Crunched',
'ScssPhp\ScssPhp\Formatter\Debug' => 'Debug'
'compressed' => 'Compressed',
'expanded' => 'Expanded',
shadoath marked this conversation as resolved.
Show resolved Hide resolved
)
)
)
Expand Down
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ Ideally you should setup a scss folder and a css folder within your theme. This

#### Compiling Mode

Compiling comes in five modes:
Compiling comes in two modes:

- Compressed - More compressed css. Entire rule block on one line. No indentation.
- Expanded - Full open css. One line per property. Brackets close on their own line.

**Removed** compiling modes

- Nested - Lightly compressed css. Brackets close with css block. Indents to match scss nesting.
- Compressed - More compressed css. Entire rule block on one line. No indentation.
- Compact - Removes all line breaks, unnecessary whitespace, and single-line comments.
- Crunched - Same as Compressed, but also removes multi-line comments.

Expand Down
10 changes: 7 additions & 3 deletions wp-scss.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ function wpscss_plugin_action_links($links, $file) {

add_filter('option_wpscss_options', 'wpscss_plugin_db_cleanup');
function wpscss_plugin_db_cleanup($option_values){
$option_values['compiling_options'] = str_replace("Leafo", "ScssPhp", $option_values['compiling_options']);
$compiling_options = str_replace("Leafo", "ScssPhp", $option_values['compiling_options']);
$compiling_options = str_replace("ScssPhp\\ScssPhp\\Formatter\\", "", $compiling_options);
$compiling_options = str_replace(["Compact", "Crunched"], "compressed", $compiling_options);
$compiling_options = str_replace("Nested", "expanded", $compiling_options);
$compiling_options = strtolower($compiling_options);
$option_values['compiling_options'] = $compiling_options;
return $option_values;
}

Expand Down Expand Up @@ -161,7 +166,7 @@ function wpscss_settings_error() {
$wpscss_settings = array(
'scss_dir' => $base_compiling_folder . $scss_dir_setting,
'css_dir' => $base_compiling_folder . $css_dir_setting,
'compiling' => isset($wpscss_options['compiling_options']) ? $wpscss_options['compiling_options'] : 'ScssPhp\ScssPhp\Formatter\Expanded',
'compiling' => isset($wpscss_options['compiling_options']) ? $wpscss_options['compiling_options'] : 'compressed',
'always_recompile' => isset($wpscss_options['always_recompile']) ? $wpscss_options['always_recompile'] : false,
'errors' => isset($wpscss_options['errors']) ? $wpscss_options['errors'] : 'show',
'sourcemaps' => isset($wpscss_options['sourcemap_options']) ? $wpscss_options['sourcemap_options'] : 'SOURCE_MAP_NONE',
Expand Down Expand Up @@ -194,7 +199,6 @@ function wp_scss_needs_compiling() {
wpscss_handle_errors();
}
}

add_action('wp_loaded', 'wp_scss_needs_compiling');

function wp_scss_compile() {
Expand Down