admin
2 years ago
database
2 years ago
helpers
2 years ago
class-json-manager.php
2 years ago
class-notification-center.php
2 years ago
class-notification.php
2 years ago
index.php
2 years ago
class-notification.php
245 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Notification |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop; |
| 12 | |
| 13 | use MyThemeShop\Helpers\Str; |
| 14 | use MyThemeShop\Helpers\HTML; |
| 15 | |
| 16 | /** |
| 17 | * Notification class. |
| 18 | */ |
| 19 | class Notification { |
| 20 | |
| 21 | /** |
| 22 | * Notification type. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | const ERROR = 'error'; |
| 27 | |
| 28 | /** |
| 29 | * Notification type. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | const SUCCESS = 'success'; |
| 34 | |
| 35 | /** |
| 36 | * Notification type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | const INFO = 'info'; |
| 41 | |
| 42 | /** |
| 43 | * Notification type. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | const WARNING = 'warning'; |
| 48 | |
| 49 | /** |
| 50 | * Screen check. |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | const SCREEN_ANY = 'any'; |
| 55 | |
| 56 | /** |
| 57 | * User capability check. |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | const CAPABILITY_ANY = ''; |
| 62 | |
| 63 | /** |
| 64 | * The notification message. |
| 65 | * |
| 66 | * @var string |
| 67 | */ |
| 68 | public $message = ''; |
| 69 | |
| 70 | /** |
| 71 | * Contains optional arguments: |
| 72 | * |
| 73 | * - type: The notification type, i.e. 'updated' or 'error' |
| 74 | * - id: The ID of the notification |
| 75 | * - dismissal_key: Option name to save dismissal information in, ID will be used if not supplied. |
| 76 | * - screen: Only display on plugin page or on every page. |
| 77 | * |
| 78 | * @var array Options of this Notification. |
| 79 | */ |
| 80 | public $options = []; |
| 81 | |
| 82 | /** |
| 83 | * Internal flag for whether notifications has been displayed. |
| 84 | * |
| 85 | * @var bool |
| 86 | */ |
| 87 | private $displayed = false; |
| 88 | |
| 89 | /** |
| 90 | * Notification class constructor. |
| 91 | * |
| 92 | * @param string $message Message string. |
| 93 | * @param array $options Set of options. |
| 94 | */ |
| 95 | public function __construct( $message, $options = [] ) { |
| 96 | $this->message = $message; |
| 97 | $this->options = wp_parse_args( |
| 98 | $options, |
| 99 | [ |
| 100 | 'id' => '', |
| 101 | 'classes' => '', |
| 102 | 'type' => self::SUCCESS, |
| 103 | 'screen' => self::SCREEN_ANY, |
| 104 | 'capability' => self::CAPABILITY_ANY, |
| 105 | ] |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Adds string (view) behaviour to the Notification. |
| 111 | * |
| 112 | * @codeCoverageIgnore |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | public function __toString() { |
| 117 | return $this->render(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Return data from options. |
| 122 | * |
| 123 | * @param string $id Data to get. |
| 124 | * @return mixed |
| 125 | */ |
| 126 | public function args( $id ) { |
| 127 | return $this->options[ $id ]; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Is this Notification persistent. |
| 132 | * |
| 133 | * @codeCoverageIgnore |
| 134 | * |
| 135 | * @return bool True if persistent, False if fire and forget. |
| 136 | */ |
| 137 | public function is_persistent() { |
| 138 | return ! empty( $this->args( 'id' ) ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Is this notification displayed. |
| 143 | * |
| 144 | * @codeCoverageIgnore |
| 145 | * |
| 146 | * @return bool |
| 147 | */ |
| 148 | public function is_displayed() { |
| 149 | return $this->displayed; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Can display on current screen. |
| 154 | * |
| 155 | * @codeCoverageIgnore |
| 156 | * |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function can_display() { |
| 160 | // Removed. |
| 161 | if ( $this->displayed ) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | $screen = get_current_screen(); |
| 166 | if ( self::SCREEN_ANY === $this->args( 'screen' ) || Str::contains( $this->args( 'screen' ), $screen->id ) ) { |
| 167 | $this->displayed = true; |
| 168 | } |
| 169 | |
| 170 | if ( self::CAPABILITY_ANY !== $this->args( 'capability' ) && ! current_user_can( $this->args( 'capability' ) ) ) { |
| 171 | $this->displayed = false; |
| 172 | } |
| 173 | |
| 174 | return $this->displayed; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Dismiss persisten notification. |
| 179 | * |
| 180 | * @codeCoverageIgnore |
| 181 | */ |
| 182 | public function dismiss() { |
| 183 | $this->displayed = true; |
| 184 | $this->options['id'] = ''; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return the object properties as an array. |
| 189 | * |
| 190 | * @codeCoverageIgnore |
| 191 | * |
| 192 | * @return array |
| 193 | */ |
| 194 | public function to_array() { |
| 195 | return [ |
| 196 | 'message' => $this->message, |
| 197 | 'options' => $this->options, |
| 198 | ]; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Renders the notification as a string. |
| 203 | * |
| 204 | * @codeCoverageIgnore |
| 205 | * |
| 206 | * @return string The rendered notification. |
| 207 | */ |
| 208 | public function render() { |
| 209 | $attributes = []; |
| 210 | |
| 211 | // Default notification classes. |
| 212 | $classes = [ |
| 213 | 'notice', |
| 214 | 'notice-' . $this->args( 'type' ), |
| 215 | $this->args( 'classes' ), |
| 216 | ]; |
| 217 | |
| 218 | // Maintain WordPress visualisation of alerts when they are not persistent. |
| 219 | if ( $this->is_persistent() ) { |
| 220 | $classes[] = 'is-dismissible'; |
| 221 | $classes[] = 'wp-helpers-notice'; |
| 222 | $attributes['id'] = $this->args( 'id' ); |
| 223 | $attributes['data-security'] = wp_create_nonce( $this->args( 'id' ) ); |
| 224 | } |
| 225 | |
| 226 | if ( ! empty( $classes ) ) { |
| 227 | $attributes['class'] = implode( ' ', array_filter( $classes ) ); |
| 228 | } |
| 229 | |
| 230 | // Build the output DIV. |
| 231 | $output = '<div' . HTML::attributes_to_string( $attributes ) . '>' . wpautop( $this->message ) . '</div>' . PHP_EOL; |
| 232 | |
| 233 | /** |
| 234 | * Filter: 'wp_helpers_notifications_render' - Allows developer to filter notifications before the output is finalized. |
| 235 | * |
| 236 | * @param string $output HTML output. |
| 237 | * @param array $message Notice message. |
| 238 | * @param array $options Notice args. |
| 239 | */ |
| 240 | $output = apply_filters( 'wp_helpers_notifications_render', $output, $this->message, $this->options ); |
| 241 | |
| 242 | return $output; |
| 243 | } |
| 244 | } |
| 245 |