-
Notifications
You must be signed in to change notification settings - Fork 186
Alternative to #741 - Autosubmit Tweak #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e17c5b3
1fada25
23b8907
698c7a5
e9b935d
4659913
18732c9
3d3b67e
25cea8e
d632012
11f8a94
92887fe
a64db22
fb80aac
34415d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -144,15 +144,18 @@ public static function is_supported_for_user( $user = null ) { | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Generate a random eight-digit string to send out as an auth code. | ||||||
| * Generate a random string to send out as an auth code. Default is an 8 digit numeric code, but the length and characters can be customized. | ||||||
| * | ||||||
| * @since 0.1-dev | ||||||
| * | ||||||
| * @param int $length The code length. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| * @param string|array $chars Valid auth code characters. | ||||||
| * @return string | ||||||
| */ | ||||||
| public static function get_code( $length = 8, $chars = '1234567890' ) { | ||||||
| public static function get_code( $length = null, $chars = '1234567890' ) { | ||||||
| if ( is_null( $length ) ) { | ||||||
| $length = self::get_code_length( 8, static::class ); | ||||||
| } | ||||||
| $code = ''; | ||||||
| if ( is_array( $chars ) ) { | ||||||
| $chars = implode( '', $chars ); | ||||||
|
|
@@ -163,6 +166,33 @@ public static function get_code( $length = 8, $chars = '1234567890' ) { | |||||
| return $code; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get the code length for a provider. | ||||||
| * | ||||||
| * @since 0.17.0 | ||||||
| * | ||||||
| * @param int $default_length Default code length if not filtered. | ||||||
| * @param string|null $provider The provider class name. Null uses the called class. | ||||||
| * @return int Number of characters. | ||||||
| */ | ||||||
| public static function get_code_length( $default_length = 8, $provider = null ) { | ||||||
| /** | ||||||
| * Filter the default code length for a provider. | ||||||
| * | ||||||
| * @since 0.17.0 | ||||||
| * | ||||||
| * @param int $code_length Length of the code. Default 8. | ||||||
| * @param string $provider The provider class name. | ||||||
| */ | ||||||
| $code_length = (int) apply_filters( | ||||||
| 'two_factor_code_length', | ||||||
| $default_length, | ||||||
| $provider ? $provider : static::class | ||||||
| ); | ||||||
|
|
||||||
| return $code_length; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Sanitizes a numeric code to be used as an auth code. | ||||||
| * | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -774,6 +774,10 @@ public function is_available_for_user( $user ) { | |||||||||||||||||
| * @codeCoverageIgnore | ||||||||||||||||||
| */ | ||||||||||||||||||
| public function authentication_page( $user ) { | ||||||||||||||||||
|
|
||||||||||||||||||
| /** This filter is documented in providers/class-two-factor-backup-codes.php */ | ||||||||||||||||||
| $code_length = apply_filters( 'two_factor_autosubmit_length', self::DEFAULT_DIGIT_COUNT, $this ); | ||||||||||||||||||
|
|
||||||||||||||||||
| require_once ABSPATH . '/wp-admin/includes/template.php'; | ||||||||||||||||||
| ?> | ||||||||||||||||||
| <?php | ||||||||||||||||||
|
|
@@ -789,7 +793,7 @@ public function authentication_page( $user ) { | |||||||||||||||||
| ?> | ||||||||||||||||||
| <p> | ||||||||||||||||||
| <label for="authcode"><?php esc_html_e( 'Authentication Code:', 'two-factor' ); ?></label> | ||||||||||||||||||
| <input type="text" inputmode="numeric" name="authcode" id="authcode" class="input authcode" value="" size="20" pattern="[0-9 ]*" placeholder="123 456" autocomplete="one-time-code" data-digits="<?php echo esc_attr( self::DEFAULT_DIGIT_COUNT ); ?>" /> | ||||||||||||||||||
| <input type="text" inputmode="numeric" name="authcode" id="authcode" class="input authcode" value="" size="20" pattern="[0-9 ]*" placeholder="123 456" autocomplete="one-time-code" data-digits="<?php echo esc_attr( $code_length ); ?>" /> | ||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we probably should work a bit on the placeholder as well. the space in between 3 and 4 doesnt make sense. additionally based on the code_length we could also generate this also dynamically. BTW the spacing after entering the numbers manually i dont feel its a great UX - what do you think?: two-factor/providers/js/two-factor-login-authcode.js Lines 16 to 21 in cbc73d5
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spacing is specifically for UX. My understanding is that studies have shown (and my own experience verified) that it is far easier to remember two groups of three digits, than a six digit number.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to generate it at the required length and split it in to groups? AI: Copilot / GPT-5.3-Codex |
||||||||||||||||||
| </p> | ||||||||||||||||||
| <?php | ||||||||||||||||||
| /** This action is documented in providers/class-two-factor-backup-codes.php */ | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,6 +88,54 @@ public function test_get_instance() { | |||||||
| $this->assertSame( $instance_one, $instance_two ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Test that get_code_length() returns the default value when no filter is applied. | ||||||||
| * | ||||||||
| * @covers Two_Factor_Provider::get_code_length | ||||||||
| */ | ||||||||
| public function test_get_code_length_returns_default() { | ||||||||
| $this->assertSame( 8, Two_Factor_Provider::get_code_length( 8 ) ); | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to do what the test says it does.
Suggested change
|
||||||||
| $this->assertSame( 6, Two_Factor_Provider::get_code_length( 6 ) ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Test that the two_factor_code_length filter can override the default code length. | ||||||||
| * | ||||||||
| * @covers Two_Factor_Provider::get_code_length | ||||||||
| */ | ||||||||
| public function test_get_code_length_filter_overrides_default() { | ||||||||
| $set_length_to_4 = function () { | ||||||||
| return 4; | ||||||||
| }; | ||||||||
| add_filter( 'two_factor_code_length', $set_length_to_4 ); | ||||||||
| $this->assertSame( 4, Two_Factor_Provider::get_code_length( 8 ) ); | ||||||||
| remove_filter( 'two_factor_code_length', $set_length_to_4 ); | ||||||||
|
|
||||||||
| $set_length_to_12 = function () { | ||||||||
| return 12; | ||||||||
| }; | ||||||||
| add_filter( 'two_factor_code_length', $set_length_to_12 ); | ||||||||
| $this->assertSame( 12, Two_Factor_Provider::get_code_length( 8 ) ); | ||||||||
| remove_filter( 'two_factor_code_length', $set_length_to_12 ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Test that get_code( null ) uses the filtered code length from two_factor_code_length. | ||||||||
| * | ||||||||
| * @covers Two_Factor_Provider::get_code | ||||||||
| * @covers Two_Factor_Provider::get_code_length | ||||||||
| */ | ||||||||
| public function test_get_code_with_null_uses_filtered_length() { | ||||||||
| $set_length_to_5 = function () { | ||||||||
| return 5; | ||||||||
| }; | ||||||||
| add_filter( 'two_factor_code_length', $set_length_to_5 ); | ||||||||
| $code = Two_Factor_Provider::get_code( null ); | ||||||||
| remove_filter( 'two_factor_code_length', $set_length_to_5 ); | ||||||||
|
|
||||||||
| $this->assertSame( 5, strlen( $code ) ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Verify get_key() returns the provider's class name. | ||||||||
| * | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.