monsterinsights_add_capabilities()   D
last analyzed

Complexity

Conditions 23
Paths 29

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 23
eloc 32
c 2
b 0
f 0
nc 29
nop 4
dl 0
loc 54
rs 4.1666

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
 * Capabilities class.
4
 *
5
 * @access public
6
 * @since 6.0.0
7
 *
8
 * @package MonsterInsights
9
 * @subpackage Capabilities
10
 * @author  Chris Christoff
11
 */
12
13
// Exit if accessed directly
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Map MonsterInsights Capabilities.
20
 *
21
 * Using meta caps, we're creating virtual capabilities that are
22
 * for backwards compatibility reasons given to users with manage_options, and to
23
 * users who have at least of the roles selected in the options on the permissions
24
 * tab of the MonsterInsights settings.
25
 *
26
 * @access public
27
 *
28
 * @param array $caps Array of capabilities the user has.
29
 * @param string $cap The current cap being filtered.
30
 * @param int $user_id User to check permissions for.
31
 * @param array $args Extra parameters. Unused.
32
 *
33
 * @return array Array of caps needed to have this meta cap. If returned array is empty, user has the capability.
34
 * @since 6.0.0
35
 *
36
 */
37
function monsterinsights_add_capabilities( $caps, $cap, $user_id, $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
function monsterinsights_add_capabilities( $caps, $cap, $user_id, /** @scrutinizer ignore-unused */ $args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
39
	switch ( $cap ) {
40
		case 'monsterinsights_view_dashboard' :
41
			$roles = monsterinsights_get_option( 'view_reports', array() );
42
43
			$user_can_via_settings = false;
44
			if ( ! empty( $roles ) && is_array( $roles ) ) {
0 ignored issues
show
introduced by
The condition is_array($roles) is always false.
Loading history...
45
				foreach ( $roles as $role ) {
46
					if ( is_string( $role ) ) {
47
						if ( user_can( $user_id, $role ) ) {
48
							$user_can_via_settings = true;
49
							break;
50
						}
51
					}
52
				}
53
			} else if ( ! empty( $roles ) && is_string( $roles ) ) {
54
				if ( user_can( $user_id, $roles ) ) {
55
					$user_can_via_settings = true;
56
				}
57
			}
58
59
			if ( user_can( $user_id, 'manage_options' ) || $user_can_via_settings ) {
60
				$caps = array();
61
			}
62
63
			break;
64
		case 'monsterinsights_save_settings' :
65
			$roles = monsterinsights_get_option( 'save_settings', array() );
66
67
			$user_can_via_settings = false;
68
			if ( ! empty( $roles ) && is_array( $roles ) ) {
0 ignored issues
show
introduced by
The condition is_array($roles) is always false.
Loading history...
69
				foreach ( $roles as $role ) {
70
					if ( is_string( $role ) ) {
71
						if ( user_can( $user_id, $role ) ) {
72
							$user_can_via_settings = true;
73
							break;
74
						}
75
					}
76
				}
77
			} else if ( ! empty( $roles ) && is_string( $roles ) ) {
78
				if ( user_can( $user_id, $roles ) ) {
79
					$user_can_via_settings = true;
80
				}
81
			}
82
83
			if ( user_can( $user_id, 'manage_options' ) || $user_can_via_settings ) {
84
				$caps = array();
85
			}
86
87
			break;
88
	}
89
90
	return $caps;
91
}
92
93
add_filter( 'map_meta_cap', 'monsterinsights_add_capabilities', 10, 4 );
94