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