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