Make WordPress Core

Ticket #38280: 38280.2.diff

File 38280.2.diff, 6.1 KB (added by desrosj, 7 years ago)

Updated ticket adds unit tests, casts get_term_meta() call into an array to prevent PHP notice, improved function Docblock.

  • 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        $term_count_meta = get_term_meta( $term_id, '_wp_term_counts', true );
     2921
     2922        if ( ! $term_count_meta ) {
     2923                $term = get_term( $term_id, $taxonomy );
     2924                return (int) $term->count;
     2925        }
     2926
     2927        return (int) $term_count_meta[ $object_type ];
     2928}
     2929
    29002930//
    29012931// Cache
    29022932//
     
    33873417
    33883418        foreach ( (array) $terms as $term ) {
    33893419                $count = 0;
     3420                $term_count_meta = (array) get_term_meta( $term, '_wp_term_counts', true );
    33903421
     3422                // Remove any object types that no longer have taxonomy support.
     3423                $term_count_meta = array_intersect( $term_count_meta, $object_types );
     3424
    33913425                // 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 ) );
     3426                if ( $check_attachments ) {
     3427                        $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 ) );
    33943428
    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 ) );
     3429                        $count += $attachment_count;
     3430                        $term_count_meta['attachment'] = $attachment_count;
     3431                }
    33973432
     3433                if ( $object_types ) {
     3434                        foreach ( $object_types as $type ) {
     3435                                $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 ) );
     3436
     3437                                $term_count_meta[ $type ] = $current_count;
     3438                                $count += $current_count;
     3439                        }
     3440                }
     3441
     3442                if ( 1 < count( $term_count_meta ) ) {
     3443                        update_term_meta( $term, '_wp_term_counts', $term_count_meta );
     3444                } else {
     3445                        delete_term_meta( $term, '_wp_term_counts' );
     3446                }
     3447
    33983448                /** This action is documented in wp-includes/taxonomy.php */
    33993449                do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
    34003450                $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                $this->assertEquals( array( 'post' => 1, 'wptests_cpt' => 1 ), get_term_meta( $term_id, '_wp_term_counts', true ) );
     2262
     2263                $term_object = get_term( $term_id, 'category' );
     2264                $this->assertEquals( 2, $term_object->count );
     2265        }
     2266
    22222267        public static function maybe_filter_count() {
    22232268                return 'foo';
    22242269        }