class-kirki-output.php (6339B)
1 <?php 2 /** 3 * Handles CSS output for fields. 4 * 5 * @package Kirki 6 * @subpackage Controls 7 * @copyright Copyright (c) 2016, Aristeides Stathopoulos 8 * @license http://opensource.org/licenses/https://opensource.org/licenses/MIT 9 * @since 2.2.0 10 */ 11 12 if ( ! class_exists( 'Kirki_Output' ) ) { 13 14 /** 15 * Handles field CSS output. 16 */ 17 class Kirki_Output { 18 19 /** 20 * The Kirki configuration used in the field. 21 * 22 * @access protected 23 * @var string 24 */ 25 protected $config_id = 'global'; 26 27 /** 28 * The field's `output` argument. 29 * 30 * @access protected 31 * @var array 32 */ 33 protected $output = array(); 34 35 /** 36 * An array of the generated styles. 37 * 38 * @access protected 39 * @var array 40 */ 41 protected $styles = array(); 42 43 /** 44 * The value. 45 * 46 * @access protected 47 * @var string|array 48 */ 49 protected $value; 50 51 /** 52 * The class constructor. 53 * 54 * @access public 55 * @param string $config_id The config ID. 56 * @param array $output The output argument. 57 * @param string|array $value The value. 58 */ 59 public function __construct( $config_id, $output, $value ) { 60 61 $this->config_id = $config_id; 62 $this->value = $value; 63 $this->output = $output; 64 65 $this->parse_output(); 66 } 67 68 /** 69 * If we have a sanitize_callback defined, apply it to the value. 70 * 71 * @param array $output The output args. 72 * @param string|array $value The value. 73 * 74 * @return string|array 75 */ 76 protected function apply_sanitize_callback( $output, $value ) { 77 78 if ( isset( $output['sanitize_callback'] ) && null !== $output['sanitize_callback'] ) { 79 80 // If the sanitize_callback is invalid, return the value. 81 if ( ! is_callable( $output['sanitize_callback'] ) ) { 82 return $value; 83 } 84 return call_user_func( $output['sanitize_callback'], $this->value ); 85 } 86 87 return $value; 88 89 } 90 91 /** 92 * If we have a value_pattern defined, apply it to the value. 93 * 94 * @param array $output The output args. 95 * @param string|array $value The value. 96 * 97 * @return string|array 98 */ 99 protected function apply_value_pattern( $output, $value ) { 100 101 if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) ) { 102 if ( is_string( $output['value_pattern'] ) ) { 103 if(!is_array($value)){ 104 return str_replace( '$', $value, $output['value_pattern'] ); 105 } 106 107 foreach($value as $k => $v){ 108 if(is_string($v)){ 109 $output['value_pattern'] = preg_replace('/(\['.$k.'\])/', $v , $output['value_pattern']); 110 } 111 } 112 return $output['value_pattern']; 113 } 114 } 115 116 return $value; 117 118 } 119 120 /** 121 * Parses the output arguments. 122 * Calls the process_output method for each of them. 123 * 124 * @access protected 125 */ 126 protected function parse_output() { 127 foreach ( $this->output as $output ) { 128 $skip = false; 129 130 // Apply any sanitization callbacks defined. 131 $value = $this->apply_sanitize_callback( $output, $this->value ); 132 133 // Skip if value is empty. 134 if ( '' === $this->value ) { 135 $skip = true; 136 } 137 138 // No need to proceed this if the current value is the same as in the "exclude" value. 139 if ( isset( $output['exclude'] ) && false !== $output['exclude'] && is_array( $output['exclude'] ) ) { 140 foreach ( $output['exclude'] as $exclude ) { 141 if ( $skip ) { 142 continue; 143 } 144 145 // Skip if value is defined as excluded. 146 if ( $exclude === $value ) { 147 $skip = true; 148 } 149 } 150 } 151 if ( $skip ) { 152 continue; 153 } 154 155 // Apply any value patterns defined. 156 $value = $this->apply_value_pattern( $output, $value ); 157 158 if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { 159 $output['element'] = array_unique( $output['element'] ); 160 sort( $output['element'] ); 161 $output['element'] = implode( ',', $output['element'] ); 162 } 163 164 $value = $this->process_value( $value, $output ); 165 $this->process_output( $output, $value ); 166 } 167 } 168 169 /** 170 * Parses an output and creates the styles array for it. 171 * 172 * @access protected 173 * @param array $output The field output. 174 * @param string $value The value. 175 * 176 * @return void 177 */ 178 protected function process_output( $output, $value ) { 179 if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) { 180 return; 181 } 182 $output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global'; 183 $output['prefix'] = ( isset( $output['prefix'] ) ) ? $output['prefix'] : ''; 184 $output['units'] = ( isset( $output['units'] ) ) ? $output['units'] : ''; 185 $output['suffix'] = ( isset( $output['suffix'] ) ) ? $output['suffix'] : ''; 186 187 $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value . $output['units'] . $output['suffix']; 188 } 189 190 /** 191 * Some CSS properties are unique. 192 * We need to tweak the value to make everything works as expected. 193 * 194 * @access protected 195 * @param string $property The CSS property. 196 * @param string $value The value. 197 * 198 * @return array 199 */ 200 protected function process_property_value( $property, $value ) { 201 $properties = apply_filters( 'kirki/' . $this->config_id . '/output/property-classnames', array( 202 'font-family' => 'Kirki_Output_Property_Font_Family', 203 'background-image' => 'Kirki_Output_Property_Background_Image', 204 'background-position' => 'Kirki_Output_Property_Background_Position', 205 ) ); 206 if ( array_key_exists( $property, $properties ) ) { 207 $classname = $properties[ $property ]; 208 $obj = new $classname( $property, $value ); 209 return $obj->get_value(); 210 } 211 return $value; 212 } 213 214 /** 215 * Returns the value. 216 * 217 * @access protected 218 * @param string|array $value The value. 219 * @param array $output The field "output". 220 * @return string|array 221 */ 222 protected function process_value( $value, $output ) { 223 if ( isset( $output['property'] ) ) { 224 return $this->process_property_value( $output['property'], $value ); 225 } 226 return $value; 227 } 228 229 /** 230 * Exploses the private $styles property to the world 231 * 232 * @access protected 233 * @return array 234 */ 235 public function get_styles() { 236 return $this->styles; 237 } 238 } 239 }