Make WordPress Core

Changeset 50169

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r50120 r50169  
    38923892    }
    38933893
     3894
     3895
     3896
     3897
     3898
     3899
     3900
     3901
     3902
     3903
     3904
     3905
    38943906    foreach ( (array) $terms as $term ) {
    38953907        $count = 0;
     
    38973909        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    38983910        if ( $check_attachments ) {
    3899             $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
     3911            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
     3912            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
    39003913        }
    39013914
    39023915        if ( $object_types ) {
    39033916            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    3904             $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
     3917            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
    39053918        }
    39063919
  • 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_( $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_() {
    263273        return array(
    264274            // 0. Published post
     
    270280            // 3. Private post
    271281            array( 'private', 0 ),
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
    272332        );
    273333    }
Note: See TracChangeset for help on using the changeset viewer.