class-kirki-settings.php (4733B)
1 <?php 2 /** 3 * Handles sections created via the Kirki API. 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_Settings' ) ) { 14 15 /** 16 * Each setting is a separate instance 17 */ 18 class Kirki_Settings { 19 20 /** 21 * TYhe global $wp_customize object. 22 * 23 * @access protected 24 * @var WP_Customize_Manager 25 */ 26 protected $wp_customize; 27 28 /** 29 * The setting-stypes we're using. 30 * 31 * @access protected 32 * @var array 33 */ 34 protected $setting_types = array(); 35 36 /** 37 * Creates a new Kirki_Settings object. 38 * Class constructor. 39 * 40 * @access public 41 * @param array $args The field definition as sanitized in Kirki_Field. 42 */ 43 public function __construct( $args = array() ) { 44 45 // Set the $wp_customize property. 46 global $wp_customize; 47 if ( ! $wp_customize ) { 48 return; 49 } 50 $this->wp_customize = $wp_customize; 51 52 // Set the setting_types. 53 $this->set_setting_types(); 54 // Add the settings. 55 $this->add_settings( $args ); 56 57 } 58 59 /** 60 * Adds the settings for this field. 61 * If settings are defined as an array, then it goes through them 62 * and calls the add_setting method. 63 * If not an array, then it just calls add_setting 64 * 65 * @access private 66 * @param array $args The field definition as sanitized in Kirki_Field. 67 */ 68 final private function add_settings( $args = array() ) { 69 70 // Get the classname we'll be using to create our setting(s). 71 if ( ! isset( $args['type'] ) || ! array_key_exists( $args['type'], $this->setting_types ) ) { 72 $args['type'] = 'default'; 73 } 74 $classname = $this->setting_types[ $args['type'] ]; 75 76 // If settings are defined as an array, then we need to go through them 77 // and call add_setting for each one of them separately. 78 if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) { 79 80 // Make sure defaults have been defined. 81 if ( ! isset( $args['default'] ) || ! is_array( $args['default'] ) ) { 82 $args['default'] = array(); 83 } 84 foreach ( $args['settings'] as $key => $value ) { 85 $default = ( isset( $defaults[ $key ] ) ) ? $defaults[ $key ] : ''; 86 $this->add_setting( $classname, $value, $default, $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] ); 87 } 88 } 89 $this->add_setting( $classname, $args['settings'], $args['default'], $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] ); 90 } 91 92 /** 93 * This is where we're finally adding the setting to the Customizer. 94 * 95 * @access private 96 * @param string $classname The name of the class that will be used to create this setting. 97 * We're getting this from $this->setting_types. 98 * @param string $setting The setting-name. 99 * If settings is an array, then this method is called per-setting. 100 * @param string|array $default Default value for this setting. 101 * @param string $type The data type we're using. Valid options: theme_mod|option. 102 * @param string $capability @see https://codex.wordpress.org/Roles_and_Capabilities. 103 * @param string $transport Use refresh|postMessage. 104 * @param string|array $sanitize_callback A callable sanitization function or method. 105 */ 106 final private function add_setting( $classname, $setting, $default, $type, $capability, $transport, $sanitize_callback ) { 107 108 $this->wp_customize->add_setting( new $classname( $this->wp_customize, $setting, array( 109 'default' => $default, 110 'type' => $type, 111 'capability' => $capability, 112 'transport' => $transport, 113 'sanitize_callback' => $sanitize_callback, 114 ) ) ); 115 116 } 117 118 /** 119 * Sets the $this->setting_types property. 120 * Makes sure the kirki/setting_types filter is applied 121 * and that the defined classes actually exist. 122 * If a defined class does not exist, it is removed. 123 */ 124 final private function set_setting_types() { 125 126 // Apply the kirki/setting_types filter. 127 $this->setting_types = apply_filters( 'kirki/setting_types', array( 128 'default' => 'Kirki_Settings_Default_Setting', 129 'repeater' => 'Kirki_Settings_Repeater_Setting', 130 ) ); 131 132 // Make sure the defined classes actually exist. 133 foreach ( $this->setting_types as $key => $classname ) { 134 135 if ( ! class_exists( $classname ) ) { 136 unset( $this->setting_types[ $key ] ); 137 } 138 } 139 } 140 } 141 }