Passed
Push — master ( 26770f...806cdd )
by Chris
03:20
created

cmb_do_meta_boxes()   D

Complexity

Conditions 19
Paths 12

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 59
rs 4.5166
c 0
b 0
f 0
cc 19
nc 12
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php 
2
3
/**
4
 * Create CMB Meta boxes anywhere you like (other than the post edit screen).
5
 *
6
 * This is functional, but a little hacky.
7
 */
8
9
/**
10
 * Draw the meta boxes in places other than the post edit screen
11
 * 
12
 * @return null
13
 */
14
function cmb_draw_meta_boxes( $pages, $context = 'normal', $object = null ) {
15
16
	cmb_do_meta_boxes( $pages, $context, $object );
17
18
	wp_enqueue_script('post');
19
20
}
21
22
/**
23
 * Meta-Box template function
24
 *
25
 * @since 2.5.0
26
 *
27
 * @param string|object $screen Screen identifier
28
 * @param string $context box context
29
 * @param mixed $object gets passed to the box callback function as first parameter
30
 * @return int number of meta_boxes
31
 */
32
function cmb_do_meta_boxes( $screen, $context, $object ) {
33
34
	global $wp_meta_boxes;
35
36
	static $already_sorted = false;
37
38
	if ( empty( $screen ) )
39
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
41
	elseif ( is_string( $screen ) )
42
		$screen = convert_to_screen( $screen );
43
44
	$page = $screen->id;
45
46
	$hidden = get_hidden_meta_boxes( $screen );
47
48
	$i = 0;
49
50
	do {
51
		// Grab the ones the user has manually sorted. Pull them out of their previous context/priority and into the one the user chose
52
53
		if ( ! $already_sorted && $sorted = get_user_option( "meta-box-order_$page" ) )
54
			foreach ( $sorted as $box_context => $ids )
55
				foreach ( explode(',', $ids ) as $id )
56
					if ( $id && 'dashboard_browser_nag' !== $id )
57
						add_meta_box( $id, null, null, $screen, $box_context, 'sorted' );
58
59
		$already_sorted = true;
60
61
		if ( ! isset( $wp_meta_boxes ) || ! isset( $wp_meta_boxes[$page] ) || ! isset( $wp_meta_boxes[$page][$context] ) )
62
			break;
63
64
		foreach ( array( 'high', 'sorted', 'core', 'default', 'low' ) as $priority ) {
65
66
			if ( isset( $wp_meta_boxes[$page][$context][$priority] ) ) {
67
68
				foreach ( (array) $wp_meta_boxes[$page][$context][$priority] as $box ) {
69
70
					if ( false == $box || ! $box['title'] )
71
						continue;
72
73
					$i++;
74
75
					$hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : ''; ?>
76
77
					<div id="<?php esc_attr_e( $box['id'] ); ?>" class="<?php esc_attr_e( postbox_classes( $box['id'], $page ) . $hidden_class ); ?>">
78
79
						<?php call_user_func( $box['callback'], $object, $box ); ?>
80
81
					</div>
82
83
				<?php }
84
85
			}
86
87
		}
88
	} while( 0 );
89
90
	return $i;
91
92
}