Completed
Push — dev ( b41ca9...f5091d )
by Eric
01:45
created

widgets.php ➔ bitsy_slbd_count_widgets()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 12
nop 1
dl 0
loc 30
rs 6.7272
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 and add classes to widget areas
9
 * so widgets can be displayed one, two, or three 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 ( $widget_count >= 4 ) {
26
				// Four or more widgets active
27
				$widget_classes .= ' 3u';
28
			} elseif ( 3 == $widget_count ) {
29
				// Three widgets active
30
				$widget_classes .= ' 4u';
31
			} elseif ( 2 == $widget_count ) {
32
				// Two widgets active
33
				$widget_classes .= ' 6u';
34
			} elseif ( 1 == $widget_count ) {
35
				// One widget active
36
				$widget_classes .= ' 12u';
37
			}
38
39
			return $widget_classes;
40
		}
41
	}
42
}
43
/**
44
 * Initializes themes widgets.
45
 */
46
if ( ! function_exists( 'bitsy_widgets_init' ) ) {
47
48
	function bitsy_widgets_init() {
49
50
		register_sidebar( array(
51
			'name'          => __( 'Right Sidebar', 'spurs' ),
52
			'id'            => 'right-sidebar',
53
			'description'   => 'Right sidebar widget area',
54
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
55
			'after_widget'  => '</aside>',
56
			'before_title'  => '<h3 class="widget-title">',
57
			'after_title'   => '</h3>',
58
		) );
59
60
		register_sidebar( array(
61
			'name'          => __( 'Left Sidebar', 'spurs' ),
62
			'id'            => 'left-sidebar',
63
			'description'   => 'Left sidebar widget area',
64
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
65
			'after_widget'  => '</aside>',
66
			'before_title'  => '<h3 class="widget-title">',
67
			'after_title'   => '</h3>',
68
		) );
69
70
		register_sidebar( array(
71
			'name'          => __( 'Footer Full', 'spurs' ),
72
			'id'            => 'footer-full',
73
			'description'   => 'Widget area below main content and above footer',
74
			'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...
75
			'after_widget'  => '</div><!-- .footer-widget -->',
76
			'before_title'  => '<h3 class="widget-title">',
77
			'after_title'   => '</h3>',
78
		) );
79
80
	}
81
}
82
add_action( 'widgets_init', 'bitsy_widgets_init' );