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