Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Last active December 17, 2020 09:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monkeymonk/2ea17e2260daaecd0049c46c8d6c85fd to your computer and use it in GitHub Desktop.
Save monkeymonk/2ea17e2260daaecd0049c46c8d6c85fd to your computer and use it in GitHub Desktop.
Wordpress Admin Notice Helper Class #wordpress
<?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