PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / 0.0.3
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager v0.0.3
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / modules / core / components / notices.php

notices.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager 0.0.3, at modules/core/components/notices.php

88 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Modules\Core\Components;
4
5 use Cookiez\Classes\Utils\Notice_Base;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Class Notices
13 *
14 * Handles the registration and display of notices.
15 */
16 class Notices {
17 const AJAX_ACTION = 'cookiez_admin_notice_dismiss';
18
19 /**
20 * @var Notice_Base[] $notices
21 */
22 public array $notices = [];
23
24 /**
25 * Register a notice.
26 *
27 * @param Notice_Base $notice_instance
28 * @return void
29 */
30 public function register_notice( Notice_Base $notice_instance ) {
31 $this->notices[ $notice_instance->get_id() ] = $notice_instance;
32 }
33
34 /**
35 * Show notices.
36 *
37 * @return void
38 */
39 public function show_notices() {
40 foreach ( $this->notices as $notice ) {
41 $notice->maybe_show_notice();
42 }
43 }
44
45 /**
46 * Handle dismiss request from the React app.
47 *
48 * @return void
49 */
50 public function handle_dismiss() {
51 if ( empty( $_REQUEST['notice_id'] ) ) {
52 wp_send_json_error( [ 'message' => 'Invalid ID' ] );
53 }
54
55 $notice = $this->get_notice( sanitize_text_field( wp_unslash( $_REQUEST['notice_id'] ) ) );
56 if ( ! $notice ) {
57 wp_send_json_error( [ 'message' => 'Invalid ID' ] );
58 }
59
60 $notice->handle_dismiss();
61
62 wp_send_json_success( [] );
63 }
64
65 /**
66 * @param string $sanitize_text_field
67 *
68 * @return Notice_Base|null
69 */
70 private function get_notice( string $sanitize_text_field ): ?Notice_Base {
71 return $this->notices[ $sanitize_text_field ] ?? null;
72 }
73
74 /**
75 * @return void
76 */
77 public function __construct() {
78 if ( ! is_admin() ) {
79 return;
80 }
81 add_action( 'admin_notices', [ $this, 'show_notices' ] );
82 add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'handle_dismiss' ] );
83 add_action( 'init', function () {
84 do_action( 'cookiez_register_notices', $this );
85 } );
86 }
87 }
88