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