Completed
Pull Request — develop (#1517)
by Aristeides
07:18 queued 04:36
created

Kirki_Init::set_url()   C

Complexity

Conditions 8
Paths 72

Size

Total Lines 47
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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