widgets.php
11.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Declaring widgets
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'understrap_add_widget_categories_class' ) ) {
/**
* Adds Bootstrap class to select tag in the Categories widget.
*
* @since 1.2.0
*
* @param array $cat_args An array of Categories widget drop-down arguments.
* @return array The filtered array of Categories widget drop-down arguments.
*/
function understrap_add_widget_categories_class( $cat_args ) {
if ( isset( $cat_args['class'] ) ) {
$cat_args['class'] .= ' ' . understrap_get_select_control_class();
} else {
$cat_args['class'] = understrap_get_select_control_class();
}
return $cat_args;
}
}
add_filter( 'widget_categories_dropdown_args', 'understrap_add_widget_categories_class' );
if ( ! function_exists( 'understrap_add_block_widget_categories_class' ) ) {
/**
* Adds Bootstrap class to select tag in the Categories block widget.
*
* @since 1.2.0
*
* @param string $output The taxonomy drop-down HTML output.
* @param array $parsed_args Arguments used to build the drop-down.
* @return string The filtered taxonomy drop-down HTML output.
*/
function understrap_add_block_widget_categories_class( $output, $parsed_args ) {
$class = understrap_get_select_control_class();
if ( isset( $parsed_args['class'] ) && ! empty( $parsed_args['class'] ) ) {
$search = array(
"class=\"{$parsed_args['class']}\"",
"class='{$parsed_args['class']}'",
);
$replace = array(
"class=\"{$parsed_args['class']} {$class}\"",
"class=\"{$parsed_args['class']} {$class}\"",
);
} else {
$search = '<select';
$replace = "<select class=\"{$class}\"";
}
return str_replace( $search, $replace, $output );
}
}
add_filter( 'wp_dropdown_cats', 'understrap_add_block_widget_categories_class', 10, 2 );
if ( ! function_exists( 'understrap_add_block_widget_archives_classes' ) ) {
/**
* Adds Bootstrap class to select tag in the Archives widget.
*
* @since 1.2.0
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @return string The filtered block content.
*/
function understrap_add_block_widget_archives_classes( $block_content, $block ) {
if ( isset( $block['attrs']['displayAsDropdown'] ) && true === $block['attrs']['displayAsDropdown'] ) {
return str_replace(
'<select',
'<select class="' . understrap_get_select_control_class() . '"',
$block_content
);
}
return $block_content;
}
}
add_filter( 'render_block_core/archives', 'understrap_add_block_widget_archives_classes', 10, 2 );
if ( ! function_exists( 'understrap_add_block_widget_search_classes' ) ) {
/**
* Adds Bootstrap classes to search block widget.
*
* @since 1.2.0
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @return string The filtered block content.
*/
function understrap_add_block_widget_search_classes( $block_content, $block ) {
$search = array(
'wp-block-search__input ',
'wp-block-search__input"',
'wp-block-search__button ',
);
$replace = array(
'wp-block-search__input form-control ',
'wp-block-search__input form-control"',
'wp-block-search__button btn btn-primary ',
);
if ( isset( $block['attrs']['buttonPosition'] ) && 'button-inside' === $block['attrs']['buttonPosition'] ) {
$search[] = 'wp-block-search__inside-wrapper';
$replace[] = 'wp-block-search__inside-wrapper input-group';
if ( 'bootstrap4' === get_theme_mod( 'understrap_bootstrap_version', 'bootstrap4' ) ) {
$search[] = '<button';
$search[] = '</button>';
$replace[] = '<div class="input-group-append"><button';
$replace[] = '</button></div>';
}
}
return str_replace( $search, $replace, $block_content );
}
}
add_filter( 'render_block_core/search', 'understrap_add_block_widget_search_classes', 10, 2 );
/**
* Add active class to first item of carousel hero widget area.
*
* @since 1.2.0
*
* @param array $params {
* Parameters passed to a widget’s display callback.
*
* @type array $args {
* An array of widget display arguments.
*
* @type string $name Name of the sidebar the widget is assigned to.
* @type string $id ID of the sidebar the widget is assigned to.
* @type string $description The sidebar description.
* @type string $class CSS class applied to the sidebar container.
* @type string $before_widget HTML markup to prepend to each widget in the sidebar.
* @type string $after_widget HTML markup to append to each widget in the sidebar.
* @type string $before_title HTML markup to prepend to the widget title when displayed.
* @type string $after_title HTML markup to append to the widget title when displayed.
* @type string $widget_id ID of the widget.
* @type string $widget_name Name of the widget.
* }
* @type array $widget_args {
* An array of multi-widget arguments.
*
* @type int $number Number increment used for multiples of the same widget.
* }
* }
* @return array Maybe filtered parameters.
*/
function understrap_hero_active_carousel_item( $params ) {
if (
! isset( $params[0] )
|| ! isset( $params[0]['id'] )
|| 'hero' !== $params[0]['id']
) {
return $params;
}
static $item_number = 1;
if (
1 === $item_number
&& isset( $params[0]['before_widget'] )
&& is_string( $params[0]['before_widget'] )
) {
$params[0]['before_widget'] = str_replace(
'carousel-item',
'carousel-item active',
$params[0]['before_widget']
);
}
$item_number++;
return $params;
}
add_filter( 'dynamic_sidebar_params', 'understrap_hero_active_carousel_item' );
/**
* Add filter to the parameters passed to a widget's display callback.
* The filter is evaluated on both the front and the back end!
*
* @link https://developer.wordpress.org/reference/hooks/dynamic_sidebar_params/
*/
add_filter( 'dynamic_sidebar_params', 'understrap_widget_classes' );
if ( ! function_exists( 'understrap_widget_classes' ) ) {
/**
* Count number of visible widgets in a sidebar and add classes to widgets accordingly,
* so widgets can be displayed one, two, three or four per row.
*
* @global array $sidebars_widgets
*
* @param array $params {
* Parameters passed to a widget’s display callback.
*
* @type array $args {
* An array of widget display arguments.
*
* @type string $name Name of the sidebar the widget is assigned to.
* @type string $id ID of the sidebar the widget is assigned to.
* @type string $description The sidebar description.
* @type string $class CSS class applied to the sidebar container.
* @type string $before_widget HTML markup to prepend to each widget in the sidebar.
* @type string $after_widget HTML markup to append to each widget in the sidebar.
* @type string $before_title HTML markup to prepend to the widget title when displayed.
* @type string $after_title HTML markup to append to the widget title when displayed.
* @type string $widget_id ID of the widget.
* @type string $widget_name Name of the widget.
* }
* @type array $widget_args {
* An array of multi-widget arguments.
*
* @type int $number Number increment used for multiples of the same widget.
* }
* }
* @return array Maybe filtered parameters.
*/
function understrap_widget_classes( $params ) {
global $sidebars_widgets;
/*
* When the corresponding filter is evaluated on the front end
* this takes into account that there might have been made other changes.
*/
$sidebars_widgets_count = apply_filters( 'sidebars_widgets', $sidebars_widgets );
// Only apply changes if sidebar ID is set and the widget's classes depend on the number of widgets in the sidebar.
if ( isset( $params[0]['id'] ) && strpos( $params[0]['before_widget'], 'dynamic-classes' ) ) {
$sidebar_id = $params[0]['id'];
$widget_count = count( $sidebars_widgets_count[ $sidebar_id ] );
$widget_classes = 'widget-count-' . $widget_count;
if ( 0 === $widget_count % 4 || $widget_count > 6 ) {
// Four widgets per row if there are exactly four or more than six widgets.
$widget_classes .= ' col-md-3';
} elseif ( 6 === $widget_count ) {
// If exactly six widgets are published.
$widget_classes .= ' col-md-2';
} elseif ( $widget_count >= 3 ) {
// Three widgets per row if there's three or more widgets.
$widget_classes .= ' col-md-4';
} elseif ( 2 === $widget_count ) {
// If two widgets are published.
$widget_classes .= ' col-md-6';
} elseif ( 1 === $widget_count ) {
// If just on widget is active.
$widget_classes .= ' col-md-12';
}
// Replace the placeholder class 'dynamic-classes' with the classes stored in $widget_classes.
$params[0]['before_widget'] = str_replace( 'dynamic-classes', $widget_classes, $params[0]['before_widget'] );
}
return $params;
}
} // End of if function_exists( 'understrap_widget_classes' ).
add_action( 'widgets_init', 'understrap_widgets_init' );
if ( ! function_exists( 'understrap_widgets_init' ) ) {
/**
* Initializes themes widgets.
*/
function understrap_widgets_init() {
register_sidebar(
array(
'name' => __( 'Right Sidebar', 'understrap' ),
'id' => 'right-sidebar',
'description' => __( 'Right sidebar widget area', 'understrap' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => __( 'Left Sidebar', 'understrap' ),
'id' => 'left-sidebar',
'description' => __( 'Left sidebar widget area', 'understrap' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => __( 'Hero Slider', 'understrap' ),
'id' => 'hero',
'description' => __( 'Hero slider area. Place two or more widgets here and they will slide!', 'understrap' ),
'before_widget' => '<div class="carousel-item">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
)
);
register_sidebar(
array(
'name' => __( 'Hero Canvas', 'understrap' ),
'id' => 'herocanvas',
'description' => __( 'Full size canvas hero area for Bootstrap and other custom HTML markup', 'understrap' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
)
);
register_sidebar(
array(
'name' => __( 'Top Full', 'understrap' ),
'id' => 'statichero',
'description' => __( 'Full top widget with dynamic grid', 'understrap' ),
'before_widget' => '<div id="%1$s" class="static-hero-widget %2$s dynamic-classes">',
'after_widget' => '</div><!-- .static-hero-widget -->',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'name' => __( 'Footer Full', 'understrap' ),
'id' => 'footerfull',
'description' => __( 'Full sized footer widget with dynamic grid', 'understrap' ),
'before_widget' => '<div id="%1$s" class="footer-widget %2$s dynamic-classes">',
'after_widget' => '</div><!-- .footer-widget -->',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
}
} // End of function_exists( 'understrap_widgets_init' ).