attempts_log = $attempts_log; $this->config = $config; $this->repository = $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; } /** * 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 ); add_action( 'admin_post_robotstxt_2fa_export_users', array( $this, 'handle_csv_export' ) ); } /** * 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' ), self::MENU_ICON ); 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;'; ?>

get_users_table()->prepare_items(); $this->get_users_table()->display(); ?>

-1, 'fields' => 'all', ) ); $rows = array(); foreach ( $users as $user ) { if ( ! $user instanceof \WP_User ) { continue; } $settings = $this->repository->get_user_settings( $user->ID ); $is_enabled = $settings['enabled']; $required_methods = $this->config->get_required_methods_for_user( $user ); $is_forced = ! empty( $required_methods ); if ( $is_enabled ) { $status = 'active'; } elseif ( $is_forced ) { $status = 'required'; } else { $status = 'optional'; } $last_verified_ts = 0; $raw_verifications = get_user_meta( $user->ID, 'robotstxt_2fa_last_verifications', true ); if ( is_array( $raw_verifications ) ) { foreach ( $raw_verifications as $method_data ) { if ( is_array( $method_data ) ) { foreach ( $method_data as $context_data ) { if ( is_array( $context_data ) && isset( $context_data['verified_at'] ) && is_int( $context_data['verified_at'] ) ) { $last_verified_ts = max( $last_verified_ts, $context_data['verified_at'] ); } } } } } $last_verified_str = $last_verified_ts > 0 ? (string) wp_date( 'Y-m-d H:i', $last_verified_ts ) : ''; $unused_codes = 0; $raw_codes = get_user_meta( $user->ID, 'robotstxt_2fa_recovery_codes', true ); if ( is_array( $raw_codes ) && isset( $raw_codes['codes'] ) && is_array( $raw_codes['codes'] ) ) { foreach ( $raw_codes['codes'] as $code_entry ) { if ( is_array( $code_entry ) && empty( $code_entry['used_at'] ) ) { ++$unused_codes; } } } $rows[] = array( 'ID' => (string) $user->ID, 'user_login' => $user->user_login, 'user_email' => $user->user_email, 'roles' => implode( '|', $user->roles ), 'status' => $status, 'methods' => implode( '|', $settings['methods'] ), 'last_verified' => $last_verified_str, 'unused_codes' => (string) $unused_codes, ); } header( 'Content-Type: text/csv' ); 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( 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( 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. * * @since 1.3.0 * * @param array $columns Existing columns. * * @return array */ 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 '● ' . esc_html__( 'Active', 'robotstxt-2fa' ); } return '○ ' . esc_html__( 'Off', 'robotstxt-2fa' ); } }