function nebula_render_scss($specific_scss = null, $child = false)
{
    $override = apply_filters('pre_nebula_render_scss', false, $specific_scss, $child);
    if ($override !== false) {
        return $override;
    }
    if (nebula_option('nebula_scss', 'enabled') && (isset($_GET['sass']) || isset($_GET['scss']) || $_GET['settings-updated'] == 'true') && (is_dev() || is_client())) {
        $specific_scss = 'all';
    }
    $theme_directory = get_template_directory();
    $theme_directory_uri = get_template_directory_uri();
    if ($child) {
        $theme_directory = get_stylesheet_directory();
        $theme_directory_uri = get_stylesheet_directory_uri();
    }
    $stylesheets_directory = $theme_directory . '/stylesheets';
    $stylesheets_directory_uri = $theme_directory_uri . '/stylesheets';
    require_once get_template_directory() . '/includes/libs/scssphp/scss.inc.php';
    //SCSSPHP is a compiler for SCSS 3.x
    $scss = new \Leafo\ScssPhp\Compiler();
    $scss->addImportPath($stylesheets_directory . '/scss/partials/');
    if (nebula_option('nebula_minify_css', 'enabled') && !is_debug()) {
        $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed');
        //Minify CSS (while leaving "/*!" comments for WordPress).
    } else {
        $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compact');
        //Compact, but readable, CSS lines
        if (is_debug()) {
            $scss->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS);
            //Adds line number reference comments in the rendered CSS file for debugging.
        }
    }
    if (empty($specific_scss) || $specific_scss == 'all') {
        //Partials
        $latest_partial = 0;
        foreach (glob($stylesheets_directory . '/scss/partials/*') as $partial_file) {
            if (filemtime($partial_file) > $latest_partial) {
                $latest_partial = filemtime($partial_file);
            }
        }
        //Combine Developer Stylesheets
        if (nebula_option('nebula_dev_stylesheets')) {
            nebula_combine_dev_stylesheets($stylesheets_directory, $stylesheets_directory_uri);
        }
        //Compile each SCSS file
        foreach (glob($stylesheets_directory . '/scss/*.scss') as $file) {
            //@TODO "Nebula" 0: Change to glob_r() but will need to create subdirectories if they don't exist.
            $file_path_info = pathinfo($file);
            if (is_file($file) && $file_path_info['extension'] == 'scss' && $file_path_info['filename'][0] != '_') {
                //If file exists, and has .scss extension, and doesn't begin with "_".
                $file_counter++;
                $css_filepath = $file_path_info['filename'] == 'style' ? $theme_directory . '/style.css' : $stylesheets_directory . '/css/' . $file_path_info['filename'] . '.css';
                if (!file_exists($css_filepath) || filemtime($file) > filemtime($css_filepath) || $latest_partial > filemtime($css_filepath) || is_debug() || $specific_scss == 'all') {
                    //If .css file doesn't exist, or is older than .scss file (or any partial), or is debug mode, or forced
                    ini_set('memory_limit', '512M');
                    //Increase memory limit for this script. //@TODO "Nebula" 0: Is this the best thing to do here? Other options?
                    WP_Filesystem();
                    global $wp_filesystem;
                    $existing_css_contents = file_exists($css_filepath) ? $wp_filesystem->get_contents($css_filepath) : '';
                    if (!strpos(strtolower($existing_css_contents), 'scss disabled')) {
                        //If the correlating .css file doesn't contain a comment to prevent overwriting
                        $this_scss_contents = $wp_filesystem->get_contents($file);
                        //Copy SCSS file contents
                        $compiled_css = $scss->compile($this_scss_contents);
                        //Compile the SCSS
                        $enhanced_css = nebula_scss_variables($compiled_css);
                        //Compile server-side variables into SCSS
                        $wp_filesystem->put_contents($css_filepath, $enhanced_css);
                        //Save the rendered CSS.
                    }
                }
            }
        }
        if (!$child && is_child_theme()) {
            //If not in the second (child) pass, and is a child theme.
            nebula_render_scss($specific_scss, true);
            //Re-run on child theme stylesheets
        }
    } else {
        if (file_exists($specific_scss)) {
            //If $specific_scss is a filepath
            WP_Filesystem();
            global $wp_filesystem;
            $scss_contents = $wp_filesystem->get_contents($specific_scss);
            $compiled_css = $scss->compile($scss_contents);
            //Compile the SCSS
            $enhanced_css = nebula_scss_variables($compiled_css);
            //Compile server-side variables into SCSS
            $wp_filesystem->put_contents(str_replace('.scss', '.css', $specific_scss), $enhanced_css);
            //Save the rendered CSS in the same directory.
        } else {
            //If $scss_file is raw SCSS string
            $compiled_css = $scss->compile($specific_scss);
            return nebula_scss_variables($compiled_css);
            //Return the rendered CSS
        }
    }
}
Example #2
0
function nebula_render_scss($specific_scss = null)
{
    require_once get_template_directory() . '/includes/libs/scssphp/scss.inc.php';
    //SCSSPHP is a compiler for SCSS 3.x
    $scss = new \Leafo\ScssPhp\Compiler();
    $scss->addImportPath(get_template_directory() . '/stylesheets/scss/partials/');
    if (nebula_option('nebula_minify_css', 'enabled') && !is_debug()) {
        $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed');
        //Minify CSS (while leaving "/*!" comments for WordPress).
    } else {
        $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compact');
        //Compact, but readable, CSS lines
        if (is_debug()) {
            //$scss->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS); //Adds line number reference comments in the rendered CSS file for debugging. //@TODO: "Nebula" 0: This is broken!! This line was working at one point and has not been changed since... However, it's just choking up on login.scss and tinymce.scss and style.scss for some reason- it compiles others before that...
            //$scss->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS); //Using this one for testing...
        }
    }
    if (empty($specific_scss) || $specific_scss == 'all') {
        //Partials
        $latest_partial = 0;
        foreach (glob(get_template_directory() . '/stylesheets/scss/partials/*') as $partial_file) {
            if (filemtime($partial_file) > $latest_partial) {
                $latest_partial = filemtime($partial_file);
            }
        }
        //Combine Developer Stylesheets
        if (nebula_option('nebula_dev_stylesheets')) {
            $file_counter = 0;
            $partials = array('variables', 'mixins', 'helpers');
            $automation_warning = "/**** Warning: This is an automated file! Anything added to this file manually will be removed! ****/\r\n\r\n";
            $dev_stylesheet_files = glob(get_template_directory() . '/stylesheets/scss/dev/*css');
            $dev_scss_file = get_template_directory() . '/stylesheets/scss/dev.scss';
            if (!empty($dev_stylesheet_files) || strlen($dev_scss_file) > strlen($automation_warning) + 10) {
                //If there are dev SCSS (or CSS) files -or- if dev.scss needs to be reset
                file_put_contents(get_template_directory() . '/stylesheets/scss/dev.scss', $automation_warning);
                //Empty /stylesheets/scss/dev.scss
            }
            foreach ($dev_stylesheet_files as $file) {
                $file_path_info = pathinfo($file);
                if (is_file($file) && in_array($file_path_info['extension'], array('css', 'scss'))) {
                    $file_counter++;
                    //Include partials in dev.scss
                    if ($file_counter == 1) {
                        $import_partials = '';
                        foreach ($partials as $partial) {
                            $import_partials .= "@import '" . $partial . "';\r\n";
                        }
                        file_put_contents($dev_scss_file, $automation_warning . $import_partials . "\r\n");
                    }
                    $this_scss_contents = file_get_contents($file);
                    //Copy file contents
                    $empty_scss = $this_scss_contents == '' ? ' (empty)' : '';
                    $dev_scss_contents = file_get_contents(get_template_directory() . '/stylesheets/scss/dev.scss');
                    $dev_scss_contents .= "\r\n/* ==========================================================================\r\n   " . 'File #' . $file_counter . ': ' . get_template_directory_uri() . "/stylesheets/scss/dev/" . $file_path_info['filename'] . '.' . $file_path_info['extension'] . $empty_scss . "\r\n   ========================================================================== */\r\n\r\n" . $this_scss_contents . "\r\n\r\n/* End of " . $file_path_info['filename'] . '.' . $file_path_info['extension'] . " */\r\n\r\n\r\n";
                    file_put_contents(get_template_directory() . '/stylesheets/scss/dev.scss', $dev_scss_contents);
                }
            }
            if ($file_counter > 0) {
                add_action('wp_enqueue_scripts', 'enqueue_dev_styles');
                function enqueue_dev_styles()
                {
                    wp_enqueue_style('nebula-dev_styles', get_template_directory_uri() . '/stylesheets/css/dev.css?c=' . rand(1, 99999), array('nebula-main'), null);
                }
            }
        }
        //Compile each SCSS file
        foreach (glob(get_template_directory() . '/stylesheets/scss/*.scss') as $file) {
            //@TODO "Nebula" 0: Change to glob_r() but will need to create subdirectories if they don't exist.
            $file_path_info = pathinfo($file);
            if (is_file($file) && $file_path_info['extension'] == 'scss' && $file_path_info['filename'][0] != '_') {
                //If file exists, and has .scss extension, and doesn't begin with "_".
                $file_counter++;
                $css_filepath = $file_path_info['filename'] == 'style' ? get_template_directory() . '/style.css' : get_template_directory() . '/stylesheets/css/' . $file_path_info['filename'] . '.css';
                if (!file_exists($css_filepath) || filemtime($file) > filemtime($css_filepath) || $latest_partial > filemtime($css_filepath) || is_debug() || $specific_scss == 'all') {
                    //If .css file doesn't exist, or is older than .scss file (or any partial), or is debug mode, or forced
                    ini_set('memory_limit', '512M');
                    //Increase memory limit for this script. //@TODO "Nebula" 0: Is this the best thing to do here? Other options?
                    $existing_css_contents = file_exists($css_filepath) ? file_get_contents($css_filepath) : '';
                    if (!strpos(strtolower($existing_css_contents), 'scss disabled')) {
                        //If the correlating .css file doesn't contain a comment to prevent overwriting
                        $this_scss_contents = file_get_contents($file);
                        //Copy SCSS file contents
                        $compiled_css = $scss->compile($this_scss_contents);
                        //Compile the SCSS
                        $enhanced_css = nebula_scss_variables($compiled_css);
                        //Compile server-side variables into SCSS
                        file_put_contents($css_filepath, $enhanced_css);
                        //Save the rendered CSS
                    }
                }
            }
        }
    } else {
        if (file_exists($specific_scss)) {
            //If $specific_scss is a filepath
            $scss_contents = file_get_contents($specific_scss);
            //Copy SCSS file contents
            $compiled_css = $scss->compile($scss_contents);
            //Compile the SCSS
            $enhanced_css = nebula_scss_variables($compiled_css);
            //Compile server-side variables into SCSS
            file_put_contents(str_replace('.scss', '.css', $specific_scss), $enhanced_css);
            //Save the rendered CSS in the same directory
        } else {
            //If $scss_file is raw SCSS string
            $compiled_css = $scss->compile($specific_scss);
            return nebula_scss_variables($compiled_css);
            //Return the rendered CSS
        }
    }
}