PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.1.0
WP 2FA – Two-factor authentication for WordPress v2.1.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / UserListing.php
wp-2fa / includes / classes / Admin Last commit date
Controllers 4 years ago SettingsPages 4 years ago Views 4 years ago HelpContactUs.php 4 years ago PremiumFeatures.php 4 years ago SettingsPage.php 4 years ago SetupWizard.php 4 years ago User.php 4 years ago UserListing.php 4 years ago UserNotices.php 4 years ago UserProfile.php 4 years ago UserRegistered.php 4 years ago index.php 5 years ago
UserListing.php
113 lines
1 <?php
2
3 namespace WP2FA\Admin;
4
5 use WP2FA\Utils\UserUtils;
6
7 defined( 'ABSPATH' ) || exit; // Exit if accessed directly
8
9 /**
10 * UserListing class with user listing filters
11 */
12 if ( ! class_exists( '\WP2FA\Admin\UserListing' ) ) {
13
14 /**
15 * UserListing - Shows extra column in user table wit WP2FA status forevery user
16 */
17 class UserListing {
18
19 /**
20 * The users table column name
21 *
22 * @var string
23 */
24 private static $columnName = '2fa-status';
25
26 /**
27 * Inits all the hooks used for showing the extra user data in the users column
28 *
29 * @return void
30 */
31 public static function init() {
32 add_filter( 'manage_users_columns', [ __CLASS__, 'addWP2FAColumn' ] );
33 add_filter( 'wpmu_users_columns', [ __CLASS__, 'addWP2FAColumn' ] );
34 add_filter( 'manage_users_custom_column', [ __CLASS__, 'showColumnData' ], 10, 3 );
35 }
36
37 /**
38 * Sets the column in the admin users table
39 *
40 * @param array $columns
41 *
42 * @return array
43 */
44 public static function addWP2FAColumn( array $columns ): array {
45 $columns[ self::$columnName ] = __( '2FA Status', 'wp-2fa' );
46 return $columns;
47 }
48
49 /**
50 * Shows the user WP 2FA status data in the users table
51 *
52 * @param [type] $value
53 * @param string $columnName
54 * @param [type] $userId
55 *
56 * @return mixed
57 */
58 public static function showColumnData( $value, string $columnName, $userId ) {
59
60 switch ( $columnName ) {
61 case self::$columnName:
62 return self::getUser2faStatus( $userId );
63 default:
64 }
65
66 return $value;
67 }
68
69 /**
70 * Retrieves the translated 2FA status label for given user.
71 *
72 * This is performance optimized version that bypasses the User class on purpose. It loads the 2FA status meta
73 * field directly and turns it into a label.
74 *
75 * There is also some temporary code to figure out the 2FA status meta field if it doesn't exist. This will be
76 * removed in future versions and exist purely so we don't end up with no values in the column after migration
77 * to version 1.7.0 when this was introduced.
78 *
79 * @param int $userId
80 *
81 * @return string
82 * @see WP2FA\Admin\User
83 * @since 1.7.0
84 */
85 private static function getUser2faStatus( $userId ) {
86 // try to get the user status "id" from user's meta data
87 $status_meta_value = get_user_meta( $userId, WP_2FA_PREFIX . '2fa_status', true );
88 if ( ! empty( $status_meta_value ) ) {
89 // the status id is available, grab the label to display
90 $status_data = UserUtils::extractStatuses( [ $status_meta_value ] );
91 if ( ! empty( $status_data ) ) {
92 return $status_data['label'];
93 }
94 }
95
96 // If the user status is not saved in user meta (this can be the case prior to version 1.7.0), we figure it
97 // out and store it against the user in DB. This is not ideal in terms of performance and this is only
98 // a temporary solution.
99 // @todo remove this in future versions
100 return User::setUserStatus( new \WP_User( $userId ) );
101 }
102
103 /**
104 * Returns the users table column name
105 *
106 * @return string
107 */
108 public static function getColumnName(): string {
109 return self::$columnName;
110 }
111 }
112 }
113