| 1 | Index: wp-includes/taxonomy.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- wp-includes/taxonomy.php (revision 19033) |
|---|
| 4 | +++ wp-includes/taxonomy.php (working copy) |
|---|
| 5 | @@ -2449,8 +2449,21 @@ |
|---|
| 6 | if ( !empty($taxonomy->update_count_callback) ) { |
|---|
| 7 | call_user_func($taxonomy->update_count_callback, $terms, $taxonomy); |
|---|
| 8 | } else { |
|---|
| 9 | - // Default count updater |
|---|
| 10 | - _update_post_term_count( $terms, $taxonomy ); |
|---|
| 11 | + $object_types = (array) $taxonomy->object_type; |
|---|
| 12 | + foreach ( $object_types as &$object_type ) { |
|---|
| 13 | + if ( 0 === strpos( $object_type, 'attachment:' ) ) |
|---|
| 14 | + list( $object_type ) = explode( ':', $object_type ); |
|---|
| 15 | + } |
|---|
| 16 | + |
|---|
| 17 | + $object_types = array_filter( 'post_type_exists', $object_types ); |
|---|
| 18 | + |
|---|
| 19 | + if ( ! $object_types ) { |
|---|
| 20 | + // Only post types are attached to this taxonomy |
|---|
| 21 | + _update_post_term_count( $terms, $taxonomy ); |
|---|
| 22 | + } else { |
|---|
| 23 | + // Default count updater |
|---|
| 24 | + _update_generic_term_count( $terms, $taxonomy ); |
|---|
| 25 | + } |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | clean_term_cache($terms, '', false); |
|---|
| 29 | @@ -2842,31 +2855,62 @@ |
|---|
| 30 | function _update_post_term_count( $terms, $taxonomy ) { |
|---|
| 31 | global $wpdb; |
|---|
| 32 | |
|---|
| 33 | - $object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type); |
|---|
| 34 | + $object_types = (array) $taxonomy->object_type; |
|---|
| 35 | |
|---|
| 36 | foreach ( $object_types as &$object_type ) |
|---|
| 37 | list( $object_type ) = explode( ':', $object_type ); |
|---|
| 38 | - |
|---|
| 39 | + |
|---|
| 40 | $object_types = array_unique( $object_types ); |
|---|
| 41 | - $object_types = esc_sql($object_types); |
|---|
| 42 | |
|---|
| 43 | + if ( $check_attachments = array_search( 'attachment', $object_types ) ) |
|---|
| 44 | + unset( $object_types[ $check_attachments ] ); |
|---|
| 45 | + $check_attachments = false !== $check_attachments; |
|---|
| 46 | + |
|---|
| 47 | + if ( $object_types ) { |
|---|
| 48 | + $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); |
|---|
| 49 | + |
|---|
| 50 | foreach ( (array) $terms as $term ) { |
|---|
| 51 | + $count = 0; |
|---|
| 52 | |
|---|
| 53 | // Attachments can be 'inherit' status, we need to base count off the parent's status if so |
|---|
| 54 | - if ( in_array( 'attachment', $object_types ) ) |
|---|
| 55 | - $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 ) ); |
|---|
| 56 | - elseif ( post_type_exists( $object_type ) ) |
|---|
| 57 | - $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 ) ); |
|---|
| 58 | - else |
|---|
| 59 | - $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|---|
| 60 | + if ( $check_attachments ) |
|---|
| 61 | + $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 ) ); |
|---|
| 62 | |
|---|
| 63 | + if ( $object_types ) |
|---|
| 64 | + $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 ) ); |
|---|
| 65 | + |
|---|
| 66 | + |
|---|
| 67 | do_action( 'edit_term_taxonomy', $term, $taxonomy ); |
|---|
| 68 | $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|---|
| 69 | do_action( 'edited_term_taxonomy', $term, $taxonomy ); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | +/** |
|---|
| 74 | + * Will update term count based on number of objects. |
|---|
| 75 | + * |
|---|
| 76 | + * Default callback for the link_category taxonomy. |
|---|
| 77 | + * |
|---|
| 78 | + * @package WordPress |
|---|
| 79 | + * @subpackage Taxonomy |
|---|
| 80 | + * @since 3.3.0 |
|---|
| 81 | + * @uses $wpdb |
|---|
| 82 | + * |
|---|
| 83 | + * @param array $terms List of Term taxonomy IDs |
|---|
| 84 | + * @param object $taxonomy Current taxonomy object of terms |
|---|
| 85 | + */ |
|---|
| 86 | +function _update_generic_term_count( $terms, $taxonomy ) { |
|---|
| 87 | + global $wpdb; |
|---|
| 88 | |
|---|
| 89 | + foreach ( (array) $terms as $term ) { |
|---|
| 90 | + $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|---|
| 91 | + |
|---|
| 92 | + do_action( 'edit_term_taxonomy', $term, $taxonomy ); |
|---|
| 93 | + $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|---|
| 94 | + do_action( 'edited_term_taxonomy', $term, $taxonomy ); |
|---|
| 95 | + } |
|---|
| 96 | +} |
|---|
| 97 | + |
|---|
| 98 | /** |
|---|
| 99 | * Generates a permalink for a taxonomy term archive. |
|---|
| 100 | * |
|---|