Completed
Branch master (335b91)
by Julien
02:24
created
Category
handler.php 1 patch
Spacing   +87 added lines, -87 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,24 +90,24 @@  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() ) {
94
-				self::$instance->spit_error( sprintf( 'The library can not be used because your version of WordPress is too old. You need version %s at least.', self::$instance->wordpress_version_required ) );
93
+			if ( ! self::$instance->is_wp_compatible()) {
94
+				self::$instance->spit_error(sprintf('The library can not be used because your version of WordPress is too old. You need version %s at least.', self::$instance->wordpress_version_required));
95 95
 
96 96
 				return;
97 97
 			}
98 98
 
99 99
 			// Make sure PHP is compatible
100
-			if ( ! self::$instance->is_php_compatible() ) {
101
-				self::$instance->spit_error( sprintf( 'The library can not be used because your version of PHP is too old. You need version %s at least.', self::$instance->php_version_required ) );
100
+			if ( ! self::$instance->is_php_compatible()) {
101
+				self::$instance->spit_error(sprintf('The library can not be used because your version of PHP is too old. You need version %s at least.', self::$instance->php_version_required));
102 102
 
103 103
 				return;
104 104
 			}
105 105
 
106 106
 			self::$instance->includes();
107 107
 
108
-			add_action( 'admin_notices', array( self::$instance, 'display' ) );
109
-			add_action( 'admin_print_scripts', array( self::$instance, 'load_script' ) );
110
-			add_action( 'wp_ajax_dnh_dismiss_notice', array( self::$instance, 'dismiss_notice_ajax' ) );
108
+			add_action('admin_notices', array(self::$instance, 'display'));
109
+			add_action('admin_print_scripts', array(self::$instance, 'load_script'));
110
+			add_action('wp_ajax_dnh_dismiss_notice', array(self::$instance, 'dismiss_notice_ajax'));
111 111
 
112 112
 		}
113 113
 
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
 		 */
120 120
 		private function is_wp_compatible() {
121 121
 
122
-			if ( version_compare( get_bloginfo( 'version' ), self::$instance->wordpress_version_required, '<' ) ) {
122
+			if (version_compare(get_bloginfo('version'), self::$instance->wordpress_version_required, '<')) {
123 123
 				return false;
124 124
 			}
125 125
 
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 		 */
136 136
 		private function is_php_compatible() {
137 137
 
138
-			if ( version_compare( phpversion(), self::$instance->php_version_required, '<' ) ) {
138
+			if (version_compare(phpversion(), self::$instance->php_version_required, '<')) {
139 139
 				return false;
140 140
 			}
141 141
 
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 		 * @return void
151 151
 		 */
152 152
 		private function includes() {
153
-			require( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/helper-functions.php' );
153
+			require(trailingslashit(plugin_dir_path(__FILE__)).'includes/helper-functions.php');
154 154
 		}
155 155
 
156 156
 		/**
@@ -160,8 +160,8 @@  discard block
 block discarded – undo
160 160
 		 * @return void
161 161
 		 */
162 162
 		public function load_script() {
163
-			wp_register_script( 'dnh', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/js/main.js', array( 'jquery' ), self::$instance->version, true );
164
-			wp_enqueue_script( 'dnh' );
163
+			wp_register_script('dnh', trailingslashit(plugin_dir_url(__FILE__)).'assets/js/main.js', array('jquery'), self::$instance->version, true);
164
+			wp_enqueue_script('dnh');
165 165
 		}
166 166
 
167 167
 		/**
@@ -172,21 +172,21 @@  discard block
 block discarded – undo
172 172
 		 */
173 173
 		public function display() {
174 174
 
175
-			if ( is_null( self::$instance->notices ) || empty( self::$instance->notices ) ) {
175
+			if (is_null(self::$instance->notices) || empty(self::$instance->notices)) {
176 176
 				return;
177 177
 			}
178 178
 
179
-			foreach ( self::$instance->notices as $id => $notice ) {
179
+			foreach (self::$instance->notices as $id => $notice) {
180 180
 
181
-				$id = self::$instance->get_id( $id );
181
+				$id = self::$instance->get_id($id);
182 182
 
183 183
 				// Check if the notice was dismissed
184
-				if ( self::$instance->is_dismissed( $id ) ) {
184
+				if (self::$instance->is_dismissed($id)) {
185 185
 					continue;
186 186
 				}
187 187
 
188 188
 				// Check if the current user has required capability
189
-				if ( ! empty( $notice['cap'] ) && ! current_user_can( $notice['cap'] ) ) {
189
+				if ( ! empty($notice['cap']) && ! current_user_can($notice['cap'])) {
190 190
 					continue;
191 191
 				}
192 192
 
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 					$notice['class'],
198 198
 				);
199 199
 
200
-				printf( '<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim( implode( ' ', $class ) ), $notice['content'], "dnh-$id" );
200
+				printf('<div id="%3$s" class="%1$s"><p>%2$s</p></div>', trim(implode(' ', $class)), $notice['content'], "dnh-$id");
201 201
 
202 202
 			}
203 203
 
@@ -212,8 +212,8 @@  discard block
 block discarded – undo
212 212
 		 *
213 213
 		 * @return void
214 214
 		 */
215
-		protected function spit_error( $error ) {
216
-			printf( '<div style="margin: 20px; text-align: center;"><strong>%1$s</strong> %2$s</pre></div>', 'Dismissible Notices Handler Error:', wp_kses_post( $error ) );
215
+		protected function spit_error($error) {
216
+			printf('<div style="margin: 20px; text-align: center;"><strong>%1$s</strong> %2$s</pre></div>', 'Dismissible Notices Handler Error:', wp_kses_post($error));
217 217
 		}
218 218
 
219 219
 		/**
@@ -225,8 +225,8 @@  discard block
 block discarded – undo
225 225
 		 *
226 226
 		 * @return string
227 227
 		 */
228
-		public function get_id( $id ) {
229
-			return sanitize_key( $id );
228
+		public function get_id($id) {
229
+			return sanitize_key($id);
230 230
 		}
231 231
 
232 232
 		/**
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
 				'updated',
243 243
 			);
244 244
 
245
-			return apply_filters( 'dnh_notice_types', $types );
245
+			return apply_filters('dnh_notice_types', $types);
246 246
 
247 247
 		}
248 248
 
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 				'class'  => '', // Additional class to add to the notice
262 262
 			);
263 263
 
264
-			return apply_filters( 'dnh_default_args', $args );
264
+			return apply_filters('dnh_default_args', $args);
265 265
 
266 266
 		}
267 267
 
@@ -277,20 +277,20 @@  discard block
 block discarded – undo
277 277
 		 *
278 278
 		 * @return bool
279 279
 		 */
280
-		public function register_notice( $id, $type, $content, $args = array() ) {
280
+		public function register_notice($id, $type, $content, $args = array()) {
281 281
 
282
-			if ( is_null( self::$instance->notices ) ) {
282
+			if (is_null(self::$instance->notices)) {
283 283
 				self::$instance->notices = array();
284 284
 			}
285 285
 
286
-			$id      = self::$instance->get_id( $id );
287
-			$type    = in_array( $t = sanitize_text_field( $type ), self::$instance->get_types() ) ? $t : 'updated';
288
-			$content = wp_kses_post( $content );
289
-			$args    = wp_parse_args( $args, self::$instance->default_args() );
286
+			$id      = self::$instance->get_id($id);
287
+			$type    = in_array($t = sanitize_text_field($type), self::$instance->get_types()) ? $t : 'updated';
288
+			$content = wp_kses_post($content);
289
+			$args    = wp_parse_args($args, self::$instance->default_args());
290 290
 
291
-			if ( array_key_exists( $id, self::$instance->notices ) ) {
291
+			if (array_key_exists($id, self::$instance->notices)) {
292 292
 
293
-				self::$instance->spit_error( sprintf( 'A notice with the ID %s has already been registered.', "<code>$id</code>" ) );
293
+				self::$instance->spit_error(sprintf('A notice with the ID %s has already been registered.', "<code>$id</code>"));
294 294
 
295 295
 				return false;
296 296
 			}
@@ -300,9 +300,9 @@  discard block
 block discarded – undo
300 300
 				'content' => $content,
301 301
 			);
302 302
 
303
-			$notice = array_merge( $notice, $args );
303
+			$notice = array_merge($notice, $args);
304 304
 
305
-			self::$instance->notices[ $id ] = $notice;
305
+			self::$instance->notices[$id] = $notice;
306 306
 
307 307
 			return true;
308 308
 
@@ -316,19 +316,19 @@  discard block
 block discarded – undo
316 316
 		 */
317 317
 		public function dismiss_notice_ajax() {
318 318
 
319
-			if ( ! isset( $_POST['id'] ) ) {
319
+			if ( ! isset($_POST['id'])) {
320 320
 				echo 0;
321 321
 				exit;
322 322
 			}
323 323
 
324
-			if ( empty( $_POST['id'] ) || false === strpos( $_POST['id'], 'dnh-' ) ) {
324
+			if (empty($_POST['id']) || false === strpos($_POST['id'], 'dnh-')) {
325 325
 				echo 0;
326 326
 				exit;
327 327
 			}
328 328
 
329
-			$id = self::$instance->get_id( str_replace( 'dnh-', '', $_POST['id'] ) );
329
+			$id = self::$instance->get_id(str_replace('dnh-', '', $_POST['id']));
330 330
 
331
-			echo self::$instance->dismiss_notice( $id );
331
+			echo self::$instance->dismiss_notice($id);
332 332
 			exit;
333 333
 
334 334
 		}
@@ -342,19 +342,19 @@  discard block
 block discarded – undo
342 342
 		 *
343 343
 		 * @return bool
344 344
 		 */
345
-		public function dismiss_notice( $id ) {
345
+		public function dismiss_notice($id) {
346 346
 
347
-			$notice = self::$instance->get_notice( self::$instance->get_id( $id ) );
347
+			$notice = self::$instance->get_notice(self::$instance->get_id($id));
348 348
 
349
-			if ( false === $notice ) {
349
+			if (false === $notice) {
350 350
 				return false;
351 351
 			}
352 352
 
353
-			if ( self::$instance->is_dismissed( $id ) ) {
353
+			if (self::$instance->is_dismissed($id)) {
354 354
 				return false;
355 355
 			}
356 356
 
357
-			return 'user' === $notice['scope'] ? self::$instance->dismiss_user( $id ) : self::$instance->dismiss_global( $id );
357
+			return 'user' === $notice['scope'] ? self::$instance->dismiss_user($id) : self::$instance->dismiss_global($id);
358 358
 
359 359
 		}
360 360
 
@@ -367,17 +367,17 @@  discard block
 block discarded – undo
367 367
 		 *
368 368
 		 * @return int|bool
369 369
 		 */
370
-		private function dismiss_user( $id ) {
370
+		private function dismiss_user($id) {
371 371
 
372 372
 			$dismissed = self::$instance->dismissed_user();
373 373
 
374
-			if ( in_array( $id, $dismissed ) ) {
374
+			if (in_array($id, $dismissed)) {
375 375
 				return false;
376 376
 			}
377 377
 
378
-			array_push( $dismissed, $id );
378
+			array_push($dismissed, $id);
379 379
 
380
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
380
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
381 381
 
382 382
 		}
383 383
 
@@ -390,17 +390,17 @@  discard block
 block discarded – undo
390 390
 		 *
391 391
 		 * @return bool
392 392
 		 */
393
-		private function dismiss_global( $id ) {
393
+		private function dismiss_global($id) {
394 394
 
395 395
 			$dismissed = self::$instance->dismissed_global();
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_option( 'dnh_dismissed_notices', $dismissed );
403
+			return update_option('dnh_dismissed_notices', $dismissed);
404 404
 
405 405
 		}
406 406
 
@@ -413,16 +413,16 @@  discard block
 block discarded – undo
413 413
 		 *
414 414
 		 * @return bool
415 415
 		 */
416
-		public function restore_notice( $id ) {
416
+		public function restore_notice($id) {
417 417
 
418
-			$id     = self::$instance->get_id( $id );
419
-			$notice = self::$instance->get_notice( $id );
418
+			$id     = self::$instance->get_id($id);
419
+			$notice = self::$instance->get_notice($id);
420 420
 
421
-			if ( false === $notice ) {
421
+			if (false === $notice) {
422 422
 				return false;
423 423
 			}
424 424
 
425
-			return 'user' === $notice['scope'] ? self::$instance->restore_user( $id ) : self::$instance->restore_global( $id );
425
+			return 'user' === $notice['scope'] ? self::$instance->restore_user($id) : self::$instance->restore_global($id);
426 426
 
427 427
 		}
428 428
 
@@ -435,27 +435,27 @@  discard block
 block discarded – undo
435 435
 		 *
436 436
 		 * @return bool
437 437
 		 */
438
-		private function restore_user( $id ) {
438
+		private function restore_user($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 447
 			$dismissed = self::$instance->dismissed_user();
448 448
 
449
-			if ( ! in_array( $id, $dismissed ) ) {
449
+			if ( ! in_array($id, $dismissed)) {
450 450
 				return false;
451 451
 			}
452 452
 
453
-			$flip = array_flip( $dismissed );
454
-			$key  = $flip[ $id ];
453
+			$flip = array_flip($dismissed);
454
+			$key  = $flip[$id];
455 455
 
456
-			unset( $dismissed[ $key ] );
456
+			unset($dismissed[$key]);
457 457
 
458
-			return update_user_meta( get_current_user_id(), 'dnh_dismissed_notices', $dismissed );
458
+			return update_user_meta(get_current_user_id(), 'dnh_dismissed_notices', $dismissed);
459 459
 
460 460
 		}
461 461
 
@@ -468,27 +468,27 @@  discard block
 block discarded – undo
468 468
 		 *
469 469
 		 * @return bool
470 470
 		 */
471
-		private function restore_global( $id ) {
471
+		private function restore_global($id) {
472 472
 
473
-			$id     = self::$instance->get_id( $id );
474
-			$notice = self::$instance->get_notice( $id );
473
+			$id     = self::$instance->get_id($id);
474
+			$notice = self::$instance->get_notice($id);
475 475
 
476
-			if ( false === $notice ) {
476
+			if (false === $notice) {
477 477
 				return false;
478 478
 			}
479 479
 
480 480
 			$dismissed = self::$instance->dismissed_global();
481 481
 
482
-			if ( ! in_array( $id, $dismissed ) ) {
482
+			if ( ! in_array($id, $dismissed)) {
483 483
 				return false;
484 484
 			}
485 485
 
486
-			$flip = array_flip( $dismissed );
487
-			$key  = $flip[ $id ];
486
+			$flip = array_flip($dismissed);
487
+			$key  = $flip[$id];
488 488
 
489
-			unset( $dismissed[ $key ] );
489
+			unset($dismissed[$key]);
490 490
 
491
-			return update_option( 'dnh_dismissed_notices', $dismissed );
491
+			return update_option('dnh_dismissed_notices', $dismissed);
492 492
 
493 493
 		}
494 494
 
@@ -505,7 +505,7 @@  discard block
 block discarded – undo
505 505
 			$user   = self::$instance->dismissed_user();
506 506
 			$global = self::$instance->dismissed_global();
507 507
 
508
-			return array_merge( $user, $global );
508
+			return array_merge($user, $global);
509 509
 
510 510
 		}
511 511
 
@@ -517,9 +517,9 @@  discard block
 block discarded – undo
517 517
 		 */
518 518
 		private function dismissed_user() {
519 519
 
520
-			$dismissed = get_user_meta( get_current_user_id(), 'dnh_dismissed_notices', true );
520
+			$dismissed = get_user_meta(get_current_user_id(), 'dnh_dismissed_notices', true);
521 521
 
522
-			if ( '' === $dismissed ) {
522
+			if ('' === $dismissed) {
523 523
 				$dismissed = array();
524 524
 			}
525 525
 
@@ -534,7 +534,7 @@  discard block
 block discarded – undo
534 534
 		 * @return array
535 535
 		 */
536 536
 		private function dismissed_global() {
537
-			return get_option( 'dnh_dismissed_notices', array() );
537
+			return get_option('dnh_dismissed_notices', array());
538 538
 		}
539 539
 
540 540
 		/**
@@ -546,11 +546,11 @@  discard block
 block discarded – undo
546 546
 		 *
547 547
 		 * @return bool
548 548
 		 */
549
-		public function is_dismissed( $id ) {
549
+		public function is_dismissed($id) {
550 550
 
551 551
 			$dismissed = self::$instance->dismissed_notices();
552 552
 
553
-			if ( ! in_array( self::$instance->get_id( $id ), $dismissed ) ) {
553
+			if ( ! in_array(self::$instance->get_id($id), $dismissed)) {
554 554
 				return false;
555 555
 			}
556 556
 
@@ -577,15 +577,15 @@  discard block
 block discarded – undo
577 577
 		 *
578 578
 		 * @return array|false
579 579
 		 */
580
-		public function get_notice( $id ) {
580
+		public function get_notice($id) {
581 581
 
582
-			$id = self::$instance->get_id( $id );
582
+			$id = self::$instance->get_id($id);
583 583
 
584
-			if ( ! is_array( self::$instance->notices ) || ! array_key_exists( $id, self::$instance->notices ) ) {
584
+			if ( ! is_array(self::$instance->notices) || ! array_key_exists($id, self::$instance->notices)) {
585 585
 				return false;
586 586
 			}
587 587
 
588
-			return self::$instance->notices[ $id ];
588
+			return self::$instance->notices[$id];
589 589
 
590 590
 		}
591 591
 
Please login to merge, or discard this patch.
includes/helper-functions.php 1 patch
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
  */
19 19
 
20 20
 // If this file is called directly, abort.
21
-if ( ! defined( 'WPINC' ) ) {
21
+if ( ! defined('WPINC')) {
22 22
 	die;
23 23
 }
24 24
 
@@ -34,13 +34,13 @@  discard block
 block discarded – undo
34 34
  *
35 35
  * @return bool
36 36
  */
37
-function dnh_register_notice( $id, $type, $content, $args = array() ) {
37
+function dnh_register_notice($id, $type, $content, $args = array()) {
38 38
 
39
-	if ( ! function_exists( 'DNH' ) ) {
39
+	if ( ! function_exists('DNH')) {
40 40
 		return false;
41 41
 	}
42 42
 
43
-	return DNH()->register_notice( $id, $type, $content, $args );
43
+	return DNH()->register_notice($id, $type, $content, $args);
44 44
 
45 45
 }
46 46
 
@@ -53,13 +53,13 @@  discard block
 block discarded – undo
53 53
  *
54 54
  * @return bool
55 55
  */
56
-function dnh_restore_notice( $id ) {
56
+function dnh_restore_notice($id) {
57 57
 
58
-	if ( ! function_exists( 'DNH' ) ) {
58
+	if ( ! function_exists('DNH')) {
59 59
 		return false;
60 60
 	}
61 61
 
62
-	return DNH()->restore_notice( $id );
62
+	return DNH()->restore_notice($id);
63 63
 
64 64
 }
65 65
 
@@ -72,12 +72,12 @@  discard block
 block discarded – undo
72 72
  *
73 73
  * @return bool
74 74
  */
75
-function dnh_is_dismissed( $id ) {
75
+function dnh_is_dismissed($id) {
76 76
 
77
-	if ( ! function_exists( 'DNH' ) ) {
77
+	if ( ! function_exists('DNH')) {
78 78
 		return false;
79 79
 	}
80 80
 
81
-	return DNH()->is_dismissed( $id );
81
+	return DNH()->is_dismissed($id);
82 82
 
83 83
 }
84 84
\ No newline at end of file
Please login to merge, or discard this patch.