widgets.php ➔ bitsy_slbd_count_widgets()   C
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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