Skip to content

Commit 1d5b399

Browse files
committed
improve notice dismissing
1 parent e112641 commit 1d5b399

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

includes/admin/notices.php

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -382,36 +382,52 @@ function maybe_display_purge_cache_plugin_notice() {
382382
</p>
383383
</div>
384384
<script type="text/javascript">
385-
(function() {
386-
var notices = document.querySelectorAll('.powered-cache-dismissible-notice');
387-
notices.forEach(function(notice) {
388-
var dismissButtons = notice.querySelectorAll('.notice-dismiss, .powered-cache-dismiss-button');
389-
dismissButtons.forEach(function(button) {
390-
button.addEventListener('click', function(e) {
385+
(function() {
386+
document.addEventListener('click', function(e) {
387+
var button = e.target.closest('.notice-dismiss, .powered-cache-dismiss-button');
388+
if (!button) {
389+
return;
390+
}
391+
392+
var notice = button.closest('.powered-cache-dismissible-notice');
393+
if (!notice) {
394+
return;
395+
}
396+
397+
e.preventDefault();
398+
399+
var noticeId = notice.getAttribute('data-notice-id');
400+
var nonce = notice.getAttribute('data-nonce');
401+
402+
if (button.classList.contains('powered-cache-dismiss-button')) {
391403
e.preventDefault();
392-
var noticeId = notice.getAttribute('data-notice-id');
393-
var nonce = notice.getAttribute('data-nonce');
394-
395-
// Send AJAX request
396-
var xhr = new XMLHttpRequest();
397-
xhr.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', true);
398-
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
399-
xhr.onload = function() {
400-
if (xhr.status === 200) {
401-
// Fade out and remove the notice
402-
notice.style.opacity = '1';
403-
notice.style.transition = 'opacity 0.3s';
404-
notice.style.opacity = '0';
405-
setTimeout(function() {
406-
notice.remove();
407-
}, 300);
404+
notice.style.opacity = '1';
405+
notice.style.transition = 'opacity 0.3s';
406+
notice.style.opacity = '0';
407+
408+
setTimeout(function() {
409+
if (notice && notice.parentNode) {
410+
notice.parentNode.removeChild(notice);
408411
}
409-
};
410-
xhr.send('action=powered_cache_dismiss_notice_ajax&notice=' + encodeURIComponent(noticeId) + '&nonce=' + encodeURIComponent(nonce));
411-
});
412+
}, 300);
413+
}
414+
415+
var xhr = new XMLHttpRequest();
416+
xhr.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', true);
417+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
418+
xhr.onload = function () {
419+
if (xhr.status !== 200) {
420+
console.error('Dismiss failed');
421+
}
422+
};
423+
424+
xhr.send(
425+
'action=powered_cache_dismiss_notice_ajax'
426+
+ '&notice=' + encodeURIComponent(noticeId)
427+
+ '&nonce=' + encodeURIComponent(nonce)
428+
);
412429
});
413-
});
414-
})();
430+
})();
415431
</script>
416432
<?php
417433
}
@@ -446,17 +462,17 @@ function dismiss_notice() {
446462
* Dismiss notice via AJAX
447463
*
448464
* @return void
449-
* @since 3.6.4
465+
* @since 3.7
450466
*/
451467
function dismiss_notice_ajax() {
452468
check_ajax_referer( 'powered_cache_dismiss_notice_ajax', 'nonce' );
453469

454470
if ( ! current_user_can( 'manage_options' ) ) {
455-
wp_send_json_error( array( 'message' => __( 'Permission denied', 'powered-cache' ) ) );
471+
wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'powered-cache' ) ) );
456472
}
457473

458474
if ( empty( $_POST['notice'] ) ) {
459-
wp_send_json_error( array( 'message' => __( 'Notice ID missing', 'powered-cache' ) ) );
475+
wp_send_json_error( array( 'message' => esc_html__( 'Notice ID missing', 'powered-cache' ) ) );
460476
}
461477

462478
$notice = sanitize_text_field( wp_unslash( $_POST['notice'] ) );
@@ -467,7 +483,7 @@ function dismiss_notice_ajax() {
467483
delete_transient( $notice );
468484
}
469485

470-
wp_send_json_success( array( 'message' => __( 'Notice dismissed', 'powered-cache' ) ) );
486+
wp_send_json_success( array( 'message' => esc_html__( 'Notice dismissed', 'powered-cache' ) ) );
471487
}
472488

473489
/**

0 commit comments

Comments
 (0)