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 register_notification( $handle, $options ) |
||
| 31 | { |
||
| 32 | if( 0 === count( $this->notifications ) ) |
||
| 33 | { |
||
| 34 | $this->init(); |
||
| 35 | } |
||
| 36 | |||
| 37 | if( !key_exists( $handle, $this->notifications ) ) |
||
| 38 | { |
||
| 39 | $this->notifications[$handle] = $options; |
||
| 40 | } |
||
| 41 | else trigger_error( "The handle <strong>$handle</strong> has already been registered. Please choose a different handle for your notification." ); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function render_notifications() |
||
| 45 | { |
||
| 46 | foreach($this->notifications as $handle => $notification) |
||
| 47 | { |
||
| 48 | $this->render_notification( $handle, $notification ); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | public function render_network_notifications() |
||
| 53 | { |
||
| 54 | foreach( $this->notifications as $handle => $notification ) |
||
| 55 | { |
||
| 56 | if( $notification['network'] ) $this->render_notification( $handle, $notification ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | public function dismiss_notification() |
||
| 61 | { |
||
| 62 | $id = filter_input( INPUT_POST, 'id' ); |
||
| 63 | if( !in_array( $id, $this->dismissed_notices ) ) |
||
| 64 | { |
||
| 65 | $this->dismissed_notices[] = $id; |
||
| 66 | update_option( 'wp_dismissed_notices', $this->dismissed_notices); |
||
| 67 | } |
||
| 68 | die(); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function render_script() |
||
| 72 | { |
||
| 73 | ?> |
||
| 74 | <script> |
||
| 75 | jQuery(document).ready(function($){ |
||
| 76 | $('.notice').on('click','.notice-dismiss',function(e){ |
||
| 77 | $.post(ajaxurl,{ |
||
| 78 | action: 'dismiss_admin_notification', |
||
| 79 | id: $(this).parent().attr('id') |
||
| 80 | }); |
||
| 81 | }); |
||
| 82 | }); |
||
| 83 | </script> |
||
| 84 | <?php |
||
| 85 | } |
||
| 86 | |||
| 87 | private function init() |
||
| 88 | { |
||
| 89 | add_action( 'admin_notices', array( $this, 'render_notifications' ) ); |
||
| 90 | add_action( 'network_admin_notices', array( $this, 'render_network_notifications' ) ); |
||
| 91 | add_action( 'wp_ajax_dismiss_admin_notification', array( $this, 'dismiss_notification' ) ); |
||
| 92 | add_action( 'admin_head', array( $this, 'render_script' ) ); |
||
| 93 | $this->dismissed_notices = get_option('wp_dismissed_notices'); |
||
| 94 | if( false === $this->dismissed_notices ) $this->dismissed_notices = array(); |
||
| 95 | var_dump($this->dismissed_notices); |
||
|
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
| 96 | } |
||
| 97 | |||
| 98 | private function render_notification( $id, $n ) |
||
| 99 | { |
||
| 100 | if( in_array( $id, $this->dismissed_notices ) ) return; |
||
| 101 | |||
| 102 | printf( |
||
| 103 | '<div id="%s" class="notice notice-%s %s%s"><p>%s</p></div>', |
||
| 104 | $id, |
||
| 105 | $n['type'], |
||
| 106 | $n['dismissible']?'is-dismissible ':'', |
||
| 107 | $n['class'], |
||
| 108 | $n['html'] |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 |