Controllers
4 years ago
Helpers
4 years ago
SettingsPages
4 years ago
Views
4 years ago
class-help-contact-us.php
4 years ago
class-premium-features.php
4 years ago
class-settings-page.php
4 years ago
class-settingspage.php
4 years ago
class-setup-wizard.php
4 years ago
class-user-listing.php
4 years ago
class-user-notices.php
4 years ago
class-user-profile.php
4 years ago
class-user-registered.php
4 years ago
class-user.php
4 years ago
index.php
5 years ago
class-user-listing.php
123 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for user listing in admin manipulation. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage user-utils |
| 7 | * @copyright 2021 WP White Security |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | */ |
| 11 | |
| 12 | namespace WP2FA\Admin; |
| 13 | |
| 14 | use WP2FA\Utils\User_Utils; |
| 15 | use WP2FA\Admin\Helpers\User_Helper; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 18 | |
| 19 | /** |
| 20 | * User_Listing class with user listing filters |
| 21 | */ |
| 22 | if ( ! class_exists( '\WP2FA\Admin\User_Listing' ) ) { |
| 23 | |
| 24 | /** |
| 25 | * User_Listing - Shows extra column in user table wit WP2FA status forevery user |
| 26 | */ |
| 27 | class User_Listing { |
| 28 | |
| 29 | /** |
| 30 | * The users table column name |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private static $column_name = '2fa-status'; |
| 35 | |
| 36 | /** |
| 37 | * Inits all the hooks used for showing the extra user data in the users column |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public static function init() { |
| 42 | add_filter( 'manage_users_columns', array( __CLASS__, 'add_wp_2fa_column' ) ); |
| 43 | add_filter( 'wpmu_users_columns', array( __CLASS__, 'add_wp_2fa_column' ) ); |
| 44 | add_filter( 'manage_users_custom_column', array( __CLASS__, 'show_column_data' ), 10, 3 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets the column in the admin users table |
| 49 | * |
| 50 | * @param array $columns - Array with all the columns. |
| 51 | * |
| 52 | * @return array |
| 53 | */ |
| 54 | public static function add_wp_2fa_column( array $columns ): array { |
| 55 | $columns[ self::$column_name ] = __( '2FA Status', 'wp-2fa' ); |
| 56 | return $columns; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Shows the user WP 2FA status data in the users table |
| 61 | * |
| 62 | * @param [type] $value - The value of the column. |
| 63 | * @param string $column_name - The name of the column. |
| 64 | * @param [type] $user_id - the ID of the user. |
| 65 | * |
| 66 | * @return mixed |
| 67 | */ |
| 68 | public static function show_column_data( $value, string $column_name, $user_id ) { |
| 69 | |
| 70 | switch ( $column_name ) { |
| 71 | case self::$column_name: |
| 72 | return self::get_user2fa_status( $user_id ); |
| 73 | default: |
| 74 | } |
| 75 | |
| 76 | return $value; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Retrieves the translated 2FA status label for given user. |
| 81 | * |
| 82 | * This is performance optimized version that bypasses the User class on purpose. It loads the 2FA status meta |
| 83 | * field directly and turns it into a label. |
| 84 | * |
| 85 | * There is also some temporary code to figure out the 2FA status meta field if it doesn't exist. This will be |
| 86 | * removed in future versions and exist purely so we don't end up with no values in the column after migration |
| 87 | * to version 1.7.0 when this was introduced. |
| 88 | * |
| 89 | * @param int $user_id - The id of the user for which the info should be extracted. |
| 90 | * |
| 91 | * @return string |
| 92 | * @see WP2FA\Admin\User |
| 93 | * @since 1.7.0 |
| 94 | */ |
| 95 | private static function get_user2fa_status( $user_id ) { |
| 96 | // try to get the user status "id" from user's meta data. |
| 97 | $status_meta_value = User_Helper::get_2fa_status( $user_id ); |
| 98 | if ( ! empty( $status_meta_value ) ) { |
| 99 | // the status id is available, grab the label to display. |
| 100 | $status_data = User_Utils::extract_statuses( array( $status_meta_value ) ); |
| 101 | if ( ! empty( $status_data ) ) { |
| 102 | return $status_data['label']; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // If the user status is not saved in user meta (this can be the case prior to version 1.7.0), we figure it |
| 107 | // out and store it against the user in DB. This is not ideal in terms of performance and this is only |
| 108 | // a temporary solution. |
| 109 | // @todo remove this in future versions. |
| 110 | return User::set_user_status( new \WP_User( $user_id ) ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the users table column name |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | public static function get_column_name(): string { |
| 119 | return self::$column_name; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 |