diff --git a/README.md b/README.md index 1f0f81f..5164461 100644 --- a/README.md +++ b/README.md @@ -86,12 +86,14 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp #### Branch: `master` +* Set Carbon Fields minimum version to 2.2 (required for multisite example) * Extended base class as WordPress Toolkit; simplified base class configuration init * Moved `is_ajax()`/`prefix()` methods, `CacheObject()` init to wordpress-toolkit * Updated Composer license to conform to new [SPDX](https://spdx.org/licenses/) identifiers * Added [phpdotenv](https://github.com/etelford/phpdotenv) support ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment)) * Added `npm run zip-dev` to create installable ZIP including necessary development files * Added [Network Admin options page](https://github.com/dmhendricks/wordpress-base-plugin/blob/master/app/Settings/Network_Settings_Page.php) example +* Modified `show_notice()` to allow displaying notices in Network Admin; add additional CSS classes and element ID ## Screenshot diff --git a/app/Helpers.php b/app/Helpers.php index b6f012d..10b60fd 100644 --- a/app/Helpers.php +++ b/app/Helpers.php @@ -1,5 +1,6 @@

%2$s

', $class, $msg ); + public static function show_notice( $msg, $args = array() ) { + + $args = ArrayHelper::set_default_atts( array( + 'type' => 'success', // 'error', 'warning', 'success', 'info' + 'dismissible' => true, // notice is dismissible + 'scope' => true, // 'admin', 'network', true (both) + 'class' => null, // additional CSS classes to add to notice + 'id' => null // container element ID + ), $args); + + // Merge CSS classes + if( !is_array( $args['class'] ) ) $args['class'] = explode( ' ', $args['class'] ); + $classes = array_merge([ + 'notice', + 'notice-' . $args['type'] + ], $args['class'] ); + if( $args['dismissible'] ) $classes[] = 'is-dismissible'; + $classes = implode( ' ', array_filter( $classes ) ); + + // Set ID string, if specified + $element_id = $args['id'] ? ' id="' . $args['id'] . '"' : ''; + + // Display message in WP Admin + if( $args['scope'] === true || $args['scope'] == 'admin' ) { + add_action( 'admin_notices', function() use ( &$classes, &$element_id, &$msg ) { + printf( '

%3$s

', $classes, $element_id, $msg ); + }); + } - }); + // Display message in Network Admin + if( $args['scope'] === true || $args['scope'] == 'network' ) { + add_action( 'network_admin_notices', function() use ( &$classes, &$element_id, &$msg ) { + printf( '

%3$s

', $classes, $element_id, $msg ); + }); + } } diff --git a/app/Plugin.php b/app/Plugin.php index 1b1271e..d3404af 100644 --- a/app/Plugin.php +++ b/app/Plugin.php @@ -168,7 +168,7 @@ private function verify_dependencies( $deps = true, $args = array() ) { $notices = ''; if( $args['echo'] ) { - Helpers::show_notice($notices, 'error', false); + Helpers::show_notice( $notices, [ 'type' => 'error', 'dismissible' => false ] ); return false; } else { return $notices;