Skip to content

Commit

Permalink
Added Network Admin support to Helpers::show_notice()
Browse files Browse the repository at this point in the history
  • Loading branch information
dmhendricks committed Mar 17, 2018
1 parent 4ea6c4a commit c1fb4a3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 37 additions & 12 deletions app/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace VendorName\PluginName;
use WordPress_ToolKit\Helpers\ArrayHelper;
use Carbon_Fields\Container;
use Carbon_Fields\Field;

Expand All @@ -8,21 +9,45 @@ class Helpers extends Plugin {
/**
* Display a notice/message in WP Admin
*
* @param string $msg The message to display.
* @param string $type The type of notice. Valid values:
* error, warning, success, info
* @param bool $is_dismissible Specify whether or not the user may dismiss
* the notice.
* @param string $msg The message to display
* @param string $args Configuration options
* @since 2.0.0
*/
public static function show_notice( $msg, $type = 'error', $is_dismissible = false ) {

add_action( 'admin_notices', function() use ( &$msg, &$type, &$is_dismissible ) {

$class = 'notice notice-' . $type . ( $is_dismissible ? ' is-dismissible' : '' );
printf( '<div class="%1$s"><p>%2$s</p></div>', $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( '<div class="%1$s"%2$s><p>%3$s</p></div>', $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( '<div class="%1$s"%2$s><p>%3$s</p></div>', $classes, $element_id, $msg );
});
}

}

Expand Down
2 changes: 1 addition & 1 deletion app/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function verify_dependencies( $deps = true, $args = array() ) {
$notices = '<ul><li>' . implode( "</li>\n<li>", $notices ) . '</li></ul>';

if( $args['echo'] ) {
Helpers::show_notice($notices, 'error', false);
Helpers::show_notice( $notices, [ 'type' => 'error', 'dismissible' => false ] );
return false;
} else {
return $notices;
Expand Down

0 comments on commit c1fb4a3

Please sign in to comment.