PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.2
EmailKit – Email Customizer for WooCommerce & WP v1.5.2
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Emails / Helpers / Notice / Notice.php

Notice.php in EmailKit – Email Customizer for WooCommerce & WP 1.5.2, at includes/Admin/Emails/Helpers/Notice/Notice.php

199 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EmailKit\Admin\Emails\Helpers\Notice;
3
4 use EmailKit;
5
6 if ( ! defined( 'ABSPATH' ) ) die( 'Forbidden' );
7
8
9 class Notice {
10
11 /**
12 * Constructor
13 *
14 * @since 1.0.0
15 */
16 public function __construct() {
17 add_action( 'admin_footer', [ $this, 'enqueue_scripts' ], 9999);
18 add_action( 'wp_ajax_emailkit-notices', [ $this, 'dismiss' ] );
19 }
20
21
22 /**
23 * Dismiss Notice.
24 *
25 * @since 1.0.0
26 * @return void
27 */
28 public function dismiss() {
29
30 if(empty($_POST['emailkit_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['emailkit_nonce'])), 'emailkit_nonce')) {
31 wp_send_json_error();
32 }
33
34 $id = ( isset( $_POST['id'] ) ) ? sanitize_key($_POST['id']) : '';
35 $time = ( isset( $_POST['time'] ) ) ? sanitize_text_field(wp_unslash($_POST['time'])) : '';
36 $meta = ( isset( $_POST['meta'] ) ) ? sanitize_key($_POST['meta']) : '';
37 $is_required = ( isset( $_POST['is_required'] ) ) ? sanitize_key($_POST['is_required']) : 0;
38
39
40 if($is_required == 1){
41 return;
42 }
43
44 // Valid inputs?
45 if ( ! empty( $id ) ) {
46
47 if ( 'user' === $meta ) {
48 update_user_meta( get_current_user_id(), $id, true );
49 } else {
50 set_transient( $id, true, $time );
51 }
52
53 wp_send_json_success();
54 }
55
56 wp_send_json_error();
57 }
58
59 /**
60 * Enqueue Scripts.
61 *
62 * @since 1.0.0
63 * @return void
64 */
65 public function enqueue_scripts() {
66 echo "
67 <script>
68 jQuery(document).ready(function ($) {
69 $( '.emailkit-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
70 _this = $( this ).parents( '.emailkit-active-notice' );
71 var id = _this.attr( 'id' ) || '';
72 var time = _this.attr( 'dismissible-time' ) || '';
73 var meta = _this.attr( 'dismissible-meta' ) || '';
74 let is_required = _this.attr( 'is-required' ) || 0;
75
76 $.ajax({
77 url: ajaxurl,
78 type: 'POST',
79 data: {
80 action : 'emailkit-notices',
81 id : id,
82 meta : meta,
83 time : time,
84 is_required: is_required,
85 emailkit_nonce: '".esc_html(wp_create_nonce( 'emailkit_nonce' ))."'
86 },
87 });
88
89 });
90
91 });
92 </script>
93 ";
94 }
95
96 /**
97 * Show Notices
98 *
99 * @since 1.0.0
100 * @return void
101 */
102 public static function push($notice) {
103
104 $defaults = [
105 'id' => '',
106 'type' => 'info',
107 'show_if' => true,
108 'message' => '',
109 'class' => 'emailkit-active-notice',
110 'dismissible' => false,
111 'btn' => [],
112 'dismissible-meta' => 'user',
113 'is_required' => false,
114 'dismissible-time' => WEEK_IN_SECONDS,
115 'data' => '',
116 ];
117
118 $notice = wp_parse_args( $notice, $defaults );
119
120 $classes = [ 'emailkit-notice', 'notice' ];
121
122 $classes[] = $notice['class'];
123 if ( isset( $notice['type'] ) ) {
124 $classes[] = 'notice-' . $notice['type'];
125 }
126
127 // Is notice dismissible?
128 if ( true === $notice['dismissible'] ) {
129 $classes[] = 'is-dismissible';
130
131 // Dismissable time.
132 $notice['data'] = ' dismissible-time=' . esc_attr( $notice['dismissible-time'] ) . ' ';
133 }
134
135 // Notice ID.
136 $notice_id = 'emailkit-sites-notice-id-' . $notice['id'];
137 $notice['id'] = $notice_id;
138 if ( ! isset( $notice['id'] ) ) {
139 $notice_id = 'emailkit-sites-notice-id-' . $notice['id'];
140 $notice['id'] = $notice_id;
141 } else {
142 $notice_id = $notice['id'];
143 }
144
145 $notice['classes'] = implode( ' ', $classes );
146
147 // User meta.
148 $notice['data'] .= ' dismissible-meta=' . esc_attr( $notice['dismissible-meta'] ) . ' ';
149 if ( 'user' === $notice['dismissible-meta'] ) {
150 $expired = get_user_meta( get_current_user_id(), $notice_id, true );
151 } elseif ( 'transient' === $notice['dismissible-meta'] ) {
152 $expired = get_transient( $notice_id );
153 }
154
155 // Is required plugin?
156 if ( isset( $notice['is_required'] ) && true === $notice['is_required'] ) {
157 $notice['data'] .= "' is-required=1";
158 }
159
160 // Notice visible after transient expire.
161 if ( isset( $notice['show_if'] ) ) {
162 if ( true === $notice['show_if'] ) {
163
164 // Is transient expired?
165 if ( false === $expired || empty( $expired ) ) {
166 self::markup($notice);
167 }
168 }
169 } else {
170 self::markup($notice);
171 }
172 }
173
174 /**
175 * Markup Notice.
176 *
177 * @since 1.0.0
178 * @param array $notice Notice markup.
179 * @return void
180 */
181 public static function markup( $notice = [] ) {
182 ?>
183 <div id="<?php echo esc_attr( $notice['id'] ); ?>" class="<?php echo esc_attr( $notice['classes'] ); ?>">
184 <p>
185 <?php echo wp_kses($notice['message'], \EmailKit\Admin\Emails\Helpers\Utils::get_kses_array()); ?>
186 </p>
187
188 <?php if(!empty($notice['btn'])):?>
189 <p>
190 <a title="<?php esc_html_e('Notification','emailkit')?>" href="<?php echo esc_url($notice['btn']['url']); ?>" class="button-primary"><?php echo esc_html($notice['btn']['label']); ?></a>
191 </p>
192 <?php endif; ?>
193 </div>
194 <?php
195 }
196 }
197
198 new Notice();
199