-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclass-lil-notices.php
More file actions
128 lines (106 loc) · 3.43 KB
/
class-lil-notices.php
File metadata and controls
128 lines (106 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Lil Notices Plugin Class
*
* @since 0.1.2
*/
//avoid direct calls to this file, because now WP core and framework has been used
if ( ! function_exists( 'add_filter' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
if ( ! class_exists( 'Lil_Notices' ) ) {
class Lil_Notices {
/**
* Initialize the plugin
*
* @since 0.1
* @return void
*/
public static function init() {
if ( ( is_multisite() ) && ( is_network_admin () ) ) {
return;
}
add_action( 'admin_bar_menu', array( get_called_class(), 'admin_bar_menu' ), 999 );
add_action( 'admin_enqueue_scripts', array( get_called_class(), 'admin_enqueue_scripts' ), 999 );
} // end public function init
public static function admin_enqueue_scripts() {
wp_enqueue_script( 'ln-scripts', plugin_dir_url( __FILE__ ) . 'assets/dist/main.js', array(), LIL_NOTICES__VERSION, true );
wp_enqueue_style( 'ln-styles', plugin_dir_url( __FILE__ ) . 'assets/dist/style.css', array(), LIL_NOTICES__VERSION );
}
/**
* Add the admin bar menu item that all notices will be injected into
*/
public static function admin_bar_menu() {
global $wp_admin_bar;
$menu_id = 'ln_menu';
$wp_admin_bar->add_node( array(
'href' => '#',
'id' => $menu_id,
'parent' => 'top-secondary',
'title' => __( 'Notices' ),
) );
$wp_admin_bar->add_node( array(
'id' => 'ln_all_notices',
'meta' => array(
'html' => static::notices_content(),
),
'parent' => $menu_id,
) );
}
/**
* Returns the HTML content for the Admin Bar Menu notices dropdown
*
* @since 0.1
* @return string
*/
public static function notices_content() {
global $wp_filter;
// count the number of items attached to 'admin_notices' and 'all_admin_notices'
// might actually just pull each item from this bit as it will make more sense
$html = '';
if ( empty( $wp_filter['admin_notices'] ) && empty( $wp_filter['all_admin_notices']) ) {
$html .= apply_filters( 'ln_no_notices', __( 'Nothing to see here!', 'ln_domain' ) );
} else {
$html .= '<ul class="ln-notice-list">';
ob_start();
do_action( 'admin_notices' );
do_action( 'all_admin_notices' );
$notices = ob_get_clean();
// preg match & replace the classes that WP is using to move admin notices in under the page's H tag
$notices = preg_replace(
'/(class=[\'\"])([A-Za-z0-9\-\_\ ]*)(updated)([A-Za-z0-9\-\_\ ]*)([\"\'])/',
'$1$2ln-updated $4$5', $notices );
$notices = preg_replace(
'/(class=[\'\"])([A-Za-z0-9\-\_\ ]*)(notice[^\-\_])([A-Za-z0-9\-\_\ ]*)([\"\'])/',
'$1$2ln-notice $4$5', $notices );
$notices = preg_replace(
'/(class=[\'\"])([A-Za-z0-9\-\_\ ]*)(error)([A-Za-z0-9\-\_\ ]*)([\"\'])/',
'$1$2ln-error $4$5', $notices );
$html .= $notices;
$html .= '</ul>';
}
// since we're grabbing them above, remove the core do_action via remove_all_actions
remove_all_actions( 'admin_notices' );
remove_all_actions( 'all_admin_notices' );
return $html;
}
/**
* Activate the plugin
*
* @since 0.1
* @return void
*/
public static function activate() {
} // END public static function activate
/**
* Deactivate the plugin
*
* @since 0.1
* @return void
*/
public static function deactivate() {
} // END public static function deactivate
} // END class Lil_Notices
} // END if ( ! class_exists( 'Lil_Notices' ) )