Completed
Push — develop ( a03475...8ba485 )
by Aristeides
02:49
created

Kirki_Init::get_variables()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 39
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 13
nc 12
nop 0
dl 0
loc 39
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	 * The class constructor.
20
	 */
21
	public function __construct() {
22
		$this->set_url();
23
		add_action( 'after_setup_theme', array( $this, 'set_url' ) );
24
		add_action( 'customize_update_user_meta', array( $this, 'update_user_meta' ), 10, 2 );
25
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
26
	}
27
28
	/**
29
	 * Properly set the Kirki URL for assets.
30
	 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme
31
	 * and then does some calculations to get the proper URL for its CSS & JS assets.
32
	 */
33
	public function set_url() {
34
35
		// Get parent-theme path.
36
		$parent_theme_path = get_template_directory();
37
		$parent_theme_path = wp_normalize_path( $parent_theme_path );
38
39
		// Get child-theme path.
40
		$child_theme_path = get_stylesheet_directory_uri();
41
		$child_theme_path = wp_normalize_path( $child_theme_path );
42
		Kirki::$url = plugin_dir_url( dirname( __FILE__ ) . 'kirki.php' );
43
44
		// Is Kirki included in a parent theme?
45
		if ( false !== strpos( Kirki::$path, $parent_theme_path ) ) {
46
			Kirki::$url = get_template_directory_uri() . str_replace( $parent_theme_path, '', Kirki::$path );
47
		}
48
49
		// Is there a child-theme?
50
		if ( $child_theme_path !== $parent_theme_path ) {
51
			// Is Kirki included in a child theme?
52
			if ( false !== strpos( Kirki::$path, $child_theme_path ) ) {
53
				Kirki::$url = get_template_directory_uri() . str_replace( $child_theme_path, '', Kirki::$path );
54
			}
55
		}
56
57
		// Apply the kirki/config filter.
58
		$config = apply_filters( 'kirki/config', array() );
59
		if ( isset( $config['url_path'] ) ) {
60
			Kirki::$url = esc_url_raw( $config['url_path'] );
61
		}
62
63
	}
64
65
	/**
66
	 * Helper function that adds the fields, sections and panels to the customizer.
67
	 *
68
	 * @return void
69
	 */
70
	public function add_to_customizer() {
71
		$this->fields_from_filters();
72
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
73
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
74
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
75
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
76
		/* new Kirki_Modules_Loading(); */
77
	}
78
79
	/**
80
	 * Register control types
81
	 *
82
	 * @return  void
83
	 */
84
	public function register_control_types() {
85
		global $wp_customize;
86
87
		$section_types = apply_filters( 'kirki/section_types', array() );
88
		foreach ( $section_types as $section_type ) {
89
			$wp_customize->register_section_type( $section_type );
90
		}
91
92
		$wp_customize->register_control_type( 'Kirki_Control_Background' );
93
		$wp_customize->register_control_type( 'Kirki_Control_Code' );
94
		$wp_customize->register_control_type( 'Kirki_Control_Color' );
95
		$wp_customize->register_control_type( 'Kirki_Control_Color_Palette' );
96
		$wp_customize->register_control_type( 'Kirki_Control_Custom' );
97
		$wp_customize->register_control_type( 'Kirki_Control_Date' );
98
		$wp_customize->register_control_type( 'Kirki_Control_Dashicons' );
99
		$wp_customize->register_control_type( 'Kirki_Control_Dimension' );
100
		$wp_customize->register_control_type( 'Kirki_Control_Dimensions' );
101
		$wp_customize->register_control_type( 'Kirki_Control_Editor' );
102
		$wp_customize->register_control_type( 'Kirki_Control_Number' );
103
		$wp_customize->register_control_type( 'Kirki_Control_Radio' );
104
		$wp_customize->register_control_type( 'Kirki_Control_Radio_Buttonset' );
105
		$wp_customize->register_control_type( 'Kirki_Control_Radio_Image' );
106
		$wp_customize->register_control_type( 'Kirki_Control_Select' );
107
		$wp_customize->register_control_type( 'Kirki_Control_Slider' );
108
		$wp_customize->register_control_type( 'Kirki_Control_Switch' );
109
		$wp_customize->register_control_type( 'Kirki_Control_Generic' );
110
		$wp_customize->register_control_type( 'Kirki_Control_Toggle' );
111
		$wp_customize->register_control_type( 'Kirki_Control_Typography' );
112
		$wp_customize->register_control_type( 'Kirki_Control_Palette' );
113
		$wp_customize->register_control_type( 'Kirki_Control_Preset' );
114
		$wp_customize->register_control_type( 'Kirki_Control_Multicheck' );
115
		$wp_customize->register_control_type( 'Kirki_Control_Multicolor' );
116
		$wp_customize->register_control_type( 'Kirki_Control_Sortable' );
117
	}
