WordPress.org

Make WordPress Core

Ticket #38843: 38843-1.diff

File 38843-1.diff, 5.2 KB (added by nwjames, 6 months ago)

Modified the SQL to change the second occurence of "= 'publish

  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index f6eefe0069..d6328a19d9 100644
    function _update_post_term_count( $terms, $taxonomy ) { 
    38773877                $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
    38783878        }
    38793879
     3880        $post_statuses = array( 'publish' );
     3881
     3882        /**
     3883         * Filters the post statuses for updating the term count.
     3884         *
     3885         * @param array $post_statuses List of post statuses to include in the count. Default is 'publish'.
     3886         * @param int   $taxonomy      Current taxonomy object.
     3887         */
     3888        $post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );
     3889
    38803890        foreach ( (array) $terms as $term ) {
    38813891                $count = 0;
    38823892
    38833893                // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    38843894                if ( $check_attachments ) {
    3885                         $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 ) );
     3895                        $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 ) );
    38863896                }
    38873897
    38883898                if ( $object_types ) {
    38893899                        // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    3890                         $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 ) );
     3900                        $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 IN ('" . implode("', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
    38913901                }
    38923902
    38933903                /** This action is documented in wp-includes/taxonomy.php */
  • tests/phpunit/tests/term/termCounts.php

    diff --git tests/phpunit/tests/term/termCounts.php tests/phpunit/tests/term/termCounts.php
    index c6fa42e4fc..3d8ed55e14 100644
    class Tests_Term_termCount extends WP_UnitTestCase { 
    224224                );
    225225        }
    226226
     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
     238         *
     239         * @param string $post_status New post status.
     240         * @param int    $change      Expected change.
     241         */
     242        public function test_term_count_changes_for_update_post_term_count_statuses_filter( $post_status, $change ) {
     243                $term_count = get_term( self::$attachment_term )->count;
     244
     245                add_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
     246
     247                $post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
     248                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     249                $attachment_id = self::factory()->attachment->create_object(
     250                        array(
     251                                'file'        => 'image.jpg',
     252                                'post_parent' => $post_id,
     253                                'post_status' => 'inherit',
     254                        )
     255                );
     256                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     257
     258                $expected = $term_count + $change;
     259                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     260
     261                remove_filter( 'update_post_term_count_statuses', array( $this, 'add_custom_status_to_counted_statuses' ) );
     262
     263        }
     264
     265        /**
     266         * Data provider for test_term_count_changes_for_update_post_term_count_statuses_filter.
     267         *
     268         * @return array[] {
     269         *     @type string $post_status New post status.
     270         *     @type int    $change      Expected change.
     271         * }
     272         */
     273        function data_term_count_changes_for_update_post_term_count_statuses_filter() {
     274                return array(
     275                        // 0. Published post
     276                        array( 'publish', 2 ),
     277                        // 1. Auto draft
     278                        array( 'auto-draft', 0 ),
     279                        // 2. Draft
     280                        array( 'draft', 0 ),
     281                        // 3. Private post
     282                        array( 'private', 0 ),
     283                        // 4. Custom post status
     284                        array( 'custom', 1 ),
     285                );
     286        }
     287
     288
     289
    227290        /**
    228291         * Term counts incremented correctly for posts with attachment.
    229292         *
    230293         * @covers ::wp_update_term_count
    231294         * @dataProvider data_term_count_changes_for_post_statuses_with_attachments
    232          *
     295
    233296         * @param string $post_status New post status.
    234297         * @param int    $change      Expected change.
    235298         */