Completed
Push — develop ( 46282b...9f3e22 )
by Aristeides
02:45
created

Kirki_Init::set_url()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Initializes Kirki
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       1.0
11
 */
12
13
/**
14
 * Initialize Kirki
15
 */
16
class Kirki_Init {
17
18
	/**
19
	 * Control types.
20
	 *
21
	 * @access private
22
	 * @since 3.0.0
23
	 * @var array
24
	 */
25
	private $control_types = array();
26
27
	/**
28
	 * The class constructor.
29
	 */
30
	public function __construct() {
31
		$this->set_url();
32
		add_action( 'after_setup_theme', array( $this, 'set_url' ) );
33
		add_action( 'customize_update_user_meta', array( $this, 'update_user_meta' ), 10, 2 );
34
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
35
		add_filter( 'kirki/control_types', array( $this, 'default_control_types' ) );
36
	}
37
38
	/**
39
	 * Properly set the Kirki URL for assets.
40
	 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme
41
	 * and then does some calculations to get the proper URL for its CSS & JS assets.
42
	 */
43
	public function set_url() {
44
		if ( defined( 'ABSPATH' ) ) {
45
			// Replace path with URL.
46
			$kirki_url  = str_replace( ABSPATH, '', Kirki::$path );
47
			Kirki::$url = site_url( $kirki_url );
48
			// Escape the URL.
49
			Kirki::$url = esc_url_raw( Kirki::$url );
50
		}
51
		// Apply the kirki/config filter.
52
		$config = apply_filters( 'kirki/config', array() );
53
		if ( isset( $config['url_path'] ) ) {
54
			Kirki::$url = esc_url_raw( $config['url_path'] );
55
		}
56
	}
57
58
	/**
59
	 * Add the default Kirki control types.
60
	 *
61
	 * @access public
62
	 * @since 3.0.0
63
	 * @param array $control_types The control types array.
64
	 * @return array
65
	 */
66
	public function default_control_types( $control_types = array() ) {
67
68
		$this->control_types = array(
69
			'checkbox'              => 'WP_Customize_Control',
70
			'kirki-background'      => 'Kirki_Control_Background',
71
			'kirki-code'            => 'Kirki_Control_Code',
72
			'kirki-color'           => 'Kirki_Control_Color',
73
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
74
			'kirki-custom'          => 'Kirki_Control_Custom',
75
			'kirki-date'            => 'Kirki_Control_Date',
76
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
77
			'kirki-dimension'       => 'Kirki_Control_Dimension',
78
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
79
			'kirki-editor'          => 'Kirki_Control_Editor',
80
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
81
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
82
			'kirki-number'          => 'Kirki_Control_Number',
83
			'kirki-palette'         => 'Kirki_Control_Palette',
84
			'kirki-preset'          => 'Kirki_Control_Preset',
85
			'kirki-radio'           => 'Kirki_Control_Radio',
86
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
87
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
88
			'repeater'              => 'Kirki_Control_Repeater',
89
			'kirki-select'          => 'Kirki_Control_Select',
90
			'kirki-slider'          => 'Kirki_Control_Slider',
91
			'kirki-sortable'        => 'Kirki_Control_Sortable',
92
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
93
			'kirki-switch'          => 'Kirki_Control_Switch',
94
			'kirki-generic'         => 'Kirki_Control_Generic',
95
			'kirki-toggle'          => 'Kirki_Control_Toggle',
96
			'kirki-typography'      => 'Kirki_Control_Typography',
97
			'image'                 => 'WP_Customize_Image_Control',
98
			'cropped_image'         => 'WP_Customize_Cropped_Image_Control',
99
			'upload'                => 'WP_Customize_Upload_Control',
100
		);
101
		return array_merge( $control_types, $this->control_types );
102
103
	}
104
105
	/**
106
	 * Helper function that adds the fields, sections and panels to the customizer.
107
	 *
108
	 * @return void
109
	 */
110
	public function add_to_customizer() {
111
		$this->fields_from_filters();
112
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
113
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
114
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
115
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
116
		/* new Kirki_Modules_Loading(); */
117
	}
118
119
	/**
120
	 * Register control types
121
	 *
122
	 * @return  void
123
	 */
124
	public function register_control_types() {
125
		global $wp_customize;
126
127
		$section_types = apply_filters( 'kirki/section_types', array() );
128
		foreach ( $section_types as $section_type ) {
129
			$wp_customize->register_section_type( $section_type );
130
		}
131
		if ( empty( $this->control_types ) ) {
132
			$this->control_types = $this->default_control_types();
133
		}
134
		$do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array(
135
			'Kirki_Control_Repeater',
136
		) );
137
		foreach ( $this->control_types as $control_type ) {
138
			if ( 0 === strpos( $control_type, 'Kirki' ) && ! in_array( $control_type, $do_not_register_control_types ) ) {
139
				$wp_customize->register_control_type( $control_type );
140
			}
141
		}
142
	}
