Make WordPress Core

Ticket #18986: 18986.6.diff

File 18986.6.diff, 4.6 KB (added by nacin, 12 years ago)
  • wp-includes/taxonomy.php

     
    24492449        if ( !empty($taxonomy->update_count_callback) ) {
    24502450                call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
    24512451        } else {
    2452                 // Default count updater
    2453                 _update_post_term_count( $terms, $taxonomy );
     2452                $object_types = (array) $taxonomy->object_type;
     2453                foreach ( $object_types as &$object_type ) {
     2454                        if ( 0 === strpos( $object_type, 'attachment:' ) )
     2455                                list( $object_type ) = explode( ':', $object_type );
     2456                }
     2457
     2458                if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {
     2459                        // Only post types are attached to this taxonomy
     2460                        _update_post_term_count( $terms, $taxonomy );
     2461                } else {
     2462                        // Default count updater
     2463                        _update_generic_term_count( $terms, $taxonomy );
     2464                }
    24542465        }
    24552466
    24562467        clean_term_cache($terms, '', false);
     
    28422853function _update_post_term_count( $terms, $taxonomy ) {
    28432854        global $wpdb;
    28442855
    2845         $object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type);
     2856        $object_types = (array) $taxonomy->object_type;
    28462857       
    28472858        foreach ( $object_types as &$object_type )
    28482859                list( $object_type ) = explode( ':', $object_type );
    2849        
     2860
    28502861        $object_types = array_unique( $object_types );
    2851         $object_types = esc_sql($object_types);
    28522862
     2863        if ( $check_attachments = array_search( 'attachment', $object_types ) )
     2864                unset( $object_types[ $check_attachments ] );
     2865        $check_attachments = false !== $check_attachments;
     2866
     2867        if ( $object_types )
     2868                $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
     2869
    28532870        foreach ( (array) $terms as $term ) {
     2871                $count = 0;
    28542872
    28552873                // 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 ) );
     2874                if ( $check_attachments )
     2875                        $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 ) );
    28622876
     2877                if ( $object_types )
     2878                        $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 ) );
     2879
     2880               
    28632881                do_action( 'edit_term_taxonomy', $term, $taxonomy );
    28642882                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    28652883                do_action( 'edited_term_taxonomy', $term, $taxonomy );
    28662884        }
    28672885}
    28682886
     2887/**
     2888 * Will update term count based on number of objects.
     2889 *
     2890 * Default callback for the link_category taxonomy.
     2891 *
     2892 * @package WordPress
     2893 * @subpackage Taxonomy
     2894 * @since 3.3.0
     2895 * @uses $wpdb
     2896 *
     2897 * @param array $terms List of Term taxonomy IDs
     2898 * @param object $taxonomy Current taxonomy object of terms
     2899 */
     2900function _update_generic_term_count( $terms, $taxonomy ) {
     2901        global $wpdb;
    28692902
     2903        foreach ( (array) $terms as $term ) {
     2904                $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
     2905
     2906                do_action( 'edit_term_taxonomy', $term, $taxonomy );
     2907                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
     2908                do_action( 'edited_term_taxonomy', $term, $taxonomy );
     2909        }
     2910}
     2911
    28702912/**
    28712913 * Generates a permalink for a taxonomy term archive.
    28722914 *