Skip to content

Instantly share code, notes, and snippets.

@rumur
Last active November 15, 2016 15:04
Show Gist options
  • Save rumur/730f4ab2352e896f204fc266acb12b81 to your computer and use it in GitHub Desktop.
Save rumur/730f4ab2352e896f204fc266acb12b81 to your computer and use it in GitHub Desktop.
WordPress Admin Notice Wrapper
<?php
/**
* Class RUM_Admin_Notice
*
* Created by Rumur.
*
* @usage RUM_Admin_Notice::get_instance()->add_error( 'Error' );
* @usage RUM_Admin_Notice::get_instance()->add_warning( 'Warning' );
* @usage RUM_Admin_Notice::get_instance()->add_info( 'Info' );
* @usage RUM_Admin_Notice::get_instance()->add_success( 'Success' );
*/
class RUM_Admin_Notice {
private static $version = '0.1';
/**
* The unique instance of the plugin.
*
* @var RUM_Admin_Notice
* @since 0.1
*/
private static $instance;
/**
* The Collection of messages
*
* @var array
* @since 0.1
*/
private static $notices = array(
'info' => array(), // blue
'error' => array(), // red
'warning' => array(), // yellow/orange
'success' => array(), // green
);
/**
* Gets an instance of our plugin.
*
* @since 0.1
* @return RUM_Admin_Notice
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*
* @since 0.1
*/
private function __construct() {
add_action( 'admin_notices', array( $this, 'show_messages' ) );
}
/**
* Show all messages
*
* @since 0.1
*/
public function show_messages() {
// Show notices only for admin
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$messages = $this->get_messages();
foreach ( (array) $messages as $code => $_messages ) {
if ( empty( $messages ) ) {
continue;
}
$this->render_messages( $_messages, $code );
}
}
/**
* Render the HTML of messages
*
* @param array $messages
* @param string $type
* @since 0.1
*/
protected function render_messages( $messages = array() , $type = 'error' ) {
$notice_class = array( 'notice' );
array_push( $notice_class, 'notice-' . $type );
foreach ( (array) $messages as $md5_key => $message ) {
$class = $notice_class;
if ( $message['dismissible'] ) {
array_push( $class, 'is-dismissible' );
}
$html = '<div class="%1$s"><p>%2$s</p></div>';
printf( $html, join( ' ', $class ), $message['message'] );
}
}
/**
* Gather together
*
* @param string $type = error | info | warning | success
* @param $message
* @param $dismissible = false | true
* @since 0.1
* @return bool
*/
public function add_message( $type = 'error', $message, $dismissible = false ) {
if ( $message ) {
if ( ! in_array( $type, array_keys( self::$notices ) ) ) {
_doing_it_wrong( __CLASS__ . ':add_message', $type . ' is incorrect', self::$version );
$type = 'error';
}
$key = md5( $message );
self::$notices[ $type ][ $key ]['message'] = force_balance_tags( $message );
self::$notices[ $type ][ $key ]['dismissible'] = $dismissible;
return true;
}
return false;
}
/**
* Pull out Messages
*
* @param string $type = all | error | info | warning | success
* @since 0.1
* @return string
*/
public function get_messages( $type = 'all' ) {
switch ( $type ) {
case 'error':
case 'notice':
case 'warning':
case 'success':
return self::$notices[ $type ];
default:
return self::$notices;
}
}
/**
* Put an info
*
* @param $message
* @param bool $dismissible
* @since 0.1
*/
public function add_info( $message, $dismissible = false ) {
$this->add_message( 'info', $message, $dismissible );
}
/**
* Put an error
*
* @param $message
* @param bool $dismissible
* @since 0.1
*/
public function add_error( $message, $dismissible = false ) {
$this->add_message( 'error', $message, $dismissible );
}
/**
* Put a warning
*
* @param $message
* @param bool $dismissible
* @since 0.1
*/
public function add_warning( $message, $dismissible = false ) {
$this->add_message( 'warning', $message, $dismissible );
}
/**
* Put a success
*
* @param $message
* @param bool $dismissible
* @since 0.1
*/
public function add_success( $message, $dismissible = false ) {
$this->add_message( 'success', $message, $dismissible );
}
/**
* Pull out pieces of info
*
* @return array
* @since 0.1
*/
public function get_info() {
return array( 'info' => $this->get_messages( 'info' ) );
}
/**
* Pull out errors
*
* @return array
* @since 0.1
*/
public function get_errors() {
return array( 'error' => $this->get_messages( 'error' ) );
}
/**
* Pull out warnings
*
* @return array
* @since 0.1
*/
public function get_warnings() {
return array( 'warning' => $this->get_messages( 'warning' ) );
}
/**
* Pull out pieces of success
*
* @return array
* @since 0.1
*/
public function get_success() {
return array( 'success' => $this->get_messages( 'success' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment