Skip to content

Networks and Sites: Guard against null return values from get_site() - #16139

Open
robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/66760-get-site-null-checks
Open

robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/66760-get-site-null-checks

Conversation

@robelsust

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/66760

Description

get_site() can return null (for example, in custom sunrise.php setups where the current site is not yet defined on wp-login.php). Several core call sites access properties directly on the return value of get_site() without checking for null, emitting PHP warnings.

Changes

  • src/wp-includes/class-wp-network.php: Check $site before reading $site->domain and $site->path in WP_Network::get_main_site_id().
  • src/wp-includes/ms-load.php: Return true early in ms_site_check() when get_site() returns null.
  • src/wp-includes/general-template.php: Check $blog_details before reading $blog_details->path in wp_lostpassword_url().
  • src/wp-admin/menu.php: Check get_site() before reading ->deleted when registering the Tools → Delete Site submenu item.
  • src/wp-includes/class-wp-xmlrpc-server.php: Guard $current_blog->domain and $current_blog->path in wp_xmlrpc_server::_multisite_getUsersBlogs().
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: Pass get_current_blog_id() instead of get_site()->id to add_user_to_blog().
  • Added PHPUnit tests in:
    • tests/phpunit/tests/multisite/getMainSiteId.php
    • tests/phpunit/tests/general/wpLoginScreenUrls.php
    • tests/phpunit/tests/multisite/msSiteCheck.php

Testing

  • vendor/bin/phpcs — 0 errors / 0 warnings on all changed lines
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml tests/phpunit/tests/multisite/getMainSiteId.php — OK (11 tests, 14 assertions)
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml tests/phpunit/tests/general/wpLoginScreenUrls.php — OK (10 tests, 16 assertions)
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml tests/phpunit/tests/multisite/msSiteCheck.php — OK (1 test, 1 assertion)
  • vendor/bin/phpunit tests/phpunit/tests/general/wpLoginScreenUrls.php — OK (8 tests, 14 assertions)

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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.

1 participant