| 1 |
<?php |
| 2 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- Third-party vendor library, namespace cannot be changed |
| 3 |
namespace Oxaim\Libs; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
if(!class_exists('\Oxaim\Libs\Notice')): |
| 8 |
|
| 9 |
class Notice{ |
| 10 |
|
| 11 |
/** |
| 12 |
* scripts version |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $script_version = '2.1.1'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Unique ID to identify each notice |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $notice_id; |
| 24 |
|
| 25 |
/** |
| 26 |
* Plugin text-domain |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $text_domain; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Unique ID |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $unique_id; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Notice div container's class |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $class; |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Single button's data |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
protected $button; |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Size class |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
protected $size; |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* List of all buttons with it's config data |
| 67 |
* |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
protected $buttons; |
| 71 |
|
| 72 |
/** |
| 73 |
* Notice title |
| 74 |
* |
| 75 |
* @var string |
| 76 |
*/ |
| 77 |
protected $title; |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Notice message |
| 82 |
* |
| 83 |
* @var string |
| 84 |
*/ |
| 85 |
protected $message; |
| 86 |
|
| 87 |
/** |
| 88 |
* Left logo |
| 89 |
* |
| 90 |
* @var string |
| 91 |
*/ |
| 92 |
protected $logo; |
| 93 |
/** |
| 94 |
* Container gutter |
| 95 |
* |
| 96 |
* @var string |
| 97 |
*/ |
| 98 |
protected $gutter; |
| 99 |
|
| 100 |
/** |
| 101 |
* Left logo style |
| 102 |
* |
| 103 |
* @var string |
| 104 |
*/ |
| 105 |
protected $logo_style; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Left logo style |
| 110 |
* |
| 111 |
* @var string |
| 112 |
*/ |
| 113 |
protected $dismissible; |
| 114 |
|
| 115 |
protected $expired_time; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* html markup for notice |
| 121 |
* |
| 122 |
* @var string |
| 123 |
*/ |
| 124 |
protected $html; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* get_version |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function get_version(){ |
| 134 |
return $this->script_version; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* get_script_location |
| 139 |
* |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
public function get_script_location(){ |
| 143 |
return __FILE__; |
| 144 |
} |
| 145 |
|
| 146 |
// config |
| 147 |
|
| 148 |
/** |
| 149 |
* Configures all setter variables |
| 150 |
* |
| 151 |
* @param string $prefix |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public function config($text_domain = '', $unique_id = ''){ |
| 155 |
$this->text_domain = $text_domain; |
| 156 |
|
| 157 |
$this->unique_id = $unique_id; |
| 158 |
|
| 159 |
$this->notice_id = $text_domain . '-' . $unique_id; |
| 160 |
|
| 161 |
$this->dismissible = false; // false, user, global |
| 162 |
|
| 163 |
$this->expired_time = 1; |
| 164 |
|
| 165 |
$this->html = ''; |
| 166 |
|
| 167 |
$this->title = ''; |
| 168 |
|
| 169 |
$this->message = ''; |
| 170 |
|
| 171 |
$this->class = ''; |
| 172 |
|
| 173 |
$this->gutter = true; |
| 174 |
|
| 175 |
$this->logo = ''; |
| 176 |
|
| 177 |
$this->logo_style = ''; |
| 178 |
|
| 179 |
$this->size = [ ]; |
| 180 |
|
| 181 |
$this->button = [ |
| 182 |
'default_class' => 'button', |
| 183 |
'class' => 'button-secondary ', // button-primary button-secondary button-small button-large button-link |
| 184 |
'text' => 'Button', |
| 185 |
'url' => '#', |
| 186 |
'icon' => '' |
| 187 |
]; |
| 188 |
|
| 189 |
$this->buttons = []; |
| 190 |
|
| 191 |
return $this; |
| 192 |
} |
| 193 |
|
| 194 |
// setters begin |
| 195 |
|
| 196 |
/** |
| 197 |
* Adds classes to the container |
| 198 |
* |
| 199 |
* @param string $classname |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function set_class($classname = ''){ |
| 203 |
$this->class .= $classname; |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
public function set_type($type = ''){ |
| 209 |
$this->class .= ' notice-' . $type; |
| 210 |
|
| 211 |
return $this; |
| 212 |
} |
| 213 |
|
| 214 |
public function set_button($button = []){ |
| 215 |
$button = array_merge($this->button, $button); |
| 216 |
$this->buttons[] = $button; |
| 217 |
|
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
public function set_id($id){ |
| 222 |
$this->notice_id = $id; |
| 223 |
return $this; |
| 224 |
} |
| 225 |
|
| 226 |
public function set_title($title = ''){ |
| 227 |
$this->title .= $title; |
| 228 |
|
| 229 |
return $this; |
| 230 |
} |
| 231 |
|
| 232 |
public function set_message($message = ''){ |
| 233 |
$this->message .= $message; |
| 234 |
|
| 235 |
return $this; |
| 236 |
} |
| 237 |
|
| 238 |
public function set_gutter($gutter = true){ |
| 239 |
$this->gutter .= $gutter; |
| 240 |
$this->class .= ($gutter === true ? '' : ' no-gutter'); |
| 241 |
|
| 242 |
return $this; |
| 243 |
} |
| 244 |
|
| 245 |
public function set_logo($logo = '', $logo_style = ""){ |
| 246 |
$this->logo = $logo; |
| 247 |
|
| 248 |
$this->logo_style = $logo_style; |
| 249 |
|
| 250 |
return $this; |
| 251 |
} |
| 252 |
|
| 253 |
public function set_html($html = ''){ |
| 254 |
$this->html .= $html; |
| 255 |
|
| 256 |
return $this; |
| 257 |
} |
| 258 |
|
| 259 |
// setters ends |
| 260 |
|
| 261 |
|
| 262 |
// group getter |
| 263 |
public function get_data(){ |
| 264 |
return [ |
| 265 |
'message' => $this->message, |
| 266 |
'title' => $this->title, |
| 267 |
'buttons' => $this->buttons, |
| 268 |
'class' => $this->class, |
| 269 |
'html' => $this->html, |
| 270 |
]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function call(){ |
| 277 |
add_action( 'admin_notices', [$this, 'get_notice'] ); |
| 278 |
} |
| 279 |
|
| 280 |
public function get_notice(){ |
| 281 |
// dismissible conditions |
| 282 |
if ( 'user' === $this->dismissible) { |
| 283 |
$expired = get_user_meta( get_current_user_id(), $this->notice_id, true ); |
| 284 |
} elseif ( 'global' === $this->dismissible ) { |
| 285 |
$expired = get_transient( $this->notice_id ); |
| 286 |
}else{ |
| 287 |
$expired = ''; |
| 288 |
} |
| 289 |
|
| 290 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Vendor library global variable |
| 291 |
global $oxaim_lib_notice_list; |
| 292 |
|
| 293 |
if(!isset($oxaim_lib_notice_list[$this->notice_id])){ |
| 294 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Vendor library global variable usage |
| 295 |
$oxaim_lib_notice_list[$this->notice_id] = __FILE__; |
| 296 |
|
| 297 |
// is transient expired? |
| 298 |
if ( false === $expired || empty( $expired ) ) { |
| 299 |
$this->generate_html(); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
public function set_dismiss($scope = 'global', $time = (3600 * 24 * 7)){ |
| 305 |
$this->dismissible = $scope; |
| 306 |
$this->expired_time = $time; |
| 307 |
|
| 308 |
return $this; |
| 309 |
} |
| 310 |
|
| 311 |
public function generate_html() { |
| 312 |
|
| 313 |
?> |
| 314 |
<div |
| 315 |
id="<?php echo esc_attr($this->notice_id); ?>" |
| 316 |
class="notice wpmet-notice notice-<?php echo esc_attr($this->notice_id . ' ' .$this->class); ?> <?php echo (false === $this->dismissible ? '' : 'is-dismissible') ;?>" |
| 317 |
|
| 318 |
expired_time="<?php echo esc_attr( $this->expired_time ); ?>" |
| 319 |
dismissible="<?php echo esc_attr( $this->dismissible ); ?>" |
| 320 |
> |
| 321 |
<?php if(!empty($this->logo)):?> |
| 322 |
<img class="notice-logo" style="<?php echo esc_attr($this->logo_style); ?>" src="<?php echo esc_url($this->logo);?>" /> |
| 323 |
<?php endif; ?> |
| 324 |
|
| 325 |
<div class="notice-right-container <?php echo (empty($this->logo) ? 'notice-container-full-width' : ''); ?>"> |
| 326 |
|
| 327 |
<?php if(empty($this->html)): ?> |
| 328 |
<?php echo esc_html( empty($this->title ) ? '' : sprintf('<div class="notice-main-title notice-vert-space">%s</div>', esc_html( $this->title ) )); ?> |
| 329 |
|
| 330 |
<div class="notice-message notice-vert-space"> |
| 331 |
<?php echo wp_kses( $this->message, 'post' );?> |
| 332 |
</div> |
| 333 |
|
| 334 |
<?php if(!empty($this->buttons)): ?> |
| 335 |
<div class="button-container notice-vert-space"> |
| 336 |
<?php foreach($this->buttons as $button): ?> |
| 337 |
<a id="<?php echo esc_attr( !isset($button['id']) ? '' : $button['id'] ); ?>" href="<?php echo esc_url($button['url']); ?>" class="wpmet-notice-button <?php echo esc_attr($button['class']); ?>"> |
| 338 |
<?php if(!empty($button['icon'])) :?> |
| 339 |
<i class="notice-icon <?php echo esc_attr($button['icon']); ?>"></i> |
| 340 |
<?php endif; ?> |
| 341 |
<?php echo esc_html($button['text']);?> |
| 342 |
</a> |
| 343 |
|
| 344 |
<?php endforeach; ?> |
| 345 |
</div> |
| 346 |
<?php endif;?> |
| 347 |
|
| 348 |
<?php else:?> |
| 349 |
<?php echo wp_kses( $this->html, 'post' ); ?> |
| 350 |
<?php endif;?> |
| 351 |
|
| 352 |
</div> |
| 353 |
|
| 354 |
<?php if(false !== $this->dismissible): ?> |
| 355 |
<button type="button" class="notice-dismiss"> |
| 356 |
<span class="screen-reader-text">x</span> |
| 357 |
</button> |
| 358 |
<?php endif;?> |
| 359 |
|
| 360 |
<div style="clear:both"></div> |
| 361 |
|
| 362 |
</div> |
| 363 |
<?php |
| 364 |
} |
| 365 |
|
| 366 |
public static function init(){ |
| 367 |
add_action( 'wp_ajax_wpmet-notices', [ __CLASS__, 'dismiss_ajax_call' ] ); |
| 368 |
add_action( 'admin_head', [ __CLASS__, 'enqueue_scripts' ] ); |
| 369 |
} |
| 370 |
|
| 371 |
public static function dismiss_ajax_call() { |
| 372 |
check_ajax_referer( 'wpmet_notice_dismiss', 'nonce' ); |
| 373 |
|
| 374 |
$post_arr = wp_unslash( $_POST ); |
| 375 |
$notice_id = ( isset( $post_arr['notice_id'] ) ) ? sanitize_key( $post_arr['notice_id'] ) : ''; |
| 376 |
$dismissible = ( isset( $post_arr['dismissible'] ) ) ? sanitize_text_field( $post_arr['dismissible'] ) : ''; |
| 377 |
$expired_time = ( isset( $post_arr['expired_time'] ) ) ? absint( $post_arr['expired_time'] ) : 0; |
| 378 |
|
| 379 |
if ( ! empty( $notice_id ) ) { |
| 380 |
if ( 'user' === $dismissible ) { |
| 381 |
update_user_meta( get_current_user_id(), $notice_id, true ); |
| 382 |
} else { |
| 383 |
set_transient( $notice_id, true, $expired_time ); |
| 384 |
} |
| 385 |
|
| 386 |
wp_send_json_success(); |
| 387 |
} |
| 388 |
|
| 389 |
wp_send_json_error(); |
| 390 |
} |
| 391 |
|
| 392 |
public static function enqueue_scripts() { |
| 393 |
$nonce = wp_create_nonce( 'wpmet_notice_dismiss' ); |
| 394 |
echo " |
| 395 |
<script> |
| 396 |
jQuery(document).ready(function ($) { |
| 397 |
$( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() { |
| 398 |
|
| 399 |
_this = $( this ).parents('.wpmet-notice').eq(0); |
| 400 |
var notice_id = _this.attr( 'id' ) || ''; |
| 401 |
var expired_time = _this.attr( 'expired_time' ) || ''; |
| 402 |
var dismissible = _this.attr( 'dismissible' ) || ''; |
| 403 |
var x = $( this ).attr('class'); |
| 404 |
|
| 405 |
_this.hide(); |
| 406 |
|
| 407 |
$.ajax({ |
| 408 |
url: ajaxurl, |
| 409 |
type: 'POST', |
| 410 |
data: { |
| 411 |
action : 'wpmet-notices', |
| 412 |
notice_id : notice_id, |
| 413 |
dismissible : dismissible, |
| 414 |
expired_time : expired_time, |
| 415 |
nonce : '" . esc_js( $nonce ) . "', |
| 416 |
}, |
| 417 |
}); |
| 418 |
}); |
| 419 |
}); |
| 420 |
</script> |
| 421 |
<style> |
| 422 |
.wpmet-notice{ |
| 423 |
background-color: transparent; |
| 424 |
margin: 10px 40px 0 20px; |
| 425 |
padding: 0!important; |
| 426 |
display: flex; |
| 427 |
flex-direction: row; |
| 428 |
justify-content: flex-start; |
| 429 |
align-items: center; |
| 430 |
} |
| 431 |
|
| 432 |
.wpmet-notice .notice-right-container{ |
| 433 |
margin: .7rem .8rem .8rem; |
| 434 |
} |
| 435 |
|
| 436 |
.notice-container-full-width{ |
| 437 |
width:100%!important; |
| 438 |
} |
| 439 |
|
| 440 |
.notice-container-full-width img { |
| 441 |
max-width: 100%; |
| 442 |
border-radius: 6px; |
| 443 |
} |
| 444 |
|
| 445 |
.wpmet-notice.no-gutter{ |
| 446 |
padding: 0!important; |
| 447 |
border-width: 0!important; |
| 448 |
} |
| 449 |
.wpmet-notice.no-gutter .notice-right-container{ |
| 450 |
padding: 0!important; |
| 451 |
margin: 0!important; |
| 452 |
} |
| 453 |
|
| 454 |
.notice-right-container .notice-vert-space{ |
| 455 |
margin-bottom: .8rem; |
| 456 |
} |
| 457 |
|
| 458 |
.notice-right-container .notice-vert-space:last-child, |
| 459 |
.notice-right-container .notice-vert-space:only-child{ |
| 460 |
margin-bottom: 0; |
| 461 |
} |
| 462 |
|
| 463 |
.wpmet-notice .notice-logo{ |
| 464 |
padding: 3px; |
| 465 |
max-width: 110px; |
| 466 |
max-height: 110px; |
| 467 |
} |
| 468 |
|
| 469 |
.wpmet-notice-button { |
| 470 |
text-decoration:none; |
| 471 |
} |
| 472 |
|
| 473 |
.wpmet-notice-button > i{ |
| 474 |
margin-right: 3px; |
| 475 |
} |
| 476 |
|
| 477 |
.wpmet-notice-button .notice-icon{ |
| 478 |
display:inline-block; |
| 479 |
} |
| 480 |
|
| 481 |
.wpmet-notice-button .notice-icon:before{ |
| 482 |
vertical-align: middle!important; |
| 483 |
margin-top: -1px; |
| 484 |
} |
| 485 |
|
| 486 |
.wpmet-notice .notice-main-title{ |
| 487 |
color: #1d2327; |
| 488 |
font-size: 1.2rem; |
| 489 |
} |
| 490 |
|
| 491 |
</style> |
| 492 |
"; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
private static $instance; |
| 497 |
|
| 498 |
/** |
| 499 |
* Method: instance -> Return Notice module class instance |
| 500 |
* |
| 501 |
* @param string|null $text_domain |
| 502 |
* @param string|null $unique_id |
| 503 |
* @return mixed |
| 504 |
*/ |
| 505 |
public static function instance($text_domain = null, $unique_id = null) { |
| 506 |
if($text_domain == null){ |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
self::$instance = new self(); |
| 511 |
|
| 512 |
return self::$instance->config($text_domain, (is_null($unique_id) ? uniqid() : $unique_id)); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
endif; |