Repository navigation
Comments: Add filter for comment types excluded from queries by default - #12310
adamsilverstein wants to merge 61 commits into
Conversation
WP_Comment_Query hard-codes the exclusion of the 'note' comment type (introduced in 6.9) with no extension point. Plugins that add their own "private" comment types must instead rewrite query SQL through comments_clauses in every context and re-verify it on each release. Introduce a default_excluded_comment_types filter so extenders can contribute additional comment types to the default-excluded set, generalizing the existing 'note' handling. The default value of array( 'note' ) preserves current behavior exactly. See #65537.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…cess control. The filter docblock described excluded types as "private", which could be read as a security boundary. The exclusion only governs default visibility; excluded types remain retrievable via explicit 'type' or 'all' requests, so document that callers must enforce capability checks wherever comment data is displayed or exposed.
wp_update_comment_count_now() recalculated a post's stored comment_count with a hard-coded 'AND comment_type != note', bypassing the default_excluded_comment_types filter that hides those types from WP_Comment_Query. A type that opted out of listings still inflated comment_count and therefore get_comments_number(). Apply the same filtered exclusion set to the counter so the listings and the stored count stay consistent, removing the need for plugins to also re-implement the count via pre_wp_update_comment_count_now. See #35214, #65537.
get_pending_comments_num() recomputed the admin pending-comments count with a hard-coded 'AND comment_type != note', the same gap fixed in wp_update_comment_count_now(): a type that opts out of default listings via default_excluded_comment_types still inflated the pending bubble shown next to each post in the admin list tables. Derive the excluded set from the same filter so the pending count stays consistent with the listings and the stored comment count. See #35214, #65537.
The default-excluded set plus filter application was triplicated across
WP_Comment_Query, wp_update_comment_count_now(), and
get_pending_comments_num(), each with subtly different normalization.
Centralize it in one accessor that all three call, which is also the
seam where a future comment type registry can derive the default from
internal types.
The accessor additionally strips the special type tokens understood by
WP_Comment_Query ('all', 'comment', 'comments', 'pings'): previously a
filter returning the 'pings' alias excluded nothing from the count
functions but zeroed out an explicit type => 'pingback' query, because
the explicit-request guard compares raw tokens while the query expansion
resolves aliases.
Also document the registration-timing contract (register the filter
unconditionally rather than toggling per call - the query cache key does
not include filter output), widen the null-context description, hoist
the invariant 'all' check out of the per-type loop, add the missing
@SInCE entry to wp_update_comment_count_now(), and make the
no-duplication test capture the WHERE clause via comments_clauses
instead of asserting against $wpdb->last_query.
…pes.
When default_excluded_comment_types excludes a literal ping type (for
example 'pingback'), a query requesting the 'pings' alias built
`comment_type IN ('pingback','trackback') AND comment_type NOT IN
('pingback')`, silently dropping the explicitly requested pingbacks. The
guard compared excluded literals against the raw request tokens, so an
alias never matched the literal it stands for.
Expand the request's special tokens ('comment', 'comments', 'pings') to
their literal comment_type values before deciding what to exclude, so a
type requested via an alias is honored. This also drops the redundant
NOT IN membership check, since the list is deduplicated with
array_unique() before the clause is built.
The normalization in wp_get_default_excluded_comment_types() ran the filtered list through array_filter() with no callback, which drops the string '0' along with empty strings. A comment type literally named '0' could therefore never be excluded through the filter. Filter on strlen() instead, so only empty strings are removed and '0' survives as a valid literal type.
The default_excluded_comment_types docblock said the filter keeps types out of listings, counts, "or feeds". Comment feeds are served by WP_Query's raw SQL, which does not consult this filter, so the promise does not hold. Describe only the surfaces the filter actually covers.
The list table hardcoded `type__not_in => array( 'note' )` for its row query while the status count bubbles (via wp_count_comments()) now honor default_excluded_comment_types. A plugin that removed 'note' from the filter would therefore see the count include notes while the rows still hid them, so "All (N)" disagreed with the visible list. Source the row query's exclusions from wp_get_default_excluded_comment_types() so the list and its counts stay in agreement.
…ed-comment-types-filter
Trunk's last-modified query hard-codes the 'note' and 'reaction' exclusion, so a type added through the 'default_excluded_comment_types' filter still moved the comment feed's Last-Modified date, and emptying the filter did not bring notes back. Route it through wp_get_excluded_comment_types_clause() like the feeds and counters. The cached value keeps its fixed 'lastcommentmodified:<tz>' key, since the comment status handlers clear it by that name. A changed excluded set is therefore picked up the next time a comment changes status. See #65537, #35214.
The docblocks and inline comments around the excluded comment types had grown into design notes, some of them stale: the accessor and filter docs still described 'note' as the only built-in default and did not mention 'reaction' or get_lastcommentmodified(). Trim them to what a caller needs and bring them in line with trunk. Also drop the list table's 'comment'/'comments' alias branch, which could never subtract anything because the accessor strips those tokens, and filter the registry's internal type names to strings so the merged default set is typed for array_unique(). See #65537, #35214.
New test methods and data providers need native types to pass PHPStan at level 10. See #65537.
Trunk is in the 7.2 beta, so these additions can no longer ship in 7.2.0.
…ed-comment-types-filter # Conflicts: # src/wp-includes/class-wp-query.php
A hierarchical query caches the child IDs of each parent under a key built from the query vars alone. The excluded set is not a query var and changing it does not bump 'last_changed', so with a persistent object cache replies of a newly excluded type kept rendering in threads, and replies of a type that was no longer excluded stayed hidden. The key now includes the set already resolved for the top-level query, the same way the main query key does. See #65537.
get_lastcommentmodified() reads the excluded comment types but cached its result under a key that ignores them, and the cache is only cleared when a comment changes status. After a plugin changed the set, comment feeds kept sending a Last-Modified date taken from a comment they no longer list. The value is now stored with wp_cache_set_salted(), salted with the set. The key is unchanged, so the existing cache clearing still applies. Code that reads the key directly now gets the salted array instead of a string. See #65537.
The registry lookup was guarded by function_exists( 'get_comment_types' ) alone. Core does not define that function yet, so a plugin or theme function of the same name would have been called, and whatever it returned excluded from every comment query and count. Also require the WP_Comment_Type class, which only the core registry provides. See #65537, #35214.
The filter passes the WP_Comment_Query instance, but the counts, the feeds and get_lastcommentmodified() resolve the set with no query, and each level of a threaded query resolves it with its own instance. A callback that varies the set by query makes those disagree with the listed comments, so say so in the filter documentation. See #65537.
The list table expanded the 'pings' alias and subtracted the requested type from the excluded set itself, repeating what WP_Comment_Query does for any request other than 'all'. Only an 'all' request needs the exclusions passed in, since the query drops its defaults for it. See #65537.
The aliases 'all', 'comment', 'comments' and 'pings' are removed from the filter output with an exact match, but the database compares comment_type case-insensitively. A callback returning 'Comment' kept it in the set, and the resulting NOT IN removed every regular comment from every query. See #65537.
The list table refuses to filter by the note and reaction types, but it compared the request exactly while the database matches comment_type in any case and ignores trailing spaces. With those types removed from the excluded set, ?comment_type=Note filtered the table to notes. Normalize the request before the check, and pin that WP_Comment_Query only lifts a default exclusion for an exact type name, which the REST and admin note checks rely on. See #65537.
|
I ran another review pass over this one before asking for more eyes, and pushed the fixes that came out of it. Claude did the review and the fixes, here is its rundown:
@peterwilsoncc whenever you have a chance, I'd still like your take on the |
The accessor read the comment type registry behind class_exists() and function_exists() guards, with a test that stays skipped until the registry exists. Neither can be exercised on trunk, so reviewers would be approving code that never runs. Drop the branch and the skipped test. The registry change adds the internal types through the filter instead, which needs no guard on either side and works whichever lands first. See #65537.
The counts, the feeds and get_lastcommentmodified() resolve the excluded set without a query, and a threaded query resolves it once per level. A callback that varied the set by query made those disagree with the listed comments, and the docblock had to warn against using the argument at all. Drop the argument so the set is the same everywhere. A query that needs other types still asks for them through 'type' or 'type__in'. See #65537.
The normalization of the filter output was pinned one case per test, some through a full query, and the false return was repeated for each caller of the accessor. Cover it once with a data provider on the accessor. Also drop tests that repeat a neighbour: the note default in the pending count, the get_comment_link() pass-through, and the type__not_in duplicate check that trunk already has for internal types. See #65537.
…ed-comment-types-filter
…sor. wp_get_excluded_comment_types_clause() declared its types while the accessor it builds on did not, so callers got the list shape from the docblock alone. Declare the array return and type the filter callback that normalizes the set. A filter that returns a non-array is still cast before it is returned, so the declaration cannot raise a TypeError. See #65537.
The new tests passed query results, cache reads and REST data straight to helpers that expect a narrower type, which static analysis at the strictest level reports and which would surface as a confusing error rather than a failed assertion if a query returned something else. Assert the type before using it, through a shared helper where the same read was repeated, and declare the types of the filter callbacks. See #65537.
Stack: Custom Comment Types (#35214)
register_comment_type()registration API/wp/v2/comment-typesendpointmap_meta_cap()render_callbackdisplay callbackis_pingping groupingdefault_excluded_comment_typesfilter (this PR)wp_update_comment_counts()recount helper#12311 and #12310 are on WordPress/wordpress-develop; the rest are fork PRs targeting their parent branch and will retarget to trunk as their base lands.
I asked Claude to tighten up this description, here it is:
Trac ticket: https://core.trac.wordpress.org/ticket/35214, https://core.trac.wordpress.org/ticket/65537
Commit message
Claude drafted this commit message and I edited it:
Dev note
Claude drafted this dev note for the 7.3 field guide, I reviewed and edited:
AI Use
Claude Code drafted the code and this description, and I steered. I will review and test.