Make WordPress Core


Ignore:
Timestamp:
10/31/2025 06:55:47 PM (3 months ago)
Author:
adamsilverstein
Message:

Editor: Notes should not appear in the context of comments.

Prevent notes from inadvertently showing up in the context of comments - including on the Dashboard recent comments widget and the “Mine” count on the Comments page. Notes are stored as a custom ‘note’ comment type and this change ensures the note type is only returned when explicitly requested, or when ‘all’ types are requested.

The query for note children is modified to return all child notes. This fixes an issue where children were no longer being returned for the ‘note’ type.

Also fixes https://github.com/WordPress/gutenberg/issues/72548.

Props adamsilverstein, timothyblynjacobs, shailu25, peterwilsoncc, westonruter, mamaduka, kadamwhite.
Fixes #64145.
Fixes #64152.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r60235 r61105  
    53735373        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    53745374    }
     5375
     5376    /**
     5377     * Helper method to create standard test comments for note type exclusion tests.
     5378     *
     5379     * @since 6.9.0
     5380     *
     5381     * @return array<'comment'|'pingback'|'note', int> Array of comments created.
     5382     */
     5383    protected function create_note_type_test_comments(): array {
     5384        return array(
     5385            'comment'  => self::factory()->comment->create(
     5386                array(
     5387                    'comment_post_ID'  => self::$post_id,
     5388                    'comment_approved' => '1',
     5389                )
     5390            ),
     5391            'pingback' => self::factory()->comment->create(
     5392                array(
     5393                    'comment_post_ID'  => self::$post_id,
     5394                    'comment_approved' => '1',
     5395                    'comment_type'     => 'pingback',
     5396                )
     5397            ),
     5398            'note'     => self::factory()->comment->create(
     5399                array(
     5400                    'comment_post_ID'  => self::$post_id,
     5401                    'comment_approved' => '1',
     5402                    'comment_type'     => 'note',
     5403                )
     5404            ),
     5405        );
     5406    }
     5407
     5408    /**
     5409     * @ticket 64145
     5410     * @covers WP_Comment_Query::get_comment_ids
     5411     * @dataProvider data_note_type_exclusion
     5412     *
     5413     * @param array<string, string|array> $query_args     Query arguments for WP_Comment_Query.
     5414     * @param string[]                    $expected_types Expected comment types.
     5415     */
     5416    public function test_note_type_exclusion( array $query_args, array $expected_types ) {
     5417        $this->create_note_type_test_comments();
     5418
     5419        $query = new WP_Comment_Query();
     5420        $found = $query->query( array_merge( $query_args, array( 'fields' => 'ids' ) ) );
     5421
     5422        $actual_types = array_map(
     5423            static function ( int $comment_id ): string {
     5424                return get_comment( $comment_id )->comment_type;
     5425            },
     5426            $found
     5427        );
     5428
     5429        $this->assertSameSets( $expected_types, $actual_types, 'Expected comment query to return comments of the these types.' );
     5430    }
     5431
     5432    /**
     5433     * Data provider for note type exclusion tests.
     5434     *
     5435     * @since 6.9.0
     5436     *
     5437     * @return array<string, array{ query_args: array<string, string|array>, expected_types: string[] }>
     5438     */
     5439    public function data_note_type_exclusion(): array {
     5440        return array(
     5441            'default query excludes note'        => array(
     5442                'query_args'     => array(),
     5443                'expected_types' => array( 'comment', 'pingback' ),
     5444            ),
     5445            'empty type parameter excludes note' => array(
     5446                'query_args'     => array( 'type' => '' ),
     5447                'expected_types' => array( 'comment', 'pingback' ),
     5448            ),
     5449            'type all includes note'             => array(
     5450                'query_args'     => array( 'type' => 'all' ),
     5451                'expected_types' => array( 'comment', 'pingback', 'note' ),
     5452            ),
     5453            'explicit note type'                 => array(
     5454                'query_args'     => array( 'type' => 'note' ),
     5455                'expected_types' => array( 'note' ),
     5456            ),
     5457            'type__in with note'                 => array(
     5458                'query_args'     => array( 'type__in' => array( 'note' ) ),
     5459                'expected_types' => array( 'note' ),
     5460            ),
     5461            'type__in with note and pingback'    => array(
     5462                'query_args'     => array( 'type__in' => array( 'note', 'pingback' ) ),
     5463                'expected_types' => array( 'note', 'pingback' ),
     5464            ),
     5465            'type pings excludes note'           => array(
     5466                'query_args'     => array( 'type' => 'pings' ),
     5467                'expected_types' => array( 'pingback' ),
     5468            ),
     5469            'type__not_in with note'             => array(
     5470                'query_args'     => array( 'type__not_in' => array( 'note' ) ),
     5471                'expected_types' => array( 'comment', 'pingback' ),
     5472            ),
     5473        );
     5474    }
     5475
     5476    /**
     5477     * @ticket 64145
     5478     * @covers WP_Comment_Query::get_comment_ids
     5479     */
     5480    public function test_note_type_not_duplicated_in_type__not_in() {
     5481        global $wpdb;
     5482
     5483        $comments = $this->create_note_type_test_comments();
     5484
     5485        $query = new WP_Comment_Query();
     5486        $found = $query->query(
     5487            array(
     5488                'type__not_in' => array( 'note' ),
     5489                'fields'       => 'ids',
     5490            )
     5491        );
     5492
     5493        $this->assertSameSets( array( $comments['comment'], $comments['pingback'] ), $found );
     5494        $this->assertNotContains( $comments['note'], $found );
     5495        $note_count = substr_count( $wpdb->last_query, "'note'" );
     5496        $this->assertSame( 1, $note_count, 'The note type should only appear once in the query' );
     5497    }
     5498
     5499    /**
     5500     * @ticket 64145
     5501     * @covers ::get_comment_count
     5502     */
     5503    public function test_get_comment_count_excludes_note_type() {
     5504        $post_id = self::factory()->post->create();
     5505
     5506        self::factory()->comment->create(
     5507            array(
     5508                'comment_post_ID'  => $post_id,
     5509                'comment_approved' => '1',
     5510            )
     5511        );
     5512        self::factory()->comment->create(
     5513            array(
     5514                'comment_post_ID'  => $post_id,
     5515                'comment_approved' => '1',
     5516                'comment_type'     => 'note',
     5517            )
     5518        );
     5519        self::factory()->comment->create(
     5520            array(
     5521                'comment_post_ID'  => $post_id,
     5522                'comment_approved' => '0',
     5523                'comment_type'     => 'note',
     5524            )
     5525        );
     5526
     5527        $counts = get_comment_count( $post_id );
     5528
     5529        $this->assertSame( 1, $counts['approved'] );
     5530        $this->assertSame( 0, $counts['awaiting_moderation'] );
     5531        $this->assertSame( 0, $counts['spam'] );
     5532        $this->assertSame( 0, $counts['trash'] );
     5533        $this->assertSame( 0, $counts['post-trashed'] );
     5534        $this->assertSame( 1, $counts['all'] );
     5535        $this->assertSame( 1, $counts['total_comments'] );
     5536    }
    53755537}
Note: See TracChangeset for help on using the changeset viewer.