Ticket #17548: 17548.7.diff

File 17548.7.diff, 4.5 KB (added by joehoyle, 20 months ago)
Line 
1Index: wp-includes/default-filters.php
2===================================================================
3--- wp-includes/default-filters.php     (revision 18855)
4+++ wp-includes/default-filters.php     (working copy)
5@@ -254,6 +254,7 @@
6 add_action( 'publish_post',               '_publish_post_hook',                       5, 1 );
7 add_action( 'save_post',                  '_save_post_hook',                          5, 2 );
8 add_action( 'transition_post_status',     '_transition_post_status',                  5, 3 );
9+add_action( 'transition_post_status',     '_update_term_count_on_transition_post_status', 10, 3 );
10 add_action( 'comment_form',               'wp_comment_form_unfiltered_html_nonce'          );
11 add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'                            );
12 add_action( 'admin_init',                 'send_frame_options_header',               10, 0 );
13Index: wp-includes/taxonomy.php
14===================================================================
15--- wp-includes/taxonomy.php    (revision 18855)
16+++ wp-includes/taxonomy.php    (working copy)
17@@ -2447,13 +2447,7 @@
18                call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
19        } else {
20                // Default count updater
21-               foreach ( (array) $terms as $term) {
22-                       $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) );
23-                       do_action( 'edit_term_taxonomy', $term, $taxonomy );
24-                       $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
25-                       do_action( 'edited_term_taxonomy', $term, $taxonomy );
26-               }
27-
28+               _update_post_term_count( $terms, $taxonomy );
29        }
30 
31        clean_term_cache($terms, '', false);
32@@ -2849,7 +2843,13 @@
33        $object_types = esc_sql($object_types);
34 
35        foreach ( (array) $terms as $term ) {
36-               $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 ) );
37+               
38+               // Attachments can be 'inherit' status, we need to base count off the parent's staus if so
39+               if ( in_array( 'attachment', $object_types ) )
40+                       $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 ) );
41+               else
42+                       $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 ) );
43+
44                do_action( 'edit_term_taxonomy', $term, $taxonomy );
45                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
46                do_action( 'edited_term_taxonomy', $term, $taxonomy );
47Index: wp-includes/post.php
48===================================================================
49--- wp-includes/post.php        (revision 18855)
50+++ wp-includes/post.php        (working copy)
51@@ -2731,12 +2731,6 @@
52        $post->post_status = 'publish';
53        wp_transition_post_status('publish', $old_status, $post);
54 
55-       // Update counts for the post's terms.
56-       foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
57-               $tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids'));
58-               wp_update_term_count($tt_ids, $taxonomy);
59-       }
60-
61        do_action('edit_post', $post_id, $post);
62        do_action('save_post', $post_id, $post);
63        do_action('wp_insert_post', $post_id, $post);
64@@ -5312,4 +5306,20 @@
65 }
66 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
67 
68+/**
69+ * Update the custom taxonomies' term counts when a post's status is changed. For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
70+ *
71+ * @access private
72+ * @param string $new_status
73+ * @param string $old_status
74+ * @param object $post
75+ * @since 3.3.0
76+ */
77+function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
78+       // Update counts for the post's terms.
79+       foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
80+               $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
81+               wp_update_term_count( $tt_ids, $taxonomy );
82+       }
83+}
84 ?>