Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. /**
  2.  * Implement the Sass compiler in PHP
  3.  * https://github.com/leafo/scssphp
  4.  * This requires PHP 5.3 or higher because it uses namespacing.
  5.  */
  6. require get_template_directory() . '/inc/scssphp/scss.inc.php';
  7. use Leafo\ScssPhp\Compiler;
  8.  
  9. /* First, add the controls to the Customizer itself. */
  10. function sass_customizer( $wp_customize ) {
  11.     $wp_customize->add_setting( 'color_scheme', array(
  12.         'default'   => '#16dff9',
  13.         'transport' => 'postMessage'
  14.     ) );
  15.    
  16.     $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'color_scheme', array(
  17.         'label'       => 'Sass color picker',
  18.         'section'     => 'colors'
  19.     ) ) );
  20. }
  21. add_action( 'customize_register', 'sass_customizer' );
  22.  
  23. /* This function runs after the customizer options are saved, generating the appropriate stylesheet. */
  24. function compile_sass() {
  25.     $scss = new Compiler();
  26.     $base_color = get_theme_mod( 'color_scheme' );
  27.    
  28.     $compiler_string = '$base-color: %1$s; a { color: $base-color; &:hover { color: darken( $base-color, 15 ); } };';
  29.     $compiler_string = sprintf( $compiler_string, $base_color );
  30.    
  31.     $compiled = $scss->compile( $compiler_string );
  32.    
  33.     $file = fopen( get_template_directory() . '/css/customizer.css', 'a+' );
  34.     if ( $file ) {
  35.         fwrite( $file, $compiled );
  36.         fclose( $file );
  37.     }
  38. }
  39. add_action( 'customize_save_after', 'compile_sass' );
  40.  
  41. /* Then, load the newly-created stylesheet. */
  42. function load_customizer_css() {
  43.     wp_enqueue_style( 'customizer-css', get_template_directory_uri() . '/css/customizer.css' );
  44. }
  45. add_action( 'wp_enqueue_scripts', 'load_customizer_css' );
  46.  
  47. /* Alternatively, write the CSS directly to the header. */
  48. function write_css_to_head() {
  49.     $scss = new Compiler();
  50.     $base_color = get_theme_mod( 'color_scheme' );
  51.    
  52.     $compiler_string = '$base-color: %1$s; a { color: $base-color; &:hover { color: darken( $base-color, 15 ); } };';
  53.     $compiler_string = sprintf( $compiler_string, $base_color );
  54.    
  55.     $compiled = $scss->compile( $compiler_string );
  56.  
  57.     echo '<style type="text/css">' . $compiled . '</style>';
  58. }
  59. add_action( 'wp_head', 'write_css_to_head' );
  60. /* End implementation of the Sass compiler in PHP. This is only a test. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement