preprocessors.php
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | /** |
| 4 | * CSS preprocessor registration. |
| 5 | * |
| 6 | * To add a new preprocessor (or replace an existing one), hook into the |
| 7 | * jetpack_custom_css_preprocessors filter and add an entry to the array |
| 8 | * that is passed in. |
| 9 | * |
| 10 | * Format is: |
| 11 | * $preprocessors[ UNIQUE_KEY ] => array( 'name' => 'Processor name', 'callback' => [processing function] ); |
| 12 | * |
| 13 | * The callback function accepts a single string argument (non-CSS markup) and returns a string (CSS). |
| 14 | * |
| 15 | * @param array $preprocessors The list of preprocessors added thus far. |
| 16 | * @return array |
| 17 | */ |
| 18 | function jetpack_register_css_preprocessors( $preprocessors ) { |
| 19 | $preprocessors['less'] = array( |
| 20 | 'name' => 'LESS', |
| 21 | 'callback' => 'jetpack_less_css_preprocess', |
| 22 | ); |
| 23 | |
| 24 | $preprocessors['sass'] = array( |
| 25 | 'name' => 'Sass (SCSS Syntax)', |
| 26 | 'callback' => 'jetpack_sass_css_preprocess', |
| 27 | ); |
| 28 | |
| 29 | return $preprocessors; |
| 30 | } |
| 31 | |
| 32 | add_filter( 'jetpack_custom_css_preprocessors', 'jetpack_register_css_preprocessors' ); |
| 33 | |
| 34 | /** |
| 35 | * Compile less prepocessors? |
| 36 | * |
| 37 | * @param string $less - less. |
| 38 | */ |
| 39 | function jetpack_less_css_preprocess( $less ) { |
| 40 | require_once __DIR__ . '/preprocessors/lessc.inc.php'; |
| 41 | |
| 42 | $compiler = new lessc(); |
| 43 | |
| 44 | // Don't try to load from the filesystem. |
| 45 | $compiler->setImportDir( array() ); |
| 46 | |
| 47 | try { |
| 48 | return $compiler->compile( $less ); |
| 49 | } catch ( Exception $e ) { |
| 50 | return $less; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Compile sass prepocessors? |
| 56 | * |
| 57 | * @param string $sass - sass. |
| 58 | */ |
| 59 | function jetpack_sass_css_preprocess( $sass ) { |
| 60 | $compiler = new ScssPhp\ScssPhp\Compiler(); |
| 61 | |
| 62 | // Don't try to load from the filesystem. |
| 63 | $compiler->setImportPaths( array() ); |
| 64 | |
| 65 | try { |
| 66 | return $compiler->compileString( $sass )->getCss(); |
| 67 | } catch ( Exception $e ) { |
| 68 | return $sass; |
| 69 | } |
| 70 | } |
| 71 |