Completed
Push — master ( c7671f...27b0e7 )
by Askupa
01:56
created

Notifications.php (6 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
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 ) 
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<Amarkal\Admin\Notifications> is incompatible with the declared type object<Amarkal\Admin\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 Amarkal\Admin\Notificati...Amarkal\Admin\Singleton adds the type Amarkal\Admin\Notifications to the return on line 27 which is incompatible with the return type documented by Amarkal\Admin\Notifications::get_instance of type Amarkal\Admin\Singleton.
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
        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...
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