ru-se.com

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

class-kirki-config.php (4060B)


      1 <?php
      2 /**
      3  * Processes configurations.
      4  *
      5  * @package     Kirki
      6  * @category    Core
      7  * @author      Aristeides Stathopoulos
      8  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
      9  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     10  */
     11 
     12 if ( ! class_exists( 'Kirki_Config' ) ) {
     13 
     14 	/**
     15 	 * The Kirki_Config object
     16 	 */
     17 	final class Kirki_Config {
     18 
     19 		/**
     20 		 * Each instance is stored separately in this array.
     21 		 *
     22 		 * @static
     23 		 * @access private
     24 		 * @var array
     25 		 */
     26 		private static $instances = array();
     27 
     28 		/**
     29 		 * The finalized configuration array.
     30 		 *
     31 		 * @access protected
     32 		 * @var array
     33 		 */
     34 		protected $config_final = array();
     35 
     36 		/**
     37 		 * The configuration ID.
     38 		 *
     39 		 * @access protected
     40 		 * @var string
     41 		 */
     42 		protected $id = 'global';
     43 
     44 		/**
     45 		 * Capability (fields will inherit this).
     46 		 *
     47 		 * @access protected
     48 		 * @var string
     49 		 */
     50 		protected $capability = 'edit_theme_options';
     51 
     52 		/**
     53 		 * The data-type we'll be using.
     54 		 *
     55 		 * @access protected
     56 		 * @var string
     57 		 */
     58 		protected $option_type = 'theme_mod';
     59 
     60 		/**
     61 		 * If we're using serialized options, then this is the global option name.
     62 		 *
     63 		 * @access protected
     64 		 * @var string
     65 		 */
     66 		protected $option_name = '';
     67 
     68 		/**
     69 		 * The compiler.
     70 		 *
     71 		 * @access protected
     72 		 * @var array
     73 		 */
     74 		protected $compiler = array();
     75 
     76 		/**
     77 		 * Set to true if you want to completely disable any Kirki-generated CSS.
     78 		 *
     79 		 * @access protected
     80 		 * @var bool
     81 		 */
     82 		protected $disable_output = false;
     83 
     84 		/**
     85 		 * The class constructor.
     86 		 * Use the get_instance() static method to get the instance you need.
     87 		 *
     88 		 * @access private
     89 		 *
     90 		 * @param string $id     @see Kirki_Config::get_instance().
     91 		 * @param array  $args   @see Kirki_Config::get_instance().
     92 		 */
     93 		private function __construct( $id = 'global', $args = array() ) {
     94 
     95 			// Get defaults from the class.
     96 			$defaults = get_class_vars( __CLASS__ );
     97 			// Skip the what we don't need in this context.
     98 			unset( $defaults['config_final'] );
     99 			unset( $defaults['instances'] );
    100 			// Apply any kirki/config global filters.
    101 			$defaults = apply_filters( 'kirki/config', $defaults );
    102 			// Merge our args with the defaults.
    103 			$args = wp_parse_args( $args, $defaults );
    104 
    105 			// Modify default values with the defined ones.
    106 			foreach ( $args as $key => $value ) {
    107 				// Is this property whitelisted?
    108 				if ( property_exists( $this, $key ) ) {
    109 					$args[ $key ] = $value;
    110 				}
    111 			}
    112 
    113 			$this->config_final       = $args;
    114 			$this->config_final['id'] = $id;
    115 
    116 		}
    117 
    118 		/**
    119 		 * Use this method to get an instance of your config.
    120 		 * Each config has its own instance of this object.
    121 		 *
    122 		 * @static
    123 		 * @access public
    124 		 * @param string $id     Config ID.
    125 		 * @param array  $args   {
    126 		 * Optional. Arguments to override config defaults.
    127 		 *
    128 		 *    @type string      $capability       @see https://codex.wordpress.org/Roles_and_Capabilities
    129 		 *    @type string      $option_type      theme_mod or option.
    130 		 *    @type string      $option_name      If we want to used serialized options,
    131 		 *                                        this is where we'll be adding the option name.
    132 		 *                                        All fields using this config will be items in that array.
    133 		 *    @type array       $compiler         Not yet fully implemented
    134 		 *    @type bool        $disable_output   If set to true, no CSS will be generated
    135 		 *                                        from fields using this configuration.
    136 		 * }
    137 		 *
    138 		 * @return Kirki_Config
    139 		 */
    140 		public static function get_instance( $id = 'global', $args = array() ) {
    141 
    142 			$id = trim( esc_attr( $id ) );
    143 			$id = ( '' === $id ) ? 'global' : $id;
    144 
    145 			$id_md5 = md5( $id );
    146 			if ( ! isset( self::$instances[ $id_md5 ] ) ) {
    147 				self::$instances[ $id_md5 ] = new self( $id, $args );
    148 			}
    149 			return self::$instances[ $id_md5 ];
    150 
    151 		}
    152 
    153 		/**
    154 		 * Returns the $config_final property
    155 		 *
    156 		 * @access public
    157 		 *
    158 		 * @return array
    159 		 */
    160 		public function get_config() {
    161 
    162 			return $this->config_final;
    163 
    164 		}
    165 	}
    166 }