|
| 1 | +<?php |
| 2 | +defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 | + |
| 4 | +if ( defined( 'SCCSS_FILE' ) ) : |
| 5 | + add_action( 'wp_enqueue_scripts', 'rocket_cache_sccss', 1 ); |
| 6 | + add_action( 'update_option_sccss_settings', 'rocket_delete_sccss_cache_file' ); |
| 7 | + add_filter( 'rocket_cache_busting_filename', 'rocket_sccss_cache_busting_filename' ); |
| 8 | +endif; |
| 9 | + |
| 10 | +/** |
| 11 | + * Caches SCCSS code & remove the default enqueued URL |
| 12 | + * |
| 13 | + * @since 2.9 |
| 14 | + * @author Remy Perona |
| 15 | + */ |
| 16 | +function rocket_cache_sccss() { |
| 17 | + $sccss = rocket_get_cache_busting_paths( 'sccss.css', 'css' ); |
| 18 | + |
| 19 | + if ( ! file_exists( $sccss['filepath'] ) ) { |
| 20 | + rocket_sccss_create_cache_file( $sccss['bustingpath'], $sccss['filepath'] ); |
| 21 | + } |
| 22 | + |
| 23 | + wp_enqueue_style( 'scss', $sccss['url'] ); |
| 24 | + remove_action( 'wp_enqueue_scripts', 'sccss_register_style', 99 ); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Deletes & recreates cache for SCCSS code |
| 29 | + * |
| 30 | + * @since 2.9 |
| 31 | + * @author Remy Perona |
| 32 | + */ |
| 33 | +function rocket_delete_sccss_cache_file() { |
| 34 | + $sccss = rocket_get_cache_busting_paths( 'sccss.css', 'css' ); |
| 35 | + |
| 36 | + @unlink( $sccss['filepath'] ); |
| 37 | + rocket_sccss_create_cache_file( $sccss['bustingpath'], $sccss['filepath'] ); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Returns the filename for SCSSS cache file |
| 42 | + * |
| 43 | + * @since 2.9 |
| 44 | + * @author Remy Perona |
| 45 | + * |
| 46 | + * @param string $filename filename. |
| 47 | + * @return string filename |
| 48 | + */ |
| 49 | +function rocket_sccss_cache_busting_filename( $filename ) { |
| 50 | + if ( false !== strpos( $filename, 'sccss' ) ) { |
| 51 | + return 'sccss.css'; |
| 52 | + } |
| 53 | + |
| 54 | + return $filename; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Creates the cache file for SCCSS code |
| 59 | + * |
| 60 | + * @since 2.9 |
| 61 | + * @author Remy Perona |
| 62 | + * |
| 63 | + * @param string $cache_busting_path Path to the cache busting directory. |
| 64 | + * @param string $cache_sccss_filepath Path to the sccss cache file. |
| 65 | + */ |
| 66 | +function rocket_sccss_create_cache_file( $cache_busting_path, $cache_sccss_filepath ) { |
| 67 | + $options = get_option( 'sccss_settings' ); |
| 68 | + $raw_content = isset( $options['sccss-content'] ) ? $options['sccss-content'] : ''; |
| 69 | + $content = wp_kses( $raw_content, array( '\'', '\"' ) ); |
| 70 | + $content = str_replace( '>', '>', $content ); |
| 71 | + |
| 72 | + if ( ! is_dir( $cache_busting_path ) ) { |
| 73 | + rocket_mkdir_p( $cache_busting_path ); |
| 74 | + } |
| 75 | + |
| 76 | + rocket_put_content( $cache_sccss_filepath, $content ); |
| 77 | +} |
0 commit comments