Completed
Pull Request — master (#19)
by
unknown
23s
created
handler.php 1 patch
Spacing   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@  discard block
 block discarded – undo
19 19
  * @copyright 2018 Julien Liabeuf
20 20
  */
21 21
 
22
-if ( ! class_exists( 'Dismissible_Notices_Handler' ) ) {
22
+if ( ! class_exists('Dismissible_Notices_Handler')) {
23 23
 
24 24
 	final class Dismissible_Notices_Handler {
25 25
 
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 		 */
68 68
 		public static function instance() {
69 69
 
70
-			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Dismissible_Notices_Handler ) ) {
70
+			if ( ! isset(self::$instance) && ! (self::$instance instanceof Dismissible_Notices_Handler)) {
71 71
 				self::$instance = new Dismissible_Notices_Handler;
72 72
 				self::$instance->init();
73 73
 			}
@@ -85,11 +85,11 @@  discard block
 block discarded – undo
85 85
 		private function init() {
86 86
 
87 87
 			// Make sure WordPress is compatible
88
-			if ( ! self::$instance->is_wp_compatible() ) {
88
+			if ( ! self::$instance->is_wp_compatible()) {
89 89
 				self::$instance->spit_error(
90 90
 					sprintf(
91 91
 						/* translators: %s: required wordpress version */
92
-						esc_html__( 'The library can not be used because your version of WordPress is too old. You need version %s at least.', 'wp-dismissible-notices-handler' ),
92
+						esc_html__('The library can not be used because your version of WordPress is too old. You need version %s at least.', 'wp-dismissible-notices-handler'),
93 93
 						self::$instance->wordpress_version_required
94 94
 					)
95 95
 				);
@@ -98,11 +98,11 @@  discard block
 block discarded – undo
98 98
 			}
99 99
 
100 100
 			// Make sure PHP is compatible
101
-			if ( ! self::$instance->is_php_compatible() ) {
101
+			if ( ! self::$instance->is_php_compatible()) {
102 102
 				self::$instance->spit_error(
103 103
 					sprintf(
104 104
 						/* translators: %s: required php version */
105
-						esc_html__( 'The library can not be used because your version of PHP is too old. You need version %s at least.', 'wp-dismissible-notices-handler' ),
105
+						esc_html__('The library can not be used because your version of PHP is too old. You need version %s at least.', 'wp-dismissible-notices-handler'),
106 106
 						self::$instance->php_version_required
107 107
 					)
108 108
 				);
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
 				return;
111 111
 			}
112 112
 
113
-			add_action( 'admin_notices', array( self::$instance, 'display' ) );
114
-			add_action( 'admin_print_scripts', array( self::$instance, 'load_script' ) );
115
-			add_action( 'wp_ajax_dnh_dismiss_notice', array( self::$instance, 'dismiss_notice_ajax' ) );
113
+			add_action('admin_notices', array(self::$instance, 'display'));
114
+			add_action('admin_print_scripts', array(self::$instance, 'load_script'));
115
+			add_action('wp_ajax_dnh_dismiss_notice', array(self::$instance, 'dismiss_notice_ajax'));
116 116
 
117 117
 		}
118 118
 
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 		 */
125 125
 		private function is_wp_compatible() {
126 126
 
127
-			if ( version_compare( get_bloginfo( 'version' ), self::$instance->wordpress_version_required, '<' ) ) {
127
+			if (version_compare(get_bloginfo('version'), self::$instance->wordpress_version_required, '<')) {
128 128
 				return false;
129 129
 			}
130 130
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 		 */
141 141
 		private function is_php_compatible() {
142 142
 
143
-			if ( version_compare( phpversion(), self::$instance->php_version_required, '<' ) ) {
143
+			if (version_compare(phpversion(), self::$instance->php_version_required, '<')) {
144 144
 				return false;
145 145
 			}
146 146
 
@@ -155,8 +155,8 @@  discard block
 block discarded – undo
155 155
 		 * @return void
156 156
 		 */
157 157
 		public function load_script() {
158
-			wp_register_script( 'dnh', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/js/main.js', array( 'jquery' ), self::$instance->version, true );
159
-			wp_enqueue_script( 'dnh' );
158
+			wp_register_script('dnh', trailingslashit(plugin_dir_url(__FILE__)).'assets/js/main.js', array('jquery'), self::$instance->version, true);
159
+			wp_enqueue_script('dnh');
160 160
 		}
161 161
 
162 162
 		/**
@@ -167,21 +167,21 @@  discard block
 block discarded – undo
167 167
 		 */
168 168
 		public function display() {
169 169
 
170
-			if ( is_null( self::$instance->notices ) || empty( self::$instance->notices ) ) {
170
+			if (is_null(self::$instance->notices) || empty(self::$instance->notices)) {
171 171
 				return;
172 172
 			}
173 173
 
174
-			foreach ( self::$instance->notices as $id => $notice ) {
174
+			foreach (self::$instance->notices as $id => $notice) {
175 175
 
176
-				$id = self::$instance->get_id( $id );
176
+				$id = self::$instance->get_id($id);
177 177
 
178 178
 				// Check if the notice was dismissed
179
-				if ( self::$instance->is_dismissed( $id ) ) {
179
+				if (self::$instance->is_dismissed($id)) {
180 180
 					continue;
181 181
 				}
182 182
 
183 183
 				// Check if the current user has required capability
184
-				if ( ! empty( $notice['cap'] ) && ! current_user_can( $notice['cap'] ) ) {
184
+				if ( ! empty($notice['cap']) && ! current_user_can($notice['cap'])) {
185 185
 					continue;
186 186
 				}
187 187
 
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
 					$notice['class'],
193 193
 				);
194 194
 
195
-				printf( '<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim( implode( ' ', $class ) ), $notice['content'], "dnh-$id" );
195
+				printf('<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim(implode(' ', $class)), $notice['content'], "dnh-$id");
196 196
 
197 197
 			}
198 198
 
@@ -207,11 +207,11 @@  discard block
 block discarded – undo
207 207
 		 *
208 208
 		 * @return void
209 209
 		 */
210
-		protected function spit_error( $error ) {
210
+		protected function spit_error($error) {
211 211
 			printf(
212 212
 				'<div style="margin: 20px; text-align: center;"><strong>%1$s</strong> %2$s</pre></div>',
213
-				esc_html__( 'Dismissible Notices Handler Error:', 'wp-dismissible-notices-handler' ),
214
-				wp_kses_post( $error )
213
+				esc_html__('Dismissible Notices Handler Error:', 'wp-dismissible-notices-handler'),
214
+				wp_kses_post($error)
215 215
 			);
216 216
 		}
217 217
 
@@ -224,8 +224,8 @@  discard block
 block discarded – undo
224 224
 		 *
225 225
 		 * @return string
226 226
 		 */
227
-		public function get_id( $id ) {
228
-			return sanitize_key( $id );
227
+		public function get_id($id) {
228
+			return sanitize_key($id);
229 229
 		}
230 230
 
231 231
 		/**
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
 				'notice-info',
247 247
 			);
248 248
 
249
-			return apply_filters( 'dnh_notice_types', $types );
249
+			return apply_filters('dnh_notice_types', $types);
250 250
 
251 251
 		}
252 252
 
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
 				'class'  => '', // Additional class to add to the notice
266 266
 			);
267 267
 
268
-			return apply_filters( 'dnh_default_args', $args );
268
+			return apply_filters('dnh_default_args', $args);
269 269
 
270 270
 		}
271 271
 
@@ -281,23 +281,23 @@  discard block
 block discarded – undo
281 281
 		 *
282 282
 		 * @return bool
283 283
 		 */
284
-		public function register_notice( $id, $type, $content, $args = array() ) {
284
+		public function register_notice($id, $type, $content, $args = array()) {
285 285
 
286
-			if ( is_null( self::$instance->notices ) ) {
286
+			if (is_null(self::$instance->notices)) {
287 287
 				self::$instance->notices = array();
288 288
 			}
289 289
 
290
-			$id      = self::$instance->get_id( $id );
291
-			$type    = in_array( $t = sanitize_text_field( $type ), self::$instance->get_types() ) ? $t : 'updated';
292
-			$content = wp_kses_post( $content );
293
-			$args    = wp_parse_args( $args, self::$instance->default_args() );
290
+			$id      = self::$instance->get_id($id);
291
+			$type    = in_array($t = sanitize_text_field($type), self::$instance->get_types()) ? $t : 'updated';
292
+			$content = wp_kses_post($content);
293
+			$args    = wp_parse_args($args, self::$instance->default_args());
294 294
 
295
-			if ( array_key_exists( $id, self::$instance->notices ) ) {
296
-				if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
295
+			if (array_key_exists($id, self::$instance->notices)) {
296
+				if (defined('WP_DEBUG') && WP_DEBUG) {
297 297
 					self::$instance->spit_error(
298 298
 						sprintf(
299 299
 							/* translators: %s: required php version */
300
-							esc_html__( 'A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler' ),
300
+							esc_html__('A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler'),
301 301
 							"<code>$id</code>"
302 302
 						)
303 303
 					);
@@ -311,9 +311,9 @@  discard block
 block discarded – undo
311 311
 				'content' => $content,
312 312
 			);
313 313
 
314
-			$notice = array_merge( $notice, $args );
314
+			$notice = array_merge($notice, $args);
315 315
 
316
-			self::$instance->notices[ $id ] = $notice;
316
+			self::$instance->notices[$id] = $notice;
317 317
 
318 318
 			return true;
319 319
 
@@ -327,19 +327,19 @@  discard block
 block discarded – undo
327 327
 		 */
328 328
 		public function dismiss_notice_ajax() {
329 329
 
330
-			if ( ! isset( $_POST['id'] ) ) {
330
+			if ( ! isset($_POST['id'])) {
331 331
 				echo 0;
332 332
 				exit;
333 333
 			}
334 334
 
335
-			if ( empty( $_POST['id'] ) || false === strpos( $_POST['id'], 'dnh-' ) ) {
335
+			if (empty($_POST['id']) || false === strpos($_POST['id'], 'dnh-')) {
336 336
 				echo 0;
337 337
 				exit;
338 338
 			}
339 339
 
340
-			$id = self::$instance->get_id( str_replace( 'dnh-', '', $_POST['id'] ) );
340
+			$id = self::$instance->get_id(str_replace('dnh-', '', $_POST['id']));
341 341
 
342
-			echo self::$instance->dismiss_notice( $id );
342
+			echo self::$instance->dismiss_notice($id);
343 343
 			exit;
344 344
 
345 345
 		}
@@ -353,19 +353,19 @@  discard block
 block discarded – undo
353 353
 		 *
354 354
 		 * @return bool
355 355
 		 */
356
-		public function dismiss_notice( $id ) {
356
+		public function dismiss_notice($id) {
357 357
 
358
-			$notice = self::$instance->get_notice( self::$instance->get_id( $id ) );
358
+			$notice = self::$instance->get_notice(self::$instance->get_id($id));
359 359
 
360
-			if ( false === $notice ) {
360
+			if (false === $notice) {
361 361
 				return false;
362 362
 			}
363 363
 
364
-			if ( self::$instance->is_dismissed( $id ) ) {
364
+			if (self::$instance->is_dismissed($id)) {
365 365
 				return false;
366 366
 			}
367 367
 
368
-			return 'user' === $notice['scope'] ? self::$instance->dismiss_user( $id ) : self::$instance->dismiss_global( $id );
368
+			return 'user' === $notice['scope'] ? self::$instance->dismiss_user($id) : self::$instance->dismiss_global($id);
369 369
 
370 370
 		}
371 371
 
@@ -378,17 +378,17 @@  discard block
 block discarded – undo
378 378
 		 *
379 379
 		 * @return int|bool
380 380
 		 */
381
-		private function dismiss_user( $id ) {
381
+		private function dismiss_user($id) {
382 382
 
383 383
 			$dismissed = self::$instance->dismissed_user();
384 384
 
385
-			if ( in_array( $id, $dismissed ) ) {
385
+			if (in_array($id, $dismissed)) {
386 386
 				return false;
387 387
 			}
388 388
 
389
-			array_push( $dismissed, $id );
389
+			array_push($dismissed, $id);
390 390
 
391
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
391
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
392 392
 
393 393
 		}
394 394
 
@@ -401,17 +401,17 @@  discard block
 block discarded – undo
401 401
 		 *
402 402
 		 * @return bool
403 403
 		 */
404
-		private function dismiss_global( $id ) {
404
+		private function dismiss_global($id) {
405 405
 
406 406
 			$dismissed = self::$instance->dismissed_global();
407 407
 
408
-			if ( in_array( $id, $dismissed ) ) {
408
+			if (in_array($id, $dismissed)) {
409 409
 				return false;
410 410
 			}
411 411
 
412
-			array_push( $dismissed, $id );
412
+			array_push($dismissed, $id);
413 413
 
414
-			return update_option( 'dnh_dismissed_notices', $dismissed );
414
+			return update_option('dnh_dismissed_notices', $dismissed);
415 415
 
416 416
 		}
417 417
 
@@ -424,16 +424,16 @@  discard block
 block discarded – undo
424 424
 		 *
425 425
 		 * @return bool
426 426
 		 */
427
-		public function restore_notice( $id ) {
427
+		public function restore_notice($id) {
428 428
 
429
-			$id     = self::$instance->get_id( $id );
430
-			$notice = self::$instance->get_notice( $id );
429
+			$id     = self::$instance->get_id($id);
430
+			$notice = self::$instance->get_notice($id);
431 431
 
432
-			if ( false === $notice ) {
432
+			if (false === $notice) {
433 433
 				return false;
434 434
 			}
435 435
 
436
-			return 'user' === $notice['scope'] ? self::$instance->restore_user( $id ) : self::$instance->restore_global( $id );
436
+			return 'user' === $notice['scope'] ? self::$instance->restore_user($id) : self::$instance->restore_global($id);
437 437
 
438 438
 		}
439 439
 
@@ -446,27 +446,27 @@  discard block
 block discarded – undo
446 446
 		 *
447 447
 		 * @return bool
448 448
 		 */
449
-		private function restore_user( $id ) {
449
+		private function restore_user($id) {
450 450
 
451
-			$id     = self::$instance->get_id( $id );
452
-			$notice = self::$instance->get_notice( $id );
451
+			$id     = self::$instance->get_id($id);
452
+			$notice = self::$instance->get_notice($id);
453 453
 
454
-			if ( false === $notice ) {
454
+			if (false === $notice) {
455 455
 				return false;
456 456
 			}
457 457
 
458 458
 			$dismissed = self::$instance->dismissed_user();
459 459
 
460
-			if ( ! in_array( $id, $dismissed ) ) {
460
+			if ( ! in_array($id, $dismissed)) {
461 461
 				return false;
462 462
 			}
463 463
 
464
-			$flip = array_flip( $dismissed );
465
-			$key  = $flip[ $id ];
464
+			$flip = array_flip($dismissed);
465
+			$key  = $flip[$id];
466 466
 
467
-			unset( $dismissed[ $key ] );
467
+			unset($dismissed[$key]);
468 468
 
469
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
469
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
470 470
 
471 471
 		}
472 472
 
@@ -479,27 +479,27 @@  discard block
 block discarded – undo
479 479
 		 *
480 480
 		 * @return bool
481 481
 		 */
482
-		private function restore_global( $id ) {
482
+		private function restore_global($id) {
483 483
 
484
-			$id     = self::$instance->get_id( $id );
485
-			$notice = self::$instance->get_notice( $id );
484
+			$id     = self::$instance->get_id($id);
485
+			$notice = self::$instance->get_notice($id);
486 486
 
487
-			if ( false === $notice ) {
487
+			if (false === $notice) {
488 488
 				return false;
489 489
 			}
490 490
 
491 491
 			$dismissed = self::$instance->dismissed_global();
492 492
 
493
-			if ( ! in_array( $id, $dismissed ) ) {
493
+			if ( ! in_array($id, $dismissed)) {
494 494
 				return false;
495 495
 			}
496 496
 
497
-			$flip = array_flip( $dismissed );
498
-			$key  = $flip[ $id ];
497
+			$flip = array_flip($dismissed);
498
+			$key  = $flip[$id];
499 499
 
500
-			unset( $dismissed[ $key ] );
500
+			unset($dismissed[$key]);
501 501
 
502
-			return update_option( 'dnh_dismissed_notices', $dismissed );
502
+			return update_option('dnh_dismissed_notices', $dismissed);
503 503
 
504 504
 		}
505 505
 
@@ -516,7 +516,7 @@  discard block
 block discarded – undo
516 516
 			$user   = self::$instance->dismissed_user();
517 517
 			$global = self::$instance->dismissed_global();
518 518
 
519
-			return array_merge( $user, $global );
519
+			return array_merge($user, $global);
520 520
 
521 521
 		}
522 522
 
@@ -528,9 +528,9 @@  discard block
 block discarded – undo
528 528
 		 */
529 529
 		private function dismissed_user() {
530 530
 
531
-			$dismissed = get_user_meta( get_current_user_id(), 'dnh_dismissed_notices', true );
531
+			$dismissed = get_user_meta(get_current_user_id(), 'dnh_dismissed_notices', true);
532 532
 
533
-			if ( '' === $dismissed ) {
533
+			if ('' === $dismissed) {
534 534
 				$dismissed = array();
535 535
 			}
536 536
 
@@ -545,7 +545,7 @@  discard block
 block discarded – undo
545 545
 		 * @return array
546 546
 		 */
547 547
 		private function dismissed_global() {
548
-			return get_option( 'dnh_dismissed_notices', array() );
548
+			return get_option('dnh_dismissed_notices', array());
549 549
 		}
550 550
 
551 551
 		/**
@@ -557,11 +557,11 @@  discard block
 block discarded – undo
557 557
 		 *
558 558
 		 * @return bool
559 559
 		 */
560
-		public function is_dismissed( $id ) {
560
+		public function is_dismissed($id) {
561 561
 
562 562
 			$dismissed = self::$instance->dismissed_notices();
563 563
 
564
-			if ( ! in_array( self::$instance->get_id( $id ), $dismissed ) ) {
564
+			if ( ! in_array(self::$instance->get_id($id), $dismissed)) {
565 565
 				return false;
566 566
 			}
567 567
 
@@ -588,15 +588,15 @@  discard block
 block discarded – undo
588 588
 		 *
589 589
 		 * @return array|false
590 590
 		 */
591
-		public function get_notice( $id ) {
591
+		public function get_notice($id) {
592 592
 
593
-			$id = self::$instance->get_id( $id );
593
+			$id = self::$instance->get_id($id);
594 594
 
595
-			if ( ! is_array( self::$instance->notices ) || ! array_key_exists( $id, self::$instance->notices ) ) {
595
+			if ( ! is_array(self::$instance->notices) || ! array_key_exists($id, self::$instance->notices)) {
596 596
 				return false;
597 597
 			}
598 598
 
599
-			return self::$instance->notices[ $id ];
599
+			return self::$instance->notices[$id];
600 600
 
601 601
 		}
602 602
 
Please login to merge, or discard this patch.