deprecated
1 month ago
Aliases.php
2 months ago
Config.php
5 months ago
Control.php
5 months ago
Deprecated.php
5 months ago
Field.php
2 months ago
Framework.php
5 months ago
Init.php
2 months ago
Kirki.php
2 months ago
Modules.php
2 months ago
Pro_Namespace_Compatibility.php
5 months ago
Sanitize_Values.php
5 months ago
Scripts.php
2 months ago
Settings.php
5 months ago
Values.php
5 months ago
Settings.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles sections created via the Kirki API. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Core |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 1.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Compatibility; |
| 14 | |
| 15 | /** |
| 16 | * Each setting is a separate instance |
| 17 | */ |
| 18 | class Settings { |
| 19 | |
| 20 | /** |
| 21 | * The 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 = []; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new Settings object. |
| 38 | * Class constructor. |
| 39 | * |
| 40 | * @access public |
| 41 | * @param array $args The field definition as sanitized in Kirki\Compatibility\Field. |
| 42 | */ |
| 43 | public function __construct( $args = [] ) { |
| 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 | |
| 55 | // Add the settings. |
| 56 | $this->add_settings( $args ); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adds the settings for this field. |
| 62 | * If settings are defined as an array, then it goes through them |
| 63 | * and calls the add_setting method. |
| 64 | * If not an array, then it just calls add_setting |
| 65 | * |
| 66 | * @access private |
| 67 | * @param array $args The field definition as sanitized in Kirki\Compatibility\Field. |
| 68 | */ |
| 69 | final function add_settings( $args = [] ) { |
| 70 | |
| 71 | // Get the classname we'll be using to create our setting(s). |
| 72 | $classname = false; |
| 73 | if ( isset( $args['option_type'] ) && array_key_exists( $args['option_type'], $this->setting_types ) ) { |
| 74 | $classname = $this->setting_types[ $args['option_type'] ]; |
| 75 | } |
| 76 | if ( ! isset( $args['type'] ) || ! array_key_exists( $args['type'], $this->setting_types ) ) { |
| 77 | $args['type'] = 'default'; |
| 78 | } |
| 79 | $classname = ! $classname ? $this->setting_types[ $args['type'] ] : $classname; |
| 80 | |
| 81 | // If settings are defined as an array, then we need to go through them |
| 82 | // and call add_setting for each one of them separately. |
| 83 | if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) { |
| 84 | |
| 85 | // Make sure defaults have been defined. |
| 86 | if ( ! isset( $args['default'] ) || ! is_array( $args['default'] ) ) { |
| 87 | $args['default'] = []; |
| 88 | } |
| 89 | foreach ( $args['settings'] as $key => $value ) { |
| 90 | // ? Bagus: this $defaults var is not defined anywhere inside this function, so is this a mistake? |
| 91 | $default = ( isset( $defaults[ $key ] ) ) ? $defaults[ $key ] : ''; |
| 92 | $this->add_setting( $classname, $value, $default, $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] ); |
| 93 | } |
| 94 | } |
| 95 | $this->add_setting( $classname, $args['settings'], $args['default'], $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * This is where we're finally adding the setting to the Customizer. |
| 100 | * |
| 101 | * @access private |
| 102 | * @param string $classname The name of the class that will be used to create this setting. |
| 103 | * We're getting this from $this->setting_types. |
| 104 | * @param string $setting The setting-name. |
| 105 | * If settings is an array, then this method is called per-setting. |
| 106 | * @param string|array $default Default value for this setting. |
| 107 | * @param string $type The data type we're using. Valid options: theme_mod|option. |
| 108 | * @param string $capability @see https://codex.wordpress.org/Roles_and_Capabilities. |
| 109 | * @param string $transport Use refresh|postMessage. |
| 110 | * @param string|array $sanitize_callback A callable sanitization function or method. |
| 111 | */ |
| 112 | final function add_setting( $classname, $setting, $default, $type, $capability, $transport, $sanitize_callback ) { |
| 113 | |
| 114 | $this->wp_customize->add_setting( |
| 115 | new $classname( |
| 116 | $this->wp_customize, |
| 117 | $setting, |
| 118 | [ |
| 119 | 'default' => $default, |
| 120 | 'type' => $type, |
| 121 | 'capability' => $capability, |
| 122 | 'transport' => $transport, |
| 123 | 'sanitize_callback' => $sanitize_callback, |
| 124 | ] |
| 125 | ) |
| 126 | ); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the $this->setting_types property. |
| 132 | * Makes sure the kirki_setting_types filter is applied |
| 133 | * and that the defined classes actually exist. |
| 134 | * If a defined class does not exist, it is removed. |
| 135 | */ |
| 136 | final function set_setting_types() { |
| 137 | |
| 138 | // Apply the kirki_setting_types filter. |
| 139 | $this->setting_types = apply_filters( |
| 140 | 'kirki_setting_types', |
| 141 | [ |
| 142 | 'default' => 'WP_Customize_Setting', |
| 143 | 'repeater' => '\Kirki_Settings_Repeater_Setting', |
| 144 | 'user_meta' => '\Kirki\Util\Setting\User_Meta', |
| 145 | 'site_option' => '\Kirki\Util\Setting\Site_Option', |
| 146 | ] |
| 147 | ); |
| 148 | |
| 149 | // Make sure the defined classes actually exist. |
| 150 | foreach ( $this->setting_types as $key => $classname ) { |
| 151 | |
| 152 | if ( ! class_exists( $classname ) ) { |
| 153 | unset( $this->setting_types[ $key ] ); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 |