ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-kirki-styles-customizer.php (6540B)


      1 <?php
      2 /**
      3  * Changes the styling of the customizer
      4  * based on the settings set using the kirki/config filter.
      5  * For documentation please see
      6  * https://github.com/aristath/kirki/wiki/Styling-the-Customizer
      7  *
      8  * @package     Kirki
      9  * @category    Core
     10  * @author      Aristeides Stathopoulos
     11  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
     12  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     13  * @since       1.0
     14  */
     15 
     16 // Exit if accessed directly.
     17 if ( ! defined( 'ABSPATH' ) ) {
     18 	exit;
     19 }
     20 
     21 if ( ! class_exists( 'Kirki_Styles_Customizer' ) ) {
     22 
     23 	/**
     24 	 * Adds styles to the customizer.
     25 	 */
     26 	class Kirki_Styles_Customizer {
     27 
     28 		/**
     29 		 * Background Color.
     30 		 *
     31 		 * @access public
     32 		 * @var string
     33 		 */
     34 		public $color_back = false;
     35 
     36 		/**
     37 		 * Font Color.
     38 		 *
     39 		 * @access public
     40 		 * @var string
     41 		 */
     42 		public $color_font = false;
     43 
     44 		/**
     45 		 * Accent Color.
     46 		 *
     47 		 * @access public
     48 		 * @var string
     49 		 */
     50 		public $color_accent;
     51 
     52 		/**
     53 		 * Border Color.
     54 		 *
     55 		 * @access public
     56 		 * @var string
     57 		 */
     58 		public $border_color;
     59 
     60 		/**
     61 		 * Buttons Color.
     62 		 *
     63 		 * @access public
     64 		 * @var string
     65 		 */
     66 		public $buttons_color;
     67 
     68 		/**
     69 		 * Controls Color.
     70 		 *
     71 		 * @access public
     72 		 * @var string
     73 		 */
     74 		public $controls_color;
     75 
     76 		/**
     77 		 * Arrows Color.
     78 		 *
     79 		 * @access public
     80 		 * @var string
     81 		 */
     82 		public $arrows_color;
     83 
     84 		/**
     85 		 * Accent text Color.
     86 		 *
     87 		 * @access public
     88 		 * @var string
     89 		 */
     90 		public $color_accent_text;
     91 
     92 		/**
     93 		 * Section Background Color.
     94 		 *
     95 		 * @access public
     96 		 * @var string
     97 		 */
     98 		public $section_background_color;
     99 
    100 		/**
    101 		 * Have we already processed styling?
    102 		 *
    103 		 * @access public
    104 		 * @var bool
    105 		 */
    106 		public $process = false;
    107 
    108 		/**
    109 		 * Constructor.
    110 		 *
    111 		 * @access public
    112 		 */
    113 		public function __construct() {
    114 			add_action( 'customize_controls_print_styles', array( $this, 'customizer_styles' ), 99 );
    115 		}
    116 
    117 		/**
    118 		 * Enqueue the stylesheets required.
    119 		 *
    120 		 * @access public
    121 		 */
    122         public function customizer_styles()
    123         {
    124             
    125             if ( ! apply_filters('materialis_load_bundled_version', true)) {
    126 				wp_enqueue_style( 'kirki-customizer-css', trailingslashit( Kirki::$url ) . 'assets/css/customizer.css', null );
    127 				wp_add_inline_style( 'kirki-customizer-css', $this->custom_css() );
    128 			}
    129         }
    130 
    131 		/**
    132 		 * Gets the colors used.
    133 		 *
    134 		 * @access public
    135 		 * @return null|void
    136 		 */
    137 		public function get_colors() {
    138 
    139 			$config = apply_filters( 'kirki/config', array() );
    140 
    141 			// No need to proceed if we haven't set any colors.
    142 			if ( ( ! isset( $config['color_back'] ) || ! $config['color_back'] ) && ( ! isset( $config['color_accent'] ) || ! $config['color_accent'] ) ) {
    143 				return;
    144 			}
    145 			// Set the $process to true.
    146 			$this->process = true;
    147 			// Calculate the accent color.
    148 			if ( isset( $config['color_accent'] ) ) {
    149 				$this->color_accent = Kirki_Color::sanitize_hex( $config['color_accent'] );
    150 			}
    151 
    152 			// Calculate the background & font colors.
    153 			if ( isset( $config['color_back'] ) ) {
    154 				$this->color_back = Kirki_Color::sanitize_hex( $config['color_back'] );
    155 				$this->color_font = ( 170 > Kirki_Color::get_brightness( $this->color_back ) ) ? '#f2f2f2' : '#222';
    156 			}
    157 
    158 			if ( $this->color_back ) {
    159 				$this->buttons_color = ( 170 > Kirki_Color::get_brightness( $this->color_back ) ) ? Kirki_Color::adjust_brightness( $this->color_back, 80 ) : Kirki_Color::adjust_brightness( $this->color_back, -80 );
    160 				$this->border_color             = ( 170 > Kirki_Color::get_brightness( $this->color_back ) ) ? 'rgba(255,255,255,.2)' : 'rgba(0,0,0,.2)';
    161 				$this->arrows_color             = ( 170 > Kirki_Color::get_brightness( $this->color_back ) ) ? Kirki_Color::adjust_brightness( $this->color_back, 120 ) : Kirki_Color::adjust_brightness( $this->color_back, -120 );
    162 				$this->section_background_color = Kirki_Color::mix_colors( $this->color_back, '#ffffff', 10 );
    163 			}
    164 			$this->controls_color           = ( ( 170 > Kirki_Color::get_brightness( $this->color_accent ) ) ) ? '#ffffff;' : '#333333';
    165 			$this->color_accent_text        = ( 170 > Kirki_Color::get_brightness( $this->color_accent ) ) ? Kirki_Color::adjust_brightness( $this->color_accent, 120 ) : Kirki_Color::adjust_brightness( $this->color_accent, -120 );
    166 
    167 		}
    168 
    169 
    170 		/**
    171 		 * Add custom CSS rules to the head, applying our custom styles.
    172 		 *
    173 		 * @access public
    174 		 */
    175 		public function custom_css() {
    176 
    177 			$this->get_colors();
    178 			if ( ! $this->process ) {
    179 				return;
    180 			}
    181 			$styles = $this->include_stylesheets();
    182 			$styles = $this->replace_placeholders( $styles );
    183 
    184 			return $styles;
    185 
    186 		}
    187 
    188 		/**
    189 		 * Replaces CSS placefolders with our settings.
    190 		 *
    191 		 * @access public
    192 		 * @param string $styles The CSS.
    193 		 * @return string
    194 		 */
    195 		public function replace_placeholders( $styles ) {
    196 
    197 			// Replace CSS placeholders with actual values.
    198 			$replacements = array(
    199 				'COLOR_BACK'               => $this->color_back,
    200 				'COLOR_ACCENT_TEXT'        => $this->color_accent_text,
    201 				'COLOR_ACCENT'             => $this->color_accent,
    202 				'BORDER_COLOR'             => $this->border_color,
    203 				'BUTTONS_COLOR'            => $this->buttons_color,
    204 				'COLOR_FONT'               => $this->color_font,
    205 				'CONTROLS_COLOR'           => $this->controls_color,
    206 				'ARROWS_COLOR'             => $this->arrows_color,
    207 				'SECTION_BACKGROUND_COLOR' => $this->section_background_color,
    208 			);
    209 			foreach ( $replacements as $placeholder => $replacement ) {
    210 				$styles = str_replace( $placeholder, $replacement, $styles );
    211 			}
    212 
    213 			return $styles;
    214 
    215 		}
    216 
    217 		/**
    218 		 * Get the stylesheets.
    219 		 *
    220 		 * @access public
    221 		 * @return string
    222 		 */
    223 		public function include_stylesheets() {
    224 
    225 			$config = apply_filters( 'kirki/config', array() );
    226 			$styles = '';
    227 
    228 			// Include the width CSS if necessary.
    229 			if ( isset( $config['width'] ) ) {
    230 				$path = wp_normalize_path( Kirki::$path . '/assets/css/customizer-dynamic-css-width.php' );
    231 				$styles .= include $path;
    232 
    233 				// Replace width placeholder with actual value.
    234 				$styles = str_replace( 'WIDTH', $config['width'], $styles );
    235 			}
    236 
    237 			// Include the color modifications CSS if necessary.
    238 			if ( false !== $this->color_back && false !== $this->color_font ) {
    239 				$path = wp_normalize_path( Kirki::$path . '/assets/css/customizer-dynamic-css-colors.php' );
    240 				$styles .= include $path;
    241 			}
    242 
    243 			// Include generic CSS for controls.
    244 			$path = wp_normalize_path( Kirki::$path . '/assets/css/customizer-dynamic-css.php' );
    245 			$styles .= include $path;
    246 
    247 			return $styles;
    248 
    249 		}
    250 	}
    251 }