Notice::__construct()   F
last analyzed

Complexity

Conditions 17
Paths 3889

Size

Total Lines 41
Code Lines 24

Duplication

Lines 9
Ratio 21.95 %

Importance

Changes 0
Metric Value
cc 17
eloc 24
nc 3889
nop 1
dl 9
loc 41
rs 2.7204
c 0
b 0
f 0

How to fix   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
 * Admin Notice
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin;
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * Admin notice.
15
 *
16
 * An admin notice as an object.
17
 *
18
 * @since 3.0.0
19
 */
20
class Notice {
21
22
	/**
23
	 * Notice id.
24
	 * Will be the notice key in saved notices option.
25
	 *
26
	 * @access public
27
	 * @var string|array
28
	 */
29
	public $id = '';
30
31
	/**
32
	 * Notice type
33
	 * Gives the notice a CSS class.
34
	 *
35
	 * @access public
36
	 * @var string notice|error|updated|update-nag
37
	 */
38
	public $type = '';
39
40
	/**
41
	 * Additional classes.
42
	 *
43
	 * @access public
44
	 * @var string
45
	 */
46
	public $class = '';
47
48
	/**
49
	 * To which users the notice should be shown.
50
	 * If not set, will be visible to all users.
51
	 *
52
	 * @access public
53
	 * @var string
54
	 */
55
	public $capability = '';
56
57
	/**
58
	 * In which screen the notice should appear.
59
	 * If not set, will appear in every dashboard page/screen.
60
	 *
61
	 * @access public
62
	 * @var array
63
	 */
64
	public $screen = array();
65
66
	/**
67
	 * For which posts edit screens the notice should appear.
68
	 * If not set, will fallback on $screen rule only.
69
	 *
70
	 * @access public
71
	 * @var array
72
	 */
73
	public $post = array();
74
75
	/**
76
	 * Can the notice be dismissed by the user?
77
	 * If false, you need to set up a dismissal event.
78
	 *
79
	 * @access public
80
	 * @var bool
81
	 */
82
	public $dismissible = true;
83
84
	/**
85
	 * Whether to hide notice while keeping it stored.
86
	 * If false, will keep the notice in option without showing it.
87
	 *
88
	 * @access public
89
	 * @var bool
90
	 */
91
	public $visible = true;
92
93
	/**
94
	 * The notice content.
95
	 * Supports html. You would normally wrap this in paragraph tags.
96
	 *
97
	 * @access public
98
	 * @var string
99
	 */
100
	public $content = '';
101
102
	/**
103
	 * Make a notice.
104
	 *
105
	 * @since 3.0.0
106
	 *
107
	 * @param array $notice
108
	 */
109
	public function __construct( $notice ) {
110
111
		if ( ! empty( $notice['id'] ) && ! empty( $notice['content'] ) ) {
112
113
			// Content.
114
			$this->id = is_array( $notice['id'] ) ? array_map( 'sanitize_key', $notice['id'] ) : sanitize_key( $notice['id'] );
115
			$this->content = wp_kses_post( $notice['content'] );
116 View Code Duplication
			if ( ! empty( $notice['class'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
				$this->class = is_array( $notice['class'] ) ? join( ' ', array_map( 'esc_attr', $notice['class'] ) ) : esc_attr( $notice['class'] );
118
			}
119
120
			// Type.
121
			$default = 'notice';
122
			$type = isset( $notice['type'] ) ? esc_attr( $notice['type'] ) : $default;
123
			$types = array(
124
				'error',
125
				'notice',
126
				'updated',
127
				'update-nag',
128
			);
129
			$this->type = in_array( $type, $types ) ? $type : $default;
130
131
			// Visibility.
132
			if ( ! empty( $notice['capability'] ) ) {
133
				$this->capability = esc_attr( $notice['capability'] );
134
			}
135 View Code Duplication
			if ( ! empty( $notice['screen'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
				$this->screen = is_array( $notice['screen'] ) ? array_map( 'esc_attr', $notice['screens'] ) : array( esc_attr( $notice['screen'] ) );
137
			}
138 View Code Duplication
			if ( ! empty( $notice['post'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
				$this->post = is_array( $notice['post'] ) ? array_map( 'intval', $notice['post'] ) : array( intval( $notice['post'] ) );
140
			}
141
			if ( ! empty( $notice['dismissible'] ) ) {
142
				$this->dismissible = $notice['dismissible'] === false ? false: true;
143
			}
144
			if ( ! empty( $notice['visible'] ) ) {
145
				$this->visible = $notice['visible'] === false ? false: true;
146
			}
147
		}
148
149
	}
150
151
	/**
152
	 * Add the notice.
153
	 *
154
	 * @since 3.0.0
155
	 */
156
	public function add() {
157
		if ( ! empty( $this->id ) && ! empty( $this->content ) ) {
158
			$notices = get_option( 'simple-calendar_admin_notices', array() );
159
			if ( is_array( $this->id ) ) {
160
				foreach ( $this->id as $k => $v ) {
161
					$notices[ $k ][ $v ] = $this;
162
				}
163
			} else {
164
				$notices[ $this->id ][] = $this;
165
			}
166
			update_option( 'simple-calendar_admin_notices', $notices );
167
		}
168
	}
169
170
	/**
171
	 * Remove the notice.
172
	 *
173
	 * @since 3.0.0
174
	 */
175
	public function remove() {
176
		if ( ! empty( $this->id ) && ! empty( $this->content ) ) {
177
			$notices = get_option( 'simple-calendar_admin_notices', array() );
178
			if ( is_array( $this->id ) ) {
179
				foreach ( $this->id as $k => $v ) {
180
					unset( $notices[ $k ] );
181
				}
182
			} else {
183
				unset( $notices[ $this->id ] );
184
			}
185
			update_option( 'simple-calendar_admin_notices', $notices );
186
		}
187
	}
188
189
}
190