Completed
Pull Request — master (#1653)
by Aristeides
03:41 queued 01:33
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
136
		self::$panels[ $args['id'] ] = $args;
137
	}
138
139
	/**
140
	 * Create a new section.
141
	 *
142
	 * @static
143
	 * @access public
144
	 * @param string $id   The ID for this section.
145
	 * @param array  $args The section arguments.
146
	 */
147
	public static function add_section( $id, $args ) {
148
149
		$args['id']          = esc_attr( $id );
150
		$args['panel']       = ( isset( $args['panel'] ) ) ? esc_attr( $args['panel'] ) : '';
151
		$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
152
		$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
153
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
154
		$args['type']        = 'kirki-' . $args['type'];
155
156
		self::$sections[ $args['id'] ] = $args;
157
	}
158
159
	/**
160
	 * Create a new field.
161
	 *
162
	 * @static
163
	 * @access public
164
	 * @param string $config_id The configuration ID for this field.
165
	 * @param array  $args      The field arguments.
166
	 */
167
	public static function add_field( $config_id, $args ) {
168
169
		if ( doing_action( 'customize_register' ) ) {
170
			_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' );
171
		}
172
173
		// Early exit if 'type' is not defined.
174
		if ( ! isset( $args['type'] ) ) {
175
			return;
176
		}
177
178
		$str = str_replace( array( '-', '_' ), ' ', $args['type'] );
179
		$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
180
		if ( class_exists( $classname ) ) {
181
			new $classname( $config_id, $args );
182
			return;
183
		}
184
		if ( false !== strpos( $classname, 'Kirki_Field_Kirki_' ) ) {
185
			$classname = str_replace( 'Kirki_Field_Kirki_', 'Kirki_Field_', $classname );
186
			if ( class_exists( $classname ) ) {
187
				new $classname( $config_id, $args );
188
				return;
189
			}
190
		}
191
192
		new Kirki_Field( $config_id, $args );
193
194
	}
195
196
	/**
197
	 * Gets a parameter for a config-id.
198
	 *
199
	 * @static
200
	 * @access public
201
	 * @since 3.0.10
202
	 * @param string $id    The config-ID.
203
	 * @param string $param The parameter we want.
204
	 * @return string
205
	 */
206
	public static function get_config_param( $id, $param ) {
207
208
		if ( ! isset( self::$config[ $id ] ) || ! isset( self::$config[ $id ][ $param ] ) ) {
209
			return '';
210
		}
211
		return self::$config[ $id ][ $param ];
212
	}
213
}
214