WordPress.org

Make WordPress Core

Changeset 19035


Ignore:
Timestamp:
10/21/11 21:38:14 (6 years ago)
Author:
nacin
Message:

Separate non-post term counting from _update_post_term_count(). Introduce _update_generic_term_count(). The generic handler will be the default whenever the taxonomy is attached to an object type other than a post type (link, user). Otherwise the _update_post_term_count() handler will be the default. fixes #18986. see #17548.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r19031 r19035  
    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 
     
    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); 
     2862 
     2863    if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) { 
     2864        unset( $object_types[ $check_attachments ] ); 
     2865        $check_attachments = true; 
     2866    } 
     2867 
     2868    if ( $object_types ) 
     2869        $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 
    28522870 
    28532871    foreach ( (array) $terms as $term ) { 
     2872        $count = 0; 
    28542873 
    28552874        // 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 ) ); 
    2862  
     2875        if ( $check_attachments ) 
     2876            $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 ) ); 
     2877 
     2878        if ( $object_types ) 
     2879            $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 ) ); 
     2880 
     2881         
    28632882        do_action( 'edit_term_taxonomy', $term, $taxonomy ); 
    28642883        $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 
     
    28672886} 
    28682887 
     2888/** 
     2889 * Will update term count based on number of objects. 
     2890 * 
     2891 * Default callback for the link_category taxonomy. 
     2892 * 
     2893 * @package WordPress 
     2894 * @subpackage Taxonomy 
     2895 * @since 3.3.0 
     2896 * @uses $wpdb 
     2897 * 
     2898 * @param array $terms List of Term taxonomy IDs 
     2899 * @param object $taxonomy Current taxonomy object of terms 
     2900 */ 
     2901function _update_generic_term_count( $terms, $taxonomy ) { 
     2902    global $wpdb; 
     2903 
     2904    foreach ( (array) $terms as $term ) { 
     2905        $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); 
     2906 
     2907        do_action( 'edit_term_taxonomy', $term, $taxonomy ); 
     2908        $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 
     2909        do_action( 'edited_term_taxonomy', $term, $taxonomy ); 
     2910    } 
     2911} 
    28692912 
    28702913/** 
Note: See TracChangeset for help on using the changeset viewer.