class-kirki-init.php (7859B)
1 <?php 2 /** 3 * Initializes Kirki 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 * @since 1.0 11 */ 12 13 if ( ! class_exists( 'Kirki_Init' ) ) { 14 15 /** 16 * Initialize Kirki 17 */ 18 class Kirki_Init { 19 20 /** 21 * The class constructor. 22 */ 23 public function __construct() { 24 $this->set_url(); 25 add_action( 'after_setup_theme', array( $this, 'set_url' ) ); 26 add_action( 'customize_update_user_meta', array( $this, 'update_user_meta' ), 10, 2 ); 27 add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 ); 28 } 29 30 /** 31 * Properly set the Kirki URL for assets. 32 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme 33 * and then does some calculations to get the proper URL for its CSS & JS assets. 34 */ 35 public function set_url() { 36 37 Kirki::$url = get_template_directory_uri() . "/customizer/kirki"; 38 39 } 40 41 /** 42 * Helper function that adds the fields, sections and panels to the customizer. 43 * 44 * @return void 45 */ 46 public function add_to_customizer() { 47 $this->fields_from_filters(); 48 add_action( 'customize_register', array( $this, 'register_control_types' ) ); 49 add_action( 'customize_register', array( $this, 'add_panels' ), 97 ); 50 add_action( 'customize_register', array( $this, 'add_sections' ), 98 ); 51 add_action( 'customize_register', array( $this, 'add_fields' ), 99 ); 52 /* new Kirki_Scripts_Loading(); */ 53 } 54 55 /** 56 * Register control types 57 * 58 * @return void 59 */ 60 public function register_control_types() { 61 global $wp_customize; 62 63 $wp_customize->register_section_type( 'Kirki_Sections_Default_Section' ); 64 $wp_customize->register_section_type( 'Kirki_Sections_Expanded_Section' ); 65 $wp_customize->register_section_type( 'Kirki_Sections_Hover_Section' ); 66 67 $wp_customize->register_panel_type( 'Kirki_Panels_Expanded_Panel' ); 68 69 $wp_customize->register_control_type( 'Kirki_Controls_Checkbox_Control' ); 70 $wp_customize->register_control_type( 'Kirki_Controls_Color_Control' ); 71 $wp_customize->register_control_type( 'Kirki_Controls_Color_Palette_Control' ); 72 $wp_customize->register_control_type( 'Kirki_Controls_Custom_Control' ); 73 $wp_customize->register_control_type( 'Kirki_Controls_Date_Control' ); 74 $wp_customize->register_control_type( 'Kirki_Controls_Dashicons_Control' ); 75 $wp_customize->register_control_type( 'Kirki_Controls_Dimension_Control' ); 76 $wp_customize->register_control_type( 'Kirki_Controls_Dropdown_Pages_Control' ); 77 $wp_customize->register_control_type( 'Kirki_Controls_Editor_Control' ); 78 $wp_customize->register_control_type( 'Kirki_Controls_Number_Control' ); 79 $wp_customize->register_control_type( 'Kirki_Controls_Radio_Control' ); 80 $wp_customize->register_control_type( 'Kirki_Controls_Radio_Buttonset_Control' ); 81 $wp_customize->register_control_type( 'Kirki_Controls_Radio_Image_Control' ); 82 $wp_customize->register_control_type( 'Kirki_Controls_Select_Control' ); 83 $wp_customize->register_control_type( 'Kirki_Controls_Slider_Control' ); 84 $wp_customize->register_control_type( 'Kirki_Controls_Spacing_Control' ); 85 $wp_customize->register_control_type( 'Kirki_Controls_Switch_Control' ); 86 $wp_customize->register_control_type( 'Kirki_Controls_Generic_Control' ); 87 $wp_customize->register_control_type( 'Kirki_Controls_Toggle_Control' ); 88 $wp_customize->register_control_type( 'Kirki_Controls_Typography_Control' ); 89 $wp_customize->register_control_type( 'Kirki_Controls_Palette_Control' ); 90 $wp_customize->register_control_type( 'Kirki_Controls_Preset_Control' ); 91 $wp_customize->register_control_type( 'Kirki_Controls_Multicheck_Control' ); 92 $wp_customize->register_control_type( 'Kirki_Controls_Multicolor_Control' ); 93 $wp_customize->register_control_type( 'Kirki_Controls_Sortable_Control' ); 94 } 95 96 /** 97 * Register our panels to the WordPress Customizer. 98 * 99 * @access public 100 */ 101 public function add_panels() { 102 if ( ! empty( Kirki::$panels ) ) { 103 foreach ( Kirki::$panels as $panel_args ) { 104 new Kirki_Panel( $panel_args ); 105 } 106 } 107 } 108 109 /** 110 * Register our sections to the WordPress Customizer. 111 * 112 * @var object The WordPress Customizer object 113 * @return void 114 */ 115 public function add_sections() { 116 if ( ! empty( Kirki::$sections ) ) { 117 foreach ( Kirki::$sections as $section_args ) { 118 new Kirki_Section( $section_args ); 119 } 120 } 121 } 122 123 /** 124 * Create the settings and controls from the $fields array and register them. 125 * 126 * @var object The WordPress Customizer object 127 * @return void 128 */ 129 public function add_fields() { 130 131 global $wp_customize; 132 foreach ( Kirki::$fields as $args ) { 133 if ( isset( $args['type'] ) && 'background' === $args['type'] ) { 134 continue; 135 } 136 137 // Create the settings. 138 new Kirki_Settings( $args ); 139 140 // Check if we're on the customizer. 141 // If we are, then we will create the controls, add the scripts needed for the customizer 142 // and any other tweaks that this field may require. 143 if ( $wp_customize ) { 144 145 // Create the control. 146 new Kirki_Control( $args ); 147 148 // Create the scripts for tooltips. 149 Kirki_Scripts_Tooltips::generate_script( $args ); 150 } 151 } 152 } 153 154 /** 155 * Build the variables. 156 * 157 * @return array ('variable-name' => value) 158 */ 159 public static function get_variables() { 160 161 $variables = array(); 162 163 // Loop through all fields. 164 foreach ( Kirki::$fields as $field ) { 165 166 // Check if we have variables for this field. 167 if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { 168 169 // Loop through the array of variables. 170 foreach ( $field['variables'] as $field_variable ) { 171 172 // Is the variable ['name'] defined? If yes, then we can proceed. 173 if ( isset( $field_variable['name'] ) ) { 174 175 // Sanitize the variable name. 176 $variable_name = esc_attr( $field_variable['name'] ); 177 178 // Do we have a callback function defined? If not then set $variable_callback to false. 179 $variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; 180 181 // If we have a variable_callback defined then get the value of the option 182 // and run it through the callback function. 183 // If no callback is defined (false) then just get the value. 184 if ( $variable_callback ) { 185 $variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) ); 186 } else { 187 $variables[ $variable_name ] = Kirki::get_option( $field['settings'] ); 188 } 189 } 190 } 191 } 192 } 193 194 // Pass the variables through a filter ('kirki/variable') and return the array of variables. 195 return apply_filters( 'kirki/variable', $variables ); 196 197 } 198 199 /** 200 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter. 201 * These filters are no longer used, this is simply for backwards-compatibility. 202 */ 203 public function fields_from_filters() { 204 205 $fields = apply_filters( 'kirki/controls', array() ); 206 $fields = apply_filters( 'kirki/fields', $fields ); 207 208 if ( ! empty( $fields ) ) { 209 foreach ( $fields as $field ) { 210 Kirki::add_field( 'global', $field ); 211 } 212 } 213 214 } 215 216 /** 217 * Handle saving of settings with "user_meta" storage type. 218 * 219 * @param string $value The value being saved. 220 * @param object $wp_customize_setting $WP_Customize_Setting The WP_Customize_Setting instance when saving is happening. 221 */ 222 public function update_user_meta( $value, $wp_customize_setting ) { 223 update_user_meta( get_current_user_id(), $wp_customize_setting->id, $value ); 224 } 225 } 226 }