1
|
|
|
<?php |
|
|
|
|
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!' ); |
|
|
|
|
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 ); |
|
|
|
|
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
|
|
|
} |
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.