Completed
Pull Request — master (#1454)
by Aristeides
05:36 queued 02:56
created

Kirki_Init::add_sections()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 0
dl 0
loc 19
rs 8.8571
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) 2017, 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();
0 ignored issues
show
Unused Code introduced by
The property $control_types is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
	/**
28
	 * The class constructor.
29
	 */
30
	public function __construct() {
31
32
		global $wp_customize;
33
		if ( $wp_customize ) {
34
			$this->set_url();
35
			add_action( 'after_setup_theme', array( $this, 'set_url' ) );
36
		}
37
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
38
39
		new Kirki_Values();
40
	}
41
42
	/**
43
	 * Properly set the Kirki URL for assets.
44
	 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme
45
	 * and then does some calculations to get the proper URL for its CSS & JS assets.
46
	 */
47
	public function set_url() {
48
49
		Kirki::$path = wp_normalize_path( dirname( KIRKI_PLUGIN_FILE ) );
50
51
		// Works in most cases.
52
		// Serves as a fallback in case all other checks fail.
53
		if ( defined( 'WP_CONTENT_DIR' ) ) {
54
			$content_dir = wp_normalize_path( WP_CONTENT_DIR );
55
			if ( false !== strpos( Kirki::$path, $content_dir ) ) {
56
				$relative_path = str_replace( $content_dir, '', Kirki::$path );
57
				Kirki::$url = content_url( $relative_path );
58
			}
59
		}
60
61
		// If Kirki is installed as a plugin, use that for the URL.
62
		if ( Kirki_Util::is_plugin() ) {
63
			Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE );
64
		}
65
66
		// Get the path to the theme.
67
		$theme_path = wp_normalize_path( get_template_directory() );
68
69
		// Is Kirki included in the theme?
70
		if ( false !== strpos( Kirki::$path, $theme_path ) ) {
71
			Kirki::$url = get_template_directory_uri() . str_replace( $theme_path, '', Kirki::$path );
72
		}
73
74
		// Is there a child-theme?
75
		$child_theme_path = wp_normalize_path( get_stylesheet_directory_uri() );
76
		if ( $child_theme_path !== $theme_path ) {
77
			// Is Kirki included in a child theme?
78
			if ( false !== strpos( Kirki::$path, $child_theme_path ) ) {
79
				Kirki::$url = get_template_directory_uri() . str_replace( $child_theme_path, '', Kirki::$path );
80
			}
81
		}
82
83
		// Apply the kirki/config filter.
84
		$config = apply_filters( 'kirki/config', array() );
85
		if ( isset( $config['url_path'] ) ) {
86
			Kirki::$url = $config['url_path'];
87
		}
88
89
		// Escapes the URL.
90
		Kirki::$url = esc_url_raw( Kirki::$url );
91
		// Make sure the right protocol is used.
92
		Kirki::$url = set_url_scheme( Kirki::$url );
93
	}
94
95
	/**
96
	 * Helper function that adds the fields, sections and panels to the customizer.
97
	 *
98
	 * @return void
99
	 */
100
	public function add_to_customizer() {
101
		$this->fields_from_filters();
102
		add_action( 'customize_register', array( $this, 'register_section_types' ) );
103
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
104
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
105
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
106
	}
107
108
	/**
109
	 * Register control types
110
	 *
111
	 * @return  void
112
	 */
113
	public function register_section_types() {
114
		global $wp_customize;
115
116
		$section_types = apply_filters( 'kirki/section_types', array() );
117
		foreach ( $section_types as $section_type ) {
118
			$wp_customize->register_section_type( $section_type );
119
		}
120
	}
121
122
	/**
123
	 * Register our panels to the WordPress Customizer.
124
	 *
125
	 * @access public
126
	 */
127
	public function add_panels() {
128
		if ( ! empty( Kirki::$panels ) ) {
129
			foreach ( Kirki::$panels as $panel_args ) {
130
				// Extra checks for nested panels.
131
				if ( isset( $panel_args['panel'] ) ) {
132
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
133
						// Set the type to nested.
134
						$panel_args['type'] = 'kirki-nested';
135
					}
136
				}
137
138
				new Kirki_Panel( $panel_args );
139
			}
140
		}
141
	}
142
143
	/**
144
	 * Register our sections to the WordPress Customizer.
145
	 *
146
	 * @var object The WordPress Customizer object
147
	 * @return void
148
	 */
149
	public function add_sections() {
150
		if ( ! empty( Kirki::$sections ) ) {
151
			foreach ( Kirki::$sections as $section_args ) {
152
				// Extra checks for nested sections.
153
				if ( isset( $section_args['section'] ) ) {
154
					if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) {
155
						// Set the type to nested.
156
						$section_args['type'] = 'kirki-nested';
157
						// We need to check if the parent section is nested inside a panel.
158
						$parent_section = Kirki::$sections[ $section_args['section'] ];
159
						if ( isset( $parent_section['panel'] ) ) {
160
							$section_args['panel'] = $parent_section['panel'];
161
						}
162
					}
163
				}
164
				new Kirki_Section( $section_args );
165
			}
166
		}
167
	}
168
169
	/**
170
	 * Create the settings and controls from the $fields array and register them.
171
	 *
172
	 * @var object The WordPress Customizer object.
173
	 * @return void
174
	 */
175
	public function add_fields() {
176
177
		global $wp_customize;
178
		foreach ( Kirki::$fields as $args ) {
179
180
			// Create the settings.
181
			new Kirki_Settings( $args );
182
183
			// Check if we're on the customizer.
184
			// If we are, then we will create the controls, add the scripts needed for the customizer
185
			// and any other tweaks that this field may require.
186
			if ( $wp_customize ) {
187
188
				// Create the control.
189
				new Kirki_Control( $args );
190
191
			}
192
		}
193
	}
194
195
	/**
196
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
197
	 * These filters are no longer used, this is simply for backwards-compatibility.
198
	 *
199
	 * @access private
200
	 * @since 2.0.0
201
	 */
202
	private function fields_from_filters() {
203
204
		$fields = apply_filters( 'kirki/controls', array() );
205
		$fields = apply_filters( 'kirki/fields', $fields );
206
207
		if ( ! empty( $fields ) ) {
208
			foreach ( $fields as $field ) {
209
				Kirki::add_field( 'global', $field );
210
			}
211
		}
212
	}
213
214
	/**
215
	 * Alias for the is_plugin static method in the Kirki_Util class.
216
	 * This is here for backwards-compatibility purposes.
217
	 *
218
	 * @static
219
	 * @access public
220
	 * @since 3.0.0
221
	 * @return bool
222
	 */
223
	public static function is_plugin() {
224
		// Return result using the Kirki_Util class.
225
		return Kirki_Util::is_plugin();
226
	}
227
228
	/**
229
	 * Alias for the get_variables static method in the Kirki_Util class.
230
	 * This is here for backwards-compatibility purposes.
231
	 *
232
	 * @static
233
	 * @access public
234
	 * @since 2.0.0
235
	 * @return array Formatted as array( 'variable-name' => value ).
236
	 */
237
	public static function get_variables() {
238
		// Log error for developers.
239
		_doing_it_wrong( __METHOD__, esc_attr__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.0.10' );
240
		// Return result using the Kirki_Util class.
241
		return Kirki_Util::get_variables();
242
	}
243
244
}
245