PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 2.3.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v2.3.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 / utils / notice.php
shopengine / utils Last commit date
controls-helper.php 3 years ago elementor-data-map.php 3 years ago helper.php 3 years ago notice.php 3 years ago shipping-calculation.php 3 years ago
notice.php
182 lines
1 <?php
2 namespace ShopEngine\Utils;
3
4 if ( ! defined( 'ABSPATH' ) ) die( 'Forbidden' );
5
6 /**
7 * shopengine-builder notice class.
8 * Handles dynamically notices for lazy developers.
9 *
10 * @since 1.0.0
11 */
12 class Notice {
13
14 /**
15 * Constructor
16 *
17 * @since 1.0.0
18 */
19 public function __construct() {
20 add_action( 'admin_footer', [ $this, 'enqueue_scripts' ], 9999);
21 add_action( 'wp_ajax_shopengine-notices', [ $this, 'dismiss' ] );
22 }
23
24
25 /**
26 * Dismiss Notice.
27 *
28 * @since 1.0.0
29 * @return void
30 */
31 public function dismiss() {
32
33 $id = ( isset( $_POST['id'] ) ) ? sanitize_key($_POST['id']) : '';
34 $time = ( isset( $_POST['time'] ) ) ? sanitize_text_field($_POST['time']) : '';
35 $meta = ( isset( $_POST['meta'] ) ) ? sanitize_key($_POST['meta']) : '';
36
37 // Valid inputs?
38 if ( ! empty( $id ) ) {
39
40 if ( 'user' === $meta ) {
41 update_user_meta( get_current_user_id(), $id, true );
42 } else {
43 set_transient( $id, true, $time );
44 }
45
46 wp_send_json_success();
47 }
48
49 wp_send_json_error();
50 }
51
52 /**
53 * Enqueue Scripts.
54 *
55 * @since 1.0.0
56 * @return void
57 */
58 public function enqueue_scripts() {
59 echo "
60 <script>
61 jQuery(document).ready(function ($) {
62 $( '.shopengine-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
63 _this = $( this ).parents( '.shopengine-active-notice' );
64 var id = _this.attr( 'id' ) || '';
65 var time = _this.attr( 'dismissible-time' ) || '';
66 var meta = _this.attr( 'dismissible-meta' ) || '';
67
68 $.ajax({
69 url: ajaxurl,
70 type: 'POST',
71 data: {
72 action : 'shopengine-notices',
73 id : id,
74 meta : meta,
75 time : time,
76 },
77 });
78
79 });
80
81 });
82 </script>
83 ";
84 }
85
86 /**
87 * Show Notices
88 *
89 * @since 1.0.0
90 * @return void
91 */
92 public static function push($notice) {
93
94 $defaults = [
95 'id' => '',
96 'type' => 'info',
97 'show_if' => true,
98 'message' => '',
99 'class' => 'shopengine-active-notice',
100 'dismissible' => false,
101 'btn' => [],
102 'dismissible-meta' => 'user',
103 'dismissible-time' => WEEK_IN_SECONDS,
104 'data' => '',
105 ];
106
107 $notice = wp_parse_args( $notice, $defaults );
108
109 $classes = [ 'shopengine-notice', 'notice' ];
110
111 $classes[] = $notice['class'];
112 if ( isset( $notice['type'] ) ) {
113 $classes[] = 'notice-' . $notice['type'];
114 }
115
116 // Is notice dismissible?
117 if ( true === $notice['dismissible'] ) {
118 $classes[] = 'is-dismissible';
119
120 // Dismissable time.
121 $notice['data'] = ' dismissible-time=' . esc_attr( $notice['dismissible-time'] ) . ' ';
122 }
123
124 // Notice ID.
125 $notice_id = 'shopengine-sites-notice-id-' . $notice['id'];
126 $notice['id'] = $notice_id;
127 if ( ! isset( $notice['id'] ) ) {
128 $notice_id = 'shopengine-sites-notice-id-' . $notice['id'];
129 $notice['id'] = $notice_id;
130 } else {
131 $notice_id = $notice['id'];
132 }
133
134 $notice['classes'] = implode( ' ', $classes );
135
136 // User meta.
137 $notice['data'] .= ' dismissible-meta=' . esc_attr( $notice['dismissible-meta'] ) . ' ';
138 if ( 'user' === $notice['dismissible-meta'] ) {
139 $expired = get_user_meta( get_current_user_id(), $notice_id, true );
140 } elseif ( 'transient' === $notice['dismissible-meta'] ) {
141 $expired = get_transient( $notice_id );
142 }
143
144 // Notice visible after transient expire.
145 if ( isset( $notice['show_if'] ) ) {
146 if ( true === $notice['show_if'] ) {
147
148 // Is transient expired?
149 if ( false === $expired || empty( $expired ) ) {
150 self::markup($notice);
151 }
152 }
153 } else {
154 self::markup($notice);
155 }
156 }
157
158 /**
159 * Markup Notice.
160 *
161 * @since 1.0.0
162 * @param array $notice Notice markup.
163 * @return void
164 */
165 public static function markup( $notice = [] ) {
166 ?>
167 <div id="<?php echo esc_attr( $notice['id'] ); ?>" class="<?php echo esc_attr( $notice['classes'] ); ?>" <?php echo Helper::render($notice['data']); ?>>
168 <p>
169 <?php echo Helper::kses($notice['message']); ?>
170 </p>
171
172 <?php if(!empty($notice['btn'])):?>
173 <p>
174 <a href="<?php echo esc_url($notice['btn']['url']); ?>" class="button-primary"><?php echo esc_html($notice['btn']['label']); ?></a>
175 </p>
176 <?php endif; ?>
177 </div>
178 <?php
179 }
180 }
181
182 new Notice();