Skip to content

Commit 04913b7

Browse files
committed
Update admin notices API and sanitize tax slugs
1 parent a730f1b commit 04913b7

4 files changed

Lines changed: 94 additions & 62 deletions

File tree

includes/admin/class-admin-notices-api.php

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Admin notices API.
44
*
5-
* @package WebberZone\FreemKit\Admin
5+
* @package WebberZone\Knowledge_Base\Admin
66
*/
77

88
namespace WebberZone\FreemKit\Admin;
@@ -16,35 +16,72 @@
1616

1717
/**
1818
* Class to handle admin notices.
19-
*
20-
* @since 4.1.0
2119
*/
2220
class 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
}

includes/admin/js/admin-notices.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Admin notice dismissal handler.
3+
*
4+
* Reads configs pushed into window.adminNoticesConfigs by each plugin instance
5+
* via wp_add_inline_script. Each config object contains:
6+
* - prefix {string} Plugin prefix (matches data-notice-prefix attribute).
7+
* - action {string} AJAX action name.
8+
* - nonce {string} Nonce for the AJAX request.
9+
*/
10+
jQuery(document).ready(function ($) {
11+
var configs = window.adminNoticesConfigs || [];
12+
13+
configs.forEach(function (config) {
14+
$('.notice[data-notice-prefix="' + config.prefix + '"]').on('click', '.notice-dismiss', function () {
15+
var $notice = $(this).closest('.notice');
16+
17+
$.post(ajaxurl, {
18+
action: config.action,
19+
notice_id: $notice.data('notice-id'),
20+
dismiss_time: $notice.data('dismiss-time'),
21+
nonce: config.nonce
22+
});
23+
});
24+
});
25+
});

includes/admin/js/admin-notices.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/admin/settings/class-settings-sanitize.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ public static function sanitize_tax_slugs( &$settings, $source_key, $target_key
454454
if ( isset( $settings[ $source_key ] ) ) {
455455
$slugs = array_unique( str_getcsv( $settings[ $source_key ], ',', '"', '' ) );
456456

457+
$tax_ids = array();
458+
$tax_slugs = array();
459+
457460
foreach ( $slugs as $slug ) {
458461
// Pattern is Name (taxonomy:term_taxonomy_id).
459462
preg_match( '/(.*)\((.*):(\d+)\)/i', (string) $slug, $matches );
@@ -469,8 +472,8 @@ public static function sanitize_tax_slugs( &$settings, $source_key, $target_key
469472
}
470473
}
471474

472-
$settings[ $target_key ] = isset( $tax_ids ) ? join( ',', $tax_ids ) : '';
473-
$settings[ $source_key ] = isset( $tax_slugs ) ? self::str_putcsv( $tax_slugs ) : '';
475+
$settings[ $target_key ] = join( ',', $tax_ids );
476+
$settings[ $source_key ] = self::str_putcsv( $tax_slugs );
474477
}
475478
}
476479
}

0 commit comments

Comments
 (0)