WordPress.org

Make WordPress Core

Ticket #38280: 38280.12.diff

File 38280.12.diff, 16.2 KB (added by boonebgorges, 2 years ago)
  • src/wp-includes/taxonomy.php

    diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
    index 102345fd3c..5f01757a16 100644
    a b function wp_update_term_count_now( $terms, $taxonomy ) { 
    30913091        return true;
    30923092}
    30933093
     3094/**
     3095 * Retrieves the term count for a specific object type.
     3096 *
     3097 * @since 5.0.0
     3098 *
     3099 * @param int    $term_id     Term ID.
     3100 * @param string $taxonomy    Taxonomy name.
     3101 * @param string $object_type Object type.
     3102 *
     3103 * @return WP_Error|bool|int WP_Error if invalid taxonomy is passed, false if
     3104 *                           object is not in taxonomy, object term count otherwise.
     3105 */
     3106function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) {
     3107        if ( ! taxonomy_exists( $taxonomy ) ) {
     3108                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
     3109        }
     3110
     3111        if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) {
     3112                return new WP_Error(
     3113                        'invalid_object_type',
     3114                        __( 'Object type is not in taxonomy.' ),
     3115                        array(
     3116                                'object_type' => $object_type,
     3117                                'taxonomy'    => $taxonomy,
     3118                        )
     3119                );
     3120        }
     3121
     3122        $term = get_term( $term_id, $taxonomy );
     3123
     3124        if ( is_wp_error( $term ) ) {
     3125                return $term;
     3126        }
     3127
     3128        if ( 0 === $term->count ) {
     3129                return 0;
     3130        }
     3131
     3132        $taxonomy_object = get_taxonomy( $taxonomy );
     3133
     3134        if ( 1 >= count( $taxonomy_object->object_type ) ) {
     3135                return $term->count;
     3136        }
     3137
     3138        $term_object_count = get_term_meta( $term_id, '_wp_object_count_' . $object_type, true );
     3139        if ( $term_object_count ) {
     3140                return (int) $term_object_count;
     3141        }
     3142
     3143        $counted_object_types = (array) get_term_meta( $term_id, '_wp_counted_object_types', true );
     3144
     3145        // When the object type is marked counted and no meta key exists, the count is 0.
     3146        if ( in_array( $object_type, $counted_object_types, true ) ) {
     3147                return 0;
     3148        }
     3149
     3150        // Term has not been counted for the object type. Calculate and fetch count.
     3151        wp_update_term_count_now( array( $term->term_taxonomy_id ), $taxonomy );
     3152        return wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type );
     3153}
     3154
    30943155//
    30953156// Cache
    30963157//
    function _prime_term_caches( $term_ids, $update_meta_cache = true ) { 
    36053666 *
    36063667 * @access private
    36073668 * @since 2.3.0
     3669 * @since 5.0.0 Store term counts on a per object type basis in meta.
    36083670 *
    36093671 * @global wpdb $wpdb WordPress database abstraction object.
    36103672 *
    function _update_post_term_count( $terms, $taxonomy ) { 
    36283690        }
    36293691
    36303692        if ( $object_types ) {
    3631                 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
     3693                $object_types = array_filter( $object_types, 'post_type_exists' );
    36323694        }
    36333695
    3634         foreach ( (array) $terms as $term ) {
     3696        foreach ( (array) $terms as $tt_id ) {
    36353697                $count = 0;
    36363698
     3699                $term = get_term_by( 'term_taxonomy_id', $tt_id );
     3700
     3701                // Remove previous counts to prevent stale data if an object type is removed from a taxonomy.
     3702                $counted_object_types = (array) get_term_meta( $term->term_id, '_wp_counted_object_types', true );
     3703
     3704                foreach ( $counted_object_types as $o_type ) {
     3705                        delete_term_meta( $term->term_id, '_wp_object_count_' . $o_type );
     3706                }
     3707
     3708                delete_term_meta( $term->term_id, '_wp_counted_object_types' );
     3709
     3710                $term_count_meta = array();
     3711
     3712                if ( $object_types ) {
     3713                        foreach ( $object_types as $type ) {
     3714                                $current_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 = %s AND term_taxonomy_id = %d", $type, $tt_id ) );
     3715
     3716                                $count += $current_count;
     3717
     3718                                $term_count_meta[ $type ] = $current_count;
     3719                        }
     3720                }
     3721
    36373722                // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    36383723                if ( $check_attachments ) {
    3639                         $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 ) );
     3724                        $attachment_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", $tt_id ) );
     3725
     3726                        $count += $attachment_count;
     3727
     3728                        $term_count_meta['attachment'] = $attachment_count;
     3729
     3730                        // Re-add attachment so the meta gets saved below.
     3731                        $object_types[] = 'attachment';
    36403732                }
    36413733
    3642                 if ( $object_types ) {
    3643                         $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 ) );
     3734                // Save individual counts for each object type in term meta.
     3735                if ( 1 < count( $term_count_meta ) ) {
     3736                        foreach ( $object_types as $type ) {
     3737                                if ( ! empty( $term_count_meta[ $type ] ) ) {
     3738                                        update_term_meta( $term->term_id, '_wp_object_count_' . $type, (int) $term_count_meta[ $type ] );
     3739                                }
     3740                        }
     3741
     3742                        update_term_meta( $term->term_id, '_wp_counted_object_types', $object_types );
    36443743                }
    36453744
    36463745                /** This action is documented in wp-includes/taxonomy.php */
    3647                 do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
    3648                 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
     3746                do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
     3747                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );
    36493748
    36503749                /** This action is documented in wp-includes/taxonomy.php */
    3651                 do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
     3750                do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
    36523751        }
    36533752}
    36543753
  • tests/phpunit/tests/term/wpGetObjectTerms.php

    diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php
    index e7851dcaee..4bf496ffe0 100644
    a b class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { 
    599599                $p = self::factory()->post->create();
    600600                wp_set_object_terms( $p, $terms, 'wptests_tax' );
    601601
     602                /**
     603                 * `wp_set_object_terms()` populates the cache, but we need it empty to verify
     604                 * behavior of 'update_term_meta_cache'.
     605                 */
     606                foreach ( $terms as $t ) {
     607                        wp_cache_delete( $t, 'term_meta' );
     608                }
     609
    602610                $found = wp_get_object_terms(
    603611                        $p, 'wptests_tax', array(
    604612                                'update_term_meta_cache' => false,
  • new file tests/phpunit/tests/term/wpGetTermCountForObjectType.php

    diff --git a/tests/phpunit/tests/term/wpGetTermCountForObjectType.php b/tests/phpunit/tests/term/wpGetTermCountForObjectType.php
    new file mode 100644
    index 0000000000..15ba4b702d
    - +  
     1<?php
     2
     3/**
     4 * Class Tests_Term_wpGetTermCountForObjectType
     5 *
     6 * @group taxonomy
     7 * @ticket 38280
     8 */
     9class Tests_Term_wpGetTermCountForObjectType extends WP_UnitTestCase {
     10
     11        /**
     12         * Test when a taxonomy is invalid.
     13         */
     14        public function test_wp_get_term_count_for_object_type_invalid_taxonomy() {
     15                $actual = wp_get_term_count_for_object_type( 0, 'does-not-exist', 'post' );
     16
     17                $this->assertWPError( $actual );
     18                $this->assertSame( 'invalid_taxonomy', $actual->get_error_code() );
     19        }
     20
     21        /**
     22         * Test when an object type is not in a taxonomy.
     23         */
     24        public function test_wp_get_term_count_for_object_type_object_not_in_taxonomy() {
     25                $this->assertWPError( wp_get_term_count_for_object_type( 0, 'category', 'page' ) );
     26        }
     27
     28        /**
     29         * Test when an invalid term is passed.
     30         */
     31        public function test_wp_get_term_count_for_object_type_invalid_term() {
     32                $actual = wp_get_term_count_for_object_type( 0, 'category', 'post' );
     33
     34                $this->assertWPError( $actual );
     35                $this->assertSame( 'invalid_term', $actual->get_error_code() );
     36        }
     37
     38        /**
     39         * Test when a taxonomy belongs to a single object type. No term meta should be
     40         * stored and the count property should be used.
     41         */
     42        public function test_wp_get_term_count_for_object_type_single_object_type() {
     43                $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
     44                $post_id = self::factory()->post->create(
     45                        array(
     46                                'post_type'     => 'post',
     47                                'post_category' => array(
     48                                        $term_id,
     49                                ),
     50                        )
     51                );
     52
     53                $this->assertFalse( (bool) get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     54                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     55
     56                $term_object = get_term( $term_id, 'category' );
     57                $this->assertEquals( 1, $term_object->count );
     58
     59                wp_remove_object_terms( $post_id, array( $term_id ), 'category' );
     60
     61                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     62
     63                $term_object = get_term( $term_id, 'category' );
     64                $this->assertEquals( 0, $term_object->count );
     65        }
     66
     67        /**
     68         * Test when a taxonomy belongs to multiple object types.
     69         */
     70        public function test_wp_get_term_count_for_object_type_multiple_object_types() {
     71                register_post_type( 'wptests_cpt' );
     72                register_taxonomy(
     73                        'wptests_tax',
     74                        array(
     75                                'post',
     76                                'wptests_cpt',
     77                        )
     78                );
     79
     80                $term_id        = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     81                $post_id        = self::factory()->post->create( array( 'post_type' => 'post' ) );
     82                $custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) );
     83
     84                // When a term has no relationships, no meta should be stored.
     85                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     86                $this->assertEquals( 0,  wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
     87                $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     88                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
     89                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
     90
     91                wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     92
     93                // Term has relationships, meta should be stored caching types counted and counts for each type > 0.
     94                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     95                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
     96                $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     97                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
     98                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
     99
     100                wp_set_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' );
     101
     102                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     103                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
     104                $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     105                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
     106                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
     107
     108                // Total count should be stored in the term's count property.
     109                $term_object = get_term( $term_id, 'wptests_tax' );
     110                $this->assertEquals( 2, $term_object->count );
     111
     112                wp_remove_object_terms( $custom_post_id, array( $term_id ), 'wptests_tax' );
     113
     114                // Object count cache should be removed.
     115                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     116                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
     117                $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     118                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
     119                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
     120
     121                wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     122
     123                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     124                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'wptests_cpt' ) );
     125                $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     126                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
     127                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_wptests_cpt', true ) );
     128        }
     129
     130        /**
     131         * Test when a taxonomy belongs to multiple object types, one of which is attachments.
     132         */
     133        public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() {
     134                register_taxonomy(
     135                        'wptests_tax',
     136                        array(
     137                                'post',
     138                                'attachment',
     139                        )
     140                );
     141
     142                $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     143                $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
     144                $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', $post_id );
     145
     146                // When a term has no relationships, no meta should be stored.
     147                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     148                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
     149                $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     150                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
     151                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
     152
     153                wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     154
     155                // Term has relationships, meta should be stored caching types counted and counts for each type > 0.
     156                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     157                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
     158                $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     159                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
     160                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
     161
     162                wp_set_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' );
     163
     164                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     165                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
     166                $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     167                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_post', true ) );
     168                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
     169
     170                // Total count should be stored in the term's count property.
     171                $term_object = get_term( $term_id, 'wptests_tax' );
     172                $this->assertEquals( 2, $term_object->count );
     173
     174                wp_remove_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     175
     176                // Object count cache should be removed.
     177                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     178                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
     179                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
     180                $this->assertEquals( 1, get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
     181                wp_remove_object_terms( $attachment_id, array( $term_id ), 'wptests_tax' );
     182
     183                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'post' ) );
     184                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'wptests_tax', 'attachment' ) );
     185                $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) );
     186                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_post', true ) );
     187                $this->assertEmpty( get_term_meta( $term_id, '_wp_object_count_attachment', true ) );
     188        }
     189}