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 |
||
|
0 ignored issues
–
show
|
|||
| 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 ) |
||
|
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
| 24 | { |
||
| 25 | static::$instance = new static(); |
||
|
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
It seems like
new static() of type this<WPAdminNotifications> is incompatible with the declared type object<Singleton> of property $instance.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 26 | } |
||
| 27 | return static::$instance; |
||
|
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
| 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 | wp_die(); |
||
| 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 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.