class-kirki-control.php (5742B)
1 <?php 2 /** 3 * Controls handler 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_Control')) { 13 14 /** 15 * Our main Kirki_Control object 16 */ 17 class Kirki_Control 18 { 19 20 /** 21 * The $wp_customize WordPress global. 22 * 23 * @access protected 24 * @var WP_Customize_Manager 25 */ 26 protected $wp_customize; 27 28 /** 29 * An array of all available control types. 30 * 31 * @access protected 32 * @var array 33 */ 34 protected $control_types = array(); 35 36 /** 37 * The class constructor. 38 * Creates the actual controls in the customizer. 39 * 40 * @access public 41 * 42 * @param array $args The field definition as sanitized in Kirki_Field. 43 */ 44 public function __construct($args) 45 { 46 47 // Set the $wp_customize property. 48 global $wp_customize; 49 if ( ! $wp_customize) { 50 return; 51 } 52 $this->wp_customize = $wp_customize; 53 54 // Set the control types. 55 $this->set_control_types(); 56 // Add the control. 57 $this->add_control($args); 58 59 } 60 61 /** 62 * Get the class name of the class needed to create tis control. 63 * 64 * @access private 65 * 66 * @param array $args The field definition as sanitized in Kirki_Field. 67 * 68 * @return string the name of the class that will be used to create this control. 69 */ 70 final private function get_control_class_name($args) 71 { 72 73 // Set a default class name. 74 $class_name = 'WP_Customize_Control'; 75 // Get the classname from the array of control classnames. 76 if (array_key_exists($args['type'], $this->control_types)) { 77 $class_name = $this->control_types[$args['type']]; 78 } 79 80 return $class_name; 81 82 } 83 84 /** 85 * Adds the control. 86 * 87 * @access protected 88 * 89 * @param array $args The field definition as sanitized in Kirki_Field. 90 */ 91 final protected function add_control($args) 92 { 93 94 // Get the name of the class we're going to use. 95 $class_name = $this->get_control_class_name($args); 96 // Add the control. 97 $this->wp_customize->add_control(new $class_name($this->wp_customize, $args['settings'], $args)); 98 99 } 100 101 /** 102 * Sets the $this->control_types property. 103 * Makes sure the kirki/control_types filter is applied 104 * and that the defined classes actually exist. 105 * If a defined class does not exist, it is removed. 106 */ 107 final private function set_control_types() 108 { 109 110 $this->control_types = apply_filters('kirki/control_types', array( 111 'kirki-checkbox' => 'Kirki_Controls_Checkbox_Control', 112 'kirki-color' => 'Kirki_Controls_Color_Control', 113 'kirki-color-palette' => 'Kirki_Controls_Color_Palette_Control', 114 'kirki-custom' => 'Kirki_Controls_Custom_Control', 115 'kirki-date' => 'Kirki_Controls_Date_Control', 116 'kirki-dashicons' => 'Kirki_Controls_Dashicons_Control', 117 'kirki-dimension' => 'Kirki_Controls_Dimension_Control', 118 'kirki-editor' => 'Kirki_Controls_Editor_Control', 119 'kirki-multicolor' => 'Kirki_Controls_Multicolor_Control', 120 'kirki-multicheck' => 'Kirki_Controls_MultiCheck_Control', 121 'kirki-number' => 'Kirki_Controls_Number_Control', 122 'kirki-palette' => 'Kirki_Controls_Palette_Control', 123 'kirki-preset' => 'Kirki_Controls_Preset_Control', 124 'kirki-radio' => 'Kirki_Controls_Radio_Control', 125 'kirki-radio-buttonset' => 'Kirki_Controls_Radio_ButtonSet_Control', 126 'kirki-radio-image' => 'Kirki_Controls_Radio_Image_Control', 127 'repeater' => 'Kirki_Controls_Repeater_Control', 128 'kirki-select' => 'Kirki_Controls_Select_Control', 129 'kirki-slider' => 'Kirki_Controls_Slider_Control', 130 'kirki-sortable' => 'Kirki_Controls_Sortable_Control', 131 'kirki-spacing' => 'Kirki_Controls_Spacing_Control', 132 'kirki-switch' => 'Kirki_Controls_Switch_Control', 133 'kirki-generic' => 'Kirki_Controls_Generic_Control', 134 'kirki-toggle' => 'Kirki_Controls_Toggle_Control', 135 'kirki-typography' => 'Kirki_Controls_Typography_Control', 136 'kirki-dropdown-pages' => 'Kirki_Controls_Dropdown_Pages_Control', 137 'image' => 'WP_Customize_Image_Control', 138 'cropped_image' => 'WP_Customize_Cropped_Image_Control', 139 'upload' => 'WP_Customize_Upload_Control', 140 'media' => 'WP_Customize_Media_Control', 141 )); 142 143 // Make sure the defined classes actually exist. 144 foreach ($this->control_types as $key => $classname) { 145 146 if ( ! class_exists($classname)) { 147 unset($this->control_types[$key]); 148 } 149 } 150 } 151 } 152 }