Completed
Pull Request — master (#1592)
by Aristeides
04:27 queued 02:16
created

Kirki::get_config_param()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Kirki API class.
4
 * Takes care of adding panels, sections & fields to the customizer.
5
 * For documentation please see https://github.com/aristath/kirki/wiki
6
 *
7
 * @package     Kirki
8
 * @category    Core
9
 * @author      Aristeides Stathopoulos
10
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
11
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
12
 * @since       1.0
13
 */
14
15
// Exit if accessed directly.
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * This class acts as an interface.
22
 * Developers may use this object to add configurations, fields, panels and sections.
23
 * You can also access all available configurations, fields, panels and sections
24
 * by accessing the object's static properties.
25
 */
26
class Kirki extends Kirki_Init {
27
28
	/**
29
	 * Absolute path to the Kirki folder.
30
	 *
31
	 * @static
32
	 * @access public
33
	 * @var string
34
	 */
35
	public static $path;
36
37
	/**
38
	 * URL to the Kirki folder.
39
	 *
40
	 * @static
41
	 * @access public
42
	 * @var string
43
	 */
44
	public static $url;
45
46
	/**
47
	 * An array containing all configurations.
48
	 *
49
	 * @static
50
	 * @access public
51
	 * @var array
52
	 */
53
	public static $config = array();
54
55
	/**
56
	 * An array containing all fields.
57
	 *
58
	 * @static
59
	 * @access public
60
	 * @var array
61
	 */
62
	public static $fields = array();
63
64
	/**
65
	 * An array containing all panels.
66
	 *
67
	 * @static
68
	 * @access public
69
	 * @var array
70
	 */
71
	public static $panels = array();
72
73
	/**
74
	 * An array containing all sections.
75
	 *
76
	 * @static
77
	 * @access public
78
	 * @var array
79
	 */
80
	public static $sections = array();
81
82
	/**
83
	 * Modules object.
84
	 *
85
	 * @access public
86
	 * @since 3.0.0
87
	 * @var object
88
	 */
89
	public $modules;
90
91
	/**
92
	 * Get the value of an option from the db.
93
	 *
94
	 * @static
95
	 * @access public
96
	 * @param string $config_id The ID of the configuration corresponding to this field.
97
	 * @param string $field_id  The field_id (defined as 'settings' in the field arguments).
98
	 * @return mixed The saved value of the field.
99
	 */
100
	public static function get_option( $config_id = '', $field_id = '' ) {
101
102
		return Kirki_Values::get_value( $config_id, $field_id );
103
	}
104
105
	/**
106
	 * Sets the configuration options.
107
	 *
108
	 * @static
109
	 * @access public
110
	 * @param string $config_id The configuration ID.
111
	 * @param array  $args      The configuration options.
112
	 */
113
	public static function add_config( $config_id, $args = array() ) {
114
115
		$config = Kirki_Config::get_instance( $config_id, $args );
116
		$config_args = $config->get_config();
117
		self::$config[ $config_args['id'] ] = $config_args;
118
	}
119
120
	/**
121
	 * Create a new panel.
122
	 *
123
	 * @static
124
	 * @access public
125
	 * @param string $id   The ID for this panel.
126
	 * @param array  $args The panel arguments.
127
	 */
128
	public static function add_panel( $id = '', $args = array() ) {
129
130
		$args['id']          = esc_attr( $id );
131
		$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
132
		$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
133
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
134
		$args['type']        = 'kirki-' . $args['type'];
135
		if ( ! isset( $args['active_callback'] ) ) {
136
			$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
137
		}
138
139
		self::$panels[ $args['id'] ] = $args;
140
	}
141
142
	/**
143
	 * Create a new section.
144
	 *
145
	 * @static
146
	 * @access public
147
	 * @param string $id   The ID for this section.
148
	 * @param array  $args The section arguments.
149
	 */
150
	public static function add_section( $id, $args ) {
151
152
		$args['id']          = esc_attr( $id );
153
		$args['panel']       = ( isset( $args['panel'] ) ) ? esc_attr( $args['panel'] ) : '';
154
		$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
155
		$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
156
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
157
		$args['type']        = 'kirki-' . $args['type'];
158
		if ( ! isset( $args['active_callback'] ) ) {
159
			$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
160
		}
161
162
		self::$sections[ $args['id'] ] = $args;
163
	}
164
165
	/**
166
	 * Create a new field.
167
	 *
168
	 * @static
169
	 * @access public
170
	 * @param string $config_id The configuration ID for this field.
171
	 * @param array  $args      The field arguments.
172
	 */
173
	public static function add_field( $config_id, $args ) {
174
175
		if ( doing_action( 'customize_register' ) ) {
176
			_doing_it_wrong( __METHOD__, esc_attr__( 'Kirki fields should not be added on customize_register. Please add them directly, or on init.', 'kirki' ), '3.0.10' );
177
		}
178
179
		// Early exit if 'type' is not defined.
180
		if ( ! isset( $args['type'] ) ) {
181
			return;
182
		}
183
184
		$str = str_replace( array( '-', '_' ), ' ', $args['type'] );
185
		$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
186
		if ( class_exists( $classname ) ) {
187
			new $classname( $config_id, $args );
188
			return;
189
		}
190
		if ( false !== strpos( $classname, 'Kirki_Field_Kirki_' ) ) {
191
			$classname = str_replace( 'Kirki_Field_Kirki_', 'Kirki_Field_', $classname );
192
			if ( class_exists( $classname ) ) {
193
				new $classname( $config_id, $args );
194
				return;
195
			}
196
		}
197
198
		new Kirki_Field( $config_id, $args );
199
200
	}
201
202
	/**
203
	 * Gets a parameter for a config-id.
204
	 *
205
	 * @static
206
	 * @access public
207
	 * @since 3.0.10
208
	 * @param string $id    The config-ID.
209
	 * @param string $param The parameter we want.
210
	 * @return string
211
	 */
212
	public static function get_config_param( $id, $param ) {
213
214
		if ( ! isset( self::$config[ $id ] ) || ! isset( self::$config[ $id ][ $param ] ) ) {
215
			return '';
216
		}
217
		return self::$config[ $id ][ $param ];
218
	}
219
}
220