#233: Add WP-CLI support with wp two-factor commands#905
Open
masteradhoc wants to merge 6 commits into
Open
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @mikeselander, @gurumark. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
wp two-factor commands for per-user 2FA inspection and resetwp two-factor commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Adds a
wp two-factorWP-CLI namespace with six subcommands for inspecting and managing two-factor authentication on a per-user basis.Fixes #233
Why?
The plugin ships no WP-CLI commands today. This PR adds support for it.
How?
cli/class-two-factor-cli-command.phpcontainingTwo_Factor_CLI_Command extends WP_CLI_Command. Registered underWP_CLI::add_command( 'two-factor', ... )in the plugin bootstrap behind adefined( 'WP_CLI' ) && WP_CLIguard.Two_Factor_Core::clear_login_rate_limit( $user )added toclass-two-factor-core.php. Both thedisable(full reset) andunlockcommands call this single method rather than deleting the rate-limit meta keys at each call site.Two_Factor_Coreand provider APIs — no raw SQL, no duplicated logic.Commands:
wp two-factor status <user>--formatwp two-factor disable <user> [<provider>]--yesskips promptwp two-factor list-providerswp two-factor enable <user> <provider>wp two-factor backup-codes generate <user> [--count=<n>]wp two-factor unlock <user>All commands accept user by ID, login, or email.
disable(full reset) assertsget_available_providers_for_user()is empty after clearing state, guarding against the fail-closed email fallback.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 4.6
Used for: Full implementation of the CLI class, helper method, and bootstrap wiring, based on the given specification from my side. All generated code was reviewed and tested manually before submission.
Testing Instructions
Setup: Activate the Two-Factor plugin. Create a test user and enable at least one 2FA provider (TOTP or Email) via their profile page.
Registration
wp help two-factor— confirm all six subcommands are listedwp help two-factor disable— confirm OPTIONS and EXAMPLES are shownUser resolution
wp two-factor status 1— resolves by numeric IDwp two-factor status <login>— resolves by loginwp two-factor status <email>— resolves by emailwp two-factor status nobody— printsError: User not found: nobodystatususing_2faisfalse--format=jsonreturns valid JSONlist-providersWP_DEBUGis off--format=jsonworksdisable— full resetwp two-factor disable <user>→ prompts for confirmationwp two-factor disable <user> --yes→ no prompt;statusshowsusing_2fa: false_two_factor_enabled_providersin the DB to a non-existent class name, then rundisable --yes— command should succeed and not leave email 2FA activedisable— single providerwp two-factor disable <user> Two_Factor_Totp→ only TOTP removed; backup codes still appear instatusenablewp two-factor enable <user> Two_Factor_Email→ success; appears instatuswp two-factor enable <user> Two_Factor_Totp→ error referencing profile pagewp two-factor enable <user> Two_Factor_Backup_Codes→ error pointing tobackup-codes generatewp two-factor enable <user> FakeClass→ error "Is it a registered provider?"backup-codes generatewp two-factor backup-codes generate <user>→ prints 10 codes--count=5→ prints exactly 5 codesunlockwp two-factor unlock <user>→ "Login throttle cleared"Screenshots or screencast
N/A — CLI only, no UI changes.
Changelog Entry
Added - WP-CLI
wp two-factorcommands for per-user 2FA status, disable, enable, backup-code generation, and login-throttle reset.