ru-se.com

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

class-kirki.php (4907B)


      1 <?php
      2 /**
      3  * The Kirki API class.
      4  * Takes care of adding panels, sections & fields to the customizer.
      5  * For documentation please see https://github.com/aristath/kirki/wiki
      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' ) ) {
     21 
     22 	/**
     23 	 * This class acts as an interface.
     24 	 * Developers may use this object to add configurations, fields, panels and sections.
     25 	 * You can also access all available configurations, fields, panels and sections
     26 	 * by accessing the object's static properties.
     27 	 */
     28 	class Kirki extends Kirki_Init {
     29 
     30 		/**
     31 		 * Absolute path to the Kirki folder.
     32 		 *
     33 		 * @static
     34 		 * @access public
     35 		 * @var string
     36 		 */
     37 		public static $path;
     38 
     39 		/**
     40 		 * URL to the Kirki folder.
     41 		 *
     42 		 * @static
     43 		 * @access public
     44 		 * @var string
     45 		 */
     46 		public static $url;
     47 
     48 		/**
     49 		 * An array containing all configurations.
     50 		 *
     51 		 * @static
     52 		 * @access public
     53 		 * @var array
     54 		 */
     55 		public static $config   = array();
     56 
     57 		/**
     58 		 * An array containing all fields.
     59 		 *
     60 		 * @static
     61 		 * @access public
     62 		 * @var array
     63 		 */
     64 		public static $fields   = array();
     65 
     66 		/**
     67 		 * An array containing all panels.
     68 		 *
     69 		 * @static
     70 		 * @access public
     71 		 * @var array
     72 		 */
     73 		public static $panels   = array();
     74 
     75 		/**
     76 		 * An array containing all sections.
     77 		 *
     78 		 * @static
     79 		 * @access public
     80 		 * @var array
     81 		 */
     82 		public static $sections = array();
     83 
     84 		/**
     85 		 * Get the value of an option from the db.
     86 		 *
     87 		 * @static
     88 		 * @access public
     89 		 * @param string $config_id The ID of the configuration corresponding to this field.
     90 		 * @param string $field_id  The field_id (defined as 'settings' in the field arguments).
     91 		 * @return mixed The saved value of the field.
     92 		 */
     93 		public static function get_option( $config_id = '', $field_id = '' ) {
     94 
     95 			return Kirki_Values::get_value( $config_id, $field_id );
     96 
     97 		}
     98 
     99 		/**
    100 		 * Sets the configuration options.
    101 		 *
    102 		 * @static
    103 		 * @access public
    104 		 * @param string $config_id The configuration ID.
    105 		 * @param array  $args      The configuration options.
    106 		 */
    107 		public static function add_config( $config_id, $args = array() ) {
    108 
    109 			$config = Kirki_Config::get_instance( $config_id, $args );
    110 			$config_args = $config->get_config();
    111 			self::$config[ $config_args['id'] ] = $config_args;
    112 
    113 		}
    114 
    115 		/**
    116 		 * Create a new panel.
    117 		 *
    118 		 * @static
    119 		 * @access public
    120 		 * @param string $id   The ID for this panel.
    121 		 * @param array  $args The panel arguments.
    122 		 */
    123 		public static function add_panel( $id = '', $args = array() ) {
    124 
    125 			$args['id']          = esc_attr( $id );
    126 			$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
    127 			$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
    128 			$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
    129 			$args['type']        = 'kirki-' . $args['type'];
    130 			if ( ! isset( $args['active_callback'] ) ) {
    131 				$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
    132 			}
    133 
    134 			self::$panels[ $args['id'] ] = $args;
    135 
    136 		}
    137 
    138 		/**
    139 		 * Create a new section.
    140 		 *
    141 		 * @static
    142 		 * @access public
    143 		 * @param string $id   The ID for this section.
    144 		 * @param array  $args The section arguments.
    145 		 */
    146 		public static function add_section( $id, $args ) {
    147 
    148 			$args['id']          = esc_attr( $id );
    149 			$args['panel']       = ( isset( $args['panel'] ) ) ? esc_attr( $args['panel'] ) : '';
    150 			$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
    151 			$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
    152 			$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
    153 			$args['type']        = 'kirki-' . $args['type'];
    154 			if ( ! isset( $args['active_callback'] ) ) {
    155 				$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
    156 			}
    157 
    158 			self::$sections[ $args['id'] ] = $args;
    159 
    160 		}
    161 
    162 		/**
    163 		 * Create a new field.
    164 		 *
    165 		 * @static
    166 		 * @access public
    167 		 * @param string $config_id The configuration ID for this field.
    168 		 * @param array  $args      The field arguments.
    169 		 */
    170 		public static function add_field( $config_id, $args ) {
    171 
    172 			if ( isset( $args['type'] ) ) {
    173 				$str = str_replace( array( '-', '_' ), ' ', $args['type'] );
    174 				$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
    175 				if ( class_exists( $classname ) ) {
    176 					new $classname( $config_id, $args );
    177 					return;
    178 				}
    179 			}
    180 
    181 			new Kirki_Field( $config_id, $args );
    182 
    183 		}
    184 	}
    185 }