This commit is contained in:
Javier Casares 2026-11-04 14:37:18 +00:00
commit 7379505d20
10 changed files with 912 additions and 139 deletions

View file

@ -24,6 +24,18 @@ class Dashboard_Page {
*/
public const PAGE_SLUG = 'robotstxt-2fa-dashboard';
/**
* Brand SVG menu icon (shield with keyhole) as a base64 data URI.
*
* WordPress core renders base64-encoded SVG data URIs natively in admin
* menus (centered at 20px via the .wp-menu-image.svg CSS class), which
* keeps the icon crisp and independent of the Dashicons font while
* remaining fully backward compatible.
*
* @since 1.6.9
*/
public const MENU_ICON = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDIwIDIwIiByb2xlPSJpbWciIGFyaWEtaGlkZGVuPSJ0cnVlIiBmb2N1c2FibGU9ImZhbHNlIj48cGF0aCBmaWxsPSIjMDM4N2M5IiBkPSJNMTAgMSAxOCAzLjl2NS4zYzAgNS40LTMuMyA4LjYtOCA5LjgtNC43LTEuMi04LTQuNC04LTkuOFYzLjlaIi8+PGNpcmNsZSBjeD0iMTAiIGN5PSI4IiByPSIyLjIiIGZpbGw9IiNmZmVmZDQiLz48cGF0aCBmaWxsPSIjZmZlZmQ0IiBkPSJNOC44IDkuOSA3LjYgMTMuN2g0LjhMMTEuMiA5LjlaIi8+PC9zdmc+';
/**
* Failed attempts log.
*
@ -108,7 +120,7 @@ class Dashboard_Page {
'manage_options',
self::PAGE_SLUG,
array( $this, 'render_page' ),
'dashicons-shield'
self::MENU_ICON
);
add_submenu_page(

View file

@ -538,6 +538,11 @@ class Migration_Page {
* @return array<int, array{user: \WP_User, source: string}>
*/
private function get_candidates_from_wordfence(): array {
/**
* WordPress database abstraction. Typed for PHPStan level max.
*
* @var \wpdb $wpdb
*/
global $wpdb;
$table = $wpdb->prefix . self::WORDFENCE_TABLE;
@ -550,21 +555,17 @@ class Migration_Page {
return array();
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names are trusted constants.
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is a trusted prefix constant; the user ID is a validated integer.
$rows = $wpdb->get_results(
"SELECT s.user_id, s.secret FROM `{$table}` s",
ARRAY_A
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( ! is_array( $rows ) ) {
return array();
}
$candidates = array();
foreach ( $rows as $row ) {
if ( ! is_array( $row ) || ! isset( $row['user_id'] ) || ! is_numeric( $row['user_id'] ) ) {
if ( ! isset( $row['user_id'] ) || ! is_numeric( $row['user_id'] ) ) {
continue;
}
@ -716,14 +717,19 @@ class Migration_Page {
* @return string
*/
private function read_wordfence_secret( int $user_id ): string {
/**
* WordPress database abstraction. Typed for PHPStan level max.
*
* @var \wpdb $wpdb
*/
global $wpdb;
$table = $wpdb->prefix . self::WORDFENCE_TABLE;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is a trusted constant.
$secret_binary = $wpdb->get_var( $wpdb->prepare( "SELECT `secret` FROM `{$table}` WHERE `user_id` = %d LIMIT 1", $user_id ) );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is a trusted prefix constant; the user ID is a validated integer.
$secret_binary = $wpdb->get_var( "SELECT `secret` FROM `{$table}` WHERE `user_id` = {$user_id} LIMIT 1" );
if ( ! is_string( $secret_binary ) || '' === $secret_binary ) {
if ( ! is_string( $secret_binary ) ) {
return '';
}

View file

@ -431,8 +431,13 @@ class Settings_Page {
</thead>
<tbody>
<?php foreach ( $roles as $role_key => $role_details ) : ?>
<?php
$role_name = isset( $role_details['name'] ) && is_string( $role_details['name'] )
? $role_details['name']
: $role_key;
?>
<tr data-2fa-role="<?php echo esc_attr( $role_key ); ?>">
<td><?php echo esc_html( translate_user_role( $role_details['name'] ) ); ?></td>
<td><?php echo esc_html( translate_user_role( $role_name ) ); ?></td>
<?php foreach ( $methods as $method_key => $method_label ) : ?>
<td style="text-align:center;">
<label>
@ -441,7 +446,7 @@ class Settings_Page {
printf(
/* translators: 1: role name, 2: method name. */
esc_html__( '%1$s: require %2$s', 'robotstxt-2fa' ),
esc_html( translate_user_role( $role_details['name'] ) ),
esc_html( translate_user_role( $role_name ) ),
esc_html( $method_label )
);
?>
@ -802,7 +807,7 @@ class Settings_Page {
/**
* Resolve role_methods from stored options, migrating legacy force_roles + default_method if needed.
*
* @param array<string, mixed> $options Raw stored options.
* @param array<mixed> $options Raw stored options (untyped values from the options table).
*
* @return array<string, array<int, string>>
*/
@ -1145,7 +1150,7 @@ class Settings_Page {
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' ),
'dashicons-shield'
Dashboard_Page::MENU_ICON
);
// Rename the first auto-created submenu entry to "Settings".

View file

@ -338,9 +338,14 @@ class Users_List_Table extends \WP_List_Table {
<div class="alignleft actions">
<select name="role_filter">
<option value=""><?php esc_html_e( '— All roles —', 'robotstxt-2fa' ); ?></option>
<?php foreach ( $roles as $role_key => $role_info ) : ?>
<?php
foreach ( $roles as $role_key => $role_info ) :
$role_name = isset( $role_info['name'] ) && is_string( $role_info['name'] )
? $role_info['name']
: $role_key;
?>
<option value="<?php echo esc_attr( $role_key ); ?>" <?php selected( $current_role, $role_key ); ?>>
<?php echo esc_html( translate_user_role( $role_info['name'] ) ); ?>
<?php echo esc_html( translate_user_role( $role_name ) ); ?>
</option>
<?php endforeach; ?>
</select>

View file

@ -405,6 +405,10 @@ Failed 2FA attempts (last 7 days): %4$d
$admins = get_users( array( 'capability' => 'manage_options' ) );
foreach ( $admins as $admin ) {
if ( ! $admin instanceof \WP_User ) {
continue;
}
$this->send_mail( $admin->user_email, $subject, $message );
}
}

View file

@ -299,14 +299,22 @@ class Login_Form_Manager {
return;
}
/**
* Registered filters and actions. Values are WP_Hook objects, but a
* corrupted container from third-party code is tolerated, hence mixed.
*
* @var array<string, mixed>|null $wp_filter
*/
global $wp_filter;
if ( ! isset( $wp_filter['init'] ) || ! $wp_filter['init'] instanceof \WP_Hook ) {
$init_hooks = $wp_filter['init'] ?? null;
if ( ! $init_hooks instanceof \WP_Hook ) {
return;
}
foreach ( array_keys( $wp_filter['init']->callbacks ) as $priority ) {
if ( isset( $wp_filter['init']->callbacks[ $priority ]['altcha_interceptor'] ) ) {
foreach ( array_keys( $init_hooks->callbacks ) as $priority ) {
if ( isset( $init_hooks->callbacks[ $priority ]['altcha_interceptor'] ) ) {
remove_action( 'init', 'altcha_interceptor', (int) $priority );
return;

View file

@ -33,8 +33,11 @@ class Rest_Controller {
/**
* REST namespace.
*
* Named REST_NAMESPACE instead of NAMESPACE: a class constant named
* "namespace" (case-insensitively) is deprecated in PHP 8.6.
*/
private const NAMESPACE = 'robotstxt-2fa/v1';
private const REST_NAMESPACE = 'robotstxt-2fa/v1';
/**
* Capability required to access any endpoint.
@ -103,7 +106,7 @@ class Rest_Controller {
*/
public function register_routes(): void {
register_rest_route(
self::NAMESPACE,
self::REST_NAMESPACE,
'/settings',
array(
array(
@ -120,7 +123,7 @@ class Rest_Controller {
);
register_rest_route(
self::NAMESPACE,
self::REST_NAMESPACE,
'/users',
array(
'methods' => WP_REST_Server::READABLE,

View file

@ -182,9 +182,14 @@ class Frontend_Profile {
* @return void
*/
public function maybe_enqueue_assets(): void {
/**
* Current post. Typed for PHPStan level max.
*
* @var \WP_Post|string|null $post
*/
global $post;
if ( ! is_a( $post, 'WP_Post' ) || ! has_shortcode( $post->post_content, 'robotstxt_2fa_profile' ) ) {
if ( ! $post instanceof \WP_Post || ! has_shortcode( $post->post_content, 'robotstxt_2fa_profile' ) ) {
return;
}

View file

@ -205,8 +205,8 @@ class Profile_Settings {
if ( $grace_days > 0 && $this->grace_period->is_active( $user, $grace_days ) ) {
$remaining = $this->grace_period->get_days_remaining( $user, $grace_days );
/* translators: %d: number of days remaining to complete the setup. */
$grace_note = sprintf(
/* translators: %d: number of days remaining to complete the setup. */
_n(
'You have %d day left to complete the setup.',
'You have %d days left to complete the setup.',

View file

@ -251,7 +251,7 @@ class Trusted_Devices {
return false;
}
if ( ! defined( 'AUTH_SALT' ) || '' === AUTH_SALT ) {
if ( ! defined( 'AUTH_SALT' ) || ! is_string( AUTH_SALT ) || '' === AUTH_SALT ) {
return false;
}
@ -283,7 +283,7 @@ class Trusted_Devices {
* @return void
*/
private function set_cookie( int $user_id, string $token, int $expires_at ): void {
if ( ! defined( 'AUTH_SALT' ) || '' === AUTH_SALT ) {
if ( ! defined( 'AUTH_SALT' ) || ! is_string( AUTH_SALT ) || '' === AUTH_SALT ) {
return;
}
@ -295,7 +295,7 @@ class Trusted_Devices {
$value,
array(
'expires' => $expires_at,
'path' => defined( 'COOKIEPATH' ) ? COOKIEPATH : '/',
'path' => defined( 'COOKIEPATH' ) && is_string( COOKIEPATH ) ? COOKIEPATH : '/',
'domain' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '',
'secure' => is_ssl(),
'httponly' => true,
@ -315,7 +315,7 @@ class Trusted_Devices {
'',
array(
'expires' => time() - YEAR_IN_SECONDS,
'path' => defined( 'COOKIEPATH' ) ? COOKIEPATH : '/',
'path' => defined( 'COOKIEPATH' ) && is_string( COOKIEPATH ) ? COOKIEPATH : '/',
'domain' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '',
'secure' => is_ssl(),
'httponly' => true,
@ -364,12 +364,20 @@ class Trusted_Devices {
/**
* Retrieve the raw device map from user meta.
*
* Non-string keys (corrupted meta) are discarded so callers always
* receive a token-hash-keyed map.
*
* @param int $user_id User ID.
*
* @return array<string, mixed>
*/
private function get_raw_devices( int $user_id ): array {
$raw = get_user_meta( $user_id, self::META_KEY, true );
return is_array( $raw ) ? $raw : array();
if ( ! is_array( $raw ) ) {
return array();
}
return array_filter( $raw, 'is_string', ARRAY_FILTER_USE_KEY );
}
}