This commit is contained in:
Javier Casares 2026-06-06 05:22:18 +00:00
commit 74f1f42deb
5 changed files with 125 additions and 43 deletions

View file

@ -46,11 +46,13 @@ class Dashboard_Page {
private User_Settings_Repository $repository;
/**
* Users list table.
* Users list table — instantiated lazily inside render_page() after admin
* includes are loaded. WP_List_Table::__construct() calls convert_to_screen()
* which is unavailable at plugins_loaded time.
*
* @var Users_List_Table
* @var Users_List_Table|null
*/
private Users_List_Table $users_table;
private ?Users_List_Table $users_table = null;
/**
* Constructor.
@ -63,7 +65,19 @@ class Dashboard_Page {
$this->attempts_log = $attempts_log;
$this->config = $config;
$this->repository = $repository;
$this->users_table = new Users_List_Table( $config, $repository );
}
/**
* Return (and lazily create) the users list table instance.
*
* @return Users_List_Table
*/
private function get_users_table(): Users_List_Table {
if ( null === $this->users_table ) {
$this->users_table = new Users_List_Table( $this->config, $this->repository );
}
return $this->users_table;
}
/**
@ -170,16 +184,19 @@ class Dashboard_Page {
<form method="get">
<input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE_SLUG ); ?>" />
<?php
$this->users_table->prepare_items();
$this->users_table->display();
$this->get_users_table()->prepare_items();
$this->get_users_table()->display();
?>
</form>
<?php // @phpstan-ignore-next-line -- redundant but kept for defence in depth; render_page() already gates on manage_options.
if ( current_user_can( 'manage_options' ) ) : ?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:1em;">
<input type="hidden" name="action" value="robotstxt_2fa_export_users" />
<?php wp_nonce_field( 'robotstxt_2fa_export_users' ); ?>
<?php submit_button( __( 'Export CSV', 'robotstxt-2fa' ), 'secondary', 'submit', false ); ?>
</form>
<?php endif; ?>
<h2><?php esc_html_e( 'Recent failed attempts', 'robotstxt-2fa' ); ?></h2>
@ -300,15 +317,35 @@ class Dashboard_Page {
header( 'Content-Disposition: attachment; filename="2fa-users-' . gmdate( 'Y-m-d' ) . '.csv"' );
$headers = array( 'ID', 'Login', 'Email', 'Roles', 'Status', 'Methods', 'Last Verified', 'Unused Codes' );
echo implode( ',', array_map( 'addslashes', $headers ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV output to a file download.
echo implode( ',', array_map( array( $this, 'csv_field' ), $headers ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV file download, RFC 4180 escaped.
foreach ( $rows as $row ) {
echo implode( ',', array_map( 'addslashes', array_values( $row ) ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV output to a file download.
echo implode( ',', array_map( array( $this, 'csv_field' ), array_values( $row ) ) ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV file download, RFC 4180 escaped.
}
exit;
}
/**
* Encode a single value as an RFC 4180-compliant CSV field.
*
* Fields containing commas, double-quotes, or line breaks are wrapped in
* double-quotes; any embedded double-quotes are doubled.
*
* @since 1.5.1
*
* @param string $value Raw field value.
*
* @return string
*/
private function csv_field( string $value ): string {
if ( str_contains( $value, ',' ) || str_contains( $value, '"' ) || str_contains( $value, "\n" ) || str_contains( $value, "\r" ) ) {
return '"' . str_replace( '"', '""', $value ) . '"';
}
return $value;
}
/**
* Add a 2FA status column to the users list table.
*

View file

@ -668,7 +668,13 @@ class Settings_Page {
}
if ( isset( $raw_settings['geoip_database_path'] ) && is_string( $raw_settings['geoip_database_path'] ) ) {
$sanitized['geoip_database_path'] = sanitize_text_field( $raw_settings['geoip_database_path'] );
$geoip_path = sanitize_text_field( $raw_settings['geoip_database_path'] );
// Allow saving an empty path (disables GeoIP). For non-empty paths, accept
// the value as-is so admins can save a path even when the file does not
// yet exist on this server (e.g. when editing settings before uploading the DB).
// The actual existence + symlink check is performed at read time in Geo_Restrictions.
$sanitized['geoip_database_path'] = $geoip_path;
}
if ( isset( $raw_settings['geoip_country_allow'] ) && is_string( $raw_settings['geoip_country_allow'] ) ) {

View file

@ -160,7 +160,7 @@ class Geo_Restrictions {
private function get_country_for_ip(): string {
$db_path = $this->config->get_geoip_database_path();
if ( '' === $db_path || ! file_exists( $db_path ) ) {
if ( '' === $db_path || ! file_exists( $db_path ) || ! is_file( $db_path ) || is_link( $db_path ) ) {
return '';
}

View file

@ -239,6 +239,9 @@ class Plugin {
* @param int $window Seconds since last 2FA verification. Default 900 (15 minutes).
*/
$window = (int) apply_filters( 'robotstxt_2fa_app_password_verification_window', 900 );
if ( $window <= 0 ) {
$window = 900;
}
if ( $verified_at > 0 && ( time() - $verified_at ) <= $window ) {
return $result;

View file

@ -300,9 +300,9 @@ class Cli_Command extends WP_CLI_Command {
if ( 'csv' === $format ) {
$headers = array( 'ID', 'Login', 'Email', '2FA', 'Frequency' );
\WP_CLI::line( implode( ',', array_map( 'addslashes', $headers ) ) );
\WP_CLI::line( implode( ',', array_map( array( $this, 'csv_field' ), $headers ) ) );
foreach ( $rows as $row ) {
\WP_CLI::line( implode( ',', array_map( 'addslashes', array_values( $row ) ) ) );
\WP_CLI::line( implode( ',', array_map( array( $this, 'csv_field' ), array_values( $row ) ) ) );
}
return;
}
@ -451,6 +451,23 @@ class Cli_Command extends WP_CLI_Command {
\WP_CLI::warning( 'Remember to allow the bypass to expire or revoke it once access is restored.' );
}
/**
* Encode a single value as an RFC 4180-compliant CSV field.
*
* @since 1.5.1
*
* @param string $value Raw field value.
*
* @return string
*/
private function csv_field( string $value ): string {
if ( str_contains( $value, ',' ) || str_contains( $value, '"' ) || str_contains( $value, "\n" ) || str_contains( $value, "\r" ) ) {
return '"' . str_replace( '"', '""', $value ) . '"';
}
return $value;
}
/**
* Resolve a user ID or login string to a WP_User object.
*