Completed
Pull Request — master (#7)
by Julien
03:35
created
handler.php 1 patch
Spacing   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -20,11 +20,11 @@  discard block
 block discarded – undo
20 20
  */
21 21
 
22 22
 // If this file is called directly, abort.
23
-if ( ! defined( 'WPINC' ) ) {
23
+if ( ! defined('WPINC')) {
24 24
 	die;
25 25
 }
26 26
 
27
-if ( ! class_exists( 'Dismissible_Notices_Handler' ) ) {
27
+if ( ! class_exists('Dismissible_Notices_Handler')) {
28 28
 
29 29
 	final class Dismissible_Notices_Handler {
30 30
 
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 		 */
73 73
 		public static function instance() {
74 74
 
75
-			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Dismissible_Notices_Handler ) ) {
75
+			if ( ! isset(self::$instance) && ! (self::$instance instanceof Dismissible_Notices_Handler)) {
76 76
 				self::$instance = new Dismissible_Notices_Handler;
77 77
 				self::$instance->init();
78 78
 			}
@@ -90,11 +90,11 @@  discard block
 block discarded – undo
90 90
 		private function init() {
91 91
 
92 92
 			// Make sure WordPress is compatible
93
-			if ( ! self::$instance->is_wp_compatible() ) {
93
+			if ( ! self::$instance->is_wp_compatible()) {
94 94
 				self::$instance->spit_error(
95 95
 					sprintf(
96 96
 						/* translators: %s: required wordpress version */
97
-						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' ),
97
+						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'),
98 98
 						self::$instance->wordpress_version_required
99 99
 					)
100 100
 				);
@@ -103,11 +103,11 @@  discard block
 block discarded – undo
103 103
 			}
104 104
 
105 105
 			// Make sure PHP is compatible
106
-			if ( ! self::$instance->is_php_compatible() ) {
106
+			if ( ! self::$instance->is_php_compatible()) {
107 107
 				self::$instance->spit_error(
108 108
 					sprintf(
109 109
 						/* translators: %s: required php version */
110
-						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' ),
110
+						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'),
111 111
 						self::$instance->php_version_required
112 112
 					)
113 113
 				);
@@ -117,9 +117,9 @@  discard block
 block discarded – undo
117 117
 
118 118
 			self::$instance->includes();
119 119
 
120
-			add_action( 'admin_notices', array( self::$instance, 'display' ) );
121
-			add_action( 'admin_print_scripts', array( self::$instance, 'load_script' ) );
122
-			add_action( 'wp_ajax_dnh_dismiss_notice', array( self::$instance, 'dismiss_notice_ajax' ) );
120
+			add_action('admin_notices', array(self::$instance, 'display'));
121
+			add_action('admin_print_scripts', array(self::$instance, 'load_script'));
122
+			add_action('wp_ajax_dnh_dismiss_notice', array(self::$instance, 'dismiss_notice_ajax'));
123 123
 
124 124
 		}
125 125
 
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 		 */
132 132
 		private function is_wp_compatible() {
133 133
 
134
-			if ( version_compare( get_bloginfo( 'version' ), self::$instance->wordpress_version_required, '<' ) ) {
134
+			if (version_compare(get_bloginfo('version'), self::$instance->wordpress_version_required, '<')) {
135 135
 				return false;
136 136
 			}
137 137
 
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
 		 */
148 148
 		private function is_php_compatible() {
149 149
 
150
-			if ( version_compare( phpversion(), self::$instance->php_version_required, '<' ) ) {
150
+			if (version_compare(phpversion(), self::$instance->php_version_required, '<')) {
151 151
 				return false;
152 152
 			}
153 153
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 		 * @return void
163 163
 		 */
164 164
 		private function includes() {
165
-			require( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/helper-functions.php' );
165
+			require(trailingslashit(plugin_dir_path(__FILE__)).'includes/helper-functions.php');
166 166
 		}
167 167
 
168 168
 		/**
@@ -172,8 +172,8 @@  discard block
 block discarded – undo
172 172
 		 * @return void
173 173
 		 */
174 174
 		public function load_script() {
175
-			wp_register_script( 'dnh', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/js/main.js', array( 'jquery' ), self::$instance->version, true );
176
-			wp_enqueue_script( 'dnh' );
175
+			wp_register_script('dnh', trailingslashit(plugin_dir_url(__FILE__)).'assets/js/main.js', array('jquery'), self::$instance->version, true);
176
+			wp_enqueue_script('dnh');
177 177
 		}
178 178
 
179 179
 		/**
@@ -184,21 +184,21 @@  discard block
 block discarded – undo
184 184
 		 */
185 185
 		public function display() {
186 186
 
187
-			if ( is_null( self::$instance->notices ) || empty( self::$instance->notices ) ) {
187
+			if (is_null(self::$instance->notices) || empty(self::$instance->notices)) {
188 188
 				return;
189 189
 			}
190 190
 
191
-			foreach ( self::$instance->notices as $id => $notice ) {
191
+			foreach (self::$instance->notices as $id => $notice) {
192 192
 
193
-				$id = self::$instance->get_id( $id );
193
+				$id = self::$instance->get_id($id);
194 194
 
195 195
 				// Check if the notice was dismissed
196
-				if ( self::$instance->is_dismissed( $id ) ) {
196
+				if (self::$instance->is_dismissed($id)) {
197 197
 					continue;
198 198
 				}
199 199
 
200 200
 				// Check if the current user has required capability
201
-				if ( ! empty( $notice['cap'] ) && ! current_user_can( $notice['cap'] ) ) {
201
+				if ( ! empty($notice['cap']) && ! current_user_can($notice['cap'])) {
202 202
 					continue;
203 203
 				}
204 204
 
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 					$notice['class'],
210 210
 				);
211 211
 
212
-				printf( '<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim( implode( ' ', $class ) ), $notice['content'], "dnh-$id" );
212
+				printf('<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim(implode(' ', $class)), $notice['content'], "dnh-$id");
213 213
 
214 214
 			}
215 215
 
@@ -224,11 +224,11 @@  discard block
 block discarded – undo
224 224
 		 *
225 225
 		 * @return void
226 226
 		 */
227
-		protected function spit_error( $error ) {
227
+		protected function spit_error($error) {
228 228
 			printf(
229 229
 				'<div style="margin: 20px; text-align: center;"><strong>%1$s</strong> %2$s</pre></div>',
230
-				esc_html__( 'Dismissible Notices Handler Error:', 'wp-dismissible-notices-handler' ),
231
-				wp_kses_post( $error )
230
+				esc_html__('Dismissible Notices Handler Error:', 'wp-dismissible-notices-handler'),
231
+				wp_kses_post($error)
232 232
 			);
233 233
 		}
234 234
 
@@ -241,8 +241,8 @@  discard block
 block discarded – undo
241 241
 		 *
242 242
 		 * @return string
243 243
 		 */
244
-		public function get_id( $id ) {
245
-			return sanitize_key( $id );
244
+		public function get_id($id) {
245
+			return sanitize_key($id);
246 246
 		}
247 247
 
248 248
 		/**
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
 				'notice-info',
264 264
 			);
265 265
 
266
-			return apply_filters( 'dnh_notice_types', $types );
266
+			return apply_filters('dnh_notice_types', $types);
267 267
 
268 268
 		}
269 269
 
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 				'class'  => '', // Additional class to add to the notice
283 283
 			);
284 284
 
285
-			return apply_filters( 'dnh_default_args', $args );
285
+			return apply_filters('dnh_default_args', $args);
286 286
 
287 287
 		}
288 288
 
@@ -298,23 +298,23 @@  discard block
 block discarded – undo
298 298
 		 *
299 299
 		 * @return bool
300 300
 		 */
301
-		public function register_notice( $id, $type, $content, $args = array() ) {
301
+		public function register_notice($id, $type, $content, $args = array()) {
302 302
 
303
-			if ( is_null( self::$instance->notices ) ) {
303
+			if (is_null(self::$instance->notices)) {
304 304
 				self::$instance->notices = array();
305 305
 			}
306 306
 
307
-			$id      = self::$instance->get_id( $id );
308
-			$type    = in_array( $t = sanitize_text_field( $type ), self::$instance->get_types() ) ? $t : 'updated';
309
-			$content = wp_kses_post( $content );
310
-			$args    = wp_parse_args( $args, self::$instance->default_args() );
307
+			$id      = self::$instance->get_id($id);
308
+			$type    = in_array($t = sanitize_text_field($type), self::$instance->get_types()) ? $t : 'updated';
309
+			$content = wp_kses_post($content);
310
+			$args    = wp_parse_args($args, self::$instance->default_args());
311 311
 
312
-			if ( array_key_exists( $id, self::$instance->notices ) ) {
312
+			if (array_key_exists($id, self::$instance->notices)) {
313 313
 
314 314
 				self::$instance->spit_error(
315 315
 					sprintf(
316 316
 						/* translators: %s: required php version */
317
-						esc_html__( 'A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler' ),
317
+						esc_html__('A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler'),
318 318
 						"<code>$id</code>"
319 319
 					)
320 320
 				);
@@ -327,9 +327,9 @@  discard block
 block discarded – undo
327 327
 				'content' => $content,
328 328
 			);
329 329
 
330
-			$notice = array_merge( $notice, $args );
330
+			$notice = array_merge($notice, $args);
331 331
 
332
-			self::$instance->notices[ $id ] = $notice;
332
+			self::$instance->notices[$id] = $notice;
333 333
 
334 334
 			return true;
335 335
 
@@ -343,19 +343,19 @@  discard block
 block discarded – undo
343 343
 		 */
344 344
 		public function dismiss_notice_ajax() {
345 345
 
346
-			if ( ! isset( $_POST['id'] ) ) {
346
+			if ( ! isset($_POST['id'])) {
347 347
 				echo 0;
348 348
 				exit;
349 349
 			}
350 350
 
351
-			if ( empty( $_POST['id'] ) || false === strpos( $_POST['id'], 'dnh-' ) ) {
351
+			if (empty($_POST['id']) || false === strpos($_POST['id'], 'dnh-')) {
352 352
 				echo 0;
353 353
 				exit;
354 354
 			}
355 355
 
356
-			$id = self::$instance->get_id( str_replace( 'dnh-', '', $_POST['id'] ) );
356
+			$id = self::$instance->get_id(str_replace('dnh-', '', $_POST['id']));
357 357
 
358
-			echo self::$instance->dismiss_notice( $id );
358
+			echo self::$instance->dismiss_notice($id);
359 359
 			exit;
360 360
 
361 361
 		}
@@ -369,19 +369,19 @@  discard block
 block discarded – undo
369 369
 		 *
370 370
 		 * @return bool
371 371
 		 */
372
-		public function dismiss_notice( $id ) {
372
+		public function dismiss_notice($id) {
373 373
 
374
-			$notice = self::$instance->get_notice( self::$instance->get_id( $id ) );
374
+			$notice = self::$instance->get_notice(self::$instance->get_id($id));
375 375
 
376
-			if ( false === $notice ) {
376
+			if (false === $notice) {
377 377
 				return false;
378 378
 			}
379 379
 
380
-			if ( self::$instance->is_dismissed( $id ) ) {
380
+			if (self::$instance->is_dismissed($id)) {
381 381
 				return false;
382 382
 			}
383 383
 
384
-			return 'user' === $notice['scope'] ? self::$instance->dismiss_user( $id ) : self::$instance->dismiss_global( $id );
384
+			return 'user' === $notice['scope'] ? self::$instance->dismiss_user($id) : self::$instance->dismiss_global($id);
385 385
 
386 386
 		}
387 387
 
@@ -394,17 +394,17 @@  discard block
 block discarded – undo
394 394
 		 *
395 395
 		 * @return int|bool
396 396
 		 */
397
-		private function dismiss_user( $id ) {
397
+		private function dismiss_user($id) {
398 398
 
399 399
 			$dismissed = self::$instance->dismissed_user();
400 400
 
401
-			if ( in_array( $id, $dismissed ) ) {
401
+			if (in_array($id, $dismissed)) {
402 402
 				return false;
403 403
 			}
404 404
 
405
-			array_push( $dismissed, $id );
405
+			array_push($dismissed, $id);
406 406
 
407
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
407
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
408 408
 
409 409
 		}
410 410
 
@@ -417,17 +417,17 @@  discard block
 block discarded – undo
417 417
 		 *
418 418
 		 * @return bool
419 419
 		 */
420
-		private function dismiss_global( $id ) {
420
+		private function dismiss_global($id) {
421 421
 
422 422
 			$dismissed = self::$instance->dismissed_global();
423 423
 
424
-			if ( in_array( $id, $dismissed ) ) {
424
+			if (in_array($id, $dismissed)) {
425 425
 				return false;
426 426
 			}
427 427
 
428
-			array_push( $dismissed, $id );
428
+			array_push($dismissed, $id);
429 429
 
430
-			return update_option( 'dnh_dismissed_notices', $dismissed );
430
+			return update_option('dnh_dismissed_notices', $dismissed);
431 431
 
432 432
 		}
433 433
 
@@ -440,16 +440,16 @@  discard block
 block discarded – undo
440 440
 		 *
441 441
 		 * @return bool
442 442
 		 */
443
-		public function restore_notice( $id ) {
443
+		public function restore_notice($id) {
444 444
 
445
-			$id     = self::$instance->get_id( $id );
446
-			$notice = self::$instance->get_notice( $id );
445
+			$id     = self::$instance->get_id($id);
446
+			$notice = self::$instance->get_notice($id);
447 447
 
448
-			if ( false === $notice ) {
448
+			if (false === $notice) {
449 449
 				return false;
450 450
 			}
451 451
 
452
-			return 'user' === $notice['scope'] ? self::$instance->restore_user( $id ) : self::$instance->restore_global( $id );
452
+			return 'user' === $notice['scope'] ? self::$instance->restore_user($id) : self::$instance->restore_global($id);
453 453
 
454 454
 		}
455 455
 
@@ -462,27 +462,27 @@  discard block
 block discarded – undo
462 462
 		 *
463 463
 		 * @return bool
464 464
 		 */
465
-		private function restore_user( $id ) {
465
+		private function restore_user($id) {
466 466
 
467
-			$id     = self::$instance->get_id( $id );
468
-			$notice = self::$instance->get_notice( $id );
467
+			$id     = self::$instance->get_id($id);
468
+			$notice = self::$instance->get_notice($id);
469 469
 
470
-			if ( false === $notice ) {
470
+			if (false === $notice) {
471 471
 				return false;
472 472
 			}
473 473
 
474 474
 			$dismissed = self::$instance->dismissed_user();
475 475
 
476
-			if ( ! in_array( $id, $dismissed ) ) {
476
+			if ( ! in_array($id, $dismissed)) {
477 477
 				return false;
478 478
 			}
479 479
 
480
-			$flip = array_flip( $dismissed );
481
-			$key  = $flip[ $id ];
480
+			$flip = array_flip($dismissed);
481
+			$key  = $flip[$id];
482 482
 
483
-			unset( $dismissed[ $key ] );
483
+			unset($dismissed[$key]);
484 484
 
485
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
485
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
486 486
 
487 487
 		}
488 488
 
@@ -495,27 +495,27 @@  discard block
 block discarded – undo
495 495
 		 *
496 496
 		 * @return bool
497 497
 		 */
498
-		private function restore_global( $id ) {
498
+		private function restore_global($id) {
499 499
 
500
-			$id     = self::$instance->get_id( $id );
501
-			$notice = self::$instance->get_notice( $id );
500
+			$id     = self::$instance->get_id($id);
501
+			$notice = self::$instance->get_notice($id);
502 502
 
503
-			if ( false === $notice ) {
503
+			if (false === $notice) {
504 504
 				return false;
505 505
 			}
506 506
 
507 507
 			$dismissed = self::$instance->dismissed_global();
508 508
 
509
-			if ( ! in_array( $id, $dismissed ) ) {
509
+			if ( ! in_array($id, $dismissed)) {
510 510
 				return false;
511 511
 			}
512 512
 
513
-			$flip = array_flip( $dismissed );
514
-			$key  = $flip[ $id ];
513
+			$flip = array_flip($dismissed);
514
+			$key  = $flip[$id];
515 515
 
516
-			unset( $dismissed[ $key ] );
516
+			unset($dismissed[$key]);
517 517
 
518
-			return update_option( 'dnh_dismissed_notices', $dismissed );
518
+			return update_option('dnh_dismissed_notices', $dismissed);
519 519
 
520 520
 		}
521 521
 
@@ -532,7 +532,7 @@  discard block
 block discarded – undo
532 532
 			$user   = self::$instance->dismissed_user();
533 533
 			$global = self::$instance->dismissed_global();
534 534
 
535
-			return array_merge( $user, $global );
535
+			return array_merge($user, $global);
536 536
 
537 537
 		}
538 538
 
@@ -544,9 +544,9 @@  discard block
 block discarded – undo
544 544
 		 */
545 545
 		private function dismissed_user() {
546 546
 
547
-			$dismissed = get_user_meta( get_current_user_id(), 'dnh_dismissed_notices', true );
547
+			$dismissed = get_user_meta(get_current_user_id(), 'dnh_dismissed_notices', true);
548 548
 
549
-			if ( '' === $dismissed ) {
549
+			if ('' === $dismissed) {
550 550
 				$dismissed = array();
551 551
 			}
552 552
 
@@ -561,7 +561,7 @@  discard block
 block discarded – undo
561 561
 		 * @return array
562 562
 		 */
563 563
 		private function dismissed_global() {
564
-			return get_option( 'dnh_dismissed_notices', array() );
564
+			return get_option('dnh_dismissed_notices', array());
565 565
 		}
566 566
 
567 567
 		/**
@@ -573,11 +573,11 @@  discard block
 block discarded – undo
573 573
 		 *
574 574
 		 * @return bool
575 575
 		 */
576
-		public function is_dismissed( $id ) {
576
+		public function is_dismissed($id) {
577 577
 
578 578
 			$dismissed = self::$instance->dismissed_notices();
579 579
 
580
-			if ( ! in_array( self::$instance->get_id( $id ), $dismissed ) ) {
580
+			if ( ! in_array(self::$instance->get_id($id), $dismissed)) {
581 581
 				return false;
582 582
 			}
583 583
 
@@ -604,15 +604,15 @@  discard block
 block discarded – undo
604 604
 		 *
605 605
 		 * @return array|false
606 606
 		 */
607
-		public function get_notice( $id ) {
607
+		public function get_notice($id) {
608 608
 
609
-			$id = self::$instance->get_id( $id );
609
+			$id = self::$instance->get_id($id);
610 610
 
611
-			if ( ! is_array( self::$instance->notices ) || ! array_key_exists( $id, self::$instance->notices ) ) {
611
+			if ( ! is_array(self::$instance->notices) || ! array_key_exists($id, self::$instance->notices)) {
612 612
 				return false;
613 613
 			}
614 614
 
615
-			return self::$instance->notices[ $id ];
615
+			return self::$instance->notices[$id];
616 616
 
617 617
 		}
618 618
 
Please login to merge, or discard this patch.