* @return object */ public static function get_singleton() { if (!isset(self::$instance)) { self::$instance = new FV_Admin_Notice_Helper(); $notices = get_option( 'fv_notices', array() ); if ( empty($notices) ) { $notices = array(); } self::$instance->notices = $notices; self::$instance->notices_were_updated = false; } return self::$instance; } /** * Initializes variables */ public function init() { //$default_notices = array( 'default' => array(), 'error' => array() ); } /** * Queues up a message to be displayed to the user * * @param string $message The text to show the user * @param string $type 'update' for a success or notification message, or 'error' for an error message * 'default', 'info', 'danger', 'success', 'primary', 'warning' * */ public function enqueue($message, $type = 'default') { /* if ( isset( $this->notices[ $type ] ) && in_array( $message, array_values( $this->notices[ $type ] ) ) ) { return; } */ if ( !isset($this->notices[$type]) ) { $this->notices[$type] = array(); } if (!empty($type) && !isset($this->notices[$type][crc32($message)])) { $this->notices[$type][crc32($message)] = (string)$message; $this->notices_were_updated = true; } } /** * Displays updates and errors */ public function print_notices() { foreach (array('default', 'info', 'danger', 'success', 'primary', 'warning') as $type) { if (isset($this->notices[$type]) && count($this->notices[$type])) { require('views/admin-notice.php'); $this->notices[$type] = array(); $this->notices_were_updated = true; } } } /** * Writes notices to the database */ public function shutdown() { //var_dump( $this->notices_were_updated ); if ($this->notices_were_updated) { //$_SESSION['user_notices'] = $this->notices; update_option( 'fv_notices', $this->notices ); } } } // end Admin_Notice_Helper FV_Admin_Notice_Helper::get_singleton(); // Create the instance immediately to make sure hook callbacks are registered in time if (!function_exists('wp_add_notice')) { /** * Queues up a message to be displayed to the user * * @param string $message The text to show the user * @param string $type 'update' for a success or notification message, or 'error' for an error message * 'default', 'info', 'danger', 'success', 'primary', 'warning' * */ function wp_add_notice($message, $type = 'default') { FV_Admin_Notice_Helper::get_singleton()->enqueue($message, $type); } } }