v1.4.0
This commit is contained in:
parent
8e9c6bc472
commit
467560d420
1 changed files with 1478 additions and 74 deletions
213
includes/admin/class-dashboard-page.php
Normal file
213
includes/admin/class-dashboard-page.php
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
<?php
|
||||
/**
|
||||
* Admin dashboard for 2FA audit overview.
|
||||
*
|
||||
* @package Robotstxt_2FA
|
||||
*/
|
||||
|
||||
namespace Robotstxt\TwoFA\Admin;
|
||||
|
||||
use Robotstxt\TwoFA\Attempts_Log;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the top-level 2FA admin menu and audit dashboard page.
|
||||
*/
|
||||
class Dashboard_Page {
|
||||
/**
|
||||
* Slug for the dashboard admin page.
|
||||
*/
|
||||
public const PAGE_SLUG = 'robotstxt-2fa-dashboard';
|
||||
|
||||
/**
|
||||
* Failed attempts log.
|
||||
*
|
||||
* @var Attempts_Log
|
||||
*/
|
||||
private Attempts_Log $attempts_log;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Attempts_Log $attempts_log Failed attempts log instance.
|
||||
*/
|
||||
public function __construct( Attempts_Log $attempts_log ) {
|
||||
$this->attempts_log = $attempts_log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register WordPress hooks.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks(): void {
|
||||
add_action( 'admin_menu', array( $this, 'register_menu' ) );
|
||||
add_filter( 'manage_users_columns', array( $this, 'add_users_column' ) );
|
||||
add_filter( 'manage_users_custom_column', array( $this, 'render_users_column' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the top-level and dashboard submenu pages.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_menu(): void {
|
||||
add_menu_page(
|
||||
__( '2FA (by ROBOTSTXT)', 'robotstxt-2fa' ),
|
||||
__( '2FA', 'robotstxt-2fa' ),
|
||||
'manage_options',
|
||||
self::PAGE_SLUG,
|
||||
array( $this, 'render_page' ),
|
||||
'dashicons-shield'
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
self::PAGE_SLUG,
|
||||
__( 'Dashboard — 2FA', 'robotstxt-2fa' ),
|
||||
__( 'Dashboard', 'robotstxt-2fa' ),
|
||||
'manage_options',
|
||||
self::PAGE_SLUG,
|
||||
array( $this, 'render_page' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the audit dashboard page.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_page(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'robotstxt-2fa' ) );
|
||||
}
|
||||
|
||||
$count_result = count_users();
|
||||
$total_users = $count_result['total_users'];
|
||||
$query_2fa = new \WP_User_Query(
|
||||
array(
|
||||
'meta_key' => 'robotstxt_2fa_user_settings',
|
||||
'meta_compare' => 'EXISTS',
|
||||
'fields' => 'ID',
|
||||
'number' => -1,
|
||||
)
|
||||
);
|
||||
$users_with_2fa = $query_2fa->get_total();
|
||||
|
||||
$log = get_option( 'robotstxt_2fa_failed_log', array() );
|
||||
$log = is_array( $log ) ? $log : array();
|
||||
$week_ago = time() - WEEK_IN_SECONDS;
|
||||
|
||||
$failed_7d = 0;
|
||||
|
||||
foreach ( $log as $entry ) {
|
||||
if ( is_array( $entry ) && isset( $entry['timestamp'] ) && is_numeric( $entry['timestamp'] ) && (int) $entry['timestamp'] >= $week_ago ) {
|
||||
++$failed_7d;
|
||||
}
|
||||
}
|
||||
|
||||
$recent_attempts = $this->attempts_log->get_recent( 20 );
|
||||
|
||||
$card_style = 'background:#fff;border:1px solid #dcdcde;padding:16px 20px;min-width:160px;text-align:center;';
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e( 'Two-Factor Authentication Dashboard', 'robotstxt-2fa' ); ?></h1>
|
||||
|
||||
<div style="display:flex;gap:16px;flex-wrap:wrap;margin:1.5em 0;">
|
||||
<div style="<?php echo esc_attr( $card_style ); ?>">
|
||||
<div style="font-size:2em;font-weight:700;"><?php echo absint( $total_users ); ?></div>
|
||||
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( 'Total users', 'robotstxt-2fa' ); ?></div>
|
||||
</div>
|
||||
<div style="<?php echo esc_attr( $card_style ); ?>">
|
||||
<div style="font-size:2em;font-weight:700;"><?php echo absint( $users_with_2fa ); ?></div>
|
||||
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( '2FA enabled', 'robotstxt-2fa' ); ?></div>
|
||||
</div>
|
||||
<div style="<?php echo esc_attr( $card_style ); ?>">
|
||||
<div style="font-size:2em;font-weight:700;"><?php echo absint( $failed_7d ); ?></div>
|
||||
<div style="font-size:0.875em;color:#646970;"><?php esc_html_e( 'Failed attempts (7 days)', 'robotstxt-2fa' ); ?></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2><?php esc_html_e( 'Recent failed attempts', 'robotstxt-2fa' ); ?></h2>
|
||||
|
||||
<?php if ( empty( $recent_attempts ) ) : ?>
|
||||
<p><?php esc_html_e( 'No failed attempts recorded.', 'robotstxt-2fa' ); ?></p>
|
||||
<?php else : ?>
|
||||
<table class="widefat striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php esc_html_e( 'Date/Time', 'robotstxt-2fa' ); ?></th>
|
||||
<th><?php esc_html_e( 'User', 'robotstxt-2fa' ); ?></th>
|
||||
<th><?php esc_html_e( 'Method', 'robotstxt-2fa' ); ?></th>
|
||||
<th><?php esc_html_e( 'IP', 'robotstxt-2fa' ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ( $recent_attempts as $attempt ) : ?>
|
||||
<tr>
|
||||
<td><?php echo esc_html( gmdate( 'Y-m-d H:i:s', $attempt['timestamp'] ) ); ?></td>
|
||||
<td><?php echo esc_html( $attempt['user_login'] ); ?></td>
|
||||
<td><?php echo esc_html( $attempt['method'] ); ?></td>
|
||||
<td><?php echo esc_html( $attempt['ip'] ); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php endif; ?>
|
||||
|
||||
<p style="margin-top:1.5em;">
|
||||
<a href="<?php echo esc_url( admin_url( 'users.php' ) ); ?>"><?php esc_html_e( 'View all users →', 'robotstxt-2fa' ); ?></a>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 2FA status column to the users list table.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param array<string, string> $columns Existing columns.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function add_users_column( array $columns ): array {
|
||||
$columns['robotstxt_2fa'] = __( '2FA', 'robotstxt-2fa' );
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the 2FA status cell in the users list table.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param mixed $output Cell output (typically empty string or existing content).
|
||||
* @param string $column_name Current column identifier.
|
||||
* @param int $user_id User ID for the current row.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render_users_column( mixed $output, string $column_name, int $user_id ): string {
|
||||
if ( 'robotstxt_2fa' !== $column_name ) {
|
||||
return is_string( $output ) ? $output : '';
|
||||
}
|
||||
|
||||
$raw_settings = get_user_meta( $user_id, 'robotstxt_2fa_user_settings', true );
|
||||
$enabled = is_array( $raw_settings ) && ! empty( $raw_settings['enabled'] );
|
||||
|
||||
if ( $enabled ) {
|
||||
return '<span style="color:#00a32a;">●</span> ' . esc_html__( 'Active', 'robotstxt-2fa' );
|
||||
}
|
||||
|
||||
return '<span style="color:#999;">○</span> ' . esc_html__( 'Off', 'robotstxt-2fa' );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue