-
-
Notifications
You must be signed in to change notification settings - Fork 413
Expand file tree
/
Copy pathInit.php
More file actions
310 lines (272 loc) · 9 KB
/
Init.php
File metadata and controls
310 lines (272 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* Initializes Kirki
*
* @package Kirki
* @category Core
* @author Themeum
* @copyright Copyright (c) 2023, Themeum
* @license https://opensource.org/licenses/MIT
* @since 1.0
*/
namespace Kirki\Compatibility;
/**
* Initialize Kirki
*/
class Init {
/**
* Control types.
*
* @access private
* @since 3.0.0
* @var array
*/
private $control_types = [];
/**
* Should we show a nag for the deprecated fontawesome field?
*
* @static
* @access private
* @since 3.0.42
* @var bool
*/
private static $show_fa_nag = false;
/**
* The class constructor.
*/
public function __construct() {
add_action( 'wp_loaded', [ $this, 'add_to_customizer' ], 1 );
add_filter( 'kirki_control_types', [ $this, 'default_control_types' ] );
add_action( 'customize_register', [ $this, 'remove_controls' ], 99999 );
add_action( 'admin_notices', [ $this, 'admin_notices' ] );
add_action( 'admin_init', [ $this, 'dismiss_nag' ] );
// ? Bagus: is this necessary? The Values class doesn't have constructor, so this does nothing.
new Values();
}
/**
* Add the default Kirki control types.
*
* @access public
* @since 3.0.0
* @param array $control_types The control types array.
* @return array
*/
public function default_control_types( $control_types = [] ) {
$this->control_types = [
'kirki-composite' => '\Kirki\Control\Composite',
'checkbox' => '\Kirki\Control\Checkbox',
'kirki-color' => '\Kirki\Control\ReactColorful',
'kirki-color-palette' => '\Kirki\Control\Color_Palette',
'kirki-custom' => '\Kirki\Control\Custom',
'kirki-date' => '\Kirki\Control\Date',
'kirki-dashicons' => '\Kirki\Control\Dashicons',
'kirki-dimension' => '\Kirki\Control\Dimension',
'kirki-dimensions' => '\Kirki\Control\Dimensions',
'kirki-editor' => '\Kirki\Control\Editor',
'kirki-image' => '\Kirki\Control\Image',
'kirki-multicolor' => '\Kirki\Control\Multicolor',
'kirki-multicheck' => '\Kirki\Control\Multicheck',
'kirki-number' => '\Kirki\Control\Number',
'kirki-radio' => '\Kirki\Control\Radio',
'kirki-radio-buttonset' => '\Kirki\Control\Radio_Buttonset',
'kirki-radio-image' => '\Kirki\Control\Radio_Image',
'repeater' => '\Kirki\Control\Repeater',
'kirki-select' => '\Kirki\Control\Select',
'kirki-slider' => '\Kirki\Control\Slider',
'kirki-sortable' => '\Kirki\Control\Sortable',
'kirki-spacing' => '\Kirki\Control\Dimensions',
'kirki-switch' => '\Kirki\Control\Checkbox_Switch',
'kirki-generic' => '\Kirki\Control\Generic',
'kirki-toggle' => '\Kirki\Control\Checkbox_Toggle',
'image' => '\Kirki\Control\Image',
'cropped_image' => '\Kirki\Control\Cropped_Image',
'upload' => '\Kirki\Control\Upload',
];
return array_merge( $this->control_types, $control_types );
}
/**
* Helper function that adds the fields to the customizer.
*/
public function add_to_customizer() {
$this->fields_from_filters();
add_action( 'customize_register', [ $this, 'register_control_types' ] );
add_action( 'customize_register', [ $this, 'add_fields' ], 99 );
}
/**
* Register control types
*/
public function register_control_types() {
global $wp_customize;
$this->control_types = $this->default_control_types();
if ( ! class_exists( 'WP_Customize_Code_Editor_Control' ) ) {
unset( $this->control_types['code_editor'] );
}
foreach ( $this->control_types as $key => $classname ) {
if ( ! class_exists( $classname ) ) {
unset( $this->control_types[ $key ] );
}
}
$skip_control_types = apply_filters(
'kirki_control_types_exclude',
[
'\Kirki\Control\Repeater',
'\WP_Customize_Control',
]
);
foreach ( $this->control_types as $control_type ) {
if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
$wp_customize->register_control_type( $control_type );
}
}
}
/**
* Create the settings and controls from the $fields array and register them.
*
* @var object The WordPress Customizer object.
*/
public function add_fields() {
global $wp_customize;
foreach ( Kirki::$fields as $args ) {
// Create the settings.
new \Kirki\Compatibility\Settings( $args );
// Check if we're on the customizer.
// If we are, then we will create the controls, add the scripts needed for the customizer
// and any other tweaks that this field may require.
if ( $wp_customize ) {
// Create the control.
new Control( $args );
}
}
}
/**
* Process fields added using the 'kirki_fields' and 'kirki_controls' filter.
* These filters are no longer used, this is simply for backwards-compatibility.
*
* @access private
* @since 2.0.0
*/
private function fields_from_filters() {
$fields = apply_filters( 'kirki_controls', [] );
$fields = apply_filters( 'kirki_fields', $fields );
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
$field['kirki_config'] = 'global';
Kirki::add_field( 'global', $field );
}
}
}
/**
* Alias for the is_plugin static method in the Kirki\Util\Util class.
* This is here for backwards-compatibility purposes.
*
* @static
* @access public
* @since 3.0.0
* @return bool
*/
public static function is_plugin() {
return Util::is_plugin();
}
/**
* Alias for the get_variables static method in the Kirki\Util\Util class.
* This is here for backwards-compatibility purposes.
*
* @static
* @access public
* @since 2.0.0
* @return array Formatted as array( 'variable-name' => value ).
*/
public static function get_variables() {
// Log error for developers.
_doing_it_wrong( __METHOD__, esc_html__( 'We detected you\'re using Kirki\Compatibility\Init::get_variables(). Please use \Kirki\Util\Util::get_variables() instead.', 'kirki' ), '3.0.10' );
// ! This will be failed, because Util class is under Kirki\Util namespace.
return Util::get_variables();
}
/**
* Remove controls.
*
* @since 3.0.17
* @param object $wp_customize The customizer object.
* @return void
*/
public function remove_controls( $wp_customize ) {
foreach ( Kirki::$controls_to_remove as $control ) {
$wp_customize->remove_control( $control );
}
}
/**
* Shows an admin notice.
*
* @access public
* @since 3.0.42
* @return void
*/
public function admin_notices() {
// No need for a nag if we don't need to recommend installing the FA plugin.
if ( ! self::$show_fa_nag ) {
return;
}
// No need for a nag if FA plugin is already installed.
if ( defined( 'FONTAWESOME_DIR_PATH' ) ) {
return;
}
// No need for a nag if current user can't install plugins.
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
// No need for a nag if user has dismissed it.
$dismissed = get_user_meta( get_current_user_id(), 'kirki_fa_nag_dismissed', true );
if ( true === $dismissed || 1 === $dismissed || '1' === $dismissed ) {
return;
}
?>
<div class="notice notice-info is-dismissible">
<p>
<?php esc_html_e( 'Your theme uses a Font Awesome field for icons. To avoid issues with missing icons on your frontend we recommend you install the official Font Awesome plugin.', 'kirki' ); ?>
</p>
<p>
<a class="button button-primary" href="<?php echo esc_url( admin_url( 'plugin-install.php?tab=plugin-information&plugin=font-awesome&TB_iframe=true&width=600&height=550' ) ); ?>"><?php esc_html_e( 'Install Plugin', 'kirki' ); ?></a>
<a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( '?dismiss-nag=font-awesome-kirki' ), 'kirki-dismiss-nag', 'nonce' ) ); ?>"><?php esc_html_e( 'Don\'t show this again', 'kirki' ); ?></a>
</p>
</div>
<?php
}
/**
* Dismisses the nag.
*
* @access public
* @since 3.0.42
* @return void
*/
public function dismiss_nag() {
if ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'kirki-dismiss-nag' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
if ( get_current_user_id() && isset( $_GET['dismiss-nag'] ) && 'font-awesome-kirki' === $_GET['dismiss-nag'] ) {
update_user_meta( get_current_user_id(), 'kirki_fa_nag_dismissed', true );
}
}
}
/**
* Handles showing a nag if the theme is using the deprecated fontawesome field
*
* @static
* @access protected
* @since 3.0.42
* @param array $args The field arguments.
* @return void
*/
protected static function maybe_show_fontawesome_nag( $args ) {
// If we already know we want it, skip check.
if ( self::$show_fa_nag ) {
return;
}
// Check if the field is fontawesome.
if ( isset( $args['type'] ) && in_array( $args['type'], [ 'fontawesome', 'kirki-fontawesome' ], true ) ) {
// Skip check if theme has disabled FA enqueueing via a filter.
if ( ! apply_filters( 'kirki_load_fontawesome', true ) ) {
return;
}
// If we got this far, we need to show the nag.
self::$show_fa_nag = true;
}
}
}