Pressbooks\Modules\Export\Epub\Epub201::scrapeKneadAndSaveCss PHP Method

scrapeKneadAndSaveCss() protected method

Parse CSS, copy assets, rewrite copy.
protected scrapeKneadAndSaveCss ( string $path_to_original_stylesheet, string $path_to_copy_of_stylesheet )
$path_to_original_stylesheet string *
$path_to_copy_of_stylesheet string
    protected function scrapeKneadAndSaveCss($path_to_original_stylesheet, $path_to_copy_of_stylesheet)
    {
        $sass = Container::get('Sass');
        $scss_dir = pathinfo($path_to_original_stylesheet, PATHINFO_DIRNAME);
        $path_to_epub_assets = $this->tmpDir . '/OEBPS/assets';
        $scss = file_get_contents($path_to_copy_of_stylesheet);
        if ($this->extraCss) {
            $scss .= "\n" . $this->loadTemplate($this->extraCss);
        }
        $scss = $sass->applyOverrides($scss, $this->cssOverrides);
        if ($sass->isCurrentThemeCompatible(1)) {
            $css = $sass->compile($scss, [$sass->pathToUserGeneratedSass(), $sass->pathToPartials(), $sass->pathToFonts(), get_stylesheet_directory()]);
        } elseif ($sass->isCurrentThemeCompatible(2)) {
            $css = $sass->compile($scss, $sass->defaultIncludePaths('epub'));
        } else {
            $css = static::injectHouseStyles($scss);
        }
        // Search for url("*"), url('*'), and url(*)
        $url_regex = '/url\\(([\\s])?([\\"|\'])?(.*?)([\\"|\'])?([\\s])?\\)/i';
        $css = preg_replace_callback($url_regex, function ($matches) use($scss_dir, $path_to_epub_assets) {
            $url = $matches[3];
            $filename = sanitize_file_name(basename($url));
            if (preg_match('#^images/#', $url) && substr_count($url, '/') == 1) {
                // Look for "^images/"
                // Count 1 slash so that we don't touch stuff like "^images/out/of/bounds/"	or "^images/../../denied/"
                $my_image = realpath("{$scss_dir}/{$url}");
                if ($my_image) {
                    copy($my_image, "{$path_to_epub_assets}/{$filename}");
                    return "url(assets/{$filename})";
                }
            } elseif (preg_match('#^../../images/epub/#', $url) && substr_count($url, '/') == 4) {
                // Look for "^../../images/epub/"
                // Count 4 slashes so that we explicitly select the path to the new assets directory
                $my_image = realpath("{$scss_dir}/{$url}");
                if ($my_image) {
                    copy($my_image, "{$path_to_epub_assets}/{$filename}");
                    return "url(assets/{$filename})";
                }
            } elseif (preg_match('#^https?://#i', $url) && preg_match('/(' . $this->supportedImageExtensions . ')$/i', $url)) {
                // Look for images via http(s), pull them in locally
                if ($new_filename = $this->fetchAndSaveUniqueImage($url, $path_to_epub_assets)) {
                    return "url(assets/{$new_filename})";
                }
            } elseif (preg_match('#^themes-book/pressbooks-book/fonts/[a-zA-Z0-9_-]+(' . $this->supportedFontExtensions . ')$#i', $url)) {
                // Look for themes-book/pressbooks-book/fonts/*.ttf (or .otf), copy into our Epub
                $my_font = realpath(PB_PLUGIN_DIR . $url);
                if ($my_font) {
                    copy($my_font, "{$path_to_epub_assets}/{$filename}");
                    return "url(assets/{$filename})";
                }
            } elseif (preg_match('#^https?://#i', $url) && preg_match('/(' . $this->supportedFontExtensions . ')$/i', $url)) {
                // Look for fonts via http(s), pull them in locally
                if ($new_filename = $this->fetchAndSaveUniqueFont($url, $path_to_epub_assets)) {
                    return "url(assets/{$new_filename})";
                }
            }
            return $matches[0];
            // No change
        }, $css);
        // Overwrite the new file with new info
        file_put_contents($path_to_copy_of_stylesheet, $css);
        if (WP_DEBUG) {
            Container::get('Sass')->debug($css, $scss, 'epub');
        }
    }