PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / trunk / vendor / arraytics / plugin-notice-sdk / src / Notice.php

Notice.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at trunk/vendor/arraytics/plugin-notice-sdk/src/Notice.php

475 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Arraytics\PluginNotice;
4
5 class Notice
6 {
7 /**
8 * Singleton instance of the Notice class.
9 *
10 * @var self|null
11 */
12 protected static $instance;
13
14 /**
15 * Plugin version (set from main plugin).
16 *
17 * @var string
18 */
19 protected $version = '1.0.0'; // default fallback
20
21 /**
22 * Unique identifier for the notice.
23 *
24 * @var string
25 */
26 protected $notice_id;
27
28 /**
29 * Text domain of the plugin.
30 *
31 * @var string
32 */
33 protected $text_domain;
34
35 /**
36 * Unique ID used internally.
37 *
38 * @var string
39 */
40 protected $unique_id;
41
42 /**
43 * Additional CSS classes for the notice container.
44 *
45 * @var string
46 */
47 protected $class = '';
48
49 /**
50 * Default button configuration.
51 *
52 * @var array
53 */
54 protected $button;
55
56 /**
57 * Size-specific settings (currently unused).
58 *
59 * @var array
60 */
61 protected $size = [];
62
63 /**
64 * List of buttons added to the notice.
65 *
66 * @var array
67 */
68 protected $buttons = [];
69
70 /**
71 * Title of the notice.
72 *
73 * @var string
74 */
75 protected $title = '';
76
77 /**
78 * Message body of the notice.
79 *
80 * @var string
81 */
82 protected $message = '';
83
84 /**
85 * URL of the left-side logo image.
86 *
87 * @var string
88 */
89 protected $logo = '';
90
91 /**
92 * Whether the notice container should have gutter (padding).
93 *
94 * @var string
95 */
96 protected $gutter = '';
97
98 /**
99 * Inline style for the logo image.
100 *
101 * @var string
102 */
103 protected $logo_style = '';
104
105 /**
106 * Dismissible type: false, 'user', or 'global'.
107 *
108 * @var string|bool
109 */
110 protected $dismissible = false;
111
112 /**
113 * Expiration time in seconds for the dismissible notice.
114 *
115 * @var int
116 */
117 protected $expired_time = 1;
118
119 /**
120 * Raw HTML content to override default markup.
121 *
122 * @var string
123 */
124 protected $html = '';
125
126 /**
127 * Get an instance of the Notice class.
128 *
129 * @param string|null $text_domain
130 * @param string|null $unique_id
131 * @return self|false
132 */
133 public static function instance($text_domain = null, $unique_id = null)
134 {
135 if (!$text_domain) {
136 return false;
137 }
138 self::$instance = new self();
139 return self::$instance->config($text_domain, $unique_id ?: uniqid());
140 }
141
142 /**
143 * Initialize the dismiss AJAX and enqueue scripts.
144 *
145 * @return void
146 */
147 public static function init()
148 {
149 add_action('wp_ajax_wpmet-notices', [__CLASS__, 'dismiss_ajax_call']);
150 }
151
152 /**
153 * Configure basic properties of the notice.
154 *
155 * @param string $text_domain
156 * @param string $unique_id
157 * @return $this
158 */
159 public function config($text_domain, $unique_id)
160 {
161 $this->text_domain = $text_domain;
162 $this->unique_id = $unique_id;
163 $this->notice_id = "$text_domain-$unique_id";
164 $this->dismissible = false;
165 $this->expired_time = 1;
166
167 $this->button = [
168 'default_class' => 'button',
169 'class' => 'button-secondary',
170 'text' => 'Button',
171 'url' => '#',
172 'icon' => '',
173 ];
174
175 return $this;
176 }
177
178 /**
179 * Set additional CSS classes.
180 *
181 * @param string $classname
182 * @return $this
183 */
184 public function set_class($classname = '')
185 {
186 $this->class .= $classname;
187 return $this;
188 }
189
190 /**
191 * Set the notice type.
192 *
193 * @param string $type
194 * @return $this
195 */
196 public function set_type($type = '')
197 {
198 $this->class .= " notice-$type";
199 return $this;
200 }
201
202 /**
203 * Add a button to the notice.
204 *
205 * @param array $button
206 * @return $this
207 */
208 public function set_button($button = [])
209 {
210 $this->buttons[] = array_merge($this->button, $button);
211 return $this;
212 }
213
214 /**
215 * Set a custom ID for the notice.
216 *
217 * @param string $id
218 * @return $this
219 */
220 public function set_id($id)
221 {
222 $this->notice_id = $id;
223 return $this;
224 }
225
226 /**
227 * Set the title of the notice.
228 *
229 * @param string $title
230 * @return $this
231 */
232 public function set_title($title = '')
233 {
234 $this->title .= $title;
235 return $this;
236 }
237
238 /**
239 * Set the message of the notice.
240 *
241 * @param string $message
242 * @return $this
243 */
244 public function set_message($message = '')
245 {
246 $this->message .= $message;
247 return $this;
248 }
249
250 /**
251 * Enable or disable gutter spacing.
252 *
253 * @param bool $gutter
254 * @return $this
255 */
256 public function set_gutter($gutter = true)
257 {
258 $this->gutter = $gutter;
259 $this->class .= $gutter ? '' : ' no-gutter';
260 return $this;
261 }
262
263 /**
264 * Set logo and style.
265 *
266 * @param string $logo
267 * @param string $logo_style
268 * @return $this
269 */
270 public function set_logo($logo = '', $logo_style = '')
271 {
272 $this->logo = $logo;
273 $this->logo_style = $logo_style;
274 return $this;
275 }
276
277 /**
278 * Set custom HTML content.
279 *
280 * @param string $html
281 * @return $this
282 */
283 public function set_html($html = '')
284 {
285 $this->html .= $html;
286 return $this;
287 }
288
289 /**
290 * Configure dismiss behavior.
291 *
292 * @param string $scope 'user' or 'global'
293 * @param int $time Expiry time in seconds
294 * @return $this
295 */
296 public function set_dismiss($scope = 'global', $time = 604800)
297 {
298 $this->dismissible = $scope;
299 $this->expired_time = $time;
300 return $this;
301 }
302
303 /**
304 * Get the compiled data for rendering.
305 *
306 * @return array
307 */
308 public function get_data()
309 {
310 return [
311 'message' => $this->message,
312 'title' => $this->title,
313 'buttons' => $this->buttons,
314 'class' => $this->class,
315 'html' => $this->html,
316 ];
317 }
318
319 /**
320 * Add the notice to the WordPress admin_notices hook.
321 *
322 * @return void
323 */
324 public function call()
325 {
326 add_action('admin_notices', [$this, 'get_notice']);
327 }
328
329 /**
330 * Display the notice HTML based on conditions.
331 *
332 * @return void
333 */
334 public function get_notice()
335 {
336 switch ($this->dismissible) {
337 case 'user':
338 $expired = get_user_meta(get_current_user_id(), $this->notice_id, true);
339 break;
340 case 'global':
341 $expired = get_transient($this->notice_id);
342 break;
343 default:
344 $expired = '';
345 break;
346 }
347
348 global $notice_list;
349 $notice_list = isset($notice_list) ? $notice_list : [];
350
351 if (!isset($notice_list[$this->notice_id]) && (false === $expired || empty($expired))) {
352 $notice_list[$this->notice_id] = __FILE__;
353 $this->generate_html();
354 }
355 }
356
357 /**
358 * Render the HTML for the notice.
359 *
360 * @return void
361 */
362 public function generate_html()
363 {
364 ?>
365 <div
366 id="<?php echo esc_attr($this->notice_id); ?>"
367 class="notice wpmet-notice notice-<?php echo esc_attr($this->notice_id . ' ' . $this->class); ?> <?php echo $this->dismissible === false ? '' : 'is-dismissible'; ?>"
368 expired_time="<?php echo esc_attr($this->expired_time); ?>"
369 dismissible="<?php echo esc_attr($this->dismissible); ?>"
370 >
371 <?php if (!empty($this->logo)) : ?>
372 <img class="notice-logo" style="<?php echo esc_attr($this->logo_style); ?>" src="<?php echo esc_url($this->logo); ?>" />
373 <?php endif; ?>
374
375 <div class="notice-right-container <?php echo empty($this->logo) ? 'notice-container-full-width' : ''; ?>">
376 <?php if (empty($this->html)) : ?>
377 <?php if (!empty($this->title)) : ?>
378 <div class="notice-main-title notice-vert-space"><?php echo esc_html($this->title); ?></div>
379 <?php endif; ?>
380
381 <div class="notice-message notice-vert-space">
382 <?php echo wp_kses_post($this->message); ?>
383 </div>
384
385 <?php if (!empty($this->buttons)) : ?>
386 <div class="button-container notice-vert-space">
387 <?php foreach ($this->buttons as $button) : ?>
388 <a id="<?php echo isset($button['id']) ? esc_attr($button['id']) : ''; ?>"
389 href="<?php echo esc_url($button['url']); ?>"
390 class="wpmet-notice-button <?php echo esc_attr($button['class']); ?>">
391 <?php if (!empty($button['icon'])) : ?>
392 <i class="notice-icon <?php echo esc_attr($button['icon']); ?>"></i>
393 <?php endif; ?>
394 <?php echo esc_html($button['text']); ?>
395 </a>&nbsp;
396 <?php endforeach; ?>
397 </div>
398 <?php endif; ?>
399 <?php else : ?>
400 <?php echo wp_kses_post($this->html); ?>
401 <?php endif; ?>
402 </div>
403
404 <?php if ($this->dismissible !== false) : ?>
405 <button type="button" class="notice-dismiss">
406 <span class="screen-reader-text">x#dismiss</span>
407 </button>
408 <?php endif; ?>
409
410 <div style="clear:both"></div>
411 </div>
412 <?php
413 }
414
415 /**
416 * Handle AJAX dismissal request.
417 *
418 * @return void
419 */
420 public static function dismiss_ajax_call()
421 {
422 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
423 if (empty($nonce) || !wp_verify_nonce($nonce, 'wpmet-notices')) {
424 wp_send_json_error();
425 }
426
427 $notice_id = isset($_POST['notice_id']) ? sanitize_text_field(wp_unslash($_POST['notice_id'])) : '';
428 $dismissible = isset($_POST['dismissible']) ? sanitize_text_field(wp_unslash($_POST['dismissible'])) : '';
429 $expired = isset($_POST['expired_time']) ? (int) sanitize_text_field(wp_unslash($_POST['expired_time'])) : 0;
430
431 if ($notice_id) {
432 if ($dismissible === 'user') {
433 update_user_meta(get_current_user_id(), $notice_id, true);
434 } else {
435 set_transient($notice_id, true, $expired);
436 }
437 wp_send_json_success();
438 }
439
440 wp_send_json_error();
441 }
442
443 /**
444 * Set the plugin version dynamically.
445 *
446 * @param string $version
447 * @return $this
448 */
449 public function set_version($version)
450 {
451 $this->version = $version;
452 return $this;
453 }
454
455 /**
456 * Get plugin version.
457 *
458 * @return string
459 */
460 public function get_version()
461 {
462 return $this->version;
463 }
464
465 /**
466 * Get file location of this script.
467 *
468 * @return string
469 */
470 public function get_script_location()
471 {
472 return __FILE__;
473 }
474 }
475