Issues (377)

core/class-kirki-settings.php (2 issues)

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    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
53
		// Add the settings.
54
		$this->add_settings( $args );
55
56
	}
57
58
	/**
59
	 * Adds the settings for this field.
60
	 * If settings are defined as an array, then it goes through them
61
	 * and calls the add_setting method.
62
	 * If not an array, then it just calls add_setting
63
	 *
64
	 * @access private
65
	 * @param array $args The field definition as sanitized in Kirki_Field.
66
	 */
67
	final private function add_settings( $args = array() ) {
68
69
		// Get the classname we'll be using to create our setting(s).
70
		$classname = false;
71
		if ( isset( $args['option_type'] ) && array_key_exists( $args['option_type'], $this->setting_types ) ) {
72
			$classname = $this->setting_types[ $args['option_type'] ];
73
		}
74
		if ( ! isset( $args['type'] ) || ! array_key_exists( $args['type'], $this->setting_types ) ) {
75
			$args['type'] = 'default';
76
		}
77
		$classname = ! $classname ? $this->setting_types[ $args['type'] ] : $classname;
78
79
		// If settings are defined as an array, then we need to go through them
80
		// and call add_setting for each one of them separately.
81
		if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) {
82
83
			// Make sure defaults have been defined.
84
			if ( ! isset( $args['default'] ) || ! is_array( $args['default'] ) ) {
85
				$args['default'] = array();
86
			}
87
			foreach ( $args['settings'] as $key => $value ) {
88
				$default = ( isset( $defaults[ $key ] ) ) ? $defaults[ $key ] : '';
0 ignored issues
show
Comprehensibility Best Practice introduced by Aristeides Stathopoulos
The variable $defaults does not exist. Did you maybe mean $default?
Loading history...
89
				$this->add_setting( $classname, $value, $default, $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] );
90
			}
91
		}
92
		$this->add_setting( $classname, $args['settings'], $args['default'], $args['option_type'], $args['capability'], $args['transport'], $args['sanitize_callback'] );
93
	}
94
95
	/**
96
	 * This is where we're finally adding the setting to the Customizer.
97
	 *
98
	 * @access private
99
	 * @param string       $classname           The name of the class that will be used to create this setting.
100
	 *                                          We're getting this from $this->setting_types.
101
	 * @param string       $setting             The setting-name.
102
	 *                                          If settings is an array, then this method is called per-setting.
103
	 * @param string|array $default             Default value for this setting.
104
	 * @param string       $type                The data type we're using. Valid options: theme_mod|option.
105
	 * @param string       $capability          @see https://codex.wordpress.org/Roles_and_Capabilities.
106
	 * @param string       $transport           Use refresh|postMessage.
107
	 * @param string|array $sanitize_callback   A callable sanitization function or method.
108
	 */
109
	final private function add_setting( $classname, $setting, $default, $type, $capability, $transport, $sanitize_callback ) {
110
111
		$this->wp_customize->add_setting(
112
			new $classname(
113
				$this->wp_customize, $setting, array(
114
					'default'           => $default,
115
					'type'              => $type,
116
					'capability'        => $capability,
117
					'transport'         => $transport,
118
					'sanitize_callback' => $sanitize_callback,
119
				)
120
			)
121
		);
122
123
	}
124
125
	/**
126
	 * Sets the $this->setting_types property.
127
	 * Makes sure the kirki_setting_types filter is applied
128
	 * and that the defined classes actually exist.
129
	 * If a defined class does not exist, it is removed.
130
	 */
131
	final private function set_setting_types() {
132
133
		// Apply the kirki_setting_types filter.
134
		$this->setting_types = apply_filters(
135
			'kirki_setting_types', array(
0 ignored issues
show
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
136
				'default'     => 'WP_Customize_Setting',
137
				'repeater'    => 'Kirki_Settings_Repeater_Setting',
138
				'user_meta'   => 'Kirki_Setting_User_Meta',
139
				'site_option' => 'Kirki_Setting_Site_Option',
140
			)
141
		);
142
143
		// Make sure the defined classes actually exist.
144
		foreach ( $this->setting_types as $key => $classname ) {
145
146
			if ( ! class_exists( $classname ) ) {
147
				unset( $this->setting_types[ $key ] );
148
			}
149
		}
150
	}
151
}
152