PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.2.8
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.2.8
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / admin / controllers / class-notice-controller.php
superb-blocks / src / admin / controllers Last commit date
class-dashboard-controller.php 2 years ago class-notice-controller.php 2 years ago class-settings-controller.php 2 years ago class-troubleshooting-controller.php 2 years ago
class-notice-controller.php
186 lines
1 <?php
2
3 namespace SuperbAddons\Admin\Controllers;
4
5 defined('ABSPATH') || exit();
6
7 // 1.0
8
9 class AdminNoticeController
10 {
11 const PREFIX = 'spbaddons_notice_';
12 const PREFIX_DELAY = 'spbaddons_notice_delay_';
13
14 const ALLOWED_HTML = [
15 'div' => [
16 'class' => [],
17 ],
18 'p' => [
19 'class' => [],
20 ],
21 'h2' => [
22 'class' => [],
23 ],
24 'ul' => [
25 'class' => [],
26 ],
27 'li' => [
28 'class' => [],
29 ],
30 'span' => [
31 'class' => [],
32 ],
33 'a' => [
34 'class' => [],
35 'href' => [],
36 'rel' => [],
37 'target' => [],
38 ],
39 'em' => [
40 'class' => [],
41 ],
42 'strong' => [
43 'class' => [],
44 ],
45 'img' => [
46 'class' => [],
47 'alt' => [],
48 'src' => [],
49 'width' => [],
50 'height' => [],
51 ],
52 'br' => [],
53 'style' => [],
54 ];
55
56 private static $notices = [];
57
58 public static function init($options)
59 {
60 $notices = [];
61 if (isset($options['notices']) && is_array($options['notices'])) {
62 foreach ($options['notices'] as $notice) {
63 if (!isset($notice['unique_id']) || !isset($notice['content'])) {
64 continue;
65 }
66
67 $notices[] = $notice;
68 }
69 }
70
71 self::$notices = $notices;
72
73 add_action('admin_notices', array(__CLASS__, 'AdminNotices'));
74 add_action('wp_ajax_spbtic_dismiss_notice', array(__CLASS__, 'MaybeDismissNotice'));
75 }
76
77 public static function AdminNotices()
78 {
79 foreach (self::$notices as $notice) {
80 $notice_path = trailingslashit(SUPERBADDONS_PLUGIN_DIR) . 'src/admin/notices/' . $notice['content'];
81 if (!file_exists($notice_path)) {
82 continue;
83 }
84
85 // Check if the notice has been dismissed.
86 if (get_user_meta(get_current_user_id(), self::PREFIX . $notice['unique_id'], true)) {
87 continue;
88 }
89
90 // Check if the notice is delayed
91 if (isset($notice['delay'])) {
92 $delay_init = get_user_meta(get_current_user_id(), self::PREFIX_DELAY . $notice['unique_id'], true);
93 if (!$delay_init) {
94 update_user_meta(get_current_user_id(), self::PREFIX_DELAY . $notice['unique_id'], time());
95 continue;
96 }
97
98 $delay = strtotime($notice['delay'], $delay_init);
99 if ($delay > time()) {
100 continue;
101 }
102 }
103
104 ob_start();
105 include_once $notice_path;
106 $content = ob_get_clean();
107 echo wp_kses($content, self::ALLOWED_HTML);
108 }
109
110 self::PrintScripts();
111 }
112
113 public static function PrintScripts()
114 {
115 ?>
116 <script>
117 window.addEventListener("load", function() {
118 setTimeout(function() {
119 var notice_ids = <?php echo json_encode(array_column(self::$notices, 'unique_id')); ?>;
120 var nonce = "<?php echo esc_attr(wp_create_nonce('spbtic_dismiss_notice')); ?>";
121 var ajaxurl = "<?php echo esc_url(admin_url('admin-ajax.php')); ?>";
122
123 notice_ids.forEach(function(notice) {
124 var dismissBtn = document.querySelector(
125 "." + notice + " .notice-dismiss"
126 );
127
128 if (!dismissBtn) return;
129
130 // Add an event listener to the dismiss button.
131 dismissBtn.addEventListener("click", function(event) {
132 var httpRequest = new XMLHttpRequest(),
133 postData = "";
134
135 // Build the data to send in our request.
136 // Data has to be formatted as a string here.
137 postData += "id=" + notice;
138 postData += "&action=spbtic_dismiss_notice";
139 postData += "&nonce=" + nonce;
140
141 httpRequest.open("POST", ajaxurl);
142 httpRequest.setRequestHeader(
143 "Content-Type",
144 "application/x-www-form-urlencoded"
145 );
146 httpRequest.send(postData);
147 });
148 });
149 }, 0);
150 });
151 </script>
152 <?php
153 }
154
155 public static function MaybeDismissNotice()
156 {
157 // Sanity check: Early exit if we're not on a spbtic_dismiss_notice action.
158 if (!isset($_POST['action']) || 'spbtic_dismiss_notice' !== $_POST['action']) {
159 return;
160 }
161
162 // Sanity check: Early exit if the ID of the notice does not exist.
163 if (!isset($_POST['id']) || !in_array($_POST['id'], array_column(self::$notices, 'unique_id'))) {
164 return;
165 }
166
167 // Notice ID exists in array, so we can safely use it.
168 $notice_id = sanitize_text_field($_POST['id']);
169
170 // Security check: Make sure nonce is OK. check_ajax_referer exits if it fails.
171 check_ajax_referer('spbtic_dismiss_notice', 'nonce', true);
172
173 update_user_meta(get_current_user_id(), self::PREFIX . $notice_id, true);
174 }
175
176 public static function Cleanup()
177 {
178 foreach (self::$notices as $notice) {
179 delete_user_meta(get_current_user_id(), self::PREFIX . $notice['unique_id']);
180 if (isset($notice['delay'])) {
181 delete_user_meta(get_current_user_id(), self::PREFIX_DELAY . $notice['unique_id']);
182 }
183 }
184 }
185 }
186