|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Declaring widgets |
|
4
|
|
|
* |
|
5
|
|
|
* @package spurs |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
// Exit if accessed directly. |
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Add filter to the parameters passed to a widget's display callback. |
|
13
|
|
|
* The filter is evaluated on both the front and the back end! |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://developer.wordpress.org/reference/hooks/dynamic_sidebar_params/ |
|
16
|
|
|
*/ |
|
17
|
|
|
add_filter( 'dynamic_sidebar_params', 'spurs_widget_classes' ); |
|
18
|
|
|
|
|
19
|
|
|
if ( ! function_exists( 'spurs_widget_classes' ) ) { |
|
20
|
|
|
/** |
|
21
|
|
|
* Count number of visible widgets in a sidebar and add classes to widgets accordingly, |
|
22
|
|
|
* so widgets can be displayed one, two, three or four per row. |
|
23
|
|
|
* |
|
24
|
|
|
* @global array $sidebars_widgets |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $params { |
|
27
|
|
|
* @type array $args { |
|
28
|
|
|
* An array of widget display arguments. |
|
29
|
|
|
* |
|
30
|
|
|
* @type string $name Name of the sidebar the widget is assigned to. |
|
31
|
|
|
* @type string $id ID of the sidebar the widget is assigned to. |
|
32
|
|
|
* @type string $description The sidebar description. |
|
33
|
|
|
* @type string $class CSS class applied to the sidebar container. |
|
34
|
|
|
* @type string $before_widget HTML markup to prepend to each widget in the sidebar. |
|
35
|
|
|
* @type string $after_widget HTML markup to append to each widget in the sidebar. |
|
36
|
|
|
* @type string $before_title HTML markup to prepend to the widget title when displayed. |
|
37
|
|
|
* @type string $after_title HTML markup to append to the widget title when displayed. |
|
38
|
|
|
* @type string $widget_id ID of the widget. |
|
39
|
|
|
* @type string $widget_name Name of the widget. |
|
40
|
|
|
* } |
|
41
|
|
|
* @type array $widget_args { |
|
42
|
|
|
* An array of multi-widget arguments. |
|
43
|
|
|
* |
|
44
|
|
|
* @type int $number Number increment used for multiples of the same widget. |
|
45
|
|
|
* } |
|
46
|
|
|
* } |
|
47
|
|
|
* @return array $params |
|
48
|
|
|
*/ |
|
49
|
|
|
function spurs_widget_classes( $params ) { |
|
50
|
|
|
|
|
51
|
|
|
global $sidebars_widgets; |
|
52
|
|
|
|
|
53
|
|
|
/* |
|
54
|
|
|
* When the corresponding filter is evaluated on the front end |
|
55
|
|
|
* this takes into account that there might have been made other changes. |
|
56
|
|
|
*/ |
|
57
|
|
|
$sidebars_widgets_count = apply_filters( 'sidebars_widgets', $sidebars_widgets ); |
|
58
|
|
|
|
|
59
|
|
|
// Only apply changes if sidebar ID is set and the widget's classes depend on the number of widgets in the sidebar. |
|
60
|
|
|
if ( isset( $params[0]['id'] ) && strpos( $params[0]['before_widget'], 'dynamic-classes' ) ) { |
|
61
|
|
|
$sidebar_id = $params[0]['id']; |
|
62
|
|
|
$widget_count = count( $sidebars_widgets_count[ $sidebar_id ] ); |
|
63
|
|
|
|
|
64
|
|
|
$widget_classes = 'widget-count-' . $widget_count; |
|
65
|
|
|
if ( 0 === $widget_count % 4 || $widget_count > 6 ) { |
|
66
|
|
|
// Four widgets per row if there are exactly four or more than six. |
|
67
|
|
|
$widget_classes .= ' col-md-3'; |
|
68
|
|
|
} elseif ( 6 === $widget_count ) { |
|
69
|
|
|
// If two widgets are published. |
|
70
|
|
|
$widget_classes .= ' col-md-2'; |
|
71
|
|
|
} elseif ( $widget_count >= 3 ) { |
|
72
|
|
|
// Three widgets per row if there's three or more widgets. |
|
73
|
|
|
$widget_classes .= ' col-md-4'; |
|
74
|
|
|
} elseif ( 2 === $widget_count ) { |
|
75
|
|
|
// If two widgets are published. |
|
76
|
|
|
$widget_classes .= ' col-md-6'; |
|
77
|
|
|
} elseif ( 1 === $widget_count ) { |
|
78
|
|
|
// If just on widget is active. |
|
79
|
|
|
$widget_classes .= ' col-md-12'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Replace the placeholder class 'dynamic-classes' with the classes stored in $widget_classes. |
|
83
|
|
|
$params[0]['before_widget'] = str_replace( 'dynamic-classes', $widget_classes, $params[0]['before_widget'] ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $params; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} // endif function_exists( 'spurs_widget_classes' ). |
|
90
|
|
|
|
|
91
|
|
|
add_action( 'widgets_init', 'spurs_widgets_init' ); |
|
92
|
|
|
if ( ! function_exists( 'spurs_widgets_init' ) ) { |
|
93
|
|
|
/** |
|
94
|
|
|
* Initializes themes widgets. |
|
95
|
|
|
*/ |
|
96
|
|
|
function spurs_widgets_init() { |
|
97
|
|
|
register_sidebar( array( |
|
98
|
|
|
'name' => __( 'Right Sidebar', 'spurs' ), |
|
99
|
|
|
'id' => 'sidebar-right', |
|
100
|
|
|
'description' => 'Right sidebar widget area', |
|
101
|
|
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">', |
|
102
|
|
|
'after_widget' => '</aside>', |
|
103
|
|
|
'before_title' => '<h3 class="widget-title">', |
|
104
|
|
|
'after_title' => '</h3>', |
|
105
|
|
|
) ); |
|
106
|
|
|
|
|
107
|
|
|
register_sidebar( array( |
|
108
|
|
|
'name' => __( 'Left Sidebar', 'spurs' ), |
|
109
|
|
|
'id' => 'sidebar-left', |
|
110
|
|
|
'description' => 'Left sidebar widget area', |
|
111
|
|
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">', |
|
112
|
|
|
'after_widget' => '</aside>', |
|
113
|
|
|
'before_title' => '<h3 class="widget-title">', |
|
114
|
|
|
'after_title' => '</h3>', |
|
115
|
|
|
) ); |
|
116
|
|
|
|
|
117
|
|
|
/*register_sidebar( array( |
|
118
|
|
|
'name' => __( 'Top Full', 'spurs' ), |
|
119
|
|
|
'id' => 'hero-static', |
|
120
|
|
|
'description' => __( 'Full top widget with dynamic grid', 'spurs' ), |
|
121
|
|
|
'before_widget' => '<div id="%1$s" class="hero-static-widget %2$s dynamic-classes">', |
|
122
|
|
|
'after_widget' => '</div>', |
|
123
|
|
|
'before_title' => '<h3 class="widget-title">', |
|
124
|
|
|
'after_title' => '</h3>', |
|
125
|
|
|
) ); |
|
126
|
|
|
|
|
127
|
|
|
register_sidebar( array( |
|
128
|
|
|
'name' => __( 'Footer Full', 'spurs' ), |
|
129
|
|
|
'id' => 'footer-full', |
|
130
|
|
|
'description' => 'Widget area below main content and above footer', |
|
131
|
|
|
'before_widget' => '<div id="%1$s" class="footer-widget %2$s dynamic-classes">', |
|
132
|
|
|
'after_widget' => '</div>', |
|
133
|
|
|
'before_title' => '<h3 class="widget-title">', |
|
134
|
|
|
'after_title' => '</h3>', |
|
135
|
|
|
) );*/ |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} // endif function_exists( 'spurs_widgets_init' ). |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
|