notice.php
671 lines
| 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.2.0'; |
| 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 | * style css for notice |
| 129 | * |
| 130 | * @var string |
| 131 | */ |
| 132 | protected $style_css; |
| 133 | |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * set if the notice will be shown to admins only |
| 138 | */ |
| 139 | protected $admin_only = true; |
| 140 | |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * get_version |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | public function get_version() { |
| 149 | return $this->script_version; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * get_script_location |
| 155 | * |
| 156 | * @return string |
| 157 | */ |
| 158 | public function get_script_location() { |
| 159 | return __FILE__; |
| 160 | } |
| 161 | |
| 162 | // config |
| 163 | |
| 164 | /** |
| 165 | * Configures all setter variables |
| 166 | * |
| 167 | * @param string $prefix |
| 168 | * @return void |
| 169 | */ |
| 170 | public function config( $text_domain = '', $unique_id = '' ) { |
| 171 | $this->text_domain = $text_domain; |
| 172 | |
| 173 | $this->unique_id = $unique_id; |
| 174 | |
| 175 | $this->notice_id = $text_domain . '-' . $unique_id; |
| 176 | |
| 177 | $this->dismissible = false; // false, user, global |
| 178 | |
| 179 | $this->expired_time = 1; |
| 180 | |
| 181 | $this->html = ''; |
| 182 | |
| 183 | $this->title = ''; |
| 184 | |
| 185 | $this->message = ''; |
| 186 | |
| 187 | $this->class = ''; |
| 188 | |
| 189 | $this->gutter = true; |
| 190 | |
| 191 | $this->logo = ''; |
| 192 | |
| 193 | $this->logo_style = ''; |
| 194 | |
| 195 | $this->size = array(); |
| 196 | |
| 197 | $this->button = array( |
| 198 | 'default_class' => 'button', |
| 199 | 'class' => 'button-secondary ', // button-primary button-secondary button-small button-large button-link |
| 200 | 'text' => 'Button', |
| 201 | 'url' => '#', |
| 202 | 'icon' => '', |
| 203 | ); |
| 204 | |
| 205 | $this->buttons = array(); |
| 206 | |
| 207 | return $this; |
| 208 | } |
| 209 | |
| 210 | // setters begin |
| 211 | |
| 212 | /** |
| 213 | * Adds classes to the container |
| 214 | * |
| 215 | * @param string $classname |
| 216 | * @return void |
| 217 | */ |
| 218 | public function set_class( $classname = '' ) { |
| 219 | $this->class .= $classname; |
| 220 | |
| 221 | return $this; |
| 222 | } |
| 223 | |
| 224 | public function set_type( $type = '' ) { |
| 225 | $this->class .= ' notice-' . $type; |
| 226 | |
| 227 | return $this; |
| 228 | } |
| 229 | |
| 230 | public function set_style_css( $style_css = '' ) { |
| 231 | $this->style_css = $style_css; |
| 232 | |
| 233 | return $this; |
| 234 | } |
| 235 | |
| 236 | public function set_button( $button = array() ) { |
| 237 | $button = array_merge( $this->button, $button ); |
| 238 | $this->buttons[] = $button; |
| 239 | |
| 240 | return $this; |
| 241 | } |
| 242 | |
| 243 | public function set_id( $id ) { |
| 244 | $this->notice_id = $id; |
| 245 | return $this; |
| 246 | } |
| 247 | |
| 248 | public function set_admin_only( $admin_only ) { |
| 249 | $this->$admin_only = $admin_only; |
| 250 | return $this; |
| 251 | } |
| 252 | |
| 253 | public function set_title( $title = '' ) { |
| 254 | $this->title .= $title; |
| 255 | |
| 256 | return $this; |
| 257 | } |
| 258 | |
| 259 | public function set_message( $message = '' ) { |
| 260 | $this->message .= $message; |
| 261 | |
| 262 | return $this; |
| 263 | } |
| 264 | |
| 265 | public function set_gutter( $gutter = true ) { |
| 266 | $this->gutter .= $gutter; |
| 267 | $this->class .= ( $gutter === true ? '' : ' no-gutter' ); |
| 268 | |
| 269 | return $this; |
| 270 | } |
| 271 | |
| 272 | public function set_logo( $logo = '', $logo_style = '' ) { |
| 273 | $this->logo = $logo; |
| 274 | |
| 275 | $this->logo_style = $logo_style; |
| 276 | |
| 277 | return $this; |
| 278 | } |
| 279 | |
| 280 | public function set_html( $html = '' ) { |
| 281 | $this->html .= $html; |
| 282 | |
| 283 | return $this; |
| 284 | } |
| 285 | |
| 286 | // setters ends |
| 287 | |
| 288 | |
| 289 | // group getter |
| 290 | public function get_data() { |
| 291 | return array( |
| 292 | 'message' => $this->message, |
| 293 | 'title' => $this->title, |
| 294 | 'buttons' => $this->buttons, |
| 295 | 'class' => $this->class, |
| 296 | 'html' => $this->html, |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | public function call() { |
| 304 | // check if current user is admin |
| 305 | if ( ! current_user_can( 'manage_options' ) ) { |
| 306 | return false; |
| 307 | } |
| 308 | add_action( 'admin_notices', array( $this, 'get_notice' ) ); |
| 309 | } |
| 310 | |
| 311 | public function get_notice() { |
| 312 | // dismissible conditions |
| 313 | if ( 'user' === $this->dismissible ) { |
| 314 | $expired = get_user_meta( get_current_user_id(), $this->notice_id, true ); |
| 315 | } elseif ( 'global' === $this->dismissible ) { |
| 316 | $expired = get_transient( $this->notice_id ); |
| 317 | } else { |
| 318 | $expired = ''; |
| 319 | } |
| 320 | |
| 321 | global $oxaim_lib_notice_list; |
| 322 | |
| 323 | if ( ! isset( $oxaim_lib_notice_list[ $this->notice_id ] ) ) { |
| 324 | $oxaim_lib_notice_list[ $this->notice_id ] = __FILE__; |
| 325 | |
| 326 | // is transient expired? |
| 327 | if ( false === $expired || empty( $expired ) ) { |
| 328 | $this->generate_html(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | public function set_dismiss( $scope = 'global', $time = ( 3600 * 24 * 7 ) ) { |
| 334 | $this->dismissible = $scope; |
| 335 | $this->expired_time = $time; |
| 336 | |
| 337 | return $this; |
| 338 | } |
| 339 | |
| 340 | public function generate_html() { |
| 341 | |
| 342 | ?> |
| 343 | <div |
| 344 | id="<?php echo esc_attr( $this->notice_id ); ?>" |
| 345 | class="notice wpmet-notice notice-<?php echo esc_attr( $this->notice_id . ' ' . $this->class ); ?> <?php echo ( false === $this->dismissible ? '' : 'is-dismissible' ); ?>" |
| 346 | |
| 347 | expired_time="<?php echo esc_attr( $this->expired_time ); ?>" |
| 348 | dismissible="<?php echo esc_attr( $this->dismissible ); ?>" |
| 349 | > |
| 350 | <?php if ( ! empty( $this->logo ) ) : ?> |
| 351 | <img class="notice-logo" style="<?php echo esc_attr( $this->logo_style ); ?>" src="<?php echo esc_url( $this->logo ); ?>" /> |
| 352 | <?php endif; ?> |
| 353 | |
| 354 | <div class="notice-right-container <?php echo ( empty( $this->logo ) ? 'notice-container-full-width' : '' ); ?>"> |
| 355 | |
| 356 | <style> |
| 357 | <?php echo esc_html( $this->style_css); ?> |
| 358 | </style> |
| 359 | |
| 360 | <?php if ( empty( $this->html ) ) : ?> |
| 361 | <?php echo ( empty( $this->title ) ? '' : sprintf( '<div class="notice-main-title notice-vert-space">%s</div>', esc_html($this->title) ) ); ?> |
| 362 | |
| 363 | <div class="notice-message notice-vert-space"> |
| 364 | <?php echo wp_kses( $this->message, $this->get_kses_array() ); ?> |
| 365 | </div> |
| 366 | |
| 367 | <?php if ( ! empty( $this->buttons ) ) : ?> |
| 368 | <div class="button-container notice-vert-space"> |
| 369 | <?php foreach ( $this->buttons as $button ) : ?> |
| 370 | <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'] ); ?>"> |
| 371 | <?php if ( ! empty( $button['icon'] ) ) : ?> |
| 372 | <i class="notice-icon <?php echo esc_attr( $button['icon'] ); ?>"></i> |
| 373 | <?php endif; ?> |
| 374 | <?php echo esc_html( $button['text'] ); ?> |
| 375 | </a> |
| 376 | |
| 377 | <?php endforeach; ?> |
| 378 | </div> |
| 379 | <?php endif; ?> |
| 380 | |
| 381 | <?php else : ?> |
| 382 | <?php echo wp_kses( $this->html, $this->get_kses_array() ); ?> |
| 383 | <?php endif; ?> |
| 384 | |
| 385 | </div> |
| 386 | |
| 387 | <?php if ( false !== $this->dismissible ) : ?> |
| 388 | <button type="button" class="notice-dismiss"> |
| 389 | <span class="screen-reader-text">x</span> |
| 390 | </button> |
| 391 | <?php endif; ?> |
| 392 | |
| 393 | <div style="clear:both"></div> |
| 394 | |
| 395 | </div> |
| 396 | <?php |
| 397 | } |
| 398 | |
| 399 | public static function init() { |
| 400 | add_action( 'wp_ajax_wpmet-notices', array( __CLASS__, 'dismiss_ajax_call' ) ); |
| 401 | add_action( 'admin_head', array( __CLASS__, 'enqueue_scripts' ) ); |
| 402 | } |
| 403 | |
| 404 | public static function dismiss_ajax_call() { |
| 405 | if( empty( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpmet-notices' ) ){ |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // check if current user is admin |
| 410 | if ( ! current_user_can( 'manage_options' ) ) { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | $notice_id = ( isset( $_POST['notice_id'] ) ) ? sanitize_text_field( wp_unslash( $_POST['notice_id'] ) ) : ''; |
| 415 | $dismissible = ( isset( $_POST['dismissible'] ) ) ? sanitize_text_field( wp_unslash( $_POST['dismissible'] ) ) : ''; |
| 416 | $expired_time = ( isset( $_POST['expired_time'] ) ) ? sanitize_text_field( wp_unslash( $_POST['expired_time'] ) ) : ''; |
| 417 | |
| 418 | if ( ! empty( $notice_id ) ) { |
| 419 | if ( 'user' === $dismissible ) { |
| 420 | update_user_meta( get_current_user_id(), $notice_id, true ); |
| 421 | } else { |
| 422 | set_transient( $notice_id, true, $expired_time ); |
| 423 | } |
| 424 | |
| 425 | if( $notice_id == 'elementskit-lite-edit_with_emailkit_banner' ) { |
| 426 | $counter = get_option('elementskit-lite-edit_with_emailkit_banner_dismissed_'.get_current_user_id(), 0); |
| 427 | update_option('elementskit-lite-edit_with_emailkit_banner_dismissed_'.get_current_user_id(), $counter+1); |
| 428 | } |
| 429 | |
| 430 | wp_send_json_success(); |
| 431 | } |
| 432 | |
| 433 | wp_send_json_error(); |
| 434 | } |
| 435 | |
| 436 | public static function enqueue_scripts() { |
| 437 | // check if current user is admin |
| 438 | if ( ! current_user_can( 'manage_options' ) ) { |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | echo " |
| 443 | <script> |
| 444 | jQuery(document).ready(function ($) { |
| 445 | $( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() { |
| 446 | |
| 447 | _this = $( this ).parents('.wpmet-notice').eq(0); |
| 448 | var notice_id = _this.attr( 'id' ) || ''; |
| 449 | var expired_time = _this.attr( 'expired_time' ) || ''; |
| 450 | var dismissible = _this.attr( 'dismissible' ) || ''; |
| 451 | var x = $( this ).attr('class'); |
| 452 | |
| 453 | // console.log({ |
| 454 | // _this, x, notice_id, expired_time, dismissible |
| 455 | // }); |
| 456 | // return; |
| 457 | |
| 458 | _this.hide(); |
| 459 | |
| 460 | $.ajax({ |
| 461 | url: ajaxurl, |
| 462 | type: 'POST', |
| 463 | data: { |
| 464 | action : 'wpmet-notices', |
| 465 | notice_id : notice_id, |
| 466 | dismissible : dismissible, |
| 467 | expired_time : expired_time, |
| 468 | nonce : '" . esc_js( wp_create_nonce( 'wpmet-notices' ) ) . "' |
| 469 | }, |
| 470 | }); |
| 471 | }); |
| 472 | }); |
| 473 | </script> |
| 474 | <style> |
| 475 | .wpmet-notice{ |
| 476 | margin-bottom: 15px; |
| 477 | padding: 0!important; |
| 478 | display: flex; |
| 479 | flex-direction: row; |
| 480 | justify-content: flex-start; |
| 481 | align-items: center; |
| 482 | } |
| 483 | |
| 484 | .wpmet-notice .notice-right-container{ |
| 485 | margin: .7rem .8rem .8rem; |
| 486 | } |
| 487 | |
| 488 | .notice-container-full-width{ |
| 489 | width:100%!important; |
| 490 | } |
| 491 | |
| 492 | .wpmet-notice.no-gutter{ |
| 493 | padding: 0!important; |
| 494 | border-width: 0!important; |
| 495 | } |
| 496 | .wpmet-notice.no-gutter .notice-right-container{ |
| 497 | padding: 0!important; |
| 498 | margin: 0!important; |
| 499 | } |
| 500 | |
| 501 | .notice-right-container .notice-vert-space{ |
| 502 | margin-bottom: .8rem; |
| 503 | } |
| 504 | |
| 505 | .notice-right-container .notice-vert-space:last-child, |
| 506 | .notice-right-container .notice-vert-space:only-child{ |
| 507 | margin-bottom: 0; |
| 508 | } |
| 509 | |
| 510 | .wpmet-notice .notice-logo{ |
| 511 | padding: 3px; |
| 512 | max-width: 110px; |
| 513 | max-height: 110px; |
| 514 | } |
| 515 | |
| 516 | .wpmet-notice-button { |
| 517 | text-decoration:none; |
| 518 | } |
| 519 | |
| 520 | .wpmet-notice-button > i{ |
| 521 | margin-right: 3px; |
| 522 | } |
| 523 | |
| 524 | .wpmet-notice-button .notice-icon{ |
| 525 | display:inline-block; |
| 526 | } |
| 527 | |
| 528 | .wpmet-notice-button .notice-icon:before{ |
| 529 | vertical-align: middle!important; |
| 530 | margin-top: -1px; |
| 531 | } |
| 532 | |
| 533 | .wpmet-notice .notice-main-title{ |
| 534 | color: #1d2327; |
| 535 | font-size: 1.2rem; |
| 536 | } |
| 537 | |
| 538 | </style> |
| 539 | "; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | private static $instance; |
| 544 | |
| 545 | /** |
| 546 | * Method: instance -> Return Notice module class instance |
| 547 | * |
| 548 | * @param string|null $text_domain |
| 549 | * @param string|null $unique_id |
| 550 | * @return mixed |
| 551 | */ |
| 552 | public static function instance( $text_domain = null, $unique_id = null ) { |
| 553 | if ( $text_domain == null ) { |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | self::$instance = new self(); |
| 558 | |
| 559 | return self::$instance->config( $text_domain, ( is_null( $unique_id ) ? uniqid() : $unique_id ) ); |
| 560 | } |
| 561 | |
| 562 | public function get_kses_array(){ |
| 563 | return array( |
| 564 | 'a' => array( |
| 565 | 'class' => array(), |
| 566 | 'href' => array(), |
| 567 | 'rel' => array(), |
| 568 | 'title' => array(), |
| 569 | 'target' => array(), |
| 570 | 'style' => array(), |
| 571 | ), |
| 572 | 'b' => array( |
| 573 | 'class' => array(), |
| 574 | 'style' => array(), |
| 575 | ), |
| 576 | 'div' => array( |
| 577 | 'class' => array(), |
| 578 | 'title' => array(), |
| 579 | 'style' => array(), |
| 580 | ), |
| 581 | 'strong' => array( |
| 582 | 'class' => array(), |
| 583 | 'style' => array(), |
| 584 | ), |
| 585 | 'h1' => array( |
| 586 | 'class' => array(), |
| 587 | 'style' => array(), |
| 588 | ), |
| 589 | 'h2' => array( |
| 590 | 'class' => array(), |
| 591 | 'style' => array(), |
| 592 | ), |
| 593 | 'h3' => array( |
| 594 | 'class' => array(), |
| 595 | 'style' => array(), |
| 596 | ), |
| 597 | 'h4' => array( |
| 598 | 'class' => array(), |
| 599 | 'style' => array(), |
| 600 | ), |
| 601 | 'i' => array( |
| 602 | 'class' => array(), |
| 603 | 'style' => array(), |
| 604 | ), |
| 605 | 'img' => array( |
| 606 | 'alt' => array(), |
| 607 | 'class' => array(), |
| 608 | 'height' => array(), |
| 609 | 'src' => array(), |
| 610 | 'width' => array(), |
| 611 | 'style' => array(), |
| 612 | 'title' => array(), |
| 613 | 'srcset' => array(), |
| 614 | 'loading' => array(), |
| 615 | 'sizes' => array(), |
| 616 | ), |
| 617 | 'figure' => array( |
| 618 | 'class' => array(), |
| 619 | 'style' => array(), |
| 620 | ), |
| 621 | 'li' => array( |
| 622 | 'class' => array(), |
| 623 | 'style' => array(), |
| 624 | ), |
| 625 | 'ol' => array( |
| 626 | 'class' => array(), |
| 627 | 'style' => array(), |
| 628 | ), |
| 629 | 'p' => array( |
| 630 | 'class' => array(), |
| 631 | 'style' => array(), |
| 632 | ), |
| 633 | 'span' => array( |
| 634 | 'class' => array(), |
| 635 | 'title' => array(), |
| 636 | 'style' => array(), |
| 637 | ), |
| 638 | 'iframe' => array( |
| 639 | 'width' => array(), |
| 640 | 'height' => array(), |
| 641 | 'scrolling' => array(), |
| 642 | 'frameborder' => array(), |
| 643 | 'allow' => array(), |
| 644 | 'src' => array(), |
| 645 | 'class' => array(), |
| 646 | 'style' => array(), |
| 647 | ), |
| 648 | 'strike' => array(), |
| 649 | 'br' => array(), |
| 650 | 'table' => array(), |
| 651 | 'ul' => array( |
| 652 | 'class' => array(), |
| 653 | 'style' => array(), |
| 654 | ), |
| 655 | 'svg' => array( |
| 656 | 'class' => true, |
| 657 | 'aria-hidden' => true, |
| 658 | 'aria-labelledby' => true, |
| 659 | 'role' => true, |
| 660 | 'xmlns' => true, |
| 661 | 'width' => true, |
| 662 | 'height' => true, |
| 663 | 'viewbox' => true, // <= Must be lower case! |
| 664 | 'preserveaspectratio' => true, |
| 665 | ) |
| 666 | ); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | endif; |
| 671 |