| 1 |
<?php |
| 2 |
|
| 3 |
namespace Arraytics\PluginNotice; |
| 4 |
|
| 5 |
class Notice |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Singleton instance of the Notice class. |
| 9 |
* |
| 10 |
* @var self|null |
| 11 |
*/ |
| 12 |
protected static $instance; |
| 13 |
|
| 14 |
/** |
| 15 |
* Plugin version (set from main plugin). |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $version = '1.0.0'; // default fallback |
| 20 |
|
| 21 |
/** |
| 22 |
* Unique identifier for the notice. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $notice_id; |
| 27 |
|
| 28 |
/** |
| 29 |
* Text domain of the plugin. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $text_domain; |
| 34 |
|
| 35 |
/** |
| 36 |
* Unique ID used internally. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $unique_id; |
| 41 |
|
| 42 |
/** |
| 43 |
* Additional CSS classes for the notice container. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $class = ''; |
| 48 |
|
| 49 |
/** |
| 50 |
* Default button configuration. |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
protected $button; |
| 55 |
|
| 56 |
/** |
| 57 |
* Size-specific settings (currently unused). |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
protected $size = []; |
| 62 |
|
| 63 |
/** |
| 64 |
* List of buttons added to the notice. |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
protected $buttons = []; |
| 69 |
|
| 70 |
/** |
| 71 |
* Title of the notice. |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
protected $title = ''; |
| 76 |
|
| 77 |
/** |
| 78 |
* Message body of the notice. |
| 79 |
* |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
protected $message = ''; |
| 83 |
|
| 84 |
/** |
| 85 |
* URL of the left-side logo image. |
| 86 |
* |
| 87 |
* @var string |
| 88 |
*/ |
| 89 |
protected $logo = ''; |
| 90 |
|
| 91 |
/** |
| 92 |
* Whether the notice container should have gutter (padding). |
| 93 |
* |
| 94 |
* @var string |
| 95 |
*/ |
| 96 |
protected $gutter = ''; |
| 97 |
|
| 98 |
/** |
| 99 |
* Inline style for the logo image. |
| 100 |
* |
| 101 |
* @var string |
| 102 |
*/ |
| 103 |
protected $logo_style = ''; |
| 104 |
|
| 105 |
/** |
| 106 |
* Dismissible type: false, 'user', or 'global'. |
| 107 |
* |
| 108 |
* @var string|bool |
| 109 |
*/ |
| 110 |
protected $dismissible = false; |
| 111 |
|
| 112 |
/** |
| 113 |
* Expiration time in seconds for the dismissible notice. |
| 114 |
* |
| 115 |
* @var int |
| 116 |
*/ |
| 117 |
protected $expired_time = 1; |
| 118 |
|
| 119 |
/** |
| 120 |
* Raw HTML content to override default markup. |
| 121 |
* |
| 122 |
* @var string |
| 123 |
*/ |
| 124 |
protected $html = ''; |
| 125 |
|
| 126 |
/** |
| 127 |
* Get an instance of the Notice class. |
| 128 |
* |
| 129 |
* @param string|null $text_domain |
| 130 |
* @param string|null $unique_id |
| 131 |
* @return self|false |
| 132 |
*/ |
| 133 |
public static function instance($text_domain = null, $unique_id = null) |
| 134 |
{ |
| 135 |
if (!$text_domain) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
self::$instance = new self(); |
| 139 |
return self::$instance->config($text_domain, $unique_id ?: uniqid()); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Initialize the dismiss AJAX and enqueue scripts. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public static function init() |
| 148 |
{ |
| 149 |
static $initialized = false; |
| 150 |
|
| 151 |
// Prevent multiple initializations |
| 152 |
if ( $initialized ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
add_action('wp_ajax_wpmet-notices', [__CLASS__, 'dismiss_ajax_call']); |
| 157 |
add_action( 'admin_head', [ __CLASS__, 'enqueue_scripts' ] ); |
| 158 |
|
| 159 |
$initialized = true; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Enqueue comparison scripts and styles |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public static function enqueue_scripts() |
| 168 |
{ |
| 169 |
?> |
| 170 |
<script> |
| 171 |
jQuery(document).ready(function ($) { |
| 172 |
$( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() { |
| 173 |
var _this = $( this ).parents('.wpmet-notice').eq(0); |
| 174 |
var notice_id = _this.attr( 'id' ) || ''; |
| 175 |
var expired_time = _this.attr( 'expired_time' ) || ''; |
| 176 |
var dismissible = _this.attr( 'dismissible' ) || ''; |
| 177 |
var nonce = '<?php echo wp_create_nonce( 'wpmet-notices' ); ?>'; |
| 178 |
|
| 179 |
_this.hide(); |
| 180 |
|
| 181 |
$.ajax({ |
| 182 |
url: ajaxurl, |
| 183 |
type: 'POST', |
| 184 |
data: { |
| 185 |
action: 'wpmet-notices', |
| 186 |
notice_id: notice_id, |
| 187 |
dismissible: dismissible, |
| 188 |
expired_time: expired_time, |
| 189 |
nonce: nonce |
| 190 |
}, |
| 191 |
}); |
| 192 |
}); |
| 193 |
}); |
| 194 |
</script> |
| 195 |
<?php |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Configure basic properties of the notice. |
| 200 |
* |
| 201 |
* @param string $text_domain |
| 202 |
* @param string $unique_id |
| 203 |
* @return $this |
| 204 |
*/ |
| 205 |
public function config($text_domain, $unique_id) |
| 206 |
{ |
| 207 |
$this->text_domain = $text_domain; |
| 208 |
$this->unique_id = $unique_id; |
| 209 |
$this->notice_id = "$text_domain-$unique_id"; |
| 210 |
$this->dismissible = false; |
| 211 |
$this->expired_time = 1; |
| 212 |
|
| 213 |
$this->button = [ |
| 214 |
'default_class' => 'button', |
| 215 |
'class' => 'button-secondary', |
| 216 |
'text' => 'Button', |
| 217 |
'url' => '#', |
| 218 |
'icon' => '', |
| 219 |
]; |
| 220 |
|
| 221 |
return $this; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Set additional CSS classes. |
| 226 |
* |
| 227 |
* @param string $classname |
| 228 |
* @return $this |
| 229 |
*/ |
| 230 |
public function set_class($classname = '') |
| 231 |
{ |
| 232 |
$this->class .= $classname; |
| 233 |
return $this; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Set the notice type. |
| 238 |
* |
| 239 |
* @param string $type |
| 240 |
* @return $this |
| 241 |
*/ |
| 242 |
public function set_type($type = '') |
| 243 |
{ |
| 244 |
$this->class .= " notice-$type"; |
| 245 |
return $this; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Add a button to the notice. |
| 250 |
* |
| 251 |
* @param array $button |
| 252 |
* @return $this |
| 253 |
*/ |
| 254 |
public function set_button($button = []) |
| 255 |
{ |
| 256 |
$this->buttons[] = array_merge($this->button, $button); |
| 257 |
return $this; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Set a custom ID for the notice. |
| 262 |
* |
| 263 |
* @param string $id |
| 264 |
* @return $this |
| 265 |
*/ |
| 266 |
public function set_id($id) |
| 267 |
{ |
| 268 |
$this->notice_id = $id; |
| 269 |
return $this; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Set the title of the notice. |
| 274 |
* |
| 275 |
* @param string $title |
| 276 |
* @return $this |
| 277 |
*/ |
| 278 |
public function set_title($title = '') |
| 279 |
{ |
| 280 |
$this->title .= $title; |
| 281 |
return $this; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Set the message of the notice. |
| 286 |
* |
| 287 |
* @param string $message |
| 288 |
* @return $this |
| 289 |
*/ |
| 290 |
public function set_message($message = '') |
| 291 |
{ |
| 292 |
$this->message .= $message; |
| 293 |
return $this; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Enable or disable gutter spacing. |
| 298 |
* |
| 299 |
* @param bool $gutter |
| 300 |
* @return $this |
| 301 |
*/ |
| 302 |
public function set_gutter($gutter = true) |
| 303 |
{ |
| 304 |
$this->gutter = $gutter; |
| 305 |
$this->class .= $gutter ? '' : ' no-gutter'; |
| 306 |
return $this; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Set logo and style. |
| 311 |
* |
| 312 |
* @param string $logo |
| 313 |
* @param string $logo_style |
| 314 |
* @return $this |
| 315 |
*/ |
| 316 |
public function set_logo($logo = '', $logo_style = '') |
| 317 |
{ |
| 318 |
$this->logo = $logo; |
| 319 |
$this->logo_style = $logo_style; |
| 320 |
return $this; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Set custom HTML content. |
| 325 |
* |
| 326 |
* @param string $html |
| 327 |
* @return $this |
| 328 |
*/ |
| 329 |
public function set_html($html = '') |
| 330 |
{ |
| 331 |
$this->html .= $html; |
| 332 |
return $this; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Configure dismiss behavior. |
| 337 |
* |
| 338 |
* @param string $scope 'user' or 'global' |
| 339 |
* @param int $time Expiry time in seconds |
| 340 |
* @return $this |
| 341 |
*/ |
| 342 |
public function set_dismiss($scope = 'global', $time = 604800) |
| 343 |
{ |
| 344 |
$this->dismissible = $scope; |
| 345 |
$this->expired_time = $time; |
| 346 |
return $this; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Get the compiled data for rendering. |
| 351 |
* |
| 352 |
* @return array |
| 353 |
*/ |
| 354 |
public function get_data() |
| 355 |
{ |
| 356 |
return [ |
| 357 |
'message' => $this->message, |
| 358 |
'title' => $this->title, |
| 359 |
'buttons' => $this->buttons, |
| 360 |
'class' => $this->class, |
| 361 |
'html' => $this->html, |
| 362 |
]; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Add the notice to the WordPress admin_notices hook. |
| 367 |
* |
| 368 |
* @return void |
| 369 |
*/ |
| 370 |
public function call() |
| 371 |
{ |
| 372 |
add_action('admin_notices', [$this, 'get_notice']); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Display the notice HTML based on conditions. |
| 377 |
* |
| 378 |
* @return void |
| 379 |
*/ |
| 380 |
public function get_notice() |
| 381 |
{ |
| 382 |
switch ($this->dismissible) { |
| 383 |
case 'user': |
| 384 |
$expired = get_user_meta(get_current_user_id(), $this->notice_id, true); |
| 385 |
break; |
| 386 |
case 'global': |
| 387 |
$expired = get_transient($this->notice_id); |
| 388 |
break; |
| 389 |
default: |
| 390 |
$expired = ''; |
| 391 |
break; |
| 392 |
} |
| 393 |
|
| 394 |
global $notice_list; |
| 395 |
$notice_list = isset($notice_list) ? $notice_list : []; |
| 396 |
|
| 397 |
if (!isset($notice_list[$this->notice_id]) && (false === $expired || empty($expired))) { |
| 398 |
$notice_list[$this->notice_id] = __FILE__; |
| 399 |
$this->generate_html(); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Render the HTML for the notice. |
| 405 |
* |
| 406 |
* @return void |
| 407 |
*/ |
| 408 |
public function generate_html() |
| 409 |
{ |
| 410 |
?> |
| 411 |
<div |
| 412 |
id="<?php echo esc_attr($this->notice_id); ?>" |
| 413 |
class="notice wpmet-notice notice-<?php echo esc_attr($this->notice_id . ' ' . $this->class); ?> <?php echo $this->dismissible === false ? '' : 'is-dismissible'; ?>" |
| 414 |
expired_time="<?php echo esc_attr($this->expired_time); ?>" |
| 415 |
dismissible="<?php echo esc_attr($this->dismissible); ?>" |
| 416 |
> |
| 417 |
<?php if (!empty($this->logo)) : ?> |
| 418 |
<img class="notice-logo" style="<?php echo esc_attr($this->logo_style); ?>" src="<?php echo esc_url($this->logo); ?>" /> |
| 419 |
<?php endif; ?> |
| 420 |
|
| 421 |
<div class="notice-right-container <?php echo empty($this->logo) ? 'notice-container-full-width' : ''; ?>"> |
| 422 |
<?php if (empty($this->html)) : ?> |
| 423 |
<?php if (!empty($this->title)) : ?> |
| 424 |
<div class="notice-main-title notice-vert-space"><?php echo esc_html($this->title); ?></div> |
| 425 |
<?php endif; ?> |
| 426 |
|
| 427 |
<div class="notice-message notice-vert-space"> |
| 428 |
<?php echo wp_kses_post($this->message); ?> |
| 429 |
</div> |
| 430 |
|
| 431 |
<?php if (!empty($this->buttons)) : ?> |
| 432 |
<div class="button-container notice-vert-space"> |
| 433 |
<?php foreach ($this->buttons as $button) : ?> |
| 434 |
<a id="<?php echo isset($button['id']) ? esc_attr($button['id']) : ''; ?>" |
| 435 |
href="<?php echo esc_url($button['url']); ?>" |
| 436 |
class="wpmet-notice-button <?php echo esc_attr($button['class']); ?>"> |
| 437 |
<?php if (!empty($button['icon'])) : ?> |
| 438 |
<i class="notice-icon <?php echo esc_attr($button['icon']); ?>"></i> |
| 439 |
<?php endif; ?> |
| 440 |
<?php echo esc_html($button['text']); ?> |
| 441 |
</a> |
| 442 |
<?php endforeach; ?> |
| 443 |
</div> |
| 444 |
<?php endif; ?> |
| 445 |
<?php else : ?> |
| 446 |
<?php echo wp_kses_post($this->html); ?> |
| 447 |
<?php endif; ?> |
| 448 |
</div> |
| 449 |
|
| 450 |
<?php if ($this->dismissible !== false) : ?> |
| 451 |
<button type="button" class="notice-dismiss"> |
| 452 |
<span class="screen-reader-text">x#dismiss</span> |
| 453 |
</button> |
| 454 |
<?php endif; ?> |
| 455 |
|
| 456 |
<div style="clear:both"></div> |
| 457 |
</div> |
| 458 |
<?php |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Handle AJAX dismissal request. |
| 463 |
* |
| 464 |
* @return void |
| 465 |
*/ |
| 466 |
public static function dismiss_ajax_call() |
| 467 |
{ |
| 468 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 469 |
if (empty($nonce) || !wp_verify_nonce($nonce, 'wpmet-notices')) { |
| 470 |
wp_send_json_error(); |
| 471 |
} |
| 472 |
|
| 473 |
$notice_id = isset($_POST['notice_id']) ? sanitize_text_field(wp_unslash($_POST['notice_id'])) : ''; |
| 474 |
$dismissible = isset($_POST['dismissible']) ? sanitize_text_field(wp_unslash($_POST['dismissible'])) : ''; |
| 475 |
$expired = isset($_POST['expired_time']) ? (int) sanitize_text_field(wp_unslash($_POST['expired_time'])) : 0; |
| 476 |
|
| 477 |
if ($notice_id) { |
| 478 |
if ($dismissible === 'user') { |
| 479 |
update_user_meta(get_current_user_id(), $notice_id, true); |
| 480 |
} else { |
| 481 |
set_transient($notice_id, true, $expired); |
| 482 |
} |
| 483 |
wp_send_json_success(); |
| 484 |
} |
| 485 |
|
| 486 |
wp_send_json_error(); |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Set the plugin version dynamically. |
| 491 |
* |
| 492 |
* @param string $version |
| 493 |
* @return $this |
| 494 |
*/ |
| 495 |
public function set_version($version) |
| 496 |
{ |
| 497 |
$this->version = $version; |
| 498 |
return $this; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Get plugin version. |
| 503 |
* |
| 504 |
* @return string |
| 505 |
*/ |
| 506 |
public function get_version() |
| 507 |
{ |
| 508 |
return $this->version; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Get file location of this script. |
| 513 |
* |
| 514 |
* @return string |
| 515 |
*/ |
| 516 |
public function get_script_location() |
| 517 |
{ |
| 518 |
return __FILE__; |
| 519 |
} |
| 520 |
} |
| 521 |
|