Skip to content

Commit

Permalink
Merge pull request #190 from niaccurshi/master
Browse files Browse the repository at this point in the history
Allow compile() from outside the plugin
  • Loading branch information
shadoath authored May 18, 2021
2 parents 3edb476 + 1bd386e commit e1d4950
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 88 deletions.
199 changes: 118 additions & 81 deletions class/class-wp-scss.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Wp_Scss {
* Compiling preferences properites
*
* @var string
* @access public
* @access private
*/
public $scss_dir, $css_dir, $compile_method, $scssc, $compile_errors, $sourcemaps;
private $scss_dir, $css_dir, $compile_method, $scssc, $compile_errors, $sourcemaps, $cache;

/**
* Set values for Wp_Scss::properties
Expand All @@ -25,27 +25,62 @@ class Wp_Scss {
* @var array compile_errors - catches errors from compile
*/
public function __construct ($scss_dir, $css_dir, $compile_method, $sourcemaps) {
global $scssc;

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

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

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

$this->sourcemaps = $sourcemaps;
}

/**
* METHOD GET SCSS DIRECTORY
* Returns the stored SCSS directory for this compile instance
*
* @return string - Value of the SCSS directory
*
* @access public
*/
public function get_scss_dir() {
return $this->scss_dir;
}

/**
* METHOD GET CSS DIRECTORY
* Returns the stored CSS directory for this compile instance
*
* @return string - Value of the CSS directory
*
* @access public
*/
public function get_css_dir() {
return $this->css_dir;
}

/**
* METHOD GET CSS DIRECTORY
* Returns the stored CSS directory for this compile instance
*
* @return array - List of errors from the compile process, if any
*
* @access public
*/
public function get_compile_errors() {
return $this->compile_errors;
}

/**
* METHOD COMPILE
* Loops through scss directory and compilers files that end
* with .scss and do not have '_' in front.
*
* @function compiler - passes input content through scssphp,
* puts compiled css into cache file
*
* @var array input_files - array of .scss files with no '_' in front
* @var array sdir_arr - an array of all the files in the scss directory
*
Expand All @@ -54,91 +89,93 @@ public function __construct ($scss_dir, $css_dir, $compile_method, $sourcemaps)
* @access public
*/
public function compile() {
global $scssc, $cache;
$cache = WPSCSS_PLUGIN_DIR . '/cache/';

$input_files = array();

// Loop through directory and get .scss file that do not start with '_'
foreach(new DirectoryIterator($this->scss_dir) as $file) {
if (substr($file, 0, 1) != "_" && pathinfo($file->getFilename(), PATHINFO_EXTENSION) == 'scss') {
array_push($input_files, $file->getFilename());
}
}

//Compiler - Takes scss $in and writes compiled css to $out file
// catches errors and puts them the object's compiled_errors property
if (!function_exists( 'compiler' )) {
function compiler($in, $out, $instance) {
global $scssc, $cache;
// For each input file, find matching css file and compile
foreach ($input_files as $scss_file) {
$input = $this->scss_dir . $scss_file;
$outputName = preg_replace("/\.[^$]*/", ".css", $scss_file);
$output = $this->css_dir . $outputName;

if (!file_exists($cache)) {
mkdir($cache, 0644);
}
if (is_writable($cache)) {
try {
$map = basename($out) . '.map';
$scssc->setSourceMap(constant('ScssPhp\ScssPhp\Compiler::' . $instance->sourcemaps));
$scssc->setSourceMapOptions(array(
'sourceMapWriteTo' => $instance->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 = $scssc->compile(file_get_contents($in), $in);

file_put_contents($cache.basename($out), $css);
} catch (Exception $e) {
$errors = array (
'file' => basename($in),
'message' => $e->getMessage(),
);
array_push($instance->compile_errors, $errors);
$this->compiler($input, $output, $this);
}

if (count($this->compile_errors) < 1) {
if ( is_writable($this->css_dir) ) {
foreach (new DirectoryIterator($this->cache) as $this->cache_file) {
if ( pathinfo($this->cache_file->getFilename(), PATHINFO_EXTENSION) == 'css') {
file_put_contents($this->css_dir . $this->cache_file, file_get_contents($this->cache . $this->cache_file));
unlink($this->cache . $this->cache_file->getFilename()); // Delete file on successful write
}
} else {
$errors = array (
'file' => $cache,
'message' => "File Permission Error, permission denied. Please make the cache directory writable."
);
array_push($instance->compile_errors, $errors);
}
} else {
$errors = array(
'file' => 'CSS Directory',
'message' => "File Permissions Error, permission denied. Please make your CSS directory writable."
);
array_push($this->compile_errors, $errors);
}
}
}

$input_files = array();
// Loop through directory and get .scss file that do not start with '_'
foreach(new DirectoryIterator($this->scss_dir) as $file) {
if (substr($file, 0, 1) != "_" && pathinfo($file->getFilename(), PATHINFO_EXTENSION) == 'scss') {
array_push($input_files, $file->getFilename());
}
}
/**
* METHOD COMPILER
* Takes scss $in and writes compiled css to $out file
* catches errors and puts them the object's compiled_errors property
*
* @function compiler - passes input content through scssphp,
* puts compiled css into cache file
*
* @var array input_files - array of .scss files with no '_' in front
* @var array sdir_arr - an array of all the files in the scss directory
*
* @return nothing - Puts successfully compiled css into appropriate location
* Puts error in 'compile_errors' property
* @access public
*/
private function compiler($in, $out, $instance) {

// For each input file, find matching css file and compile
foreach ($input_files as $scss_file) {
$input = $this->scss_dir.$scss_file;
$outputName = preg_replace("/\.[^$]*/",".css", $scss_file);
$output = $this->css_dir.$outputName;
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->setSourceMapOptions(array(
'sourceMapWriteTo' => $instance->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.
));

compiler($input, $output, $this);
}
$css = $this->scssc->compile(file_get_contents($in), $in);

if (count($this->compile_errors) < 1) {
if ( is_writable($this->css_dir) ) {
foreach (new DirectoryIterator($cache) as $cache_file) {
if ( pathinfo($cache_file->getFilename(), PATHINFO_EXTENSION) == 'css') {
file_put_contents($this->css_dir.$cache_file, file_get_contents($cache.$cache_file));
unlink($cache.$cache_file->getFilename()); // Delete file on successful write
}
}
} else {
$errors = array(
'file' => 'CSS Directory',
'message' => "File Permissions Error, permission denied. Please make your CSS directory writable."
);
array_push($this->compile_errors, $errors);
}
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);
}
}else{
} else {
$errors = array (
'file' => 'SCSS compiler',
'message' => "Compiling Error, function 'compiler' already exists."
'file' => $this->cache,
'message' => "File Permission Error, permission denied. Please make the cache directory writable."
);
array_push($this->compile_errors, $errors);
array_push($instance->compile_errors, $errors);
}
}


/**
* METHOD NEEDS_COMPILING
* Gets the most recently modified file in the scss directory
Expand Down Expand Up @@ -245,7 +282,7 @@ public function enqueue_files($base_folder_path, $css_folder) {
}

public function set_variables(array $variables) {
global $scssc;
$scssc->setVariables($variables);

$this->scssc->setVariables($variables);
}
} // End Wp_Scss Class
16 changes: 9 additions & 7 deletions wp-scss.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function wp_scss_compile() {
* half of entries in the file.
*/

$log_file = $wpscss_compiler->scss_dir.'error_log.log';
$log_file = $wpscss_compiler->get_scss_dir() . 'error_log.log';

function wpscss_error_styles() {
echo
Expand Down Expand Up @@ -266,15 +266,17 @@ function wpscss_settings_show_errors($errors) {
function wpscss_handle_errors() {
global $wpscss_settings, $log_file, $wpscss_compiler;
// Show to logged in users: All the methods for checking user login are set up later in the WP flow, so this only checks that there is a cookie
if ( !is_admin() && $wpscss_settings['errors'] === 'show-logged-in' && !empty($_COOKIE[LOGGED_IN_COOKIE]) && count($wpscss_compiler->compile_errors) > 0) {
wpscss_settings_show_errors($wpscss_compiler->compile_errors);

$compile_errors = $wpscss_compiler->get_compile_errors();
if ( !is_admin() && $wpscss_settings['errors'] === 'show-logged-in' && !empty($_COOKIE[LOGGED_IN_COOKIE]) && count($compile_errors) > 0) {
wpscss_settings_show_errors($compile_errors);
// Show in the header to anyone
} else if ( !is_admin() && $wpscss_settings['errors'] === 'show' && count($wpscss_compiler->compile_errors) > 0) {
wpscss_settings_show_errors($wpscss_compiler->compile_errors);
} else if ( !is_admin() && $wpscss_settings['errors'] === 'show' && count($compile_errors) > 0) {
wpscss_settings_show_errors($compile_errors);
} else { // Hide errors and print them to a log file.
foreach ($wpscss_compiler->compile_errors as $error) {
foreach ($compile_errors as $error) {
$error_string = date('m/d/y g:i:s', time()) .': ';
$error_string .= $error['file'] .' - '. $error['message'] . PHP_EOL;
$error_string .= $error['file'] . ' - ' . $error['message'] . PHP_EOL;
file_put_contents($log_file, $error_string, FILE_APPEND);
$error_string = "";
}
Expand Down

0 comments on commit e1d4950

Please sign in to comment.