Skip to content

Add an opt-in optimized front-end My Sites toolbar #1716

Description

@superdav42

Objective

Add an opt-in Ultimate Multisite setting that reduces authenticated front-end toolbar overhead on large networks by limiting the WordPress My Sites dropdown to the current site.

The complete site list must remain available from WordPress's My Sites administration page and all wp-admin/network-admin screens.

Evidence

Production-scale local profiling used 114 network sites and a super administrator whose toolbar exposed 26 sites.

  • Authenticated front-end baseline: 31,182 object-cache lookups.
  • Restricting the toolbar fixture to one visible site: 25,756 lookups.
  • Reduction: 5,426 lookups (17.4%) for one request.
  • switch_blog tracing recorded 160 events. Two toolbar-related paths each produced 26 switch/restore pairs:
    • WP_Ultimo\Managers\Site_Manager::hide_customer_sites_from_super_admin_list()
    • WordPress core wp_admin_bar_my_sites_menu()
  • Every switch initializes active Polylang and WooCommerce integrations for that blog, amplifying options, network-options, users, terms, and translation cache traffic.

The issue is not the database query that selects sites. It is eagerly preparing and rendering every site in the authenticated front-end toolbar.

Reproducer

  1. Configure a multisite network with at least 25 sites visible to a super administrator.
  2. Enable Ultimate Multisite and log in as that super administrator.
  3. Request a front-end page with the WordPress toolbar enabled.
  4. Trace switch_blog/restore_current_blog and object-cache lookups for the request.
  5. Observe one switch/restore pair per visible site from hide_customer_sites_from_super_admin_list() and another pair per site from wp_admin_bar_my_sites_menu().
  6. Repeat with only the current site exposed to the toolbar; compare switch and cache-read counts.

Symptom command (run with an authenticated cookie against the local multisite fixture while request-scoped cache and switch_blog tracers are active):

curl -sS -H 'Cookie: <authenticated-super-admin-cookie>' https://<local-multisite-host>/ > /dev/null

Actual output:

object-cache lookups: 31,182
switch_blog events: 160
Site_Manager toolbar-related pairs: 26
WordPress My Sites menu pairs: 26

Expected output:

When the opt-in setting is enabled, front-end toolbar preparation is bounded to the current site and does not add one switch/restore pair per visible network site. Administrative My Sites listings remain complete.

Causal status: confirmed

Owning-path proof:

  • Production entry point: WP_Admin_Bar::initialize() calls get_blogs_of_user() while preparing an authenticated toolbar.
  • Call chain: get_blogs_of_user()pre_get_blogs_of_userSite_Manager::hide_customer_sites_from_super_admin_list() → per-site details; core later runs wp_admin_bar_my_sites_menu()switch_to_blog() for every returned site.
  • Integrated verification: the same authenticated front-end request fell from 31,182 to 25,756 object-cache lookups when the toolbar fixture exposed only the current site.

Proposed setting

Add an opt-in toggle under Ultimate Multisite → Settings → Sites → Site Options:

Optimize the front-end My Sites toolbar

Show only the current site in the front-end toolbar to reduce processing on large networks. The complete site list remains available from the My Sites administration page.

Suggested setting key: optimize_frontend_my_sites_toolbar.

  • Default to false for backward compatibility.
  • Apply only to authenticated front-end requests where the toolbar is displayed.
  • Keep existing behavior unchanged in site admin, network admin, user admin, REST, AJAX, cron, and WP-CLI requests.
  • Preserve the current-site Dashboard/Visit Site links, the top-level My Sites link, and Network Admin links.
  • Do not alter membership, site ownership, or capability results outside the toolbar optimization path.

Implementation context

Expected files:

  • inc/class-settings.php
    • Register the toggle in the existing sites section near the Site Options fields.
    • Follow existing add_field(..., ['type' => 'toggle', 'default' => 0]) patterns.
  • inc/managers/class-site-manager.php
    • init() registers hide_customer_sites_from_super_admin_list() on pre_get_blogs_of_user at priority 999.
    • Add a narrowly scoped fast path when the setting is enabled and the request is the authenticated front-end toolbar path.
    • Return only the current/active site needed by WP_Admin_Bar; avoid reading blogname/siteurl details for every eligible site.
    • Leave the existing full-list path untouched in administrative and non-toolbar contexts.
  • tests/WP_Ultimo/Managers/Site_Manager_Test.php
    • Extend the existing tests around hide_customer_sites_from_super_admin_list().
  • tests/WP_Ultimo/Settings_Test.php
    • Verify registration and the backward-compatible default.

If safely distinguishing the toolbar's nested get_blogs_of_user() call requires a small dedicated helper or lifecycle flag, keep that implementation internal to Ultimate Multisite and document why the scope cannot rely on ! is_admin() alone.

Acceptance criteria

  • The new setting exists, is documented in its field description, and defaults off.
  • With the setting off, the existing site list and toolbar behavior are unchanged.
  • With the setting on, an authenticated front-end toolbar request prepares only the current site.
  • The full site list remains available in wp-admin/my-sites.php, site admin, and network admin.
  • Non-toolbar calls to get_blogs_of_user() retain complete, correctly filtered results.
  • Super-admin filtering and customer-owned-site hiding remain correct.
  • The toolbar retains current-site, My Sites, and Network Admin navigation.
  • Tests cover enabled/disabled, front-end/admin, toolbar/non-toolbar, super-admin/non-super-admin, and multisite edge cases.
  • A performance regression test or bounded instrumentation demonstrates that toolbar work no longer scales linearly with the number of visible sites when enabled.

Verification

Run:

vendor/bin/phpcs inc/class-settings.php inc/managers/class-site-manager.php tests/WP_Ultimo/Managers/Site_Manager_Test.php tests/WP_Ultimo/Settings_Test.php
vendor/bin/phpunit --filter Site_Manager_Test
vendor/bin/phpunit --filter Settings_Test

Then verify in a multisite fixture with at least 25 visible sites:

  1. Setting off: full front-end toolbar site list remains present.
  2. Setting on: front-end toolbar shows only the current site plus My Sites/Network Admin navigation.
  3. Setting on: wp-admin/my-sites.php and network-admin site listings remain complete.
  4. Compare switch_blog counts before and after; the optimized front-end request should not perform one switch/restore pair per visible toolbar site.

Out of scope

  • Changing WordPress core toolbar behavior globally.
  • Removing the toolbar entirely.
  • Altering site ownership or membership visibility.
  • Optimizing unrelated Polylang or WooCommerce callbacks.

Metadata

Metadata

Assignees

Labels

auto-dispatchenhancementNew feature or requestorigin:interactiveCreated by interactive user sessionpriority:mediumMedium severity — moderate quality issuestatus:in-reviewPR open, awaiting review/mergetier:standardAuto-created by pulse labelless backfill (t2112)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions