Skip to content

Fix fatal in after_provider_deactivated() when providers return WP_Error#343

Merged
obenland merged 3 commits intotrunkfrom
fix/handle-wp-error-in-after-provider-deactivated
Mar 24, 2026
Merged

Fix fatal in after_provider_deactivated() when providers return WP_Error#343
obenland merged 3 commits intotrunkfrom
fix/handle-wp-error-in-after-provider-deactivated

Conversation

@obenland
Copy link
Copy Markdown
Member

@obenland obenland commented Mar 23, 2026

Summary

Fixes fatal errors and login failures on wp.org caused by get_available_providers_for_user() returning a WP_Error after the backwards-incompatible change in WordPress/two-factor#586.

  • require_ordinary_provider(): Clean up the raw user meta when stripping all providers, so get_available_providers_for_user() returns an empty array instead of a WP_Error. This is the root cause fix — it was blocking users from logging in with "You have Two Factor method(s) enabled, but the provider(s) no longer exist."
  • after_provider_deactivated(): Remove the stale workaround for Require an "ordinary" provider to use Backup Codes #164 (landed in decc620) and guard against WP_Error. This fixes the Cannot use object of type WP_Error as array fatal.
  • REST API 2fa_available_providers field: Guard against WP_Error return from get_available_providers_for_user().

Test plan

  • All existing tests pass
  • New tests for all three fixes
  • New tests fail without the fix, pass with it

🤖 Generated with Claude Code

Remove the stale workaround for #164 (merged in decc620) that unset
backup codes from available providers — require_ordinary_provider()
already handles this.

Handle get_available_providers_for_user() returning a WP_Error, a
change introduced in WordPress/two-factor#586.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a fatal error in after_provider_deactivated() when the upstream Two-Factor plugin returns a WP_Error from Two_Factor_Core::get_available_providers_for_user(), and adds coverage to prevent regressions.

Changes:

  • Remove the stale workaround that unsets Backup Codes from the available providers list.
  • Treat WP_Error from get_available_providers_for_user() as “no providers available” and clear the 2FA session meta accordingly.
  • Add a PHPUnit test validating that after_provider_deactivated() does not fatal and clears the session when a WP_Error is returned.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
wporg-two-factor.php Removes the Backup Codes workaround and adds WP_Error handling when checking available providers.
tests/test-wporg-two-factor.php Adds a regression test to ensure deactivation handling is safe when available providers resolution returns WP_Error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test-wporg-two-factor.php Outdated
@obenland obenland requested a review from Copilot March 23, 2026 15:56
@obenland obenland force-pushed the fix/handle-wp-error-in-after-provider-deactivated branch 3 times, most recently from 07c13aa to 01eee37 Compare March 23, 2026 16:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wporg-two-factor.php
Comment thread tests/test-wporg-two-factor.php
…ites

- require_ordinary_provider(): Clean up raw user meta when stripping all
  providers, preventing the WP_Error that blocks users from logging in.
- REST API 2fa_available_providers field: Guard against WP_Error return.
- after_provider_deactivated(): Guard against WP_Error return (previous commit).

Fixes the backwards-incompatible change introduced in
WordPress/two-factor#586.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@obenland obenland force-pushed the fix/handle-wp-error-in-after-provider-deactivated branch from 01eee37 to 702dbf4 Compare March 23, 2026 16:03
@obenland obenland requested a review from Copilot March 23, 2026 16:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/settings/test-rest-api.php Outdated
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@obenland obenland force-pushed the fix/handle-wp-error-in-after-provider-deactivated branch from 63529b3 to 1d1400a Compare March 23, 2026 16:10
Comment thread wporg-two-factor.php
@obenland obenland merged commit 0f2f03c into trunk Mar 24, 2026
2 checks passed
@obenland obenland deleted the fix/handle-wp-error-in-after-provider-deactivated branch March 24, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants