Completed
Branch dev (0b2b90)
by Eric
01:41
created

widgets.php ➔ bitsy_slbd_count_widgets()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 14
nop 1
dl 0
loc 33
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package     bitsy
4
 * @author      Eric Amundson <[email protected]>
5
 */
6
7
/**
8
 * Count number of widgets in a sidebar
9
 * Used to add classes to widget areas so widgets can be displayed one, two, three or four per row
10
 */
11
if ( ! function_exists( 'bitsy_slbd_count_widgets' ) ) {
12
	function bitsy_slbd_count_widgets( $sidebar_id ) {
13
		// If loading from front page, consult $_wp_sidebars_widgets rather than options
14
		// to see if wp_convert_widget_settings() has made manipulations in memory.
15
		global $_wp_sidebars_widgets;
16
		if ( empty( $_wp_sidebars_widgets ) ) {
17
			$_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
18
		}
19
		$sidebars_widgets_count = $_wp_sidebars_widgets;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $sidebars_widgets_count exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
20
21
		if ( isset( $sidebars_widgets_count[ $sidebar_id ] ) ) {
22
			$widget_count   = count( $sidebars_widgets_count[ $sidebar_id ] );
23
			$widget_classes = 'widget-count-' . count( $sidebars_widgets_count[ $sidebar_id ] );
24
25
			if ( 0 == $widget_count % 4 || $widget_count > 6 ) {
26
				// Four widgets per row if there are exactly four or more than six
27
				$widget_classes .= ' col-md-3';
28
			} elseif ( 6 == $widget_count ) {
29
				// If two widgets are published
30
				$widget_classes .= ' col-md-2';
31
			} elseif ( $widget_count >= 3 ) {
32
				// Three widgets per row if there's three or more widgets
33
				$widget_classes .= ' col-md-4';
34
			} elseif ( 2 == $widget_count ) {
35
				// If two widgets are published
36
				$widget_classes .= ' col-md-6';
37
			} elseif ( 1 == $widget_count ) {
38
				// If just on widget is active
39
				$widget_classes .= ' col-md-12';
40
			}
41
42
			return $widget_classes;
43
		}
44
	}
45
}
46
/**
47
 * Initializes themes widgets.
48
 */
49
if ( ! function_exists( 'bitsy_widgets_init' ) ) {
50
51
	function bitsy_widgets_init() {
52
		register_sidebar( array(
53
			'name'          => __( 'Right Sidebar', 'spurs' ),
54
			'id'            => 'right-sidebar',
55
			'description'   => 'Right sidebar widget area',
56
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
57
			'after_widget'  => '</aside>',
58
			'before_title'  => '<h3 class="widget-title">',
59
			'after_title'   => '</h3>',
60
		) );
61
62
		register_sidebar( array(
63
			'name'          => __( 'Left Sidebar', 'spurs' ),
64
			'id'            => 'left-sidebar',
65
			'description'   => 'Left sidebar widget area',
66
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
67
			'after_widget'  => '</aside>',
68
			'before_title'  => '<h3 class="widget-title">',
69
			'after_title'   => '</h3>',
70
		) );
71
72
		register_sidebar( array(
73
			'name'          => __( 'Hero Slider', 'spurs' ),
74
			'id'            => 'hero',
75
			'description'   => 'Hero slider area. Place two or more widgets here and they will slide!',
76
			'before_widget' => '<div class="carousel-item">',
77
			'after_widget'  => '</div>',
78
			'before_title'  => '',
79
			'after_title'   => '',
80
		) );
81
82
		register_sidebar( array(
83
			'name'          => __( 'Hero Static', 'spurs' ),
84
			'id'            => 'static-hero',
85
			'description'   => 'Static Hero widget. no slider functionality',
86
			'before_widget' => '<div id="%1$s" class="static-hero-widget %2$s ' . bitsy_slbd_count_widgets( 'static-hero' ) . '">',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'bitsy_slbd_count_widgets'
Loading history...
87
			'after_widget'  => '</div><!-- .static-hero-widget -->',
88
			'before_title'  => '<h3 class="widget-title">',
89
			'after_title'   => '</h3>',
90
		) );
91
92
		register_sidebar( array(
93
			'name'          => __( 'Footer Full', 'spurs' ),
94
			'id'            => 'footer-full',
95
			'description'   => 'Widget area below main content and above footer',
96
			'before_widget' => '<div id="%1$s" class="footer-widget %2$s ' . bitsy_slbd_count_widgets( 'footer-full' ) . '">',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'bitsy_slbd_count_widgets'
Loading history...
97
			'after_widget'  => '</div><!-- .footer-widget -->',
98
			'before_title'  => '<h3 class="widget-title">',
99
			'after_title'   => '</h3>',
100
		) );
101
102
	}
103
} // endif function_exists( 'bitsy_widgets_init' ).
104
add_action( 'widgets_init', 'bitsy_widgets_init' );