functions.php ➔ _amarkal_admin_notification_init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 32.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * WordPress Admin Notifications
4
 *
5
 * Add static/dismissible admin notifications to WordPress
6
 *
7
 * @package   amarkal-admin-notification
8
 * @author    Askupa Software <[email protected]>
9
 * @link      https://github.com/amarkal/amarkal-admin-notification
10
 * @copyright 2017 Askupa Software
11
 */
12
13
// Prevent direct file access
14
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
15
16
/**
17
 * Prevent loading the library more than once
18
 */
19
if( defined( 'AMARKAL_ADMIN_NOTIFICATION' ) ) return false;
20
define( 'AMARKAL_ADMIN_NOTIFICATION', true );
21
22
if(!function_exists('_amarkal_admin_notification_init'))
23
{
24
    /**
25
     * Initiate admin notifications
26
     */
27
    function _amarkal_admin_notification_init() 
28
    {
29
        $notifier = Amarkal\Admin\Notifications::get_instance();
30
        $notifier->init();
31
    }
32
    add_action( 'init', '_amarkal_admin_notification_init' );
33
}
34
35
if(!function_exists('amarkal_admin_notification'))
36
{
37
    /**
38
     * Register an admin notification.
39
     * 
40
     * @param type $handle
41
     * @param type $html
42
     * @param type $type
43
     * @param type $dismissible
44
     * @param type $class
45
     * @param type $network
46
     */
47
    function amarkal_admin_notification( $handle, $html, $type = 'success', $dismissible = false, $class = '', $network = false )
48
    {
49
        $notifier = Amarkal\Admin\Notifications::get_instance();
50
        $notifier->register_notification($handle, array(
51
            'dismissible'   => $dismissible,
52
            'class'         => $class,
53
            'type'          => $type,
54
            'html'          => $html,
55
            'network'       => $network
56
        ));
57
    }
58
}
59
60
if(!function_exists('amarkal_reset_admin_notification'))
61
{
62
    /**
63
     * Reset a dismissed admin notification.
64
     * 
65
     * @param string $handle
66
     */
67
    function amarkal_reset_admin_notification( $handle )
68
    {
69
        $dismissed = get_site_option( 'amarkal_dismissed_notices', array() );
70
        $offset = 0;
71
        foreach($dismissed as $id)
72
        {
73
            if( $id === $handle ) break;
74
            $offset++;
75
        }
76
        array_splice( $dismissed, $offset, 1);
77
        update_site_option( 'amarkal_dismissed_notices', $dismissed );
78
    }
79
}
80
81
if(!function_exists('wp_admin_notification'))
82
{
83
    /**
84
     * @deprecated use amarkal_admin_notification instead
85
     */
86
    function wp_admin_notification( $handle, $html, $type = 'success', $dismissible = false, $class = '', $network = false )
87
    {
88
        trigger_error('<b>wp_admin_notification()</b> has been deprecated. Use <b>amarkal_admin_notification</b> instead.');
89
        amarkal_admin_notification( $handle, $html, $type, $dismissible, $class, $network );
0 ignored issues
show
Documentation introduced by
$dismissible is of type boolean, but the function expects a false|object<type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$network is of type boolean, but the function expects a false|object<type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
}
92
93
if(!function_exists('wp_reset_admin_notification'))
94
{
95
    /**
96
     * @deprecated use amarkal_reset_admin_notification instead
97
     */
98
    function wp_reset_admin_notification( $handle )
99
    {
100
        trigger_error('<b>wp_reset_admin_notification()</b> has been deprecated. Use <b>amarkal_reset_admin_notification</b> instead.');
101
        amarkal_reset_admin_notification( $handle );
102
    }
103
}