Comments: Add wp_update_comment_counts() to reconcile stored counts#58
Open
adamsilverstein wants to merge 1 commit into
Open
Conversation
wp_update_comment_count_now() only refreshes a post's stored comment_count when that post's comments change, so an existing count can become stale after the set of excluded comment types changes (for example when a plugin registers a type that opts out of default listings via default_excluded_comment_types). Registration runs on every request and is not a state transition, so there is no safe automatic trigger; the established core pattern for this is an explicit recount, like flush_rewrite_rules() for rewrite rules. Add a bulk recount helper that recomputes one or more posts' counts through wp_update_comment_count_now(), so it honors the same exclusion filter. A plugin that changes the excluded set calls it once, typically on activation. See #35214, #65537.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to the #12310 counter fix, addressing the second half of pfefferle's review on Trac #35214 comment:52: keeping a post's stored
comment_countconsistent when the set of excluded comment types changes.The gap
wp_update_comment_count_now()(made filter-aware in WordPress#12310) only refreshes a post's storedcomment_countwhen that post's comments change. So a count written before a type joined the excluded set stays stale until the post sees activity again. Example: a site runs withreviewcomments counting normally, then installs a plugin that excludesreviewviadefault_excluded_comment_types- existing posts keep their inflated count.Why not auto-recount on registration
register_comment_type()runs on every request duringinit; it is not a persisted state transition, and the exclusion set is driven by thedefault_excluded_comment_typesfilter, not by register/unregister. There is no clean event to hook. Investigated alternatives and rejected them:unregister_comment_type- wrong event (exclusion changes come from the filter, not unregister) and per-request.UPDATEduring aGET(breaks read replicas / page caches;get_comments_number()is deliberately read-only) and relies on diffing a per-request filter output on the hot path.The closest core analog confirms the right pattern: taxonomy term counts are recalculated only on data events, never on taxonomy registration. Rewrite rules use the same model - core exposes
flush_rewrite_rules()and documents "call it on activation" rather than auto-detecting.This PR
wp_update_comment_counts( $post_ids = null )- a bulk recount helper that recomputes one or more posts' counts through the now filter-awarewp_update_comment_count_now(), so it honors the same exclusion set by construction (no new SQL). A plugin that changes the excluded set calls it once, typically from its activation hook; it also gives a future WP-CLI/admin maintenance tool a single correct entry point.null(default) recalculates every post that has at least one comment.Naming follows the core
wp_update_*_counts()bulk family (wp_update_user_counts(),wp_update_network_counts()); singularwp_update_comment_count()already exists for a single post.Tests
New
tests/phpunit/tests/comment/wpUpdateCommentCounts.php: empty input returns 0; targeted IDs only touch those posts; duplicate IDs are deduped;nullrecalculates all posts with comments; and the headline case - a newly-excludedreviewtype drops a previously stored count to 0.--group commenttests pass (557 + 5 new).Stacked on WordPress#12310 (
feature/65537-excluded-comment-types-filter); retarget totrunkonce that lands.🤖 Generated with Claude Code