feature: Add "Clear logs" function

This commit is contained in:
Keith Solomon
2025-12-14 17:17:16 -06:00
parent 189b32ccff
commit 9375ca61a0
2 changed files with 49 additions and 0 deletions

View File

@@ -55,6 +55,7 @@ class Admin {
add_action( 'admin_post_site_sync_manual', array( $this, 'handle_manual_sync' ) );
add_action( 'admin_post_site_sync_handshake', array( $this, 'handle_handshake' ) );
add_action( 'admin_post_site_sync_reset_state', array( $this, 'handle_reset_state' ) );
add_action( 'admin_post_site_sync_clear_logs', array( $this, 'handle_clear_logs' ) );
add_action( 'admin_notices', array( $this, 'maybe_render_notices' ) );
}
@@ -185,6 +186,12 @@ class Admin {
<input type="hidden" name="action" value="site_sync_reset_state">
<?php submit_button( __( 'Reset Sync State', 'site-sync' ), 'delete' ); ?>
</form>
<p><?php esc_html_e( 'Clear the stored recent logs shown below.', 'site-sync' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<?php wp_nonce_field( 'site_sync_clear_logs', 'site_sync_clear_logs_nonce' ); ?>
<input type="hidden" name="action" value="site_sync_clear_logs">
<?php submit_button( __( 'Clear Logs', 'site-sync' ), 'secondary' ); ?>
</form>
<h2><?php esc_html_e( 'Status', 'site-sync' ); ?></h2>
<table class="widefat striped">
@@ -387,6 +394,13 @@ class Admin {
esc_html__( 'Sync state reset. Next run will resend all items.', 'site-sync' )
);
}
if ( isset( $_GET['site_sync_status'] ) && 'logs_cleared' === $_GET['site_sync_status'] ) {
printf(
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
esc_html__( 'Recent logs cleared.', 'site-sync' )
);
}
}
/**
@@ -426,6 +440,32 @@ class Admin {
exit;
}
/**
* Handles clearing the recent logs from the admin form submission.
*
* @return void
*/
public function handle_clear_logs(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'site-sync' ) );
}
check_admin_referer( 'site_sync_clear_logs', 'site_sync_clear_logs_nonce' );
Logger::clear();
$redirect = add_query_arg(
array(
'page' => 'site-sync',
'site_sync_status' => 'logs_cleared',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the reset state action from the admin form submission.
*