Skip to content

Commit 953b7c9

Browse files
authored
Simplify the Two Factor settings in user profile
2 parents 80e76ef + 54f34e5 commit 953b7c9

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

class-two-factor-core.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,25 @@ public static function get_provider_for_user( $user = null, $preferred_provider
571571
return self::get_primary_provider_for_user( $user );
572572
}
573573

574+
/**
575+
* Get the name of the primary provider selected by the user
576+
* and enabled for the user.
577+
*
578+
* @param WP_User|int $user User ID or instance.
579+
*
580+
* @return string|null
581+
*/
582+
private static function get_primary_provider_key_selected_for_user( $user ) {
583+
$primary_provider = get_user_meta( $user->ID, self::PROVIDER_USER_META_KEY, true );
584+
$available_providers = self::get_available_providers_for_user( $user );
585+
586+
if ( ! empty( $primary_provider ) && ! empty( $available_providers[ $primary_provider ] ) ) {
587+
return $primary_provider;
588+
}
589+
590+
return null;
591+
}
592+
574593
/**
575594
* Gets the Two-Factor Auth provider for the specified|current user.
576595
*
@@ -594,7 +613,7 @@ public static function get_primary_provider_for_user( $user = null ) {
594613
} elseif ( 1 === count( $available_providers ) ) {
595614
$provider = key( $available_providers );
596615
} else {
597-
$provider = get_user_meta( $user->ID, self::PROVIDER_USER_META_KEY, true );
616+
$provider = self::get_primary_provider_key_selected_for_user( $user );
598617

599618
// If the provider specified isn't enabled, just grab the first one that is.
600619
if ( ! isset( $available_providers[ $provider ] ) ) {
@@ -1788,12 +1807,7 @@ public static function user_two_factor_options( $user ) {
17881807
wp_enqueue_style( 'user-edit-2fa', plugins_url( 'user-edit.css', __FILE__ ), array(), TWO_FACTOR_VERSION );
17891808

17901809
$enabled_providers = array_keys( self::get_available_providers_for_user( $user ) );
1791-
$primary_provider = self::get_primary_provider_for_user( $user->ID );
1792-
1793-
$primary_provider_key = null;
1794-
if ( ! empty( $primary_provider ) && is_object( $primary_provider ) ) {
1795-
$primary_provider_key = $primary_provider->get_key();
1796-
}
1810+
$primary_provider_key = self::get_primary_provider_key_selected_for_user( $user );
17971811

17981812
// This is specific to the current session, not the displayed user.
17991813
$show_2fa_options = self::current_user_can_update_two_factor_options();
@@ -1822,6 +1836,7 @@ public static function user_two_factor_options( $user ) {
18221836
}
18231837
?>
18241838
<h2><?php esc_html_e( 'Two-Factor Options', 'two-factor' ); ?></h2>
1839+
18251840
<?php foreach ( $notices as $notice_type => $notice ) : ?>
18261841
<div class="<?php echo esc_attr( $notice_type ? 'notice inline notice-' . $notice_type : '' ); ?>">
18271842
<p><?php echo wp_kses_post( $notice ); ?></p>
@@ -1832,21 +1847,17 @@ public static function user_two_factor_options( $user ) {
18321847
</p>
18331848
<?php wp_nonce_field( 'user_two_factor_options', '_nonce_user_two_factor_options', false ); ?>
18341849
<input type="hidden" name="<?php echo esc_attr( self::ENABLED_PROVIDERS_USER_META_KEY ); ?>[]" value="<?php /* Dummy input so $_POST value is passed when no providers are enabled. */ ?>" />
1835-
<table class="wp-list-table widefat fixed striped table-view-list two-factor-methods-table">
1836-
<thead>
1837-
<tr>
1838-
<th class="col-enabled" scope="col"><?php esc_html_e( 'Enabled', 'two-factor' ); ?></th>
1839-
<th class="col-primary" scope="col"><?php esc_html_e( 'Primary', 'two-factor' ); ?></th>
1840-
<th class="col-name" scope="col"><?php esc_html_e( 'Type', 'two-factor' ); ?></th>
1841-
</tr>
1842-
</thead>
1850+
1851+
<table class="form-table two-factor-methods-table" role="presentation">
18431852
<tbody>
18441853
<?php foreach ( self::get_providers() as $provider_key => $object ) : ?>
18451854
<tr>
1846-
<th scope="row"><input id="enabled-<?php echo esc_attr( $provider_key ); ?>" type="checkbox" name="<?php echo esc_attr( self::ENABLED_PROVIDERS_USER_META_KEY ); ?>[]" value="<?php echo esc_attr( $provider_key ); ?>" <?php checked( in_array( $provider_key, $enabled_providers, true ) ); ?> /></th>
1847-
<th scope="row"><input type="radio" name="<?php echo esc_attr( self::PROVIDER_USER_META_KEY ); ?>" value="<?php echo esc_attr( $provider_key ); ?>" <?php checked( $provider_key, $primary_provider_key ); ?> /></th>
1855+
<th><?php echo esc_html( $object->get_label() ); ?></th>
18481856
<td>
1849-
<label class="two-factor-method-label" for="enabled-<?php echo esc_attr( $provider_key ); ?>"><?php echo esc_html( $object->get_label() ); ?></label>
1857+
<label class="two-factor-method-label">
1858+
<input id="enabled-<?php echo esc_attr( $provider_key ); ?>" type="checkbox" name="<?php echo esc_attr( self::ENABLED_PROVIDERS_USER_META_KEY ); ?>[]" value="<?php echo esc_attr( $provider_key ); ?>" <?php checked( in_array( $provider_key, $enabled_providers, true ) ); ?> />
1859+
<?php echo esc_html( sprintf( __( 'Enable %s', 'two-factor' ), $object->get_label() ) ); ?>
1860+
</label>
18501861
<?php
18511862
/**
18521863
* Fires after user options are shown.
@@ -1864,13 +1875,25 @@ public static function user_two_factor_options( $user ) {
18641875
</tr>
18651876
<?php endforeach; ?>
18661877
</tbody>
1867-
<tfoot>
1878+
</table>
1879+
<hr />
1880+
<table class="form-table two-factor-primary-method-table" role="presentation">
1881+
<tbody>
18681882
<tr>
1869-
<th class="col-enabled" scope="col"><?php esc_html_e( 'Enabled', 'two-factor' ); ?></th>
1870-
<th class="col-primary" scope="col"><?php esc_html_e( 'Primary', 'two-factor' ); ?></th>
1871-
<th class="col-name" scope="col"><?php esc_html_e( 'Type', 'two-factor' ); ?></th>
1883+
<th><?php esc_html_e( 'Primary Method', 'two-factor' ) ?></th>
1884+
<td>
1885+
<select name="<?php echo esc_attr( self::PROVIDER_USER_META_KEY ); ?>">
1886+
<option value=""><?php echo esc_html( __( 'Default', 'two-factor' ) ); ?></option>
1887+
<?php foreach ( self::get_providers() as $provider_key => $object ) : ?>
1888+
<option value="<?php echo esc_attr( $provider_key ); ?>" <?php selected( $provider_key, $primary_provider_key ); ?> <?php disabled( ! in_array( $provider_key, $enabled_providers, true ) ); ?>>
1889+
<?php echo esc_html( $object->get_label() ); ?>
1890+
</option>
1891+
<?php endforeach; ?>
1892+
</select>
1893+
<p class="description"><?php esc_html_e( 'Select the primary method to use for two-factor authentication when signing into this site.', 'two-factor' ) ?></p>
1894+
</td>
18721895
</tr>
1873-
</tfoot>
1896+
</tbody>
18741897
</table>
18751898
</fieldset>
18761899
<?php
@@ -1984,6 +2007,8 @@ public static function user_two_factor_options_update( $user_id ) {
19842007
$new_provider = isset( $_POST[ self::PROVIDER_USER_META_KEY ] ) ? $_POST[ self::PROVIDER_USER_META_KEY ] : '';
19852008
if ( ! empty( $new_provider ) && in_array( $new_provider, $enabled_providers, true ) ) {
19862009
update_user_meta( $user_id, self::PROVIDER_USER_META_KEY, $new_provider );
2010+
} else {
2011+
delete_user_meta( $user_id, self::PROVIDER_USER_META_KEY );
19872012
}
19882013

19892014
// Have we changed the two-factor settings for the current user? Alter their session metadata.

providers/class-two-factor-totp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function register_rest_routes() {
110110
* Returns the name of the provider.
111111
*/
112112
public function get_label() {
113-
return _x( 'Authenticator app', 'Provider Label', 'two-factor' );
113+
return _x( 'Authenticator App', 'Provider Label', 'two-factor' );
114114
}
115115

116116
/**
@@ -119,7 +119,7 @@ public function get_label() {
119119
* @since 0.9.0
120120
*/
121121
public function get_alternative_provider_label() {
122-
return __( 'Use your authenticator app', 'two-factor' );
122+
return __( 'Use your authenticator app for time-based one-time passwords (TOTP)', 'two-factor' );
123123
}
124124

125125
/**

tests/providers/class-two-factor-totp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function test_get_instance() {
5858
* @covers Two_Factor_Totp::get_label
5959
*/
6060
public function test_get_label() {
61-
$this->assertStringContainsString( 'Authenticator app', $this->provider->get_label() );
61+
$this->assertStringContainsString( 'Authenticator App', $this->provider->get_label() );
6262
}
6363

6464
/**

user-edit.css

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
.two-factor-methods-table .col-primary,
2-
.two-factor-methods-table .col-enabled {
3-
width: 5%;
4-
}
5-
6-
.two-factor-methods-table .col-name {
7-
width: 90%;
8-
}
9-
10-
.two-factor-methods-table tbody th {
11-
text-align: center;
12-
}
13-
141
.two-factor-methods-table tbody th,
152
.two-factor-methods-table tbody td {
163
vertical-align: top;

0 commit comments

Comments
 (0)