ru-se.com

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

class-kirki-styles-output-css.php (6235B)


      1 <?php
      2 /**
      3  * Generates the styles for the frontend.
      4  * Handles the 'output' argument of fields
      5  *
      6  * @package     Kirki
      7  * @category    Core
      8  * @author      Aristeides Stathopoulos
      9  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
     10  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     11  * @since       1.0
     12  */
     13 
     14 // Exit if accessed directly.
     15 if ( ! defined( 'ABSPATH' ) ) {
     16 	exit;
     17 }
     18 
     19 if ( ! class_exists( 'Kirki_Styles_Output_CSS' ) ) {
     20 
     21 	/**
     22 	 * Handles CSS output.
     23 	 */
     24 	final class Kirki_Styles_Output_CSS {
     25 
     26 		/**
     27 		 * The instance of this class (singleton pattern).
     28 		 *
     29 		 * @static
     30 		 * @access public
     31 		 * @var null|object
     32 		 */
     33 		public static $instance = null;
     34 
     35 		/**
     36 		 * Settings.
     37 		 *
     38 		 * @static
     39 		 * @access public
     40 		 * @var null|string|array
     41 		 */
     42 		public static $settings    = null;
     43 
     44 		/**
     45 		 * Output.
     46 		 *
     47 		 * @static
     48 		 * @access public
     49 		 * @var array
     50 		 */
     51 		public static $output      = array();
     52 
     53 		/**
     54 		 * Callback.
     55 		 *
     56 		 * @static
     57 		 * @access public
     58 		 * @var null|string|array
     59 		 */
     60 		public static $callback    = null;
     61 
     62 		/**
     63 		 * Option Name.
     64 		 *
     65 		 * @static
     66 		 * @access public
     67 		 * @var null|string
     68 		 */
     69 		public static $option_name = null;
     70 
     71 		/**
     72 		 * Field Type.
     73 		 *
     74 		 * @static
     75 		 * @access public
     76 		 * @var string
     77 		 */
     78 		public static $field_type  = null;
     79 
     80 		/**
     81 		 * Google Fonts
     82 		 *
     83 		 * @static
     84 		 * @access public
     85 		 * @var array
     86 		 */
     87 		public static $google_fonts = null;
     88 
     89 		/**
     90 		 * Standard Fonts
     91 		 *
     92 		 * @static
     93 		 * @access public
     94 		 * @var array
     95 		 */
     96 		public static $backup_fonts = null;
     97 
     98 		/**
     99 		 * CSS
    100 		 *
    101 		 * @static
    102 		 * @access public
    103 		 * @var string
    104 		 */
    105 		public static $css;
    106 
    107 		/**
    108 		 * Value
    109 		 *
    110 		 * @static
    111 		 * @access public
    112 		 * @var mixed
    113 		 */
    114 		public static $value = null;
    115 
    116 		/**
    117 		 * The class constructor.
    118 		 */
    119 		private function __construct() {
    120 			if ( is_null( self::$google_fonts ) ) {
    121 				self::$google_fonts = Kirki_Fonts::get_google_fonts();
    122 			}
    123 			if ( is_null( self::$backup_fonts ) ) {
    124 				self::$backup_fonts = Kirki_Fonts::get_backup_fonts();
    125 			}
    126 		}
    127 
    128 		/**
    129 		 * Get a single instance of this class
    130 		 *
    131 		 * @return object
    132 		 */
    133 		public static function get_instance() {
    134 			if ( null === self::$instance ) {
    135 				self::$instance = new Kirki_Styles_Output_CSS();
    136 			}
    137 			return self::$instance;
    138 		}
    139 
    140 		/**
    141 		 * Get the CSS for a field.
    142 		 *
    143 		 * @static
    144 		 * @access public
    145 		 * @param array $field The field.
    146 		 * @return array
    147 		 */
    148 		public static function css( $field ) {
    149 
    150 			// Set class vars.
    151 			self::$settings   = $field['settings'];
    152 			self::$callback   = $field['sanitize_callback'];
    153 			self::$field_type = $field['type'];
    154 			self::$output     = $field['output'];
    155 			if ( ! is_array( self::$output ) ) {
    156 				self::$output = array(
    157 					array(
    158 						'element'           => self::$output,
    159 						'sanitize_callback' => null,
    160 					),
    161 				);
    162 			}
    163 
    164 			// Get the value of this field.
    165 			self::$value = Kirki_Values::get_sanitized_field_value( $field );
    166 
    167 			// Find the class that will handle the outpout for this field.
    168 			$classname = 'Kirki_Output';
    169 			$field_output_classes = apply_filters( 'kirki/' . $field['kirki_config'] . '/output/control-classnames', array(
    170 				'kirki-spacing'    => 'Kirki_Output_Field_Spacing',
    171 				'kirki-typography' => 'Kirki_Output_Field_Typography',
    172 				'kirki-multicolor' => 'Kirki_Output_Field_Multicolor',
    173 			) );
    174 			if ( array_key_exists( self::$field_type, $field_output_classes ) ) {
    175 				$classname = $field_output_classes[ self::$field_type ];
    176 			}
    177 			$obj = new $classname( $field['kirki_config'], self::$output, self::$value );
    178 
    179 			return $obj->get_styles();
    180 
    181 		}
    182 
    183 		/**
    184 		 * Gets the array of generated styles and creates the minimized, inline CSS.
    185 		 *
    186 		 * @static
    187 		 * @access public
    188 		 * @param array $css The CSS definitions array.
    189 		 * @return string    The generated CSS.
    190 		 */
    191 		public static function styles_parse( $css = array() ) {
    192 
    193 			// Pass our styles from the kirki/styles_array filter.
    194 			$css = apply_filters( 'kirki/styles_array', $css );
    195 
    196 			// Process the array of CSS properties and produce the final CSS.
    197 			$final_css = '';
    198 			if ( ! is_array( $css ) || empty( $css ) ) {
    199 				return '';
    200 			}
    201 			foreach ( $css as $media_query => $styles ) {
    202 				$final_css .= ( 'global' != $media_query ) ? $media_query . '{' : '';
    203 				foreach ( $styles as $style => $style_array ) {
    204 					$final_css .= $style . '{';
    205 						foreach ( $style_array as $property => $value ) {
    206 							$value = ( is_string( $value ) ) ? $value : '';
    207 							$final_css .= $property . ':' . $value . ';';
    208 						}
    209 					$final_css .= '}';
    210 				}
    211 				$final_css .= ( 'global' != $media_query ) ? '}' : '';
    212 			}
    213 			return $final_css;
    214 		}
    215 
    216 		/**
    217 		 * Add prefixes if necessary.
    218 		 *
    219 		 * @param  array $css The CSS definitions array.
    220 		 * @return array
    221 		 */
    222 		public static function add_prefixes( $css ) {
    223 
    224 			if ( is_array( $css ) ) {
    225 				foreach ( $css as $media_query => $elements ) {
    226 					foreach ( $elements as $element => $style_array ) {
    227 						foreach ( $style_array as $property => $value ) {
    228 
    229 							// Add -webkit-* and -moz-*.
    230 							if ( is_string( $property ) && in_array( $property, array(
    231 								'border-radius',
    232 								'box-shadow',
    233 								'box-sizing',
    234 								'text-shadow',
    235 								'transform',
    236 								'background-size',
    237 								'transition',
    238 								'transition-property',
    239 							) ) ) {
    240 								unset( $css[ $media_query ][ $element ][ $property ] );
    241 								$css[ $media_query ][ $element ][ '-webkit-' . $property ] = $value;
    242 								$css[ $media_query ][ $element ][ '-moz-' . $property ]    = $value;
    243 								$css[ $media_query ][ $element ][ $property ]              = $value;
    244 							}
    245 
    246 							// Add -ms-* and -o-*.
    247 							if ( is_string( $property ) && in_array( $property, array(
    248 								'transform',
    249 								'background-size',
    250 								'transition',
    251 								'transition-property',
    252 							) ) ) {
    253 								unset( $css[ $media_query ][ $element ][ $property ] );
    254 								$css[ $media_query ][ $element ][ '-ms-' . $property ] = $value;
    255 								$css[ $media_query ][ $element ][ '-o-' . $property ]  = $value;
    256 								$css[ $media_query ][ $element ][ $property ]          = $value;
    257 							}
    258 						}
    259 					}
    260 				}
    261 			}
    262 
    263 			return $css;
    264 
    265 		}
    266 	}
    267 }