@@ -15,18 +15,42 @@ jQuery(document).ready(
1515 security : WZKBAdminData . security
1616 } , function ( response ) {
1717 if ( response . success ) {
18- alert ( WZKBAdminData . strings . success_message ) ;
18+ // Use WordPress admin notice instead of alert().
19+ showAdminNotice ( WZKBAdminData . strings . success_message , 'success' ) ;
1920 } else {
20- alert ( WZKBAdminData . strings . fail_message ) ;
21+ showAdminNotice ( WZKBAdminData . strings . fail_message , 'error' ) ;
2122 }
2223 } ) . fail ( function ( jqXHR , textStatus ) {
23- alert ( WZKBAdminData . strings . fail_message ) ;
24+ showAdminNotice ( WZKBAdminData . strings . fail_message , 'error' ) ;
2425 console . log ( WZKBAdminData . strings . request_fail_message + textStatus ) ;
2526 } ) . always ( function ( ) {
2627 $button . prop ( 'disabled' , false ) . find ( '.spinner' ) . remove ( ) ;
2728 } ) ;
2829 }
2930
31+ // Function to show WordPress admin notices.
32+ function showAdminNotice ( message , type ) {
33+ var noticeClass = type === 'success' ? 'notice-success' : 'notice-error' ;
34+ var $notice = $ ( '<div class="notice ' + noticeClass + ' is-dismissible"><p>' + message + '</p></div>' ) ;
35+
36+ // Insert notice after the first h1 or h2 in the page.
37+ if ( $ ( '.wrap > h1, .wrap > h2' ) . length ) {
38+ $ ( '.wrap > h1, .wrap > h2' ) . first ( ) . after ( $notice ) ;
39+ } else {
40+ $ ( '.wrap' ) . prepend ( $notice ) ;
41+ }
42+
43+ // Scroll to notice.
44+ $ ( 'html, body' ) . animate ( { scrollTop : $notice . offset ( ) . top - 100 } , 300 ) ;
45+
46+ // Auto-dismiss after 5 seconds.
47+ setTimeout ( function ( ) {
48+ $notice . fadeOut ( 300 , function ( ) {
49+ $ ( this ) . remove ( ) ;
50+ } ) ;
51+ } , 5000 ) ;
52+ }
53+
3054 // Prompt the user when they leave the page without saving the form.
3155 var formmodified = 0 ;
3256
0 commit comments