| 1 |
<?php |
| 2 |
/** |
| 3 |
* Contains the class that is used to register and retrieve notices like errors, warnings, success messages, etc. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Classes/Charitable_Notices |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2015, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Notices' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Notices |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_Notices { |
| 23 |
|
| 24 |
/** |
| 25 |
* The single instance of this class. |
| 26 |
* |
| 27 |
* @var Charitable_Notices|null |
| 28 |
* @access private |
| 29 |
* @static |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* The array of notices. |
| 35 |
* |
| 36 |
* @var array |
| 37 |
* @access protected |
| 38 |
*/ |
| 39 |
protected $notices; |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns and/or create the single instance of this class. |
| 43 |
* |
| 44 |
* @return Charitable_Notices |
| 45 |
* @access public |
| 46 |
* @since 1.0.0 |
| 47 |
*/ |
| 48 |
public static function get_instance() { |
| 49 |
if ( is_null( self::$instance ) ) { |
| 50 |
self::$instance = new Charitable_Notices(); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$instance; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Create class object. A private constructor, so this is used in a singleton context. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
* @access private |
| 61 |
* @since 1.0.0 |
| 62 |
*/ |
| 63 |
private function __construct() { |
| 64 |
|
| 65 |
/* Retrieve the notices from the session */ |
| 66 |
$this->notices = charitable_get_session()->get_notices(); |
| 67 |
|
| 68 |
/* Reset the session back to empty */ |
| 69 |
charitable_get_session()->set( 'notices', array( |
| 70 |
'error' => array(), |
| 71 |
'warning' => array(), |
| 72 |
'success' => array(), |
| 73 |
'info' => array(), |
| 74 |
) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Adds a notice message. |
| 79 |
* |
| 80 |
* @param string $message |
| 81 |
* @param string $type |
| 82 |
* @param string $key Optional. If not set, next numeric key is used. |
| 83 |
* @return void |
| 84 |
* @access public |
| 85 |
* @since 1.0.0 |
| 86 |
*/ |
| 87 |
public function add_notice( $message, $type, $key = false ) { |
| 88 |
if ( false === $key ) { |
| 89 |
$this->notices[ $type ][] = $message; |
| 90 |
} else { |
| 91 |
$this->notices[ $type ][ $key ] = $message; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Add multiple notices at once. |
| 97 |
* |
| 98 |
* @param array $messages |
| 99 |
* @param string $type |
| 100 |
* @return void |
| 101 |
* @access public |
| 102 |
* @since 1.0.0 |
| 103 |
*/ |
| 104 |
public function add_notices( $messages, $type ) { |
| 105 |
if ( ! is_array( $messages ) ) { |
| 106 |
$messages = array( $messages ); |
| 107 |
} |
| 108 |
|
| 109 |
$this->notices[ $type ] = array_merge( $this->notices[ $type ], $messages ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Adds an error message. |
| 114 |
* |
| 115 |
* @param string $message |
| 116 |
* @param string $key Optional. If not set, next numeric key is used. |
| 117 |
* @return void |
| 118 |
* @access public |
| 119 |
* @since 1.0.0 |
| 120 |
*/ |
| 121 |
public function add_error( $message, $key = false ) { |
| 122 |
$this->add_notice( $message, 'error', $key ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Adds a warning message. |
| 127 |
* |
| 128 |
* @param string $message |
| 129 |
* @param string $key Optional. If not set, next numeric key is used. |
| 130 |
* @return void |
| 131 |
* @access public |
| 132 |
* @since 1.0.0 |
| 133 |
*/ |
| 134 |
public function add_warning( $message, $key = false ) { |
| 135 |
$this->add_notice( $message, 'warning', $key ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Adds a success message. |
| 140 |
* |
| 141 |
* @param string $message |
| 142 |
* @param string $key Optional. If not set, next numeric key is used. |
| 143 |
* @return void |
| 144 |
* @access public |
| 145 |
* @since 1.0.0 |
| 146 |
*/ |
| 147 |
public function add_success( $message, $key = false ) { |
| 148 |
$this->add_notice( $message, 'success', $key ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Adds an info message. |
| 153 |
* |
| 154 |
* @param string $message |
| 155 |
* @param string $key Optional. If not set, next numeric key is used. |
| 156 |
* @return void |
| 157 |
* @access public |
| 158 |
* @since 1.0.0 |
| 159 |
*/ |
| 160 |
public function add_info( $message, $key = false ) { |
| 161 |
$this->add_notice( $message, 'info', $key ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Receives a WP_Error object and adds the error messages to our array. |
| 166 |
* |
| 167 |
* @param WP_Error $error |
| 168 |
* @return void |
| 169 |
* @access public |
| 170 |
* @since 1.0.0 |
| 171 |
*/ |
| 172 |
public function add_errors_from_wp_error( WP_Error $error ) { |
| 173 |
$this->add_notices( $error->get_error_messages(), 'error' ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Return all errors as an array. |
| 178 |
* |
| 179 |
* @return array |
| 180 |
* @access public |
| 181 |
* @since 1.0.0 |
| 182 |
*/ |
| 183 |
public function get_errors() { |
| 184 |
return $this->notices['error']; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Return all warnings as an array. |
| 189 |
* |
| 190 |
* @return array |
| 191 |
* @access public |
| 192 |
* @since 1.0.0 |
| 193 |
*/ |
| 194 |
public function get_warnings() { |
| 195 |
return $this->notices['warning']; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Return all successs as an array. |
| 200 |
* |
| 201 |
* @return array |
| 202 |
* @access public |
| 203 |
* @since 1.0.0 |
| 204 |
*/ |
| 205 |
public function get_success_notices() { |
| 206 |
return $this->notices['success']; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Return all infos as an array. |
| 211 |
* |
| 212 |
* @return array |
| 213 |
* @access public |
| 214 |
* @since 1.0.0 |
| 215 |
*/ |
| 216 |
public function get_info_notices() { |
| 217 |
return $this->notices['info']; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Return all notices as an array. |
| 222 |
* |
| 223 |
* @return array |
| 224 |
* @access public |
| 225 |
* @since 1.0.0 |
| 226 |
*/ |
| 227 |
public function get_notices() { |
| 228 |
return $this->notices; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Clear out all existing notices. |
| 233 |
* |
| 234 |
* @return void |
| 235 |
* @access public |
| 236 |
* @since 1.4.0 |
| 237 |
*/ |
| 238 |
public function clear() { |
| 239 |
$clear = array( |
| 240 |
'error' => array(), |
| 241 |
'warning' => array(), |
| 242 |
'success' => array(), |
| 243 |
'info' => array(), |
| 244 |
); |
| 245 |
|
| 246 |
$this->notices = $clear; |
| 247 |
|
| 248 |
charitable_get_session()->set( 'notices', $clear ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
endif; // End class_exists check |
| 253 |
|