ru-se.com

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

class-kirki-styles-frontend.php (6714B)


      1 <?php
      2 /**
      3  * Generates the styles for the frontend.
      4  * Handles the 'output' argument of fields.
      5  * Usage instructions on https://github.com/aristath/kirki/wiki/output
      6  *
      7  * @package     Kirki
      8  * @category    Core
      9  * @author      Aristeides Stathopoulos
     10  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
     11  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     12  * @since       1.0
     13  */
     14 
     15 // Exit if accessed directly.
     16 if ( ! defined( 'ABSPATH' ) ) {
     17 	exit;
     18 }
     19 
     20 if ( ! class_exists( 'Kirki_Styles_Frontend' ) ) {
     21 
     22 	/**
     23 	 * Applies field-generated styles to the frontend.
     24 	 */
     25 	class Kirki_Styles_Frontend {
     26 
     27 		/**
     28 		 * Whether we've already processed this or not.
     29 		 *
     30 		 * @access public
     31 		 * @var bool
     32 		 */
     33 		public $processed = false;
     34 
     35 		/**
     36 		 * The CSS array
     37 		 *
     38 		 * @access public
     39 		 * @var array
     40 		 */
     41 		public static $css_array = array();
     42 
     43 		/**
     44 		 * Set to true if you want to use the AJAX method.
     45 		 *
     46 		 * @access public
     47 		 * @var bool
     48 		 */
     49 		public static $ajax = false;
     50 
     51 		/**
     52 		 * Constructor
     53 		 *
     54 		 * @access public
     55 		 */
     56 		public function __construct() {
     57 
     58 			add_action( 'init', array( $this, 'init' ) );
     59 
     60 		}
     61 
     62 		/**
     63 		 * Init.
     64 		 *
     65 		 * @access public
     66 		 */
     67 		public function init() {
     68 
     69             if ( ! materialis_can_show_cached_value('materialis_google_fonts')) {
     70 				Kirki_Fonts_Google::get_instance();
     71             }
     72 
     73 			global $wp_customize;
     74 
     75 			$config   = apply_filters( 'kirki/config', array() );
     76 			$priority = 999;
     77 			if ( isset( $config['styles_priority'] ) ) {
     78 				$priority = absint( $config['styles_priority'] );
     79 			}
     80 
     81 			// Allow completely disabling Kirki CSS output.
     82 			if ( ( defined( 'KIRKI_NO_OUTPUT' ) && KIRKI_NO_OUTPUT ) || ( isset( $config['disable_output'] ) && true !== $config['disable_output'] ) ) {
     83 				return;
     84 			}
     85 
     86 			// If we are in the customizer, load CSS using inline-styles.
     87 			// If we are in the frontend AND self::$ajax is true, then load dynamic CSS using AJAX.
     88 			if ( ! $wp_customize && ( ( true === self::$ajax ) || ( isset( $config['inline_css'] ) && false === $config['inline_css'] ) ) ) {
     89 				add_action( 'wp_enqueue_scripts', array( $this, 'frontend_styles' ), $priority );
     90 				add_action( 'wp_ajax_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
     91 				add_action( 'wp_ajax_nopriv_kirki_dynamic_css', array( $this, 'ajax_dynamic_css' ) );
     92 			} else {
     93 				add_action( 'wp_enqueue_scripts', array( $this, 'inline_dynamic_css' ), $priority );
     94 			}
     95 		}
     96 
     97 		/**
     98 		 * Adds inline styles.
     99 		 *
    100 		 * @access public
    101 		 */
    102 		public function inline_dynamic_css() {
    103 			$configs = Kirki::$config;
    104 			if ( ! $this->processed ) {
    105 				foreach ( $configs as $config_id => $args ) {
    106                     
    107                     if ($config_id === 'global') {
    108                         continue;
    109                     }
    110                     
    111 					if ( isset( $args['disable_output'] ) && true === $args['disable_output'] ) {
    112 						continue;
    113 					}
    114                     
    115                     $styles = '';
    116                     
    117                     $cached_kirki_style_key = "materialis_cached_kirki_style_{$config_id}";
    118                     
    119                     if (materialis_can_show_cached_value($cached_kirki_style_key)) {
    120                         $styles = materialis_get_cached_value($cached_kirki_style_key, null);
    121                         if ($styles !== null) {
    122                             $styles = "/** cached kirki style */{$styles}";
    123                         }
    124                         
    125                     }
    126                     
    127                     if ( ! $styles) {
    128                         
    129 					$styles = self::loop_controls( $config_id );
    130 					$styles = apply_filters( 'kirki/' . $config_id . '/dynamic_css', $styles );
    131                         if ( ! materialis_is_customize_preview() && ! empty($styles)) {
    132                             materialis_cache_value($cached_kirki_style_key, $styles);
    133                         }
    134                     }
    135                     
    136                     
    137 					if ( ! empty( $styles ) ) {
    138 						wp_enqueue_style( 'kirki-styles-' . $config_id, trailingslashit( Kirki::$url ) . 'assets/css/kirki-styles.css', null, null );
    139 						wp_add_inline_style( 'kirki-styles-' . $config_id, $styles );
    140 					}
    141 				}
    142 				$this->processed = true;
    143 			}
    144 		}
    145 
    146 		/**
    147 		 * Get the dynamic-css.php file
    148 		 *
    149 		 * @access public
    150 		 */
    151 		public function ajax_dynamic_css() {
    152 			require wp_normalize_path( Kirki::$path . '/includes/dynamic-css.php' );
    153 			exit;
    154 		}
    155 
    156 		/**
    157 		 * Enqueues the ajax stylesheet.
    158 		 *
    159 		 * @access public
    160 		 */
    161 		public function frontend_styles() {
    162 			wp_enqueue_style( 'kirki-styles-php', admin_url( 'admin-ajax.php' ) . '?action=kirki_dynamic_css', null, null );
    163 		}
    164 
    165 		/**
    166 		 * Loop through all fields and create an array of style definitions.
    167 		 *
    168 		 * @static
    169 		 * @access public
    170 		 * @param string $config_id The configuration ID.
    171 		 */
    172 		public static function loop_controls( $config_id ) {
    173 
    174 			// Get an instance of the Kirki_Styles_Output_CSS class.
    175 			// This will make sure google fonts and backup fonts are loaded.
    176 			Kirki_Styles_Output_CSS::get_instance();
    177 
    178 			$fields = Kirki::$fields;
    179 			$css    = array();
    180 
    181 			// Early exit if no fields are found.
    182 			if ( empty( $fields ) ) {
    183 				return;
    184 			}
    185 
    186 			foreach ( $fields as $field ) {
    187 
    188 				// Only process fields that belong to $config_id.
    189 				if ( $config_id != $field['kirki_config'] ) {
    190 					continue;
    191 				}
    192 
    193 				// Only continue if field dependencies are met.
    194 				if ( ! empty( $field['required'] ) ) {
    195 					$valid = true;
    196 
    197 					foreach ( $field['required'] as $requirement ) {
    198 						if ( isset( $requirement['setting'] ) && isset( $requirement['value'] ) && isset( $requirement['operator'] ) ) {
    199 							$controller_value = Kirki_Values::get_value( $config_id, $requirement['setting'] );
    200 							if ( ! Kirki_Active_Callback::compare( $controller_value, $requirement['value'], $requirement['operator'] ) ) {
    201 								$valid = false;
    202 							}
    203 						}
    204 					}
    205 
    206 					if ( ! $valid ) {
    207 						continue;
    208 					}
    209 				}
    210 
    211 				// Only continue if $field['output'] is set.
    212 				if ( isset( $field['output'] ) && ! empty( $field['output'] ) && 'background' != $field['type'] ) {
    213                     $css  = Kirki_Helper::array_replace_recursive( $css, Kirki_Styles_Output_CSS::css( $field ) );
    214 
    215 					// Add the globals.
    216 					if ( isset( self::$css_array[ $config_id ] ) && ! empty( self::$css_array[ $config_id ] ) ) {
    217 						Kirki_Helper::array_replace_recursive( $css, self::$css_array[ $config_id ] );
    218 					}
    219 				}
    220 			}
    221 
    222 			$css = apply_filters( 'kirki/' . $config_id . '/styles', $css );
    223 
    224 			if ( is_array( $css ) ) {
    225 				return Kirki_Styles_Output_CSS::styles_parse( Kirki_Styles_Output_CSS::add_prefixes( $css ) );
    226 			}
    227 
    228 			return;
    229 
    230 		}
    231 	}
    232 }