1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\Admin; |
4
|
|
|
|
5
|
|
|
class Notifications |
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_site_option( 'amarkal_dismissed_notices', array() ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function register_notification( $handle, $options ) |
40
|
|
|
{ |
41
|
|
|
if( !key_exists( $handle, $this->notifications ) ) |
42
|
|
|
{ |
43
|
|
|
$this->notifications[$handle] = $options; |
44
|
|
|
} |
45
|
|
|
else trigger_error( "The handle <strong>$handle</strong> has already been registered. Please choose a different handle for your notification." ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function render_notifications() |
49
|
|
|
{ |
50
|
|
|
foreach($this->notifications as $handle => $notification) |
51
|
|
|
{ |
52
|
|
|
$this->render_notification( $handle, $notification ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function render_network_notifications() |
57
|
|
|
{ |
58
|
|
|
foreach( $this->notifications as $handle => $notification ) |
59
|
|
|
{ |
60
|
|
|
if( $notification['network'] ) $this->render_notification( $handle, $notification ); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function dismiss_notification() |
65
|
|
|
{ |
66
|
|
|
$id = filter_input( INPUT_POST, 'id' ); |
67
|
|
|
if( !in_array( $id, $this->dismissed_notices ) ) |
68
|
|
|
{ |
69
|
|
|
$this->dismissed_notices[] = $id; |
70
|
|
|
update_site_option( 'amarkal_dismissed_notices', $this->dismissed_notices); |
71
|
|
|
} |
72
|
|
|
wp_die(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function render_script() |
76
|
|
|
{ |
77
|
|
|
if( 0 === count( $this->notifications ) ) return; |
78
|
|
|
?> |
79
|
|
|
<script> |
80
|
|
|
jQuery(document).ready(function($){ |
81
|
|
|
$('.notice').on('click','.notice-dismiss',function(e){ |
82
|
|
|
$.post(ajaxurl,{ |
83
|
|
|
action: 'dismiss_admin_notification', |
84
|
|
|
id: $(this).parent().attr('id') |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
}); |
88
|
|
|
</script> |
89
|
|
|
<?php |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function render_notification( $id, $n ) |
93
|
|
|
{ |
94
|
|
|
if( in_array( $id, $this->dismissed_notices ) ) return; |
95
|
|
|
|
96
|
|
|
printf( |
97
|
|
|
'<div id="%s" class="notice notice-%s %s%s"><p>%s</p></div>', |
98
|
|
|
$id, |
99
|
|
|
$n['type'], |
100
|
|
|
$n['dismissible']?'is-dismissible ':'', |
101
|
|
|
$n['class'], |
102
|
|
|
$n['html'] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: