Skip to content

Fix/utils get site null guard#4319

Open
thisismyurl wants to merge 3 commits into
10up:developfrom
thisismyurl:fix/utils-get-site-null-guard
Open

Fix/utils get site null guard#4319
thisismyurl wants to merge 3 commits into
10up:developfrom
thisismyurl:fix/utils-get-site-null-guard

Conversation

@thisismyurl

Copy link
Copy Markdown

Description of the Change

closes #4318

How to test the Change

Changelog Entry

Added - New feature
Changed - Existing functionality
Deprecated - Soon-to-be removed feature
Removed - Feature
Fixed - Bug fix
Security - Vulnerability
Developer - Non-functional update

Credits

Props @username, ...

Checklist:

…tent site IDs

On PHP 8.x, calling \get_site() with an ID that does not exist returns
null rather than a WP_Site object. The wrapper Utils\\get_site() was
passing that null directly to array property access, causing a fatal
TypeError. is_site_indexable() then received false-y data and could
also propagate a null return where an array is expected.

Fix: add an instanceof WP_Site guard in get_site() that returns []
when the core function returns null, and an empty() guard in
is_site_indexable() that short-circuits to false for non-existent
site IDs. Both changes are consistent with the existing @return array
contract and the behavior the callers already expect for non-indexable
sites.
…t site IDs

Two tests covering the null-guard introduced in the prior commit:
- testGetSiteReturnsEmptyArrayForNonexistentSite
- testIsSiteIndexableReturnsFalseForNonexistentSite

Both are tagged @group skip-on-single-site since the functions are
multisite-only paths.
Copilot AI review requested due to automatic review settings June 11, 2026 15:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens multisite utility functions to handle non-existent site IDs safely and adds regression tests to cover the new behavior.

Changes:

  • Return an empty array from ElasticPress\Utils\get_site() when WordPress can’t resolve the site ID.
  • Avoid calling get_site_meta() in is_site_indexable() when the site lookup fails.
  • Add multisite-only tests for both behaviors.

Reviewed changes

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

File Description
tests/php/TestUtils.php Adds multisite-only regression tests for non-existent site IDs.
includes/utils.php Adds guards for missing sites in get_site() and is_site_indexable().

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

Comment thread includes/utils.php
Comment on lines 307 to +312
function get_site( $site_id ) {
$site = \get_site( $site_id );

if ( ! $site instanceof \WP_Site ) {
return [];
}
Comment thread tests/php/TestUtils.php
Comment on lines +110 to +113
public function testGetSiteReturnsEmptyArrayForNonexistentSite() {
$result = ElasticPress\Utils\get_site( PHP_INT_MAX );
$this->assertSame( [], $result );
}
@thisismyurl

Copy link
Copy Markdown
Author

Thanks for the review — addressing both points:

PHP_INT_MAX in tests — Valid concern. I pushed a follow-up commit that replaces PHP_INT_MAX with 999999 — a fixed large value that stays in a typical 32-bit range and is deterministic across environments.

Returning [] vs null — The empty array is intentional here. The existing callers (including is_site_indexable() in this same PR) guard with empty($site), which is falsy for both null and []. Returning [] keeps the return type consistent (array) without introducing a type union, and empty() handles it cleanly at the call site. I've noted in the function's docblock that an empty array is returned when the site is not found, so callers know to guard accordingly. Happy to discuss further if the maintainers prefer a different contract — just wanted to explain the reasoning.

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.

Utils\get_site() fatal TypeError on PHP 8.x when site ID does not exist

2 participants