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