display_inline_notice()   F
last analyzed

Complexity

Conditions 20
Paths 6937

Size

Total Lines 129
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 20
eloc 73
c 3
b 0
f 0
nc 6937
nop 6
dl 0
loc 129
rs 0

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
 * Notices admin class.
4
 *
5
 * Handles retrieving whether a particular notice has been dismissed or not,
6
 * as well as marking a notice as dismissed.
7
 *
8
 * @since 6.0.0
9
 *
10
 * @package MonsterInsights
11
 * @subpackage Notices
12
 * @author  Chris Christoff
13
 */
14
15
// Exit if accessed directly
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
final class MonsterInsights_Notice_Admin {
21
22
	/**
23
	 * Holds all dismissed notices
24
	 *
25
	 * @access public
26
	 * @since 6.0.0
27
	 * @var array $notices Array of dismissed notices.
28
	 */
29
	public $notices;
30
31
	/**
32
	 * Primary class constructor.
33
	 *
34
	 * @access public
35
	 * @since 6.0.0
36
	 */
37
	public function __construct() {
38
39
		// Populate $notices
40
		$this->notices = get_option( 'monsterinsights_notices' );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_option('monsterinsights_notices') can also be of type false. However, the property $notices is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
		if ( ! is_array( $this->notices ) ) {
42
			$this->notices = array();
43
		}
44
45
	}
46
47
	/**
48
	 * Checks if a given notice has been dismissed or not
49
	 *
50
	 * @access public
51
	 *
52
	 * @param string $notice Programmatic Notice Name
53
	 *
54
	 * @return bool Notice Dismissed
55
	 * @since 6.0.0
56
	 *
57
	 */
58
59
	public function is_dismissed( $notice ) {
60
		if ( ! isset( $this->notices[ $notice ] ) ) {
61
			return false;
62
		}
63
64
		return true;
65
66
	}
67
68
	/**
69
	 * Marks the given notice as dismissed
70
	 *
71
	 * @access public
72
	 *
73
	 * @param string $notice Programmatic Notice Name
74
	 *
75
	 * @return null
76
	 * @since 6.0.0
77
	 *
78
	 */
79
	public function dismiss( $notice ) {
80
		$this->notices[ $notice ] = true;
81
		update_option( 'monsterinsights_notices', $this->notices );
82
83
	}
84
85
86
	/**
87
	 * Marks a notice as not dismissed
88
	 *
89
	 * @access public
90
	 *
91
	 * @param string $notice Programmatic Notice Name
92
	 *
93
	 * @return null
94
	 * @since 6.0.0
95
	 *
96
	 */
97
	public function undismiss( $notice ) {
98
		unset( $this->notices[ $notice ] );
99
		update_option( 'monsterinsights_notices', $this->notices );
100
101
	}
102
103
	/**
104
	 * Displays an inline notice with some MonsterInsights styling.
105
	 *
106
	 * @access public
107
	 *
108
	 * @param string $notice Programmatic Notice Name
109
	 * @param string $title Title
110
	 * @param string $message Message
111
	 * @param string $type Message Type (updated|warning|error) - green, yellow/orange and red respectively.
112
	 * @param string $button_text Button Text (optional)
113
	 * @param string $button_url Button URL (optional)
114
	 * @param bool $is_dismissible User can Dismiss Message (default: false)
115
	 *
116
	 * @since 6.0.0
117
	 *
118
	 */
119
	public function display_inline_notice( $name, $title, $message, $type = 'success', $is_dismissible = false, $args = array() ) {
120
		/* Available/Planned $args options
121
		 * $args = array(
122
		 *  'primary'    => array(
123
		 *      'text'   => '',
124
		 *      'url'    => '',
125
		 *      'target' => '',
126
		 *      'class'  => 'button button-primary',
127
		 *  ),
128
		 *  'secondary'  => array(
129
		 *      'text'   => '',
130
		 *      'url'    => '',
131
		 *      'target' => '',
132
		 *      'class'  => 'button button-secondary',
133
		 *  ),
134
		 *  'skip_message_escape' => true // note: This param is for internal use only. Do not use as a developer.
135
		 * );
136
		 */
137
138
139
		// Check if the notice is dismissible, and if so has been dismissed.
140
		if ( $is_dismissible && $this->is_dismissed( $name ) ) {
141
			// Nothing to show here, return!
142
			return '';
143
		}
144
145
		$dismissible = ( $is_dismissible ) ? ' is-dismissible' : '';
146
147
		// Display inline notice
148
		ob_start();
149
		?>
150
		<div
151
			class="monsterinsights-notice <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice' . esc_attr($dismissible); ?>"
152
			data-notice="<?php echo esc_attr( $name ); ?>">
153
			<div
154
				class="monsterinsights-notice-icon <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice-icon' ?>">
155
			</div>
156
			<div
157
				class="monsterinsights-notice-text <?php echo 'monsterinsights-' . esc_attr( $type ) . '-notice-text' ?>">
158
				<?php
159
				// Title
160
				if ( ! empty ( $title ) ) {
161
					?>
162
					<p class="monsterinsights-notice-title"><?php echo esc_html( $title ); ?></p>
163
					<?php
164
				}
165
166
				// Message
167
				if ( ! empty( $message ) ) {
168
					if ( empty( $args['skip_message_escape'] ) ) {
169
						?>
170
						<p class="monsterinsights-notice-message"><?php echo esc_html( $message ); ?></p>
171
						<?php
172
					} else {
173
						?>
174
						<p class="monsterinsights-notice-message"><?php echo $message; // phpcs:ignore ?></p>
175
						<?php
176
					}
177
				}
178
179
				// Primary Button
180
				if ( ! empty( $args['primary']['text'] ) ) {
181
182
					$text = '';
183
					if ( ! empty( $args['primary']['text'] ) ) {
184
						$text = $args['primary']['text'];
185
					}
186
187
					$url = '#';
188
					if ( ! empty( $args['primary']['url'] ) ) {
189
						$url = $args['primary']['url'];
190
					}
191
192
					$target = '';
193
					if ( ! empty( $args['primary']['target'] ) && $args['primary']['target'] === 'blank' ) {
194
						$target = ' target="_blank" rel="noopener noreferrer"';
195
					}
196
197
					$class = 'button button-primary';
198
					if ( ! empty( $args['primary']['class'] ) ) {
199
						$class = ' class="' . $args['primary']['class'] . '"';
200
					}
201
					?>
202
					<a href="<?php echo esc_url( $url ); ?>"<?php echo esc_attr($target); ?><?php echo esc_attr($class); ?>><?php echo esc_html( $text ); ?></a>
203
					<?php
204
				}
205
206
				// Secondary Button
207
				if ( ! empty( $args['secondary']['text'] ) ) {
208
209
					$text = '';
210
					if ( ! empty( $args['secondary']['text'] ) ) {
211
						$text = $args['secondary']['text'];
212
					}
213
214
					$url = '#';
215
					if ( ! empty( $args['secondary']['url'] ) ) {
216
						$url = $args['secondary']['url'];
217
					}
218
219
					$target = '';
220
					if ( ! empty( $args['secondary']['target'] ) && $args['secondary']['target'] === 'blank' ) {
221
						$target = ' target="_blank" rel="noopener noreferrer"';
222
					}
223
224
					$class = 'button button-secondary';
225
					if ( ! empty( $args['secondary']['class'] ) ) {
226
						$class = ' class="' . $args['secondary']['class'] . '"';
227
					}
228
					?>
229
					<a href="<?php echo esc_url( $url ); ?>"<?php echo esc_attr($target); ?><?php echo esc_attr($class); ?>><?php echo esc_html( $text ); ?></a>
230
					<?php
231
				}
232
233
				// Dismiss Button
234
				if ( $is_dismissible ) {
235
					?>
236
					<button type="button" class="notice-dismiss<?php echo esc_attr($dismissible); ?>">
237
                        <span class="screen-reader-text">
238
                            <?php esc_html_e( 'Dismiss this notice', 'google-analytics-for-wordpress' ); ?>
239
                        </span>
240
					</button>
241
					<?php
242
				}
243
				?>
244
			</div>
245
		</div>
246
		<?php
247
		return ob_get_clean();
248
	}
249
}
250