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