Skip to content

Commit c1fb4a3

Browse files
committed
Added Network Admin support to Helpers::show_notice()
1 parent 4ea6c4a commit c1fb4a3

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
8686

8787
#### Branch: `master`
8888

89+
* Set Carbon Fields minimum version to 2.2 (required for multisite example)
8990
* Extended base class as WordPress Toolkit; simplified base class configuration init
9091
* Moved `is_ajax()`/`prefix()` methods, `CacheObject()` init to wordpress-toolkit
9192
* Updated Composer license to conform to new [SPDX](https://spdx.org/licenses/) identifiers
9293
* Added [phpdotenv](https://github.com/etelford/phpdotenv) support ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment))
9394
* Added `npm run zip-dev` to create installable ZIP including necessary development files
9495
* Added [Network Admin options page](https://github.com/dmhendricks/wordpress-base-plugin/blob/master/app/Settings/Network_Settings_Page.php) example
96+
* Modified `show_notice()` to allow displaying notices in Network Admin; add additional CSS classes and element ID
9597

9698
## Screenshot
9799

app/Helpers.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace VendorName\PluginName;
3+
use WordPress_ToolKit\Helpers\ArrayHelper;
34
use Carbon_Fields\Container;
45
use Carbon_Fields\Field;
56

@@ -8,21 +9,45 @@ class Helpers extends Plugin {
89
/**
910
* Display a notice/message in WP Admin
1011
*
11-
* @param string $msg The message to display.
12-
* @param string $type The type of notice. Valid values:
13-
* error, warning, success, info
14-
* @param bool $is_dismissible Specify whether or not the user may dismiss
15-
* the notice.
12+
* @param string $msg The message to display
13+
* @param string $args Configuration options
1614
* @since 2.0.0
1715
*/
18-
public static function show_notice( $msg, $type = 'error', $is_dismissible = false ) {
19-
20-
add_action( 'admin_notices', function() use ( &$msg, &$type, &$is_dismissible ) {
21-
22-
$class = 'notice notice-' . $type . ( $is_dismissible ? ' is-dismissible' : '' );
23-
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $msg );
16+
public static function show_notice( $msg, $args = array() ) {
17+
18+
$args = ArrayHelper::set_default_atts( array(
19+
'type' => 'success', // 'error', 'warning', 'success', 'info'
20+
'dismissible' => true, // notice is dismissible
21+
'scope' => true, // 'admin', 'network', true (both)
22+
'class' => null, // additional CSS classes to add to notice
23+
'id' => null // container element ID
24+
), $args);
25+
26+
// Merge CSS classes
27+
if( !is_array( $args['class'] ) ) $args['class'] = explode( ' ', $args['class'] );
28+
$classes = array_merge([
29+
'notice',
30+
'notice-' . $args['type']
31+
], $args['class'] );
32+
if( $args['dismissible'] ) $classes[] = 'is-dismissible';
33+
$classes = implode( ' ', array_filter( $classes ) );
34+
35+
// Set ID string, if specified
36+
$element_id = $args['id'] ? ' id="' . $args['id'] . '"' : '';
37+
38+
// Display message in WP Admin
39+
if( $args['scope'] === true || $args['scope'] == 'admin' ) {
40+
add_action( 'admin_notices', function() use ( &$classes, &$element_id, &$msg ) {
41+
printf( '<div class="%1$s"%2$s><p>%3$s</p></div>', $classes, $element_id, $msg );
42+
});
43+
}
2444

25-
});
45+
// Display message in Network Admin
46+
if( $args['scope'] === true || $args['scope'] == 'network' ) {
47+
add_action( 'network_admin_notices', function() use ( &$classes, &$element_id, &$msg ) {
48+
printf( '<div class="%1$s"%2$s><p>%3$s</p></div>', $classes, $element_id, $msg );
49+
});
50+
}
2651

2752
}
2853

app/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private function verify_dependencies( $deps = true, $args = array() ) {
168168
$notices = '<ul><li>' . implode( "</li>\n<li>", $notices ) . '</li></ul>';
169169

170170
if( $args['echo'] ) {
171-
Helpers::show_notice($notices, 'error', false);
171+
Helpers::show_notice( $notices, [ 'type' => 'error', 'dismissible' => false ] );
172172
return false;
173173
} else {
174174
return $notices;

0 commit comments

Comments
 (0)