Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Comments: Add filter for comment types excluded from queries by default#12310

Open
adamsilverstein wants to merge 6 commits into
WordPress:trunkfrom
adamsilverstein:feature/65537-excluded-comment-types-filter
Open

Comments: Add filter for comment types excluded from queries by default#12310
adamsilverstein wants to merge 6 commits into
WordPress:trunkfrom
adamsilverstein:feature/65537-excluded-comment-types-filter

Conversation

@adamsilverstein

Copy link
Copy Markdown
Member

Description

WP_Comment_Query excludes the note comment type (introduced in 6.9 for the editor Notes feature) from results by default, but the exclusion list is hard-coded with no extension point:

if (
    ! in_array( 'all', $raw_types['IN'], true ) &&
    ! in_array( 'note', $raw_types['IN'], true ) &&
    ! in_array( 'note', $raw_types['NOT IN'], true )
) {
    $raw_types['NOT IN'][] = 'note';
}

A plugin that introduces its own "private" comment type (one that should never appear in standard comment listings, counts, or feeds) has to rewrite the SQL through comments_clauses in every context and re-verify that work on each release. This is the "whack-a-mole" problem reported by the Alpaca issue-tracker plugin, which maintains a dedicated library to do exactly this.

Approach

Introduce a default_excluded_comment_types filter that lets extenders contribute additional comment types to the default-excluded set, generalizing the existing hard-coded note handling:

$excluded_types = apply_filters_ref_array( 'default_excluded_comment_types', array( array( 'note' ), &$this ) );

foreach ( array_unique( (array) $excluded_types ) as $excluded_type ) {
    if (
        ! in_array( 'all', $raw_types['IN'], true ) &&
        ! in_array( $excluded_type, $raw_types['IN'], true ) &&
        ! in_array( $excluded_type, $raw_types['NOT IN'], true )
    ) {
        $raw_types['NOT IN'][] = $excluded_type;
    }
}

Because WP_Comment_Query backs the majority of comment outputs (admin list table, get_comments(), REST collection queries, feeds), a single registration point here removes most of the per-context filtering a plugin currently needs.

Example usage:

add_filter( 'default_excluded_comment_types', function ( $types ) {
    $types[] = 'my_private_type';
    return $types;
} );

Scope / non-goals

This is deliberately a small step, not the full custom-comment-types API tracked in #35214. It adds no registration object, labels, capabilities, or admin UI. It only generalizes the existing default-exclusion behavior so private types can opt in without rewriting query SQL.

Backward compatibility

The default value array( 'note' ) preserves current behavior exactly. No change for sites that don't use the filter.

Testing

Adds full coverage to tests/phpunit/tests/comment/query.php:

  • A custom type added via the filter is excluded by default (alongside note).
  • Returning an empty list makes note appear again, proving the default is filterable.
  • An excluded type is still returned when explicitly requested via type, type__in, or type => 'all' (data provider).
  • The filter receives the default array( 'note' ) and the WP_Comment_Query instance.
  • A filtered type is added to the query SQL only once when also passed in type__not_in.
$ phpunit tests/phpunit/tests/comment/query.php
OK (164 tests, 482 assertions)

$ phpunit --group comment
OK (555 tests, 1381 assertions)

Trac ticket: https://core.trac.wordpress.org/ticket/65537

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.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

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,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/class-wp-comment-query.php Outdated
Comment thread src/wp-includes/class-wp-comment-query.php Outdated
Comment thread tests/phpunit/tests/comment/query.php Outdated
Comment thread tests/phpunit/tests/comment/query.php Outdated
…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.
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.

1 participant