@@ -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.
0 commit comments