Make WordPress Core

Ticket #38280: 38280.diff

File 38280.diff, 3.6 KB (added by desrosj, 7 years ago)

Stores object type specific counts in term meta when term counts are updated. Adds wp_get_term_count_for_object_type() to retrieve term counts.

  • src/wp-includes/taxonomy.php

     
    19011901         */
    19021902        return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
    19031903}
    1904 
     1904
    19051905/**
    19061906 * Add a new term to the database.
    19071907 *
     
    28822882        return true;
    28832883}
    28842884
     2885/**
     2886 * Retrieves the term count for a specific object type.
     2887 *
     2888 * @param $term_id
     2889 * @param $taxonomy
     2890 * @param $object_type
     2891 *
     2892 * @return boolean|int|WP_Error
     2893 */
     2894function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) {
     2895        if ( ! taxonomy_exists( $taxonomy ) ) {
     2896                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
     2897        }
     2898
     2899        if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) {
     2900                return false;
     2901        }
     2902
     2903        $term_count_meta = get_term_meta( $term_id, '_wp_term_counts', true );
     2904
     2905        if ( ! $term_count_meta ) {
     2906                $term = get_term( $term_id, $taxonomy );
     2907                return (int) $term->count;
     2908        }
     2909
     2910        return (int) $term_count_meta[ $object_type ];
     2911}
     2912
    28852913//
    28862914// Cache
    28872915//
     
    33633391
    33643392        foreach ( (array) $terms as $term ) {
    33653393                $count = 0;
     3394                $term_count_meta = get_term_meta( $term, '_wp_term_counts', true );
    33663395
     3396                // Remove any object types that no longer have taxonomy support.
     3397                $term_count_meta = array_intersect( $term_count_meta, $object_types );
     3398
    33673399                // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    3368                 if ( $check_attachments )
    3369                         $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 ) );
     3400                if ( $check_attachments ) {
     3401                        $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 ) );
    33703402
    3371                 if ( $object_types )
    3372                         $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 ) );
     3403                        $count += $attachment_count;
     3404                        $term_count_meta['attachment'] = $attachment_count;
     3405                }
    33733406
     3407                if ( $object_types ) {
     3408                        foreach ( $object_types as $type ) {
     3409                                $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 ) );
     3410
     3411                                $term_count_meta[ $type ] = $current_count;
     3412                                $count += $current_count;
     3413                        }
     3414                }
     3415
     3416                if ( 1 < count( $term_count_meta ) ) {
     3417                        update_term_meta( $term, '_wp_term_counts', $term_count_meta );
     3418                } else {
     3419                        delete_term_meta( $term, '_wp_term_counts' );
     3420                }
     3421
    33743422                /** This action is documented in wp-includes/taxonomy.php */
    33753423                do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
    33763424                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );