PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 3.1.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v3.1.0
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / libs / notice / notice.php
shopengine / libs / notice Last commit date
notice.php 3 years ago
notice.php
507 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 // dismissible conditions
317 if ('user' === $this->dismissible) {
318 $expired = get_user_meta(get_current_user_id(), $this->notice_id, true);
319 } elseif ('global' === $this->dismissible) {
320 $expired = get_transient($this->notice_id);
321 } else {
322 $expired = '';
323 }
324
325 global $oxaim_lib_notice_list;
326
327 if (!isset($oxaim_lib_notice_list[$this->notice_id])) {
328 $oxaim_lib_notice_list[$this->notice_id] = __FILE__;
329
330 // is transient expired?
331 if (false === $expired || empty($expired)) {
332 $this->generate_html();
333 }
334 }
335 }
336
337 public function generate_html()
338 {
339 $this->enqueue_scripts();
340 ?>
341 <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); ?>">
342 <?php if (!empty($this->logo)) : ?>
343 <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'); ?>" />
344 <?php endif; ?>
345
346 <div class="notice-right-container <?php echo (empty($this->logo) ? 'notice-container-full-width' : ''); ?>">
347
348 <?php if (empty($this->html)) : ?>
349 <?php echo (empty($this->title) ? '' : sprintf('<div class="notice-main-title notice-vert-space">%s</div>', esc_html($this->title))); ?>
350
351 <div class="notice-message notice-vert-space">
352 <?php echo wp_kses($this->message, \ShopEngine\Utils\Helper::get_kses_array()); ?>
353 </div>
354
355 <?php if (!empty($this->buttons)) : ?>
356 <div class="button-container notice-vert-space">
357 <?php foreach ($this->buttons as $button) : ?>
358 <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']); ?>">
359 <?php if (!empty($button['icon'])) : ?>
360 <i class="notice-icon <?php echo esc_attr($button['icon']); ?>"></i>
361 <?php endif; ?>
362 <?php echo esc_html($button['text']); ?>
363 </a>
364 &nbsp;
365 <?php endforeach; ?>
366 </div>
367 <?php endif; ?>
368
369 <?php else : ?>
370 <?php echo wp_kses($this->html, \ShopEngine\Utils\Helper::get_kses_array()); ?>
371 <?php endif; ?>
372
373 </div>
374
375 <?php if (false !== $this->dismissible) : ?>
376 <button type="button" class="notice-dismiss">
377 <span class="screen-reader-text">x</span>
378 </button>
379 <?php endif; ?>
380
381 <div style="clear:both"></div>
382
383 </div>
384 <?php
385 }
386
387 public function dismiss_ajax_call()
388 {
389 if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'shopengine-notices')) {
390 return false;
391 }
392
393 $notice_id = (isset($_POST['notice_id'])) ? sanitize_text_field(wp_unslash($_POST['notice_id'])) : '';
394 $dismissible = (isset($_POST['dismissible'])) ? sanitize_text_field(wp_unslash($_POST['dismissible'])) : '';
395 $expired_time = (isset($_POST['expired_time'])) ? sanitize_text_field(wp_unslash($_POST['expired_time'])) : '';
396
397 if (!empty($notice_id)) {
398 if ('user' === $dismissible) {
399 update_user_meta(get_current_user_id(), $notice_id, true);
400 } else {
401 set_transient($notice_id, true, $expired_time);
402 }
403
404 wp_send_json_success();
405 }
406
407 wp_send_json_error();
408 }
409
410 protected function enqueue_scripts()
411 {
412 echo "
413 <script>
414 jQuery(document).ready(function ($) {
415 $( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
416
417 _this = $( this ).parents('.wpmet-notice').eq(0);
418 var notice_id = _this.attr( 'id' ) || '';
419 var expired_time = _this.attr( 'expired_time' ) || '';
420 var dismissible = _this.attr( 'dismissible' ) || '';
421 var x = $( this ).attr('class');
422
423 _this.hide();
424
425 $.ajax({
426 url: ajaxurl,
427 type: 'POST',
428 data: {
429 action : 'shopengine-notices',
430 notice_id : notice_id,
431 dismissible : dismissible,
432 expired_time : expired_time,
433 nonce : '" . esc_js(wp_create_nonce('shopengine-notices')) . "'
434 },
435 });
436 });
437 });
438 </script>
439 <style>
440 .wpmet-notice{
441 margin-bottom: 15px;
442 padding: 0!important;
443 display: flex;
444 flex-direction: row;
445 justify-content: flex-start;
446 align-items: center;
447 }
448
449 .wpmet-notice .notice-right-container{
450 margin: .7rem .8rem .8rem;
451 }
452
453 .notice-container-full-width{
454 width:100%!important;
455 }
456
457 .wpmet-notice.no-gutter{
458 padding: 0!important;
459 border-width: 0!important;
460 }
461 .wpmet-notice.no-gutter .notice-right-container{
462 padding: 0!important;
463 margin: 0!important;
464 }
465
466 .notice-right-container .notice-vert-space{
467 margin-bottom: .8rem;
468 }
469
470 .notice-right-container .notice-vert-space:last-child,
471 .notice-right-container .notice-vert-space:only-child{
472 margin-bottom: 0;
473 }
474
475 .wpmet-notice .notice-logo{
476 padding: 3px;
477 max-width: 110px;
478 max-height: 110px;
479 }
480
481 .wpmet-notice-button {
482 text-decoration:none;
483 }
484
485 .wpmet-notice-button > i{
486 margin-right: 3px;
487 }
488
489 .wpmet-notice-button .notice-icon{
490 display:inline-block;
491 }
492
493 .wpmet-notice-button .notice-icon:before{
494 vertical-align: middle!important;
495 margin-top: -1px;
496 }
497
498 .wpmet-notice .notice-main-title{
499 color: #1d2327;
500 font-size: 1.2rem;
501 }
502
503 </style>
504 ";
505 }
506 }
507