Skip to content

Commit d2c9ecc

Browse files
committed
Move the form into a helper for conditional rendering
1 parent acba8c0 commit d2c9ecc

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

class-two-factor-core.php

+32-17
Original file line numberDiff line numberDiff line change
@@ -1841,12 +1841,11 @@ public static function user_two_factor_options( $user ) {
18411841
wp_enqueue_style( 'user-edit-2fa', plugins_url( 'user-edit.css', __FILE__ ), array(), TWO_FACTOR_VERSION );
18421842

18431843
$enabled_providers = array_keys( self::get_available_providers_for_user( $user ) );
1844-
$primary_provider_key = self::get_primary_provider_key_selected_for_user( $user );
18451844

18461845
// This is specific to the current session, not the displayed user.
18471846
$show_2fa_options = self::current_user_can_update_two_factor_options();
18481847

1849-
if ( ! $show_2fa_options ) {
1848+
if ( $providers && ! $show_2fa_options ) {
18501849
$url = add_query_arg(
18511850
'redirect_to',
18521851
urlencode( self::get_user_settings_page_url( $user->ID ) . '#two-factor-options' ),
@@ -1860,12 +1859,12 @@ public static function user_two_factor_options( $user ) {
18601859
);
18611860
}
18621861

1863-
printf(
1864-
'<fieldset id="two-factor-options" %s>',
1865-
$show_2fa_options ? '' : 'disabled="disabled"'
1866-
);
1862+
if ( empty( $providers ) ) {
1863+
$notices['notice two-factor-notice-no-providers-supported'] = esc_html__( 'No providers are available for your account.', 'two-factor' );
1864+
}
18671865

1868-
if ( 1 === count( $enabled_providers ) ) {
1866+
// Suggest enabling a backup method if only method is enabled and there are more available.
1867+
if ( count( $providers ) > 1 && 1 === count( $enabled_providers ) ) {
18691868
$notices['warning two-factor-warning-suggest-backup'] = esc_html__( 'To prevent being locked out of your account, consider enabling a backup method like Recovery Codes in case you lose access to your primary authentication method.', 'two-factor' );
18701869
}
18711870
?>
@@ -1876,9 +1875,34 @@ public static function user_two_factor_options( $user ) {
18761875
<p><?php echo wp_kses_post( $notice ); ?></p>
18771876
</div>
18781877
<?php endforeach; ?>
1878+
1879+
<?php
1880+
if ( $providers ) {
1881+
self::render_user_providers_form( $user, $providers );
1882+
}
1883+
1884+
/**
1885+
* Fires after the Two Factor methods table.
1886+
*
1887+
* To be used by Two Factor methods to add settings UI.
1888+
*
1889+
* @param WP_User $user The user.
1890+
* @param array $providers List of providers available to the user.
1891+
*
1892+
* @since 0.1-dev
1893+
*/
1894+
do_action( 'show_user_security_settings', $user, $providers );
1895+
}
1896+
1897+
private static function render_user_providers_form( $user, $providers ) {
1898+
$primary_provider_key = self::get_primary_provider_key_selected_for_user( $user );
1899+
1900+
?>
18791901
<p>
1880-
<?php esc_html_e( 'Configure a primary two-factor method along with a backup method, such as Recovery Codes, to avoid being locked out if you lose access to your primary method.', 'two-factor' ); ?>
1902+
<?php esc_html_e( 'Configure a primary two-factor method along with a backup method, such as Recovery Codes, to avoid being locked out if you lose access to your primary method.', 'two-factor' ); ?>
18811903
</p>
1904+
1905+
<fieldset id="two-factor-options" <?php echo $show_2fa_options ? '' : 'disabled="disabled"'; ?>>
18821906
<?php wp_nonce_field( 'user_two_factor_options', '_nonce_user_two_factor_options', false ); ?>
18831907
<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. */ ?>" />
18841908

@@ -1931,15 +1955,6 @@ public static function user_two_factor_options( $user ) {
19311955
</table>
19321956
</fieldset>
19331957
<?php
1934-
1935-
/**
1936-
* Fires after the Two Factor methods table.
1937-
*
1938-
* To be used by Two Factor methods to add settings UI.
1939-
*
1940-
* @since 0.1-dev
1941-
*/
1942-
do_action( 'show_user_security_settings', $user );
19431958
}
19441959

19451960
/**

0 commit comments

Comments
 (0)