Skip to content

Fix: Resolve admin user dynamically instead of hardcoding user ID 1#6270

Merged
swissspidy merged 19 commits intowp-cli:mainfrom
apermo:fix/admin-context-super-admin-resolution
Mar 16, 2026
Merged

Fix: Resolve admin user dynamically instead of hardcoding user ID 1#6270
swissspidy merged 19 commits intowp-cli:mainfrom
apermo:fix/admin-context-super-admin-resolution

Conversation

@apermo
Copy link
Copy Markdown
Contributor

@apermo apermo commented Mar 13, 2026

Summary

  • Replace hardcoded $admin_user_id = 1 fallback in Admin context with dynamic user resolution
  • On multisite: queries get_super_admins() via get_users() with login__in (single DB query) to find a valid super admin
  • On single site: queries for the first user with the administrator role
  • Emits a clear error message if no suitable user is found

Fixes #6269

Note

This PR was authored with the help of Claude Code (claude-opus-4-6).

Test plan

  • Verify wp plugin list works on multisite where user ID 1 is not a super admin
  • Verify wp --context=admin eval '' works on single site (resolves administrator)
  • Verify error message when no super admin exists on multisite
  • Verify error message when no administrator exists on single site
  • Run existing Behat test suite (features/context.feature)
  • Four new Behat scenarios added covering all paths

Replace hardcoded user ID 1 fallback in Admin
context with smart user resolution. On multisite,
queries get_super_admins() to find a valid super
admin. On single site, queries for the first user
with the administrator role. Emits a clear error
if no suitable user is found.

Fixes wp-cli#6269
@apermo apermo requested a review from a team as a code owner March 13, 2026 10:04
@github-actions
Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@github-actions github-actions bot added bug command:cli Related to 'cli' command labels Mar 13, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the WP_CLI\Context\Admin class to dynamically determine the appropriate admin user ID, moving away from a hardcoded fallback to user ID 1. This enhancement improves the robustness of WP-CLI commands run in an admin context, especially in multisite environments where user ID 1 might not be a super admin, and provides better error handling when no suitable admin user can be found.

Highlights

  • Dynamic Admin User Resolution: Replaced the hardcoded $admin_user_id = 1 fallback in the Admin context with dynamic user resolution, ensuring the correct admin user is identified based on the environment.
  • Multisite Super Admin Lookup: Implemented logic to query get_super_admins() on multisite installations to find a valid super admin, improving compatibility with complex WordPress setups.
  • Single Site Administrator Lookup: Implemented logic to query for the first user with the administrator role on single site installations, providing a robust fallback.
  • Improved Error Handling: Added clear error messages that are emitted when no suitable admin user (super admin on multisite or administrator on single site) is found, guiding users to specify one with --user=<login>.
  • New Behat Scenarios: Introduced three new Behat scenarios to thoroughly test the dynamic admin user resolution logic across different site configurations and error conditions.
Changelog
  • features/context.feature
    • Added a scenario to verify admin context resolves a super admin on multisite when no user is specified.
    • Added a scenario to verify admin context resolves an administrator on single site when no user is specified.
    • Added a scenario to verify admin context emits an error when no suitable admin user is found on multisite.
  • php/WP_CLI/Context/Admin.php
    • Replaced the hardcoded admin_user_id = 1 with a call to a new find_admin_user_id() method.
    • Implemented the find_admin_user_id() method to dynamically locate a super admin on multisite or an administrator on single site.
    • Added error handling within find_admin_user_id() to report if no suitable admin user is found.
Activity
  • The author, apermo, has implemented the changes to dynamically resolve the admin user ID.
  • A detailed test plan has been provided, including specific verification steps for multisite and single site scenarios, as well as error message validation.
  • Three new Behat scenarios have been added to the test suite to cover the new logic.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does a great job of removing the hardcoded admin user ID 1 and replacing it with a dynamic lookup. The new find_admin_user_id() method correctly handles both single-site and multisite installations, with clear error messages when no suitable user can be found. The added Behat tests cover the main scenarios for this new logic. I have a couple of suggestions to further improve the code: one for performance in the user lookup on multisite, and another to add a test case for the error condition on single-site installations.

apermo added 2 commits March 13, 2026 11:17
Use get_users() with login__in for single DB query
instead of looping get_user_by(). Add single-site
error case test. Replace wp super-admin commands
with direct option manipulation in tests.
Use single quotes for strings per PHPCS rules.
Fix gherkin use-and lint violations by replacing
consecutive When steps with And.
@apermo
Copy link
Copy Markdown
Contributor Author

apermo commented Mar 13, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses the issue of a hardcoded admin user ID by implementing dynamic user resolution for the admin context. The logic correctly handles both single-site and multisite installations, with clear error messages for cases where a suitable admin user cannot be found. The addition of comprehensive Behat tests ensures the new functionality is well-covered. I have one suggestion to slightly simplify the code for finding a super admin on multisite installations.

Copy link
Copy Markdown
Contributor

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

This PR updates the Admin context user resolution logic to avoid hardcoding user ID 1, selecting an appropriate admin user dynamically (super admin on multisite; administrator on single-site) and adding Behat coverage for the new behavior.

Changes:

  • Replaces the Admin context fallback user selection with find_admin_user_id() resolution logic.
  • Adds error messages when no suitable admin user can be found for the current environment.
  • Adds new Behat scenarios to cover multisite/single-site resolution and error paths.

Reviewed changes

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

File Description
php/WP_CLI/Context/Admin.php Introduces dynamic admin user resolution logic (super admin / administrator) instead of hardcoded ID 1.
features/context.feature Adds Behat scenarios intended to cover the new resolution behavior and error handling.

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

You can also share your feedback on Copilot code review. Take the survey.

apermo and others added 12 commits March 13, 2026 16:10
Revert multisite super admin lookup from get_users()
back to foreach + get_user_by('login') loop because
get_users() only fetches users on the current site
but a super admin might not be a member of any site.

Add debug logging after resolving admin user ID.
…e phar-bundled versions (wp-cli#6218)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
…6169)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
* Initial plan

* Add WP-CLI handbook URL reference to wp help output

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
… config key (wp-cli#6274)

* Initial plan

* Fix SSH alias path not forwarded to remote when path is a separate config key

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
* Initial plan

* Fix vagrant SSH strict host key checking failure

When ssh: vagrant is configured and vagrant ssh-config is parsed,
add -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null to
the SSH command to match what vagrant itself sets. This prevents
failures when the vagrant VM has been recreated and has a different
host key than what is stored in ~/.ssh/known_hosts.

Adds a Behat test scenario to verify the fix.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @swissspidy

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…e()` (wp-cli#6276)

* Initial plan

* Add optional $newline parameter to WP_CLI::log(), WP_CLI::line(), and logger info() methods

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

* Undo Base class change

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
@swissspidy swissspidy merged commit d5bbbfc into wp-cli:main Mar 16, 2026
69 checks passed
@apermo apermo deleted the fix/admin-context-super-admin-resolution branch March 16, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:cli Related to 'cli' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin context should resolve a super admin user on multisite instead of hardcoding user ID 1

6 participants