Completed
Pull Request — master (#6)
by
unknown
01:28
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
 		/**
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
 				'notice-warning'
260 260
 			);
261 261
 
262
-			return apply_filters( 'dnh_notice_types', $types );
262
+			return apply_filters('dnh_notice_types', $types);
263 263
 
264 264
 		}
265 265
 
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
 				'class'  => '', // Additional class to add to the notice
279 279
 			);
280 280
 
281
-			return apply_filters( 'dnh_default_args', $args );
281
+			return apply_filters('dnh_default_args', $args);
282 282
 
283 283
 		}
284 284
 
@@ -294,23 +294,23 @@  discard block
 block discarded – undo
294 294
 		 *
295 295
 		 * @return bool
296 296
 		 */
297
-		public function register_notice( $id, $type, $content, $args = array() ) {
297
+		public function register_notice($id, $type, $content, $args = array()) {
298 298
 
299
-			if ( is_null( self::$instance->notices ) ) {
299
+			if (is_null(self::$instance->notices)) {
300 300
 				self::$instance->notices = array();
301 301
 			}
302 302
 
303
-			$id      = self::$instance->get_id( $id );
304
-			$type    = in_array( $t = sanitize_text_field( $type ), self::$instance->get_types() ) ? $t : 'updated';
305
-			$content = wp_kses_post( $content );
306
-			$args    = wp_parse_args( $args, self::$instance->default_args() );
303
+			$id      = self::$instance->get_id($id);
304
+			$type    = in_array($t = sanitize_text_field($type), self::$instance->get_types()) ? $t : 'updated';
305
+			$content = wp_kses_post($content);
306
+			$args    = wp_parse_args($args, self::$instance->default_args());
307 307
 
308
-			if ( array_key_exists( $id, self::$instance->notices ) ) {
308
+			if (array_key_exists($id, self::$instance->notices)) {
309 309
 
310 310
 				self::$instance->spit_error(
311 311
 					sprintf(
312 312
 						/* translators: %s: required php version */
313
-						esc_html__( 'A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler' ),
313
+						esc_html__('A notice with the ID %s has already been registered.', 'wp-dismissible-notices-handler'),
314 314
 						"<code>$id</code>"
315 315
 					)
316 316
 				);
@@ -323,9 +323,9 @@  discard block
 block discarded – undo
323 323
 				'content' => $content,
324 324
 			);
325 325
 
326
-			$notice = array_merge( $notice, $args );
326
+			$notice = array_merge($notice, $args);
327 327
 
328
-			self::$instance->notices[ $id ] = $notice;
328
+			self::$instance->notices[$id] = $notice;
329 329
 
330 330
 			return true;
331 331
 
@@ -339,19 +339,19 @@  discard block
 block discarded – undo
339 339
 		 */
340 340
 		public function dismiss_notice_ajax() {
341 341
 
342
-			if ( ! isset( $_POST['id'] ) ) {
342
+			if ( ! isset($_POST['id'])) {
343 343
 				echo 0;
344 344
 				exit;
345 345
 			}
346 346
 
347
-			if ( empty( $_POST['id'] ) || false === strpos( $_POST['id'], 'dnh-' ) ) {
347
+			if (empty($_POST['id']) || false === strpos($_POST['id'], 'dnh-')) {
348 348
 				echo 0;
349 349
 				exit;
350 350
 			}
351 351
 
352
-			$id = self::$instance->get_id( str_replace( 'dnh-', '', $_POST['id'] ) );
352
+			$id = self::$instance->get_id(str_replace('dnh-', '', $_POST['id']));
353 353
 
354
-			echo self::$instance->dismiss_notice( $id );
354
+			echo self::$instance->dismiss_notice($id);
355 355
 			exit;
356 356
 
357 357
 		}
@@ -365,19 +365,19 @@  discard block
 block discarded – undo
365 365
 		 *
366 366
 		 * @return bool
367 367
 		 */
368
-		public function dismiss_notice( $id ) {
368
+		public function dismiss_notice($id) {
369 369
 
370
-			$notice = self::$instance->get_notice( self::$instance->get_id( $id ) );
370
+			$notice = self::$instance->get_notice(self::$instance->get_id($id));
371 371
 
372
-			if ( false === $notice ) {
372
+			if (false === $notice) {
373 373
 				return false;
374 374
 			}
375 375
 
376
-			if ( self::$instance->is_dismissed( $id ) ) {
376
+			if (self::$instance->is_dismissed($id)) {
377 377
 				return false;
378 378
 			}
379 379
 
380
-			return 'user' === $notice['scope'] ? self::$instance->dismiss_user( $id ) : self::$instance->dismiss_global( $id );
380
+			return 'user' === $notice['scope'] ? self::$instance->dismiss_user($id) : self::$instance->dismiss_global($id);
381 381
 
382 382
 		}
383 383
 
@@ -390,17 +390,17 @@  discard block
 block discarded – undo
390 390
 		 *
391 391
 		 * @return int|bool
392 392
 		 */
393
-		private function dismiss_user( $id ) {
393
+		private function dismiss_user($id) {
394 394
 
395 395
 			$dismissed = self::$instance->dismissed_user();
396 396
 
397
-			if ( in_array( $id, $dismissed ) ) {
397
+			if (in_array($id, $dismissed)) {
398 398
 				return false;
399 399
 			}
400 400
 
401
-			array_push( $dismissed, $id );
401
+			array_push($dismissed, $id);
402 402
 
403
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
403
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
404 404
 
405 405
 		}
406 406
 
@@ -413,17 +413,17 @@  discard block
 block discarded – undo
413 413
 		 *
414 414
 		 * @return bool
415 415
 		 */
416
-		private function dismiss_global( $id ) {
416
+		private function dismiss_global($id) {
417 417
 
418 418
 			$dismissed = self::$instance->dismissed_global();
419 419
 
420
-			if ( in_array( $id, $dismissed ) ) {
420
+			if (in_array($id, $dismissed)) {
421 421
 				return false;
422 422
 			}
423 423
 
424
-			array_push( $dismissed, $id );
424
+			array_push($dismissed, $id);
425 425
 
426
-			return update_option( 'dnh_dismissed_notices', $dismissed );
426
+			return update_option('dnh_dismissed_notices', $dismissed);
427 427
 
428 428
 		}
429 429
 
@@ -436,16 +436,16 @@  discard block
 block discarded – undo
436 436
 		 *
437 437
 		 * @return bool
438 438
 		 */
439
-		public function restore_notice( $id ) {
439
+		public function restore_notice($id) {
440 440
 
441
-			$id     = self::$instance->get_id( $id );
442
-			$notice = self::$instance->get_notice( $id );
441
+			$id     = self::$instance->get_id($id);
442
+			$notice = self::$instance->get_notice($id);
443 443
 
444
-			if ( false === $notice ) {
444
+			if (false === $notice) {
445 445
 				return false;
446 446
 			}
447 447
 
448
-			return 'user' === $notice['scope'] ? self::$instance->restore_user( $id ) : self::$instance->restore_global( $id );
448
+			return 'user' === $notice['scope'] ? self::$instance->restore_user($id) : self::$instance->restore_global($id);
449 449
 
450 450
 		}
451 451
 
@@ -458,27 +458,27 @@  discard block
 block discarded – undo
458 458
 		 *
459 459
 		 * @return bool
460 460
 		 */
461
-		private function restore_user( $id ) {
461
+		private function restore_user($id) {
462 462
 
463
-			$id     = self::$instance->get_id( $id );
464
-			$notice = self::$instance->get_notice( $id );
463
+			$id     = self::$instance->get_id($id);
464
+			$notice = self::$instance->get_notice($id);
465 465
 
466
-			if ( false === $notice ) {
466
+			if (false === $notice) {
467 467
 				return false;
468 468
 			}
469 469
 
470 470
 			$dismissed = self::$instance->dismissed_user();
471 471
 
472
-			if ( ! in_array( $id, $dismissed ) ) {
472
+			if ( ! in_array($id, $dismissed)) {
473 473
 				return false;
474 474
 			}
475 475
 
476
-			$flip = array_flip( $dismissed );
477
-			$key  = $flip[ $id ];
476
+			$flip = array_flip($dismissed);
477
+			$key  = $flip[$id];
478 478
 
479
-			unset( $dismissed[ $key ] );
479
+			unset($dismissed[$key]);
480 480
 
481
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
481
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
482 482
 
483 483
 		}
484 484
 
@@ -491,27 +491,27 @@  discard block
 block discarded – undo
491 491
 		 *
492 492
 		 * @return bool
493 493
 		 */
494
-		private function restore_global( $id ) {
494
+		private function restore_global($id) {
495 495
 
496
-			$id     = self::$instance->get_id( $id );
497
-			$notice = self::$instance->get_notice( $id );
496
+			$id     = self::$instance->get_id($id);
497
+			$notice = self::$instance->get_notice($id);
498 498
 
499
-			if ( false === $notice ) {
499
+			if (false === $notice) {
500 500
 				return false;
501 501
 			}
502 502
 
503 503
 			$dismissed = self::$instance->dismissed_global();
504 504
 
505
-			if ( ! in_array( $id, $dismissed ) ) {
505
+			if ( ! in_array($id, $dismissed)) {
506 506
 				return false;
507 507
 			}
508 508
 
509
-			$flip = array_flip( $dismissed );
510
-			$key  = $flip[ $id ];
509
+			$flip = array_flip($dismissed);
510
+			$key  = $flip[$id];
511 511
 
512
-			unset( $dismissed[ $key ] );
512
+			unset($dismissed[$key]);
513 513
 
514
-			return update_option( 'dnh_dismissed_notices', $dismissed );
514
+			return update_option('dnh_dismissed_notices', $dismissed);
515 515
 
516 516
 		}
517 517
 
@@ -528,7 +528,7 @@  discard block
 block discarded – undo
528 528
 			$user   = self::$instance->dismissed_user();
529 529
 			$global = self::$instance->dismissed_global();
530 530
 
531
-			return array_merge( $user, $global );
531
+			return array_merge($user, $global);
532 532
 
533 533
 		}
534 534
 
@@ -540,9 +540,9 @@  discard block
 block discarded – undo
540 540
 		 */
541 541
 		private function dismissed_user() {
542 542
 
543
-			$dismissed = get_user_meta( get_current_user_id(), 'dnh_dismissed_notices', true );
543
+			$dismissed = get_user_meta(get_current_user_id(), 'dnh_dismissed_notices', true);
544 544
 
545
-			if ( '' === $dismissed ) {
545
+			if ('' === $dismissed) {
546 546
 				$dismissed = array();
547 547
 			}
548 548
 
@@ -557,7 +557,7 @@  discard block
 block discarded – undo
557 557
 		 * @return array
558 558
 		 */
559 559
 		private function dismissed_global() {
560
-			return get_option( 'dnh_dismissed_notices', array() );
560
+			return get_option('dnh_dismissed_notices', array());
561 561
 		}
562 562
 
563 563
 		/**
@@ -569,11 +569,11 @@  discard block
 block discarded – undo
569 569
 		 *
570 570
 		 * @return bool
571 571
 		 */
572
-		public function is_dismissed( $id ) {
572
+		public function is_dismissed($id) {
573 573
 
574 574
 			$dismissed = self::$instance->dismissed_notices();
575 575
 
576
-			if ( ! in_array( self::$instance->get_id( $id ), $dismissed ) ) {
576
+			if ( ! in_array(self::$instance->get_id($id), $dismissed)) {
577 577
 				return false;
578 578
 			}
579 579
 
@@ -600,15 +600,15 @@  discard block
 block discarded – undo
600 600
 		 *
601 601
 		 * @return array|false
602 602
 		 */
603
-		public function get_notice( $id ) {
603
+		public function get_notice($id) {
604 604
 
605
-			$id = self::$instance->get_id( $id );
605
+			$id = self::$instance->get_id($id);
606 606
 
607
-			if ( ! is_array( self::$instance->notices ) || ! array_key_exists( $id, self::$instance->notices ) ) {
607
+			if ( ! is_array(self::$instance->notices) || ! array_key_exists($id, self::$instance->notices)) {
608 608
 				return false;
609 609
 			}
610 610
 
611
-			return self::$instance->notices[ $id ];
611
+			return self::$instance->notices[$id];
612 612
 
613 613
 		}
614 614
 
Please login to merge, or discard this patch.