These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | if(!class_exists('WPAdminNotifications')) |
||
4 | { |
||
5 | class WPAdminNotifications |
||
6 | { |
||
7 | /** |
||
8 | * @var Singleton The reference to *Singleton* instance of this class |
||
9 | */ |
||
10 | private static $instance; |
||
11 | |||
12 | private $notifications = array(); |
||
13 | |||
14 | private $dismissed_notices; |
||
15 | |||
16 | /** |
||
17 | * Returns the *Singleton* instance of this class. |
||
18 | * |
||
19 | * @return Singleton The *Singleton* instance. |
||
20 | */ |
||
21 | public static function get_instance() |
||
22 | { |
||
23 | if( null === static::$instance ) |
||
24 | { |
||
25 | static::$instance = new static(); |
||
26 | } |
||
27 | return static::$instance; |
||
28 | } |
||
29 | |||
30 | public function init() |
||
31 | { |
||
32 | add_action( 'admin_notices', array( $this, 'render_notifications' ) ); |
||
33 | add_action( 'network_admin_notices', array( $this, 'render_network_notifications' ) ); |
||
34 | add_action( 'wp_ajax_dismiss_admin_notification', array( $this, 'dismiss_notification' ) ); |
||
35 | add_action( 'admin_footer', array( $this, 'render_script' ) ); // Must be hooked to a late action hook |
||
36 | $this->dismissed_notices = get_option('wp_dismissed_notices'); |
||
37 | if( false === $this->dismissed_notices ) $this->dismissed_notices = array(); |
||
38 | } |
||
39 | |||
40 | public function register_notification( $handle, $options ) |
||
41 | { |
||
42 | if( !key_exists( $handle, $this->notifications ) ) |
||
43 | { |
||
44 | $this->notifications[$handle] = $options; |
||
45 | } |
||
46 | else trigger_error( "The handle <strong>$handle</strong> has already been registered. Please choose a different handle for your notification." ); |
||
47 | } |
||
48 | |||
49 | public function render_notifications() |
||
50 | { |
||
51 | foreach($this->notifications as $handle => $notification) |
||
52 | { |
||
53 | $this->render_notification( $handle, $notification ); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | public function render_network_notifications() |
||
58 | { |
||
59 | foreach( $this->notifications as $handle => $notification ) |
||
60 | { |
||
61 | if( $notification['network'] ) $this->render_notification( $handle, $notification ); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public function dismiss_notification() |
||
66 | { |
||
67 | $id = filter_input( INPUT_POST, 'id' ); |
||
68 | if( !in_array( $id, $this->dismissed_notices ) ) |
||
69 | { |
||
70 | $this->dismissed_notices[] = $id; |
||
71 | update_option( 'wp_dismissed_notices', $this->dismissed_notices); |
||
72 | } |
||
73 | die(); |
||
0 ignored issues
–
show
|
|||
74 | } |
||
75 | |||
76 | public function render_script() |
||
77 | { |
||
78 | if( 0 === count( $this->notifications ) ) return; |
||
79 | ?> |
||
80 | <script> |
||
81 | jQuery(document).ready(function($){ |
||
82 | $('.notice').on('click','.notice-dismiss',function(e){ |
||
83 | $.post(ajaxurl,{ |
||
84 | action: 'dismiss_admin_notification', |
||
85 | id: $(this).parent().attr('id') |
||
86 | }); |
||
87 | }); |
||
88 | }); |
||
89 | </script> |
||
90 | <?php |
||
91 | } |
||
92 | |||
93 | private function render_notification( $id, $n ) |
||
94 | { |
||
95 | if( in_array( $id, $this->dismissed_notices ) ) return; |
||
96 | |||
97 | printf( |
||
98 | '<div id="%s" class="notice notice-%s %s%s"><p>%s</p></div>', |
||
99 | $id, |
||
100 | $n['type'], |
||
101 | $n['dismissible']?'is-dismissible ':'', |
||
102 | $n['class'], |
||
103 | $n['html'] |
||
104 | ); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.