| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Core\Admin; |
| 4 |
|
| 5 |
class Notices |
| 6 |
{ |
| 7 |
|
| 8 |
/** |
| 9 |
* Not dismissible. |
| 10 |
* |
| 11 |
* Constant attended to use as the value of the $args['dismiss'] argument. |
| 12 |
* DISMISS_NONE means that the notice is not dismissible. |
| 13 |
* |
| 14 |
*/ |
| 15 |
const DISMISS_NONE = 0; |
| 16 |
|
| 17 |
/** |
| 18 |
* Dismissible global. |
| 19 |
* |
| 20 |
* Constant attended to use as the value of the $args['dismiss'] argument. |
| 21 |
* DISMISS_GLOBAL means that the notice will have the dismiss button, and after clicking this button, the notice will be dismissed for all users. |
| 22 |
* |
| 23 |
*/ |
| 24 |
const DISMISS_GLOBAL = 1; |
| 25 |
|
| 26 |
/** |
| 27 |
* Dismissible per user. |
| 28 |
* |
| 29 |
* Constant attended to use as the value of the $args['dismiss'] argument. |
| 30 |
* DISMISS_USER means that the notice will have the dismiss button, and after clicking this button, the notice will be dismissed only for the current user.. |
| 31 |
* |
| 32 |
*/ |
| 33 |
const DISMISS_USER = 2; |
| 34 |
|
| 35 |
/** |
| 36 |
* Added notices. |
| 37 |
* |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private $notices = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* Init. |
| 45 |
* |
| 46 |
*/ |
| 47 |
public function __construct() |
| 48 |
{ |
| 49 |
|
| 50 |
$this->hooks(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Hooks. |
| 55 |
* |
| 56 |
*/ |
| 57 |
public function hooks() |
| 58 |
{ |
| 59 |
|
| 60 |
add_action('admin_notices', [$this, 'display'], PHP_INT_MAX); |
| 61 |
add_action('wp_ajax_yatra_notice_dismiss', [$this, 'dismiss_ajax']); |
| 62 |
} |
| 63 |
|
| 64 |
private function enqueues() |
| 65 |
{ |
| 66 |
|
| 67 |
wp_enqueue_script( |
| 68 |
'yatra-admin-notices', |
| 69 |
YATRA_PLUGIN_URI . "/core/Admin/assets/js/notices.js", |
| 70 |
['jquery'], |
| 71 |
YATRA_VERSION, |
| 72 |
true |
| 73 |
); |
| 74 |
|
| 75 |
wp_localize_script( |
| 76 |
'yatra-admin-notices', |
| 77 |
'yatra_admin_notices', |
| 78 |
[ |
| 79 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 80 |
'nonce' => wp_create_nonce('yatra-admin'), |
| 81 |
] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
public function display() |
| 86 |
{ |
| 87 |
|
| 88 |
$dismissed_notices = get_user_meta(get_current_user_id(), 'yatra_admin_notices', true); |
| 89 |
$dismissed_notices = is_array($dismissed_notices) ? $dismissed_notices : []; |
| 90 |
$dismissed_notices = array_merge($dismissed_notices, (array)get_option('yatra_admin_notices', [])); |
| 91 |
|
| 92 |
foreach ($this->notices as $slug => $notice) { |
| 93 |
if (isset($dismissed_notices[$slug]) && !empty($dismissed_notices[$slug]['dismissed'])) { |
| 94 |
unset($this->notices[$slug]); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$output = implode('', $this->notices); |
| 99 |
|
| 100 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 101 |
|
| 102 |
// Enqueue script only when it's needed. |
| 103 |
if (strpos($output, 'is-dismissible') !== false) { |
| 104 |
$this->enqueues(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public static function add($message, $type = '', $args = []) |
| 110 |
{ |
| 111 |
|
| 112 |
static $uniq_id = 0; |
| 113 |
|
| 114 |
$defaults = [ |
| 115 |
'dismiss' => self::DISMISS_NONE, // Dismissible level: one of the self::DISMISS_* const. By default notice is not dismissible. |
| 116 |
'slug' => '', // Slug. Should be unique if dismissible is not equal self::DISMISS_NONE. |
| 117 |
'autop' => true, // `false` if not needed to pass message through wpautop(). |
| 118 |
'class' => '', // Additional CSS class. |
| 119 |
]; |
| 120 |
|
| 121 |
$args = wp_parse_args($args, $defaults); |
| 122 |
|
| 123 |
$dismissible = (int)$args['dismiss']; |
| 124 |
$dismissible = $dismissible > self::DISMISS_USER ? self::DISMISS_USER : $dismissible; |
| 125 |
|
| 126 |
$class = $dismissible > self::DISMISS_NONE ? ' is-dismissible' : ''; |
| 127 |
$global = ($dismissible === self::DISMISS_GLOBAL) ? 'global-' : ''; |
| 128 |
$slug = sanitize_key($args['slug']); |
| 129 |
|
| 130 |
++$uniq_id; |
| 131 |
|
| 132 |
$uniq_id += ($uniq_id === (int)$slug) ? 1 : 0; |
| 133 |
|
| 134 |
$id = 'yatra-notice-' . $global; |
| 135 |
$id .= empty($slug) ? $uniq_id : $slug; |
| 136 |
$type = !empty($type) ? 'notice-' . esc_attr(sanitize_key($type)) : ''; |
| 137 |
$class = empty($args['class']) ? $class : $class . ' ' . esc_attr(sanitize_key($args['class'])); |
| 138 |
$message = $args['autop'] ? wpautop($message) : $message; |
| 139 |
$notice = sprintf( |
| 140 |
'<div class="notice yatra-notice %s%s" id="%s">%s</div>', |
| 141 |
esc_attr($type), |
| 142 |
esc_attr($class), |
| 143 |
esc_attr($id), |
| 144 |
$message |
| 145 |
); |
| 146 |
|
| 147 |
if (empty($slug)) { |
| 148 |
yatra()->admin_notice->notices[] = $notice; |
| 149 |
} else { |
| 150 |
yatra()->admin_notice->notices[$slug] = $notice; |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
public static function info($message, $args = []) |
| 157 |
{ |
| 158 |
|
| 159 |
self::add($message, 'info', $args); |
| 160 |
} |
| 161 |
|
| 162 |
public static function error($message, $args = []) |
| 163 |
{ |
| 164 |
|
| 165 |
self::add($message, 'error', $args); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add success notice. |
| 170 |
* |
| 171 |
* @param string $message Message to display. |
| 172 |
* @param array $args Array of additional arguments. Details in the self::add() method. |
| 173 |
* @since 2.1.11 |
| 174 |
* |
| 175 |
*/ |
| 176 |
public static function success($message, $args = []) |
| 177 |
{ |
| 178 |
|
| 179 |
self::add($message, 'success', $args); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Add warning notice. |
| 184 |
* |
| 185 |
* @param string $message Message to display. |
| 186 |
* @param array $args Array of additional arguments. Details in the self::add() method. |
| 187 |
* @since 2.1.11 |
| 188 |
* |
| 189 |
*/ |
| 190 |
public static function warning($message, $args = []) |
| 191 |
{ |
| 192 |
|
| 193 |
self::add($message, 'warning', $args); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* AJAX routine that updates dismissed notices meta data. |
| 198 |
* |
| 199 |
*/ |
| 200 |
public function dismiss_ajax() |
| 201 |
{ |
| 202 |
|
| 203 |
// Run a security check. |
| 204 |
check_ajax_referer('yatra-admin', 'nonce'); |
| 205 |
|
| 206 |
// Sanitize POST data. |
| 207 |
$post = array_map('sanitize_key', wp_unslash($_POST)); |
| 208 |
|
| 209 |
// Update notices meta data. |
| 210 |
if (strpos($post['id'], 'global-') !== false) { |
| 211 |
|
| 212 |
// Check for permissions. |
| 213 |
if (!current_user_can('manage_yatra')) { |
| 214 |
wp_send_json_error(); |
| 215 |
} |
| 216 |
|
| 217 |
$notices = $this->dismiss_global($post['id']); |
| 218 |
$level = self::DISMISS_GLOBAL; |
| 219 |
|
| 220 |
} else { |
| 221 |
|
| 222 |
$notices = $this->dismiss_user($post['id']); |
| 223 |
$level = self::DISMISS_USER; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Allows developers to apply additional logic to the dismissing notice process. |
| 228 |
* Executes after updating option or user meta (according to the notice level). |
| 229 |
* |
| 230 |
* @param string $notice_id Notice ID (slug). |
| 231 |
* @param integer $level Notice level. |
| 232 |
* @param array $notices Dismissed notices. |
| 233 |
* |
| 234 |
*/ |
| 235 |
do_action('yatra_admin_notice_dismiss_ajax', $post['id'], $level, $notices); |
| 236 |
|
| 237 |
wp_send_json_success( |
| 238 |
[ |
| 239 |
'id' => $post['id'], |
| 240 |
'time' => time(), |
| 241 |
'level' => $level, |
| 242 |
'notices' => $notices, |
| 243 |
] |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* AJAX sub-routine that updates dismissed notices option. |
| 249 |
* |
| 250 |
* @param string $id Notice Id. |
| 251 |
* |
| 252 |
* @return array Notices. |
| 253 |
* |
| 254 |
*/ |
| 255 |
private function dismiss_global($id) |
| 256 |
{ |
| 257 |
|
| 258 |
$id = str_replace('global-', '', $id); |
| 259 |
$notices = get_option('yatra_admin_notices', []); |
| 260 |
$notices[$id] = [ |
| 261 |
'time' => time(), |
| 262 |
'dismissed' => true, |
| 263 |
]; |
| 264 |
|
| 265 |
update_option('yatra_admin_notices', $notices, true); |
| 266 |
|
| 267 |
return $notices; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* AJAX sub-routine that updates dismissed notices user meta. |
| 272 |
* |
| 273 |
* @param string $id Notice Id. |
| 274 |
* |
| 275 |
* @return array Notices. |
| 276 |
* |
| 277 |
*/ |
| 278 |
private function dismiss_user($id) |
| 279 |
{ |
| 280 |
|
| 281 |
$user_id = get_current_user_id(); |
| 282 |
$notices = get_user_meta($user_id, 'yatra_admin_notices', true); |
| 283 |
$notices = !is_array($notices) ? [] : $notices; |
| 284 |
$notices[$id] = [ |
| 285 |
'time' => time(), |
| 286 |
'dismissed' => true, |
| 287 |
]; |
| 288 |
|
| 289 |
update_user_meta($user_id, 'yatra_admin_notices', $notices); |
| 290 |
|
| 291 |
return $notices; |
| 292 |
} |
| 293 |
} |
| 294 |
|