143
144
	/**
145
	 * Register our panels to the WordPress Customizer.
146
	 *
147
	 * @access public
148
	 */
149
	public function add_panels() {
150
		if ( ! empty( Kirki::$panels ) ) {
151
			foreach ( Kirki::$panels as $panel_args ) {
152
				new Kirki_Panel( $panel_args );
153
			}
154
		}
155
	}
156
157
	/**
158
	 * Register our sections to the WordPress Customizer.
159
	 *
160
	 * @var	object	The WordPress Customizer object
161
	 * @return  void
162
	 */
163
	public function add_sections() {
164
		if ( ! empty( Kirki::$sections ) ) {
165
			foreach ( Kirki::$sections as $section_args ) {
166
				new Kirki_Section( $section_args );
167
			}
168
		}
169
	}
170
171
	/**
172
	 * Create the settings and controls from the $fields array and register them.
173
	 *
174
	 * @var	object	The WordPress Customizer object
175
	 * @return  void
176
	 */
177
	public function add_fields() {
178
179
		global $wp_customize;
180
		foreach ( Kirki::$fields as $args ) {
181
182
			// Create the settings.
183
			new Kirki_Settings( $args );
184
185
			// Check if we're on the customizer.
186
			// If we are, then we will create the controls, add the scripts needed for the customizer
187
			// and any other tweaks that this field may require.
188
			if ( $wp_customize ) {
189
190
				// Create the control.
191
				new Kirki_Control( $args );
192
193
			}
194
		}
195
	}
196
197
	/**
198
	 * Build the variables.
199
	 *
200
	 * @return array 	('variable-name' => value)
201
	 */
202
	public static function get_variables() {
203
204
		$variables = array();
205
206
		// Loop through all fields.
207
		foreach ( Kirki::$fields as $field ) {
208
209
			// Check if we have variables for this field.
210
			if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) {
211
212
				// Loop through the array of variables.
213
				foreach ( $field['variables'] as $field_variable ) {
214
215
					// Is the variable ['name'] defined? If yes, then we can proceed.
216
					if ( isset( $field_variable['name'] ) ) {
217
218
						// Sanitize the variable name.
219
						$variable_name = esc_attr( $field_variable['name'] );
220
221
						// Do we have a callback function defined? If not then set $variable_callback to false.
222
						$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;
223
224
						// If we have a variable_callback defined then get the value of the option
225
						// and run it through the callback function.
226
						// If no callback is defined (false) then just get the value.
227
						if ( $variable_callback ) {
228
							$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) );
229
						} else {
230
							$variables[ $variable_name ] = Kirki::get_option( $field['settings'] );
231
						}
232
					}
233
				}
234
			}
235
		}
236
237
		// Pass the variables through a filter ('kirki/variable') and return the array of variables.
238
		return apply_filters( 'kirki/variable', $variables );
239
240
	}
241
242
	/**
243
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
244
	 * These filters are no longer used, this is simply for backwards-compatibility.
245
	 */
246
	public function fields_from_filters() {
247
248
		$fields = apply_filters( 'kirki/controls', array() );
249
		$fields = apply_filters( 'kirki/fields', $fields );
250
251
		if ( ! empty( $fields ) ) {
252
			foreach ( $fields as $field ) {
253
				Kirki::add_field( 'global', $field );
254
			}
255
		}
256
257
	}
258
259
	/**
260
	 * Handle saving of settings with "user_meta" storage type.
261
	 *
262
	 * @param string $value The value being saved.
263
	 * @param object $wp_customize_setting $WP_Customize_Setting The WP_Customize_Setting instance when saving is happening.
264
	 */
265
	public function update_user_meta( $value, $wp_customize_setting ) {
266
		update_user_meta( get_current_user_id(), $wp_customize_setting->id, $value );
267
	}
268
}
269