google / google/site-kit-wp

Address multisite PHP Warning: Undefined array key 0 in Has_Multiple_Admins.php line 93

Open Beginner friendly
#12,671 0 comments 0 reactions 0 assignees View on GitHub
Type: Bug Type: Support
Dominant language
JavaScript
Stars
1.4k
Forks
383
Avg merge
4d 14h
Merged PRs (30d)
77

Description

## Bug Description

As reported by [a user in the support forums](https://wordpress.org/support/topic/php-warning-undefined-array-key-0-in-has_multiple_admins-php-line-93-multisite/), in multisite environments, it's possible to encounter a PHP warning for PHP 8.2 and upwards on a multisite environment when returning a list of returns an array of usernames via `superadmins()` if that array has been filtered or manipulated by another plugin.

See the function and associated actions in [Has_Multiple_Admins.php](https://github.com/google/site-kit-wp/blob/e1e7f298e4acd8ac7cd0c9735b6f1802598d0c18/includes/Core/Authentication/Has_Multiple_Admins.php#L82).

## Screenshots

## Additional Context

- PHP Version:
- OS: [e.g. iOS]
- Browser: [e.g. chrome, safari]
- Plugin Version: [e.g. 22]
- Device: [e.g. iPhone6]

---------------

_Do not alter or remove anything below. The following sections will be managed by moderators only._

## Steps to Reproduce

- Set up a WordPress multisite environment with Site Kit active.
- Add a temporary mu-plugin that forces the multisite super admin list to be sparse, for example:

```php
'admin',
);
} );
```

- Open the Site Kit setup splash screen at `wp-admin/admin.php?page=googlesitekit-splash` while logged in as an admin user.
- Let the page load fully so it requests `GET /wp-json/google-site-kit/v1/core/site/data/connection`.
- That request reaches `Has_Multiple_Admins::get()` and triggers the warning when index `0` is missing.
- Observe the PHP warning: `Undefined array key 0` in `includes/Core/Authentication/Has_Multiple_Admins.php`.

## Acceptance Criteria

- There should be no php warning when the filtered super admin array is non-zero based index.

## Implementation Brief

- [ ] Update `includes/Core/Authentication/Has_Multiple_Admins.php` so the multisite branch does not assume the first super admin is stored at array index `0`.
- Normalize the `get_super_admins()` result or safely read the first login value before calling `get_user_by()`, so the multisite branch works with sparse or otherwise non-sequential array keys.
- Only add the resolved user ID to the exclusion list when the lookup returns a valid user object.

### Test Coverage

- Update `tests/phpunit/integration/Core/Authentication/Has_Multiple_AdminsTest.php`, add a regression case where the super admin list is sparse or otherwise non-sequentially keyed.
- Assert that the warning condition is avoided and the resulting admin count still matches the expected total.

## QA Brief

*

## Changelog entry

*

Contributor guide

Open the contributing guide

Research direction

Start with includes/Core/Authentication/Has_Multiple_Admins.php around the multisite branch and review tests/phpunit/integration/Core/Authentication/Has_Multiple_AdminsTest.php. Run the existing test before adding a regression case with a sparse super admin list. Done means the case produces no PHP warning and the resulting admin count remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
authentication, backend, testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.