WordPress.org

Make WordPress Core

Ticket #38280: 38280.3.diff

File 38280.3.diff, 8.0 KB (added by desrosj, 3 years ago)
  • src/wp-includes/taxonomy.php

     
    28972897        return true;
    28982898}
    28992899
     2900/**
     2901 * Retrieves the term count for a specific object type.
     2902 *
     2903 * @param int    $term_id Term ID.
     2904 * @param string $taxonomy Taxonomy name.
     2905 * @param string $object_type Object type.
     2906 *
     2907 * @return WP_Error|bool|int WP_Error if invalid taxonomy is passed.
     2908 *                           False if object is not in taxonomy.
     2909 *                           Object term count otherwise.
     2910 */
     2911function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) {
     2912        if ( ! taxonomy_exists( $taxonomy ) ) {
     2913                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
     2914        }
     2915
     2916        if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) {
     2917                return false;
     2918        }
     2919
     2920        $taxonomy_object = get_taxonomy( $taxonomy );
     2921
     2922        if ( 1 < count( $taxonomy_object->object_type ) ) {
     2923                return (int) get_term_meta( $term_id, '_wp_object_count_' . $object_type, true );
     2924        } else {
     2925                $term = get_term( $term_id, $taxonomy );
     2926                return $term->count;
     2927        }
     2928}
     2929
    29002930//
    29012931// Cache
    29022932//
     
    33723402
    33733403        $object_types = (array) $taxonomy->object_type;
    33743404
    3375         foreach ( $object_types as &$object_type )
     3405        foreach ( $object_types as &$object_type ) {
    33763406                list( $object_type ) = explode( ':', $object_type );
     3407        }
    33773408
    33783409        $object_types = array_unique( $object_types );
    33793410
     
    33823413                $check_attachments = true;
    33833414        }
    33843415
    3385         if ( $object_types )
     3416        if ( $object_types ) {
    33863417                $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
     3418        }
    33873419
    33883420        foreach ( (array) $terms as $term ) {
    33893421                $count = 0;
    33903422
     3423                $term_count_meta = array();
     3424
     3425                if ( $object_types ) {
     3426                        foreach ( $object_types as $type ) {
     3427                                $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 = '" . $type . "' AND term_taxonomy_id = %d", $term ) );
     3428
     3429                                $count += $current_count;
     3430                                $term_count_meta[ $type ] = $current_count;
     3431                        }
     3432                }
     3433
    33913434                // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    3392                 if ( $check_attachments )
    3393                         $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 ) );
     3435                if ( $check_attachments ) {
     3436                        $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", $term ) );
    33943437
    3395                 if ( $object_types )
    3396                         $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 ) );
     3438                        $count += $attachment_count;
     3439                        $term_count_meta['attachment'] = $attachment_count;
    33973440
     3441                        // Re-add attachment so the meta gets saved below.
     3442                        $object_types[] = 'attachment';
     3443                }
     3444
     3445                // Save individual counts for each object type in term meta.
     3446                if ( 1 < count( $term_count_meta ) ) {
     3447                        foreach ( $object_types as $type ) {
     3448                                if ( empty( $term_count_meta[ $type ] ) ) {
     3449                                        delete_term_meta( $term, '_wp_object_count_' . $type );
     3450                                } else {
     3451                                        update_term_meta( $term, '_wp_object_count_' . $type, (int) $term_count_meta[ $type ] );
     3452                                }
     3453                        }
     3454                } else {
     3455                        foreach ( $object_types as $type ) {
     3456                                delete_term_meta( $term, '_wp_object_count_' . $type );
     3457                        }
     3458                }
     3459
    33983460                /** This action is documented in wp-includes/taxonomy.php */
    33993461                do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
    34003462                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
  • tests/phpunit/tests/term/getTerms.php

     
    228228                ), $terms_id_slug );
    229229        }
    230230
    231         /**
     231        /**
    232232         * @ticket 11823
    233          */
     233         */
    234234        function test_get_terms_include_exclude() {
    235235                global $wpdb;
    236236
     
    22192219                $this->assertNotEquals( 'foo', $found );
    22202220        }
    22212221
     2222        /**
     2223         * @ticket 38280
     2224         */
     2225        public function test_wp_get_term_count_for_object_type_single_object_type() {
     2226                $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
     2227                $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
     2228
     2229                wp_set_object_terms( $post_id, array( $term_id ), 'category' );
     2230
     2231                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2232
     2233                $term_object = get_term( $term_id, 'category' );
     2234                $this->assertEquals( 1, $term_object->count );
     2235        }
     2236
     2237        /**
     2238         * @ticket 38280
     2239         */
     2240        public function test_wp_get_term_count_for_object_type_multiple_object_types() {
     2241                register_post_type( 'wptests_cpt' );
     2242                register_taxonomy_for_object_type( 'category', 'wptests_cpt' );
     2243
     2244                $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
     2245                $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
     2246                $custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) );
     2247
     2248                $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2249                $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
     2250
     2251                wp_set_object_terms( $post_id, array( $term_id ), 'category' );
     2252
     2253                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2254                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
     2255
     2256                wp_set_object_terms( $custom_post_id, array( $term_id ), 'category' );
     2257
     2258                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2259                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) );
     2260
     2261                $term_object = get_term( $term_id, 'category' );
     2262                $this->assertEquals( 2, $term_object->count );
     2263        }
     2264
     2265        /**
     2266         * @ticket 38280
     2267         */
     2268        public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() {
     2269                register_taxonomy_for_object_type( 'category', 'attachment' );
     2270
     2271                $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) );
     2272                $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
     2273                $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', $post_id );
     2274
     2275                $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2276                $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
     2277
     2278                wp_set_object_terms( $post_id, array( $term_id ), 'category' );
     2279
     2280                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2281                $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
     2282
     2283                wp_set_object_terms( $attachment_id, array( $term_id ), 'category' );
     2284
     2285                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) );
     2286                $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) );
     2287
     2288                $term_object = get_term( $term_id, 'category' );
     2289                $this->assertEquals( 2, $term_object->count );
     2290        }
     2291
    22222292        public static function maybe_filter_count() {
    22232293                return 'foo';
    22242294        }