customizer.php
9.74 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Understrap Theme Customizer
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'understrap_customize_register' ) ) {
/**
* Register basic support (site title, header text color) for the Theme Customizer.
*
* @param WP_Customize_Manager $wp_customize Customizer reference.
*/
function understrap_customize_register( $wp_customize ) {
$settings = array( 'blogname', 'header_textcolor' );
foreach ( $settings as $setting ) {
$get_setting = $wp_customize->get_setting( $setting );
if ( $get_setting instanceof WP_Customize_Setting ) {
$get_setting->transport = 'postMessage';
}
}
// Override default partial for custom logo.
$wp_customize->selective_refresh->add_partial(
'custom_logo',
array(
'settings' => array( 'custom_logo' ),
'selector' => '.custom-logo-link',
'render_callback' => 'understrap_customize_partial_custom_logo',
'container_inclusive' => false,
)
);
}
}
add_action( 'customize_register', 'understrap_customize_register' );
if ( ! function_exists( 'understrap_customize_partial_custom_logo' ) ) {
/**
* Callback for rendering the custom logo, used in the custom_logo partial.
*
* @return string The custom logo markup or the site title.
*/
function understrap_customize_partial_custom_logo() {
if ( has_custom_logo() ) {
return get_custom_logo();
} else {
return get_bloginfo( 'name' );
}
}
}
if ( ! function_exists( 'understrap_theme_customize_register' ) ) {
/**
* Register individual settings through customizer's API.
*
* @param WP_Customize_Manager $wp_customize Customizer reference.
*/
function understrap_theme_customize_register( $wp_customize ) {
// Theme layout settings.
$wp_customize->add_section(
'understrap_theme_layout_options',
array(
'title' => __( 'Theme Layout Settings', 'understrap' ),
'capability' => 'edit_theme_options',
'description' => __( 'Container width and sidebar defaults', 'understrap' ),
'priority' => apply_filters( 'understrap_theme_layout_options_priority', 160 ),
)
);
$wp_customize->add_setting(
'understrap_bootstrap_version',
array(
'default' => 'bootstrap4',
'type' => 'theme_mod',
'sanitize_callback' => 'sanitize_text_field',
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'understrap_bootstrap_version',
array(
'label' => __( 'Bootstrap Version', 'understrap' ),
'description' => __( 'Choose between Bootstrap 4 or Bootstrap 5', 'understrap' ),
'section' => 'understrap_theme_layout_options',
'type' => 'select',
'choices' => array(
'bootstrap4' => __( 'Bootstrap 4', 'understrap' ),
'bootstrap5' => __( 'Bootstrap 5', 'understrap' ),
),
'priority' => apply_filters( 'understrap_bootstrap_version_priority', 10 ),
)
)
);
$wp_customize->add_setting(
'understrap_container_type',
array(
'default' => 'container',
'type' => 'theme_mod',
'sanitize_callback' => 'understrap_customize_sanitize_select',
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'understrap_container_type',
array(
'label' => __( 'Container Width', 'understrap' ),
'description' => __( 'Choose between Bootstrap\'s container and container-fluid', 'understrap' ),
'section' => 'understrap_theme_layout_options',
'type' => 'select',
'choices' => array(
'container' => __( 'Fixed width container', 'understrap' ),
'container-fluid' => __( 'Full width container', 'understrap' ),
),
'priority' => apply_filters( 'understrap_container_type_priority', 10 ),
)
)
);
$wp_customize->add_setting(
'understrap_navbar_type',
array(
'default' => 'collapse',
'type' => 'theme_mod',
'sanitize_callback' => 'understrap_customize_sanitize_select',
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'understrap_navbar_type',
array(
'label' => __( 'Responsive Navigation Type', 'understrap' ),
'description' => __(
'Choose between an expanding and collapsing navbar or an offcanvas drawer.',
'understrap'
),
'section' => 'understrap_theme_layout_options',
'type' => 'select',
'choices' => array(
'collapse' => __( 'Collapse', 'understrap' ),
'offcanvas' => __( 'Offcanvas', 'understrap' ),
),
'priority' => apply_filters( 'understrap_navbar_type_priority', 20 ),
)
)
);
$wp_customize->add_setting(
'understrap_sidebar_position',
array(
'default' => 'right',
'type' => 'theme_mod',
'sanitize_callback' => 'understrap_customize_sanitize_select',
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'understrap_sidebar_position',
array(
'label' => __( 'Sidebar Positioning', 'understrap' ),
'description' => __(
'Set sidebar\'s default position. Can either be: right, left, both or none. Note: this can be overridden on individual pages.',
'understrap'
),
'section' => 'understrap_theme_layout_options',
'type' => 'select',
'choices' => array(
'right' => __( 'Right sidebar', 'understrap' ),
'left' => __( 'Left sidebar', 'understrap' ),
'both' => __( 'Left & Right sidebars', 'understrap' ),
'none' => __( 'No sidebar', 'understrap' ),
),
'priority' => apply_filters( 'understrap_sidebar_position_priority', 20 ),
)
)
);
$wp_customize->add_setting(
'understrap_site_info_override',
array(
'default' => '',
'type' => 'theme_mod',
'sanitize_callback' => 'wp_kses_post',
'capability' => 'edit_theme_options',
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'understrap_site_info_override',
array(
'label' => __( 'Footer Site Info', 'understrap' ),
'description' => __( 'Override Understrap\'s site info located at the footer of the page.', 'understrap' ),
'section' => 'understrap_theme_layout_options',
'type' => 'textarea',
'priority' => 20,
)
)
);
$understrap_site_info = $wp_customize->get_setting( 'understrap_site_info_override' );
if ( $understrap_site_info instanceof WP_Customize_Setting ) {
$understrap_site_info->transport = 'postMessage';
}
}
} // End of if function_exists( 'understrap_theme_customize_register' ).
add_action( 'customize_register', 'understrap_theme_customize_register' );
if ( ! function_exists( 'understrap_customize_sanitize_select' ) ) {
/**
* Sanitize select.
*
* @since 1.2.0 Renamed from understrap_theme_slug_sanitize_select()
*
* @param string $input Slug to sanitize.
* @param WP_Customize_Setting $setting Setting instance.
* @return string|bool Sanitized slug if it is a valid choice; the setting default for
* invalid choices and false in all other cases.
*/
function understrap_customize_sanitize_select( $input, $setting ) {
// Ensure input is a slug (lowercase alphanumeric characters, dashes and underscores are allowed only).
$input = sanitize_key( $input );
$control = $setting->manager->get_control( $setting->id );
if ( ! $control instanceof WP_Customize_Control ) {
return false;
}
// Get the list of possible select options.
$choices = $control->choices;
// If the input is a valid key, return it; otherwise, return the default.
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
}
/**
* Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
*/
if ( ! function_exists( 'understrap_customize_preview_js' ) ) {
/**
* Setup JS integration for live previewing.
*/
function understrap_customize_preview_js() {
$file = '/js/customizer.js';
$version = filemtime( get_template_directory() . $file );
if ( false === $version ) {
$version = time();
}
wp_enqueue_script(
'understrap_customizer',
get_template_directory_uri() . $file,
array( 'customize-preview' ),
(string) $version,
true
);
}
}
add_action( 'customize_preview_init', 'understrap_customize_preview_js' );
/**
* Loads javascript for conditionally showing customizer controls.
*/
if ( ! function_exists( 'understrap_customize_controls_js' ) ) {
/**
* Setup JS integration for live previewing.
*
* @since 1.1.0
*/
function understrap_customize_controls_js() {
$file = '/js/customizer-controls.js';
$version = filemtime( get_template_directory() . $file );
if ( false === $version ) {
$version = time();
}
wp_enqueue_script(
'understrap_customizer',
get_template_directory_uri() . $file,
array( 'customize-preview' ),
(string) $version,
true
);
}
}
add_action( 'customize_controls_enqueue_scripts', 'understrap_customize_controls_js' );
if ( ! function_exists( 'understrap_default_navbar_type' ) ) {
/**
* Overrides the responsive navbar type for Bootstrap 4.
*
* @since 1.1.0
*
* @param string $current_mod Current navbar type.
* @return string Maybe filtered navbar type.
*/
function understrap_default_navbar_type( $current_mod ) {
if ( 'bootstrap5' !== get_theme_mod( 'understrap_bootstrap_version' ) ) {
$current_mod = 'collapse';
}
return $current_mod;
}
}
add_filter( 'theme_mod_understrap_navbar_type', 'understrap_default_navbar_type', 20 );