Skip to content

Fix - Unbounded get_users() full-table scan in theme-switch cleanup - #144

Open
deepench wants to merge 1 commit into
developfrom
fix/unbounded-get-users-theme-switch-cleanup
Open

deepench wants to merge 1 commit into
developfrom
fix/unbounded-get-users-theme-switch-cleanup

Conversation

@deepench

Copy link
Copy Markdown
Contributor

Changes proposed in this Pull Request:

Fixes the same issue as themegrill/spacious-pro#41, which was also filed against this free theme. When you switch away from Spacious to a different theme, two separate cleanup routines run to clear leftover notice-dismissal settings (one for the demo importer notice, one for the "please review this theme" notice). Both were fetching every single user account on the site as a full object, just to check one or two small settings for each of them. On a site with a large number of user accounts, this made switching themes unnecessarily slow. This fix makes both cleanups happen in a much lighter way, with no change in what actually gets cleaned up.

Note: the second routine (the theme-review notice cleanup) was not named in the original issue report, but has the identical pattern and is fixed here for the same reason.

How to test the changes in this Pull Request:

  1. Log in as an administrator on a site with Spacious active.
  2. Go to Appearance > Themes and switch the active theme to a different one, then switch back to Spacious (or to any other theme).
  3. The theme switch should complete normally, the same as before this change.

Types of changes:

  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (modification of the currently available functionality)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Other information:

  • Have you added an explanation of what your changes do and why you would like us to include them?
  • Have you successfully ran tests with your changes locally?
  • Have you updated the documentation accordingly?

Changelog entry

Fix - Unbounded get_users() full-table scan in theme-switch cleanup.

@tg-autopilot
tg-autopilot requested a lite review from Copilot September 25, 2026 06:33
@github-actions

Copy link
Copy Markdown

QA suite — refused, no regression spec

This PR changes product source but adds no spec, so the suite was
refused before booting WordPress — running it just to report the same
thing at the end costs runner minutes for nothing. Run
/claudegrill:verify-fix locally and let write-spec add the guard
to this branch, then push again.

Source files changed with no matching spec
inc/admin/class-spacious-tdi-notice.php
inc/admin/class-spacious-theme-review-notice.php

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.

Copilot review overview

🟡 Changes recommended

Both routines still scan all users and load metadata for the entire user set.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

This PR optimizes theme-switch cleanup by retrieving user IDs and priming metadata instead of loading full user objects.

Changes:

  • Updates both notice cleanup routines.
  • Uses ID-only user queries.
  • Bulk-primes user metadata before deletion.
File Summary
inc/​admin/​class-spacious-theme-review-notice.php Optimizes theme-review notice cleanup; the query remains unbounded.
inc/​admin/​class-spacious-tdi-notice.php Optimizes importer notice cleanup; the query remains unbounded.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +74 to +78
$user_ids = get_users( array( 'fields' => 'ID' ) );

foreach ( $get_all_users as $user ) {
$ignored_notice = get_user_meta( $user->ID, 'ignore_spacious_tdi_notice', true );
// Fetching IDs only skips core's own cache_users() priming, so prime
// it ourselves - one query for every user's meta instead of one per user.
update_meta_cache( 'user', $user_ids );
Comment on lines +196 to +206
$user_ids = get_users( array( 'fields' => 'ID' ) );
$theme_installed_time = get_option( 'spacious_theme_installed_time' );

// Delete options data.
if ( $theme_installed_time ) {
delete_option( 'spacious_theme_installed_time' );
}

// Fetching IDs only skips core's own cache_users() priming, so prime
// it ourselves - one query for every user's meta instead of two per user.
update_meta_cache( 'user', $user_ids );
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.

2 participants