Files
nolimit-discounts/includes/class-admin-audit.php
T

183 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* Audit log admin view.
*
* @package WC_Email_Discount_Manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Displays recorded REST API activity.
*/
class WC_EDM_Admin_Audit {
/**
* Entries shown per page.
*/
const PER_PAGE = 50;
/**
* Handle the clear-log action. Runs on admin_init.
*/
public function handle_actions() {
if ( ! isset( $_GET['action'] ) || 'clear_audit_log' !== $_GET['action'] ) {
return;
}
check_admin_referer( 'wc_edm_clear_audit_log' );
WC_EDM_DB::clear_audit_log();
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'audit', 'message' => 'deleted' ) ) );
exit;
}
/**
* Render the audit log tab.
*/
public function render() {
$outcome = isset( $_GET['outcome'] ) ? sanitize_key( wp_unslash( $_GET['outcome'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$outcome = in_array( $outcome, array( 'allowed', 'denied', 'rate_limited' ), true ) ? $outcome : '';
$paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$offset = ( $paged - 1 ) * self::PER_PAGE;
$entries = WC_EDM_DB::get_audit_entries( self::PER_PAGE, $offset, $outcome );
$total = WC_EDM_DB::count_audit_entries( $outcome );
$counts = array(
'' => WC_EDM_DB::count_audit_entries(),
'allowed' => WC_EDM_DB::count_audit_entries( 'allowed' ),
'denied' => WC_EDM_DB::count_audit_entries( 'denied' ),
'rate_limited' => WC_EDM_DB::count_audit_entries( 'rate_limited' ),
);
$labels = array(
'' => __( 'All', 'wc-email-discount' ),
'allowed' => __( 'Changes', 'wc-email-discount' ),
'denied' => __( 'Rejected', 'wc-email-discount' ),
'rate_limited' => __( 'Rate limited', 'wc-email-discount' ),
);
$clear_url = wp_nonce_url(
WC_EDM_Helpers::admin_url( array( 'tab' => 'audit', 'action' => 'clear_audit_log' ) ),
'wc_edm_clear_audit_log'
);
?>
<div class="wc-edm-header-row">
<h2><?php esc_html_e( 'API Audit Log', 'wc-email-discount' ); ?></h2>
<?php if ( $counts[''] > 0 ) : ?>
<a class="button wc-edm-delete" href="<?php echo esc_url( $clear_url ); ?>">
<?php esc_html_e( 'Clear Log', 'wc-email-discount' ); ?>
</a>
<?php endif; ?>
</div>
<p class="description">
<?php
printf(
/* translators: %d: retention in days */
esc_html__( 'Records every change made through the REST API, plus rejected and rate limited requests. Entries older than %d days are removed automatically.', 'wc-email-discount' ),
(int) WC_EDM_Audit_Log::retention_days()
);
?>
</p>
<ul class="subsubsub">
<?php $first = true; ?>
<?php foreach ( $labels as $key => $label ) : ?>
<li>
<?php echo $first ? '' : ' | '; ?>
<a href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array_filter( array( 'tab' => 'audit', 'outcome' => $key ) ) ) ); ?>"
class="<?php echo $outcome === $key ? 'current' : ''; ?>">
<?php echo esc_html( $label ); ?>
<span class="count">(<?php echo esc_html( $counts[ $key ] ); ?>)</span>
</a>
</li>
<?php $first = false; ?>
<?php endforeach; ?>
</ul>
<div class="clear"></div>
<?php if ( empty( $entries ) ) : ?>
<p><?php esc_html_e( 'No activity recorded yet.', 'wc-email-discount' ); ?></p>
<?php return; ?>
<?php endif; ?>
<table class="widefat striped wc-edm-table wc-edm-audit-table">
<thead>
<tr>
<th><?php esc_html_e( 'When', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'User', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'IP', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'Request', 'wc-email-discount' ); ?></th>
<th><?php esc_html_e( 'Result', 'wc-email-discount' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $entries as $entry ) : ?>
<?php
$details = $entry->details ? json_decode( $entry->details, true ) : array();
$badge = array(
'allowed' => 'wc-edm-badge-group',
'denied' => 'wc-edm-badge-denied',
'rate_limited' => 'wc-edm-badge-coupon',
);
?>
<tr>
<td><?php echo esc_html( mysql2date( get_option( 'date_format' ) . ' H:i:s', $entry->created_at ) ); ?></td>
<td>
<?php if ( $entry->user_login ) : ?>
<?php echo esc_html( $entry->user_login ); ?>
<?php else : ?>
<em><?php esc_html_e( 'Not signed in', 'wc-email-discount' ); ?></em>
<?php endif; ?>
</td>
<td><code><?php echo esc_html( $entry->ip ? $entry->ip : '—' ); ?></code></td>
<td>
<code><?php echo esc_html( $entry->method ); ?></code>
<span class="wc-edm-item-meta"><?php echo esc_html( $entry->route ); ?></span>
</td>
<td>
<span class="wc-edm-badge <?php echo esc_attr( $badge[ $entry->outcome ] ?? '' ); ?>">
<?php echo esc_html( $labels[ $entry->outcome ] ?? $entry->outcome ); ?>
</span>
<span class="wc-edm-item-meta">
<?php echo esc_html( $entry->status ); ?>
<?php echo ! empty( $details['code'] ) ? esc_html( ' · ' . $details['code'] ) : ''; ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
$pages = (int) ceil( $total / self::PER_PAGE );
if ( $pages > 1 ) :
?>
<div class="tablenav"><div class="tablenav-pages">
<?php
echo wp_kses_post(
paginate_links(
array(
'base' => esc_url_raw( add_query_arg( 'paged', '%#%' ) ),
'format' => '',
'current' => $paged,
'total' => $pages,
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
)
)
);
?>
</div></div>
<?php
endif;
}
}