Completed
Push — develop ( 6fc6a5...0826a4 )
by Aristeides
02:40
created

Kirki_Init::set_url()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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