Created
January 29, 2015 17:38
-
-
Save jesgs/8233581bf06af2495df6 to your computer and use it in GitHub Desktop.
Session-based flash messaging for WordPress
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 | |
/** | |
* Flash Messages helper class | |
* | |
* @package FlashMessages | |
* @author Jess Green <jgreen @ psy-dreamer.com> | |
* @license http://opensource.org/licenses/MIT MIT | |
*/ | |
class FlashMessages | |
{ | |
/** | |
* CSS classes. Filtered by flash_message_classes filter | |
* | |
* @var array | |
*/ | |
protected $classes = array('error', 'updated'); | |
/** | |
* Default messages | |
* @var array | |
*/ | |
static protected $messages = array(); | |
/** | |
* PHP5 Constructor function | |
* | |
* @param array $options Set properties when class is initialized | |
*/ | |
public function __construct($options) | |
{ | |
foreach ($options as $property => $value) { | |
if (property_exists($this, $property)) { | |
$this->$property = $value; | |
} | |
} | |
session_start(); | |
add_action('admin_notices', array($this, 'show_flash_message')); | |
} | |
/** | |
* End session on logout and login | |
* | |
* @return void | |
*/ | |
public function session_end() | |
{ | |
unset($_SESSION['flash_messages']); | |
} | |
/** | |
* Set messages in array | |
* | |
* @param array $messages Array of messages to set | |
* @return void | |
*/ | |
public function set_flash_messages($messages) | |
{ | |
if (isset($_SESSION['flash_messages'])) { | |
$_SESSION['flash_messages'] = array_merge($_SESSION['flash_messages'], $messages); | |
} else { | |
$_SESSION['flash_messages'] = $messages; | |
} | |
} | |
/** | |
* Get messages | |
* | |
* @return array | |
*/ | |
public function get_flash_messages() | |
{ | |
if (isset($_SESSION['flash_messages'])) { | |
return $_SESSION['flash_messages']; | |
} | |
return array(); | |
} | |
/** | |
* Queue flash messages | |
* | |
* @param string $name Name of message. updated or error | |
* @param string $message Message body | |
* | |
* @return FlashMessages | |
*/ | |
public function queue_flash_message($name, $message) | |
{ | |
$messages = array(); | |
/** | |
* Filter for modifying default array of classes for Flash messaging | |
* @param array $classes Array of classes for message div | |
* @return string | |
*/ | |
$classes = apply_filters('flashmessage_classes', $this->classes); | |
/** | |
* Filter for changing default | |
* @param string $default_class Default 'updated' class | |
* @return string | |
*/ | |
$default_class = apply_filters('flashmessages_default_class', 'updated'); | |
$class = $name; | |
if (!in_array($name, $classes)) { | |
$class = $default_class; | |
} | |
$messages[$class][] = $message; | |
$this->set_flash_messages($messages); | |
return $this; | |
} | |
/** | |
* Get flash message | |
* | |
* @return mixed | |
*/ | |
public function show_flash_message() | |
{ | |
$messages = $this->get_flash_messages(); | |
if (is_array($messages)) { | |
foreach ($messages as $class => $messages) { | |
$this->display_flash_message_html($messages, $class); | |
} | |
} | |
$this->session_end(); | |
} | |
/** | |
* Display message HTML | |
* | |
* @param array $messages Array of messages | |
* @param string $class Message CSS class | |
* @return void | |
*/ | |
private function display_flash_message_html($messages, $class) | |
{ | |
foreach ($messages as $message) { | |
$message_html = "<div id=\"message\" class=\"{$class}\"><p>{$message}</p></div>"; | |
echo apply_filters('flashmessage_html', $message_html, $message, $class); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment