This repository was archived by the owner on Nov 2, 2026. It is now read-only.
Repository navigation
fix: purge cross-site content silently and recompute bbPress counters - #385
Merged
Merged
Conversation
Purging a user from a different site's request context (switch_to_blog does not load that site's plugins) had two defects: 1. wp_delete_post()/cap checks against unregistered post types (topic, reply, artist_link_page, ...) emitted map_meta_cap _doing_it_wrong notices for every deleted object. Register a minimal post type stub (map_meta_cap => true, capability_type 'post' — identical cap semantics to a regular post) for the duration of each delete and unregister it before restore_current_blog(). 2. bbPress' delete-time counter updates never fired (bbPress is not loaded in the switched context), leaving parent topic/forum _bbp_reply_count, _bbp_last_reply_id, _bbp_last_active_id/time and hidden counts stale after hard-deleting replies/topics. Capture the reply -> topic -> forum ancestry before deleting, then recompute the same meta keys directly from the database mirroring bbPress' own bbp_update_topic_* / bbp_update_forum_* semantics. Adds tests/unit/ModerationContentPurgeTest.php covering both defects against a real second network site. Refs #383
Contributor
Homeboy Results —
|
…st_parent reads
The forum last-reply lookup used a posts-to-posts self-join, which the
shared-table SQL engine in sandboxed WP Codebox runtimes rejects
("Can't reopen table"), leaving _bbp_last_reply_id at the pre-purge
value (observed 6 vs expected 9 in CI). Resolve the forum's public topic
IDs first, then find the newest public reply via get_posts with
post_parent__in — the same shape bbPress' own WP_Query usage takes.
Read post_parent via get_post_field() so phpstan's WP_Post|array stub
does not flag property access on the switched-blog objects.
chubes4
added a commit
that referenced
this pull request
Sep 8, 2026
…ning fixtures
Code-side:
- inc/auth-tokens/service.php: add_query_arg() inserts new values without
encoding, so the raw 2FA continuation URL terminated its own redirect_to
query value at the first &. rawurlencode() it (same pattern as
read-redirect.php).
- inc/core/moderation/email.php: ! empty( $result['success'] ) fatals when
the queue returns WP_Error ('Cannot use object of type WP_Error as
array'); restore the is_array() guard (#385 regression, latent on main
because the covering test never executed under broken process isolation).
Test-side:
- ArtistDispatchAccessTest: deterministically swap the real analytics ability
for the capture double (same WP 6.9 simulated-init pattern as the
onboarding suite) and restore it in tearDown.
- ArtistInvitationRegistrationTest: stub the wp-native-auth token primitives
(network-activated in production, absent in the sandbox).
- BrowserHandoffTokenTest: stub cache implements delete_multiple().
- RegistrationEmailFailureTest: initialize canonical site rows in setUp so
wpdb table-missing errors cannot pollute the error_log assertions; the
null-result formatter expectation follows the deliberate #110 refinement
(ec_send_email returned null).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #383
Root cause
extrachill_users_purge_user_content()runs from whatever site the CLI was invoked on and doesswitch_to_blog()per object.switch_to_blog()does not load the target site's plugins, so:topic,reply,artist_link_page,artist_profile, ...) are not registered in the request. Core's delete path (and any network-active plugin hooked intobefore_delete_post/delete_post) runscurrent_user_can()against those posts, andmap_meta_cap()emits a_doing_it_wrongnotice per object. Deletes still succeeded — the notices were the bug.bbp_delete_reply/bbp_delete_topicnever fire and none of bbPress' denormalized counters are updated, leaving the parent topic/forum stale (exactly the topic 1698 drift from the issue).Fix 1 — cap notices: minimal post type stub
Chose the stub registration approach (issue option (a)). Per deleted post whose
post_typefailspost_type_exists(), a minimal stub is registered beforewp_delete_post()and unregistered beforerestore_current_blog()(post type registrations are global state, so the stub must not survive the blog switch).One deliberate deviation from the option as sketched: the stub uses
map_meta_cap => truewith the defaultcapability_type => 'post', notmap_meta_cap => false. Reason, verified againstwp-includes/capabilities.php:map_meta_cap()falls back to$caps[] = 'edit_others_posts'— a cap administrators actually have, so today's checks pass.map_meta_cap => falsestub would mapdelete_postto the per-type primitive (delete_topic, ...) which nobody is granted, silently flipping cap-check outcomes in hooked code — a behavior regression hiding behind the notice fix.map_meta_cap => true+capability_type => 'post'stub maps checks to the exact same standard caps a regular post uses (delete_others_postsetc.), byte-identical to the effective pre-fix outcome, minus the notice.Attachments were left alone:
attachmentis a core type and always registered, sowp_delete_attachment()(and its file cleanup) runs untouched.Fix 2 — bbPress counter recompute
bbPress is never loaded in this context (no
function_exists('bbp_*')guards — they'd always be false), so the recompute reads$wpdbdirectly, mirroring the exact semantics of bbPress' own updaters (read frombbpress/includes/{common,topics,forums,replies}/functions.php):Pre-delete capture (
extrachill_users_capture_bbp_context()): before any deletions, for every purgedtopic/replythe reply → topic → forum chain is read fromposts(threaded replies resolved by climbingpost_parent, depth-guarded, mirroringbbp_get_reply_topic_id()). Capture must happen before the loop because a reply's topic may itself be purged later in the same loop.Topic recompute (
extrachill_users_bbp_update_topic_counters()), matchingbbp_update_topic_*:_bbp_reply_count= COUNT ofreplyrows withpost_status = 'publish'linked by the_bbp_topic_idmeta mirror orpost_parent(bbPress counts via the meta mirror; the parent fallback covers rows missing meta that bbPress itself would still resolve)_bbp_reply_count_hidden= same linkage,post_status IN ('trash','spam','pending')(bbPress' non-public set)_bbp_last_reply_id= newest publish reply withpost_parent = topic, orderedpost_date DESC, ID DESC(bbp_get_public_child_last_id()ordering), else0_bbp_last_active_id= that reply, else the topic ID_bbp_last_active_time= that post'spost_date(only written when non-empty, like bbPress)Forum recompute (
extrachill_users_bbp_update_forum_counters()), matchingbbp_update_forum_*:_bbp_topic_count/_bbp_topic_count_hidden= public (publish,closed) / hidden (trash,spam,pending) topics via the_bbp_forum_idmeta mirror orpost_parent_bbp_reply_count/_bbp_reply_count_hidden= replies via the_bbp_forum_idmeta mirror only (a reply'spost_parentis always a topic — no parent fallback exists, same as bbPress' meta-based counting)_bbp_total_topic_count/_bbp_total_reply_count= direct + recursive public sub-forum totals (cycle-guarded), likebbp_update_forum_topic_count()_bbp_last_topic_id= newest public topic parented to the forum, ordered by_bbp_last_active_time(DATETIME) withpost_datefallback, ID DESC tiebreak_bbp_last_reply_id/_bbp_last_active_id= newest publish reply across the forum's topics vs the highest public topic ID — larger wins (bbPress' own comparison),0when the forum has no public topics_bbp_last_active_time=post_dateof the last active ID, skipped when0Topics/forums that were themselves purged are skipped (no orphan meta resurrection).
Tests
tests/unit/ModerationContentPurgeTest.php(3 tests, multisite-aware, skips gracefully if blog creation is unavailable):test_purge_of_unregistered_post_type_emits_no_map_meta_cap_notices— fixture post of a custom type on a second blog; type unregistered (mirroring the production request context); purge runs from blog 1; asserts zeromap_meta_capnotices (counted viadoing_it_wrong_run) and hard deletion. Abefore_delete_posthook callingcurrent_user_can('delete_post', ...)deterministically reproduces the hooked-plugin cap checks that surfaced this on production.test_purge_recomputes_bbp_topic_and_forum_counters— forum + topic + 3 public replies (one by the purged user, seeded as the topic's newest activity with stale counters and bbPress hierarchy meta); after purge asserts the purged reply is gone and every topic/forum counter, pointer, and last-active field matches the recomputed reality.test_purge_of_topic_zeroes_forum_pointers— purged user authors a whole topic; asserts the topic is gone and the forum's topic counts/pointers are zeroed.Verification
phpcs --standard=WordPresson both changed files: clean; byte-identical finding set tomainforcontent.php(3 pre-existing errors + 4 warnings on unchanged lines — zero new findings).phpstan analyse(level 5, repo config): 8 errors, all pre-existing onmain(identical count and locations); zero in the changed files.php -lclean on both files.HOMEBOY_RUNTIME_SETTINGS_HELPER+WP_CODEBOX_DB_*secret env) that only exists in the homeboy runner/CI environment — localhomeboy testfails at adapter bootstrap before any test executes (same for historical local runs in/mnt/extrachill-workspace/homeboy-runtime). The CI workflow (homeboy review test, which provisions MySQL 8.0 as a service container) is the authoritative gate and runs on this PR; failures, if any, will be addressed on this branch.AI-authored: this PR was written by Extra Chill Bot (GLM agent) from issue #383's reproduction notes.