118
119
	/**
120
	 * Register our panels to the WordPress Customizer.
121
	 *
122
	 * @access public
123
	 */
124
	public function add_panels() {
125
		if ( ! empty( Kirki::$panels ) ) {
126
			foreach ( Kirki::$panels as $panel_args ) {
127
				new Kirki_Panel( $panel_args );
128
			}
129
		}
130
	}
131
132
	/**
133
	 * Register our sections to the WordPress Customizer.
134
	 *
135
	 * @var	object	The WordPress Customizer object
136
	 * @return  void
137
	 */
138
	public function add_sections() {
139
		if ( ! empty( Kirki::$sections ) ) {
140
			foreach ( Kirki::$sections as $section_args ) {
141
				new Kirki_Section( $section_args );
142
			}
143
		}
144
	}
145
146
	/**
147
	 * Create the settings and controls from the $fields array and register them.
148
	 *
149
	 * @var	object	The WordPress Customizer object
150
	 * @return  void
151
	 */
152
	public function add_fields() {
153
154
		global $wp_customize;
155
		foreach ( Kirki::$fields as $args ) {
156
157
			// Create the settings.
158
			new Kirki_Settings( $args );
159
160
			// Check if we're on the customizer.
161
			// If we are, then we will create the controls, add the scripts needed for the customizer
162
			// and any other tweaks that this field may require.
163
			if ( $wp_customize ) {
164
165
				// Create the control.
166
				new Kirki_Control( $args );
167
168
			}
169
		}
170
	}
171
172
	/**
173
	 * Build the variables.
174
	 *
175
	 * @return array 	('variable-name' => value)
176
	 */
177
	public static function get_variables() {
178
179
		$variables = array();
180
181
		// Loop through all fields.
182
		foreach ( Kirki::$fields as $field ) {
183
184
			// Check if we have variables for this field.
185
			if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) {
186
187
				// Loop through the array of variables.
188
				foreach ( $field['variables'] as $field_variable ) {
189
190
					// Is the variable ['name'] defined? If yes, then we can proceed.
191
					if ( isset( $field_variable['name'] ) ) {
192
193
						// Sanitize the variable name.
194
						$variable_name = esc_attr( $field_variable['name'] );
195
196
						// Do we have a callback function defined? If not then set $variable_callback to false.
197
						$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;
198
199
						// If we have a variable_callback defined then get the value of the option
200
						// and run it through the callback function.
201
						// If no callback is defined (false) then just get the value.
202
						if ( $variable_callback ) {
203
							$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) );
204
						} else {
205
							$variables[ $variable_name ] = Kirki::get_option( $field['settings'] );
206
						}
207
					}
208
				}
209
			}
210
		}
211
212
		// Pass the variables through a filter ('kirki/variable') and return the array of variables.
213
		return apply_filters( 'kirki/variable', $variables );
214
215
	}
216
217
	/**
218
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
219
	 * These filters are no longer used, this is simply for backwards-compatibility.
220
	 */
221
	public function fields_from_filters() {
222
223
		$fields = apply_filters( 'kirki/controls', array() );
224
		$fields = apply_filters( 'kirki/fields', $fields );
225
226
		if ( ! empty( $fields ) ) {
227
			foreach ( $fields as $field ) {
228
				Kirki::add_field( 'global', $field );
229
			}
230
		}
231
232
	}
233
234
	/**
235
	 * Handle saving of settings with "user_meta" storage type.
236
	 *
237
	 * @param string $value The value being saved.
238
	 * @param object $wp_customize_setting $WP_Customize_Setting The WP_Customize_Setting instance when saving is happening.
239
	 */
240
	public function update_user_meta( $value, $wp_customize_setting ) {
241
		update_user_meta( get_current_user_id(), $wp_customize_setting->id, $value );
242
	}
243
}
244