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