-
-
Save norcross/813ad101bdef69dc306acb11f64eb7c7 to your computer and use it in GitHub Desktop.
WordPress Admin Notice Helper Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Helper to work with Wordpress admin notices | |
* | |
* @example | |
* Notice::success('All is good!'); | |
* | |
* @example | |
* Notice::warning('Do something please.', true); | |
* | |
* @example | |
* Notice::info('Are you ok?', function ($id) { | |
* return 'jQuery(document).on("click", "#' . $id . ' .notice-dismiss", function () { | |
* // do something on dismiss... | |
* });'; | |
* }); | |
*/ | |
class Notice { | |
private static $_instance = null; | |
public $notices = []; | |
public $scripts = []; | |
public function __construct() | |
{ | |
// @see https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices | |
add_action('admin_notices', [&$this, 'display']); | |
// @see https://codex.wordpress.org/Plugin_API/Action_Reference/admin_footer | |
add_action('admin_footer', [&$this, 'scripts']); | |
} | |
public function display() | |
{ | |
echo implode(' ', $this->notices); | |
} | |
public function scripts() | |
{ | |
echo implode(' ', $this->scripts); | |
} | |
public static function getInstance() | |
{ | |
if (is_null(self::$_instance)) { | |
self::$_instance = new Notice(); | |
} | |
return self::$_instance; | |
} | |
/** | |
* @param $message - Message to display | |
* @param string $type - Type of the notice (default: '') | |
* @param mixed $isDismissible - If true, allow to dismiss. Could be a function that return javascript. | |
*/ | |
public static function add($message, $type = '', $isDismissible = null) | |
{ | |
$instance = self::getInstance(); | |
$identifier = 'notice-' . (count($instance->notices) + 1); | |
$notice = '<div class="notice' . (empty($type) ? '' : ' notice-' . $type) . (is_null($isDismissible) ? '' : ' is-dismissible') . '" id="' . $identifier . '" style="padding: 15px;">' . $message . '</div>'; | |
if (is_callable($isDismissible)) { | |
$instance->scripts[] = '<script>' . $isDismissible($identifier) . '</script>'; | |
} | |
$instance->notices[] = $notice; | |
} | |
/** | |
* @param string $message - Message to display | |
* @param mixed $isDismissible - If true, allow to dismiss. Could be a function that return javascript. | |
*/ | |
public static function info($message, $isDismissible = null) | |
{ | |
self::add($message, 'info', $isDismissible); | |
} | |
/** | |
* @param string $message - Message to display | |
* @param mixed $isDismissible - If true, allow to dismiss. Could be a function that return javascript. | |
*/ | |
public static function error($message, $isDismissible = null) | |
{ | |
self::add($message, 'error', $isDismissible); | |
} | |
/** | |
* @param string $message - Message to display | |
* @param mixed $isDismissible - If true, allow to dismiss. Could be a function that return javascript. | |
*/ | |
public static function success($message, $isDismissible = null) | |
{ | |
self::add($message, 'success', $isDismissible); | |
} | |
/** | |
* @param string $message - Message to display | |
* @param mixed $isDismissible - If true, allow to dismiss. Could be a function that return javascript. | |
*/ | |
public static function warning($message, $isDismissible = null) | |
{ | |
self::add($message, 'warning', $isDismissible); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment