Make WordPress Core

Ticket #18986: 18986.diff

File 18986.diff, 4.1 KB (added by nacin, 13 years ago)
  • wp-includes/taxonomy.php

     
    24502450                call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
    24512451        } else {
    24522452                // Default count updater
    2453                 _update_post_term_count( $terms, $taxonomy );
     2453                _update_generic_term_count( $terms, $taxonomy );
    24542454        }
    24552455
    24562456        clean_term_cache($terms, '', false);
     
    28422842function _update_post_term_count( $terms, $taxonomy ) {
    28432843        global $wpdb;
    28442844
    2845         $object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type);
     2845        $object_types = (array) $taxonomy->object_type;
    28462846       
    28472847        foreach ( $object_types as &$object_type )
    28482848                list( $object_type ) = explode( ':', $object_type );
    2849        
     2849
    28502850        $object_types = array_unique( $object_types );
    2851         $object_types = esc_sql($object_types);
    28522851
     2852        if ( $check_attachments = array_search( 'attachment', $object_types ) )
     2853                unset( $object_types[ $check_attachments ] );
     2854        $check_attachments = (bool) $check_attachments;
     2855
     2856        if ( $object_types )
     2857                $object_types = array_filter( $object_types, 'post_type_exists' );
     2858
    28532859        foreach ( (array) $terms as $term ) {
     2860                $count = 0;
    28542861
    28552862                // Attachments can be 'inherit' status, we need to base count off the parent's status if so
    2856                 if ( in_array( 'attachment', $object_types ) )
    2857                         $count = $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 IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term ) );
    2858                 elseif ( post_type_exists( $object_type ) )
    2859                         $count = $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 ) );
    2860                 else
    2861                         $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
     2863                if ( $check_attachments )
     2864                        $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 ) );
    28622865
     2866                if ( $object_types )
     2867                        $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 ) );
     2868
     2869               
    28632870                do_action( 'edit_term_taxonomy', $term, $taxonomy );
    28642871                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    28652872                do_action( 'edited_term_taxonomy', $term, $taxonomy );
    28662873        }
    28672874}
    28682875
     2876/**
     2877 * Will update term count based on number of objects.
     2878 *
     2879 * Default callback for the link_category taxonomy.
     2880 *
     2881 * @package WordPress
     2882 * @subpackage Taxonomy
     2883 * @since 3.3.0
     2884 * @uses $wpdb
     2885 *
     2886 * @param array $terms List of Term taxonomy IDs
     2887 * @param object $taxonomy Current taxonomy object of terms
     2888 */
     2889function _update_generic_term_count( $terms, $taxonomy ) {
     2890        global $wpdb;
    28692891
     2892        foreach ( (array) $terms as $term ) {
     2893                $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
     2894
     2895                do_action( 'edit_term_taxonomy', $term, $taxonomy );
     2896                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
     2897                do_action( 'edited_term_taxonomy', $term, $taxonomy );
     2898        }
     2899}
     2900
    28702901/**
    28712902 * Generates a permalink for a taxonomy term archive.
    28722903 *