Make WordPress Core


Ignore:
Timestamp:
02/02/2021 09:04:58 PM (4 years ago)
Author:
antpb
Message:

Taxonomy: Add filter for post statuses when updating term count.

This adds a filter that allows $post_statuses to be modified in term count.

Props GunGeekATX, adamsilverstein, davecpage, nwjames, hellofromTonya, audrasjb, peterwilsoncc, TimothyBlynJacobs.
Fixes #38843.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/termCounts.php

    r49603 r50169  
    225225    }
    226226
    227     /**
    228      * Term counts incremented correctly for posts with attachment.
    229      *
    230      * @covers ::wp_update_term_count
    231      * @dataProvider data_term_count_changes_for_post_statuses_with_attachments
     227    function add_custom_status_to_counted_statuses( $statuses ) {
     228        array_push( $statuses, 'custom' );
     229        return $statuses;
     230    }
     231
     232    /**
     233     * Term counts incremented correctly when the `update_post_term_count_statuses` filter is used.
     234     *
     235     * @covers ::wp_update_term_count
     236     * @dataProvider data_term_count_changes_for_update_post_term_count_statuses_filter
     237     * @ticket 38843
    232238     *
    233239     * @param string $post_status New post status.
    234240     * @param int    $change      Expected change.
    235241     */
    236     public function test_term_count_changes_for_post_statuses_with_attachments( $post_status, $change ) {
     242    public function test_term_count_changes_for_update_post_term_count_statuses_filter( $post_status, $change ) {
    237243        $term_count = get_term( self::$attachment_term )->count;
    238         // Do not use shared fixture for this test as it relies on a new post.
     244
     245        add_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
     246
    239247        $post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
    240248        wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     
    250258        $expected = $term_count + $change;
    251259        $this->assertSame( $expected, get_term( self::$attachment_term )->count );
    252     }
    253 
    254     /**
    255      * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     260
     261        remove_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
     262    }
     263
     264    /**
     265     * Data provider for test_term_count_changes_for_update_post_term_count_statuses_filter.
    256266     *
    257267     * @return array[] {
     
    260270     * }
    261271     */
    262     function data_term_count_changes_for_post_statuses_with_attachments() {
     272    function data_term_count_changes_for_update_post_term_count_statuses_filter() {
    263273        return array(
    264274            // 0. Published post
     
    270280            // 3. Private post
    271281            array( 'private', 0 ),
     282            // 4. Custom post status
     283            array( 'custom', 2 ),
     284        );
     285    }
     286
     287    /**
     288     * Term counts incremented correctly for posts with attachment.
     289     *
     290     * @covers ::wp_update_term_count
     291     * @dataProvider data_term_count_changes_for_post_statuses_with_attachments
     292     *
     293     * @param string $post_status New post status.
     294     * @param int    $change      Expected change.
     295     */
     296    public function test_term_count_changes_for_post_statuses_with_attachments( $post_status, $change ) {
     297        $term_count = get_term( self::$attachment_term )->count;
     298        // Do not use shared fixture for this test as it relies on a new post.
     299        $post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
     300        wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     301        $attachment_id = self::factory()->attachment->create_object(
     302            array(
     303                'file'        => 'image.jpg',
     304                'post_parent' => $post_id,
     305                'post_status' => 'inherit',
     306            )
     307        );
     308        wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     309
     310        $expected = $term_count + $change;
     311        $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     312    }
     313
     314    /**
     315     * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     316     *
     317     * @return array[] {
     318     *     @type string $post_status New post status.
     319     *     @type int    $change      Expected change.
     320     * }
     321     */
     322    function data_term_count_changes_for_post_statuses_with_attachments() {
     323        return array(
     324            // 0. Published post
     325            array( 'publish', 2 ),
     326            // 1. Auto draft
     327            array( 'auto-draft', 0 ),
     328            // 2. Draft
     329            array( 'draft', 0 ),
     330            // 3. Private post
     331            array( 'private', 0 ),
    272332        );
    273333    }
Note: See TracChangeset for help on using the changeset viewer.