Completed
Push — master ( c86703...8997f4 )
by Askupa
02:59 queued 01:12
created

handler.php (7 issues)

Upgrade to new PHP Analysis Engine

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
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Documentation Bug introduced by
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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug Compatibility introduced by
The expression static::$instance; of type WPAdminNotifications|Singleton adds the type WPAdminNotifications to the return on line 27 which is incompatible with the return type documented by WPAdminNotifications::get_instance of type Singleton.
Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method dismiss_notification() contains an exit expression.

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.

Loading history...
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
        }
96
        
97
        private function render_notification( $id, $n )
98
        {
99
            if( in_array( $id, $this->dismissed_notices ) ) return;
100
            
101
            printf( 
102
                '<div id="%s" class="notice notice-%s %s%s"><p>%s</p></div>',
103
                $id,
104
                $n['type'], 
105
                $n['dismissible']?'is-dismissible ':'', 
106
                $n['class'], 
107
                $n['html'] 
108
            );
109
        }
110
    }
111
}
112