/**
  * Constructor
  *
  * @param string                       $dir      Root directory to .scss files
  * @param string                       $cacheDir Cache directory
  * @param \Leafo\ScssPhp\Compiler|null $scss     SCSS compiler instance
  */
 public function __construct($dir, $cacheDir = null, $scss = null)
 {
     $this->dir = $dir;
     if (!isset($cacheDir)) {
         $cacheDir = $this->join($dir, 'scss_cache');
     }
     $this->cacheDir = $cacheDir;
     if (!is_dir($this->cacheDir)) {
         mkdir($this->cacheDir, 0755, true);
     }
     if (!isset($scss)) {
         $scss = new Leafo_ScssPhp_Compiler();
         $scss->setImportPaths($this->dir);
     }
     $this->scss = $scss;
     $this->showErrorsAsCSS = false;
 }
 /**
  * Hook into wp_enqueue_style to compile stylesheets
  */
 public function style_loader_src($src, $handle)
 {
     // Quick check for SCSS files
     if (strpos($src, 'scss') === false) {
         return $src;
     }
     $url = parse_url($src);
     $pathinfo = pathinfo($url['path']);
     // Detailed check for SCSS files
     if ($pathinfo['extension'] !== 'scss') {
         return $src;
     }
     // Convert site URLs to absolute paths
     $in = preg_replace('/^' . preg_quote(site_url(), '/') . '/i', '', $src);
     // Ignore SCSS from CDNs, other domains and relative paths
     if (preg_match('#^//#', $in) || strpos($in, '/') !== 0) {
         return $src;
     }
     // Create a complete path
     $in = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . $url['path'];
     // Check and make sure the file exists
     if (file_exists($in) === false) {
         array_push($this->errors, array('file' => basename($in), 'message' => 'Source file not found.'));
         return $src;
     }
     // Generate unique filename for output
     $outName = sha1($src) . '.css';
     $wp_upload_dir = wp_upload_dir();
     $outputDir = $wp_upload_dir['basedir'] . '/sassify/';
     $outputUrl = $wp_upload_dir['baseurl'] . '/sassify/' . $outName;
     // Create the output directory if it doesn't exisit
     if (is_dir($outputDir) === false) {
         if (wp_mkdir_p($outputDir) === false) {
             array_push($this->errors, array('file' => 'Cache Directory', 'message' => 'File Permissions Error, unable to create cache directory. Please make sure the Wordpress Uploads directory is writable.'));
             return $src;
         }
     }
     // Check that the output directory is writable
     if (is_writable($outputDir) === false) {
         array_push($this->errors, array('file' => 'Cache Directory', 'message' => 'File Permissions Error, permission denied. Please make the cache directory writable.'));
         return $src;
     }
     // Full output path
     $out = $outputDir . '/' . $outName;
     // Flag if a compile is required
     $compileRequired = $this->admin->get_setting('always_compile', false);
     // Retrieve cached filemtimes
     if (($filemtimes = get_transient('sassify_filemtimes')) === false) {
         $filemtimes = array();
     }
     // Check if compile is required based on file modification times
     if ($compileRequired === false) {
         if (isset($filemtimes[$out]) === false || $filemtimes[$out] < filemtime($in)) {
             $compileRequired = true;
         }
     }
     // Retrieve variables
     $variables = apply_filters('sassify_compiler_variables', array('template_directory_uri' => get_template_directory_uri(), 'stylesheet_directory_uri' => get_stylesheet_directory_uri()));
     // If variables have been updated then recompile
     if ($compileRequired === false) {
         $signature = sha1(serialize($variables));
         if ($signature !== get_transient('sassify_variables_signature')) {
             $compileRequired = true;
             set_transient('sassify_variables_signature', $signature);
         }
     }
     // Check if the stylesheet needs to be recompiled
     if ($compileRequired) {
         $compiler = new Leafo_ScssPhp_Compiler();
         $compiler->setFormatter($this->admin->get_setting('compiling_mode', 'Leafo_ScssPhp_Formatter_Expanded'));
         $compiler->setVariables($variables);
         $compiler->setImportPaths(dirname($in));
         try {
             // Compile the SCSS to CSS
             $css = $compiler->compile(file_get_contents($in));
         } catch (Exception $e) {
             array_push($this->errors, array('file' => basename($in), 'message' => $e->getMessage()));
             return $src;
         }
         // Transform relative paths so they still work correctly
         $css = preg_replace('#(url\\((?![\'"]?(?:https?:|/))[\'"]?)#miu', '$1' . dirname($url['path']) . '/', $css);
         // Save the CSS
         file_put_contents($out, $css);
         // Cache the filemtime for the destination file
         $filemtimes[$out] = filemtime($out);
         set_transient('sassify_filemtimes', $filemtimes);
     }
     // Build URL with query string
     return empty($url['query']) ? $outputUrl : $outputUrl . '?' . $url['query'];
 }