1 | <?php |
||
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 | |||
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 | 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 | |||
45 | Kirki::$path = wp_normalize_path( dirname( KIRKI_PLUGIN_FILE ) ); |
||
46 | |||
47 | // Works in most cases. |
||
48 | // Serves as a fallback in case all other checks fail. |
||
49 | if ( defined( 'WP_CONTENT_DIR' ) ) { |
||
50 | $content_dir = wp_normalize_path( WP_CONTENT_DIR ); |
||
51 | if ( false !== strpos( Kirki::$path, $content_dir ) ) { |
||
52 | $relative_path = str_replace( $content_dir, '', Kirki::$path ); |
||
53 | Kirki::$url = content_url( $relative_path ); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | // If Kirki is installed as a plugin, use that for the URL. |
||
58 | if ( Kirki_Util::is_plugin() ) { |
||
59 | Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE ); |
||
60 | } |
||
61 | |||
62 | // Get the path to the theme. |
||
63 | $theme_path = wp_normalize_path( get_template_directory() ); |
||
64 | |||
65 | // Is Kirki included in the theme? |
||
66 | if ( false !== strpos( Kirki::$path, $theme_path ) ) { |
||
67 | Kirki::$url = get_template_directory_uri() . str_replace( $theme_path, '', Kirki::$path ); |
||
68 | } |
||
69 | |||
70 | // Is there a child-theme? |
||
71 | $child_theme_path = wp_normalize_path( get_stylesheet_directory_uri() ); |
||
72 | if ( $child_theme_path !== $theme_path ) { |
||
73 | // Is Kirki included in a child theme? |
||
74 | if ( false !== strpos( Kirki::$path, $child_theme_path ) ) { |
||
75 | Kirki::$url = get_template_directory_uri() . str_replace( $child_theme_path, '', Kirki::$path ); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | // Apply the kirki/config filter. |
||
80 | $config = apply_filters( 'kirki/config', array() ); |
||
81 | if ( isset( $config['url_path'] ) ) { |
||
82 | Kirki::$url = $config['url_path']; |
||
83 | } |
||
84 | |||
85 | // Escapes the URL. |
||
86 | Kirki::$url = esc_url_raw( Kirki::$url ); |
||
87 | // Make sure the right protocol is used. |
||
88 | Kirki::$url = set_url_scheme( Kirki::$url ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Add the default Kirki control types. |
||
93 | * |
||
94 | * @access public |
||
95 | * @since 3.0.0 |
||
96 | * @param array $control_types The control types array. |
||
97 | * @return array |
||
98 | */ |
||
99 | public function default_control_types( $control_types = array() ) { |
||
100 | |||
101 | $this->control_types = array( |
||
102 | 'checkbox' => 'WP_Customize_Control', |
||
103 | 'kirki-background' => 'Kirki_Control_Background', |
||
104 | 'kirki-code' => 'Kirki_Control_Code', |
||
105 | 'kirki-color' => 'Kirki_Control_Color', |
||
106 | 'kirki-color-palette' => 'Kirki_Control_Color_Palette', |
||
107 | 'kirki-custom' => 'Kirki_Control_Custom', |
||
108 | 'kirki-date' => 'Kirki_Control_Date', |
||
109 | 'kirki-dashicons' => 'Kirki_Control_Dashicons', |
||
110 | 'kirki-dimension' => 'Kirki_Control_Dimension', |
||
111 | 'kirki-dimensions' => 'Kirki_Control_Dimensions', |
||
112 | 'kirki-editor' => 'Kirki_Control_Editor', |
||
113 | 'kirki-fontawesome' => 'Kirki_Control_FontAwesome', |
||
114 | 'kirki-gradient' => 'Kirki_Control_Gradient', |
||
115 | 'kirki-image' => 'Kirki_Control_Image', |
||
116 | 'kirki-multicolor' => 'Kirki_Control_Multicolor', |
||
117 | 'kirki-multicheck' => 'Kirki_Control_MultiCheck', |
||
118 | 'kirki-number' => 'Kirki_Control_Number', |
||
119 | 'kirki-palette' => 'Kirki_Control_Palette', |
||
120 | 'kirki-preset' => 'Kirki_Control_Preset', |
||
121 | 'kirki-radio' => 'Kirki_Control_Radio', |
||
122 | 'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet', |
||
123 | 'kirki-radio-image' => 'Kirki_Control_Radio_Image', |
||
124 | 'repeater' => 'Kirki_Control_Repeater', |
||
125 | 'kirki-select' => 'Kirki_Control_Select', |
||
126 | 'kirki-slider' => 'Kirki_Control_Slider', |
||
127 | 'kirki-sortable' => 'Kirki_Control_Sortable', |
||
128 | 'kirki-spacing' => 'Kirki_Control_Dimensions', |
||
129 | 'kirki-switch' => 'Kirki_Control_Switch', |
||
130 | 'kirki-generic' => 'Kirki_Control_Generic', |
||
131 | 'kirki-toggle' => 'Kirki_Control_Toggle', |
||
132 | 'kirki-typography' => 'Kirki_Control_Typography', |
||
133 | 'image' => 'Kirki_Control_Image', |
||
134 | 'cropped_image' => 'WP_Customize_Cropped_Image_Control', |
||
135 | 'upload' => 'WP_Customize_Upload_Control', |
||
136 | ); |
||
137 | return array_merge( $this->control_types, $control_types ); |
||
138 | |||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Helper function that adds the fields, sections and panels to the customizer. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | public function add_to_customizer() { |
||
147 | $this->fields_from_filters(); |
||
148 | add_action( 'customize_register', array( $this, 'register_control_types' ) ); |
||
149 | add_action( 'customize_register', array( $this, 'add_panels' ), 97 ); |
||
150 | add_action( 'customize_register', array( $this, 'add_sections' ), 98 ); |
||
151 | add_action( 'customize_register', array( $this, 'add_fields' ), 99 ); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Register control types |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | public function register_control_types() { |
||
160 | global $wp_customize; |
||
161 | |||
162 | $section_types = apply_filters( 'kirki/section_types', array() ); |
||
163 | foreach ( $section_types as $section_type ) { |
||
164 | $wp_customize->register_section_type( $section_type ); |
||
165 | } |
||
166 | |||
167 | $this->control_types = $this->default_control_types( apply_filters( 'kirki/control_types', $this->control_types ) ) |
||
168 | foreach ( $this->control_types as $key => $classname ) { |
||
|
|||
169 | if ( ! class_exists( $classname ) ) { |
||
170 | unset( $control_types[ $key ] ); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array( |
||
175 | 'Kirki_Control_Repeater', |
||
176 | 'WP_Customize_Control', |
||
177 | ) ); |
||
178 | |||
179 | foreach ( $this->control_types as $control_type ) { |
||
180 | if ( ! in_array( $control_type, $do_not_register_control_types, true ) && class_exists( $control_type ) ) { |
||
181 | $wp_customize->register_control_type( $control_type ); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Register our panels to the WordPress Customizer. |
||
188 | * |
||
189 | * @access public |
||
190 | */ |
||
191 | public function add_panels() { |
||
192 | if ( ! empty( Kirki::$panels ) ) { |
||
193 | foreach ( Kirki::$panels as $panel_args ) { |
||
194 | // Extra checks for nested panels. |
||
195 | if ( isset( $panel_args['panel'] ) ) { |
||
196 | if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) { |
||
197 | // Set the type to nested. |
||
198 | $panel_args['type'] = 'kirki-nested'; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | new Kirki_Panel( $panel_args ); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Register our sections to the WordPress Customizer. |
||
209 | * |
||
210 | * @var object The WordPress Customizer object |
||
211 | * @return void |
||
212 | */ |
||
213 | public function add_sections() { |
||
214 | if ( ! empty( Kirki::$sections ) ) { |
||
215 | foreach ( Kirki::$sections as $section_args ) { |
||
216 | // Extra checks for nested sections. |
||
217 | if ( isset( $section_args['section'] ) ) { |
||
218 | if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) { |
||
219 | // Set the type to nested. |
||
220 | $section_args['type'] = 'kirki-nested'; |
||
221 | // We need to check if the parent section is nested inside a panel. |
||
222 | $parent_section = Kirki::$sections[ $section_args['section'] ]; |
||
223 | if ( isset( $parent_section['panel'] ) ) { |
||
224 | $section_args['panel'] = $parent_section['panel']; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | new Kirki_Section( $section_args ); |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Create the settings and controls from the $fields array and register them. |
||
235 | * |
||
236 | * @var object The WordPress Customizer object |
||
237 | * @return void |
||
238 | */ |
||
239 | public function add_fields() { |
||
240 | |||
241 | global $wp_customize; |
||
242 | foreach ( Kirki::$fields as $args ) { |
||
243 | |||
244 | // Create the settings. |
||
245 | new Kirki_Settings( $args ); |
||
246 | |||
247 | // Check if we're on the customizer. |
||
248 | // If we are, then we will create the controls, add the scripts needed for the customizer |
||
249 | // and any other tweaks that this field may require. |
||
250 | if ( $wp_customize ) { |
||
251 | |||
252 | // Create the control. |
||
253 | new Kirki_Control( $args ); |
||
254 | |||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Process fields added using the 'kirki/fields' and 'kirki/controls' filter. |
||
261 | * These filters are no longer used, this is simply for backwards-compatibility. |
||
262 | * |
||
263 | * @access private |
||
264 | * @since 2.0.0 |
||
265 | */ |
||
266 | private function fields_from_filters() { |
||
267 | |||
268 | $fields = apply_filters( 'kirki/controls', array() ); |
||
269 | $fields = apply_filters( 'kirki/fields', $fields ); |
||
270 | |||
271 | if ( ! empty( $fields ) ) { |
||
272 | foreach ( $fields as $field ) { |
||
273 | Kirki::add_field( 'global', $field ); |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Alias for the is_plugin static method in the Kirki_Util class. |
||
280 | * This is here for backwards-compatibility purposes. |
||
281 | * |
||
282 | * @static |
||
283 | * @access public |
||
284 | * @since 3.0.0 |
||
285 | * @return bool |
||
286 | */ |
||
287 | public static function is_plugin() { |
||
288 | _doing_it_wrong( __METHOD__, esc_attr__( 'We detected you\'re using Kirki_Init::is_plugin(). Please use Kirki_Util::is_plugin() instead.', 'kirki' ), '3.1' ); |
||
289 | // Return result using the Kirki_Util class. |
||
290 | return Kirki_Util::is_plugin(); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Alias for the get_variables static method in the Kirki_Util class. |
||
295 | * This is here for backwards-compatibility purposes. |
||
296 | * |
||
297 | * @static |
||
298 | * @access public |
||
299 | * @since 2.0.0 |
||
300 | * @return array Formatted as array( 'variable-name' => value ). |
||
301 | */ |
||
302 | public static function get_variables() { |
||
303 | // Log error for developers. |
||
304 | _doing_it_wrong( __METHOD__, esc_attr__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.1' ); |
||
305 | // Return result using the Kirki_Util class. |
||
306 | return Kirki_Util::get_variables(); |
||
307 | } |
||
308 | |||
309 | } |
||
310 |