22/**
33 * Admin notices API.
44 *
5- * @package WebberZone\FreemKit \Admin
5+ * @package WebberZone\Knowledge_Base \Admin
66 */
77
88namespace WebberZone \FreemKit \Admin ;
1616
1717/**
1818 * Class to handle admin notices.
19- *
20- * @since 4.1.0
2119 */
2220class Admin_Notices_API {
2321
2422 /**
25- * Array of registered notices .
23+ * Plugin prefix used for AJAX actions, nonces, and storage keys .
2624 *
27- * @since 4.1.0
25+ * @var string
26+ */
27+ private string $ prefix ;
28+
29+ /**
30+ * Array of registered notices.
2831 *
2932 * @var array Registered notices.
3033 */
31- public array $ notices = array ();
34+ private array $ notices = array ();
3235
3336 /**
3437 * Constructor class.
3538 *
36- * @since 4.1.0
39+ * @param string $prefix Plugin prefix for AJAX actions, nonces, and storage keys. Default 'wzkb'.
3740 */
38- public function __construct () {
41+ public function __construct ( string $ prefix = 'freemkit ' ) {
42+ $ this ->prefix = $ prefix ;
43+
44+ Hook_Registry::add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'enqueue_scripts ' ) );
3945 Hook_Registry::add_action ( 'admin_notices ' , array ( $ this , 'display_notices ' ) );
40- Hook_Registry::add_action ( 'wp_ajax_freemkit_dismiss_notice ' , array ( $ this , 'handle_notice_dismissal ' ) );
46+ Hook_Registry::add_action ( "wp_ajax_ {$ this ->prefix }_dismiss_notice " , array ( $ this , 'handle_notice_dismissal ' ) );
47+ }
48+
49+ /**
50+ * Register and enqueue the dismiss script, pushing this instance's config
51+ * into the shared window.adminNoticesConfigs array.
52+ */
53+ public function enqueue_scripts () {
54+ $ minimize = ( defined ( 'SCRIPT_DEBUG ' ) && SCRIPT_DEBUG ) ? '' : '.min ' ;
55+ $ handle = "{$ this ->prefix }-admin-notices " ;
56+
57+ wp_register_script (
58+ $ handle ,
59+ plugins_url ( "js/admin-notices {$ minimize }.js " , __FILE__ ),
60+ array ( 'jquery ' ),
61+ FREEMKIT_VERSION ,
62+ true
63+ );
64+
65+ $ config = wp_json_encode (
66+ array (
67+ 'prefix ' => $ this ->prefix ,
68+ 'action ' => "{$ this ->prefix }_dismiss_notice " ,
69+ 'nonce ' => wp_create_nonce ( "{$ this ->prefix }_dismiss_notice " ),
70+ )
71+ );
72+
73+ wp_add_inline_script (
74+ $ handle ,
75+ 'window.adminNoticesConfigs = window.adminNoticesConfigs || []; window.adminNoticesConfigs.push( ' . $ config . '); ' ,
76+ 'before '
77+ );
78+
79+ wp_enqueue_script ( $ handle );
4180 }
4281
4382 /**
4483 * Register a new notice.
4584 *
46- * @since 4.1.0
47- *
4885 * @param array $notice {
4986 * Notice arguments.
5087 *
@@ -81,11 +118,12 @@ public function register_notice( array $notice ) {
81118
82119 /**
83120 * Display registered notices.
84- *
85- * @since 4.1.0
86121 */
87122 public function display_notices () {
88123 $ screen = get_current_screen ();
124+ if ( null === $ screen ) {
125+ return ;
126+ }
89127
90128 foreach ( $ this ->notices as $ notice ) {
91129 // Skip if user doesn't have capability.
@@ -116,58 +154,21 @@ public function display_notices() {
116154 }
117155
118156 printf (
119- '<div class="%1$s" data-notice-id="%2$s" data-dismiss-time="%3$s">%4 $s</div> ' ,
157+ '<div class="%1$s" data-notice-id="%2$s" data-dismiss-time="%3$s" data-notice-prefix="%4$s">%5 $s</div> ' ,
120158 esc_attr ( $ class ),
121159 esc_attr ( $ notice ['id ' ] ),
122160 esc_attr ( $ notice ['dismiss_time ' ] ),
161+ esc_attr ( $ this ->prefix ),
123162 wp_kses_post ( $ notice ['message ' ] )
124163 );
125164 }
126-
127- $ this ->print_scripts ();
128- }
129-
130- /**
131- * Print scripts for notice dismissal.
132- *
133- * @since 4.1.0
134- */
135- public function print_scripts () {
136- static $ printed = false ;
137-
138- if ( $ printed ) {
139- return ;
140- }
141-
142- ?>
143- <script>
144- jQuery(document).ready(function($) {
145- $('.notice[data-notice-id]').on('click', '.notice-dismiss', function() {
146- var $notice = $(this).closest('.notice');
147- var noticeId = $notice.data('notice-id');
148- var dismissTime = $notice.data('dismiss-time');
149-
150- $.post(ajaxurl, {
151- action: 'freemkit_dismiss_notice',
152- notice_id: noticeId,
153- dismiss_time: dismissTime,
154- nonce: '<?php echo esc_js ( wp_create_nonce ( 'freemkit_dismiss_notice ' ) ); ?> '
155- });
156- });
157- });
158- </script>
159- <?php
160-
161- $ printed = true ;
162165 }
163166
164167 /**
165168 * Handle notice dismissal via AJAX.
166- *
167- * @since 4.1.0
168169 */
169170 public function handle_notice_dismissal () {
170- check_ajax_referer ( ' freemkit_dismiss_notice ' , 'nonce ' );
171+ check_ajax_referer ( "{ $ this -> prefix } _dismiss_notice " , 'nonce ' );
171172
172173 if ( ! current_user_can ( 'manage_options ' ) ) {
173174 wp_die ();
@@ -180,10 +181,12 @@ public function handle_notice_dismissal() {
180181 wp_die ();
181182 }
182183
184+ $ key = "{$ this ->prefix }_notice_dismissed_ {$ notice_id }" ;
185+
183186 if ( $ dismiss_time ) {
184- set_transient ( " freemkit_notice_dismissed_ { $ notice_id }" , true , $ dismiss_time );
187+ set_transient ( $ key , true , $ dismiss_time );
185188 } else {
186- update_user_meta ( get_current_user_id (), " freemkit_notice_dismissed_ { $ notice_id }" , true );
189+ update_user_meta ( get_current_user_id (), $ key , true );
187190 }
188191
189192 wp_die ();
@@ -192,22 +195,22 @@ public function handle_notice_dismissal() {
192195 /**
193196 * Check if a notice has been dismissed.
194197 *
195- * @since 4.1.0
196- *
197198 * @param string $notice_id Notice ID.
198199 * @return bool Whether the notice has been dismissed.
199200 */
200- public function is_notice_dismissed ( $ notice_id ) {
201+ private function is_notice_dismissed ( $ notice_id ) {
201202 $ notice = $ this ->notices [ $ notice_id ] ?? null ;
202203
203204 if ( ! $ notice ) {
204205 return false ;
205206 }
206207
208+ $ key = "{$ this ->prefix }_notice_dismissed_ {$ notice_id }" ;
209+
207210 if ( $ notice ['dismiss_time ' ] ) {
208- return (bool ) get_transient ( " freemkit_notice_dismissed_ { $ notice_id }" );
211+ return (bool ) get_transient ( $ key );
209212 }
210213
211- return (bool ) get_user_meta ( get_current_user_id (), " freemkit_notice_dismissed_ { $ notice_id }" , true );
214+ return (bool ) get_user_meta ( get_current_user_id (), $ key , true );
212215 }
213216}
0 commit comments