WordPress.org

Make WordPress Core

Ticket #18986: 18986.2.diff

File 18986.2.diff, 4.6 KB (added by nacin, 7 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                $object_types = array_filter( 'post_type_exists', $object_types );
     2459
     2460                if ( ! $object_types ) {
     2461                        // Only post types are attached to this taxonomy
     2462                        _update_post_term_count( $terms, $taxonomy );
     2463                } else {
     2464                        // Default count updater
     2465                        _update_generic_term_count( $terms, $taxonomy );
     2466                }
    24542467        }
    24552468
    24562469        clean_term_cache($terms, '', false);
     
    28422855function _update_post_term_count( $terms, $taxonomy ) {
    28432856        global $wpdb;
    28442857
    2845         $object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type);
     2858        $object_types = (array) $taxonomy->object_type;
    28462859       
    28472860        foreach ( $object_types as &$object_type )
    28482861                list( $object_type ) = explode( ':', $object_type );
    2849        
     2862
    28502863        $object_types = array_unique( $object_types );
    2851         $object_types = esc_sql($object_types);
    28522864
     2865        if ( $check_attachments = array_search( 'attachment', $object_types ) )
     2866                unset( $object_types[ $check_attachments ] );
     2867        $check_attachments = (bool) $check_attachments;
     2868
     2869        if ( $object_types ) {
     2870                $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
     2871
    28532872        foreach ( (array) $terms as $term ) {
     2873                $count = 0;
    28542874
    28552875                // 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 ) );
     2876                if ( $check_attachments )
     2877                        $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 ) );
    28622878
     2879                if ( $object_types )
     2880                        $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 ) );
     2881
     2882               
    28632883                do_action( 'edit_term_taxonomy', $term, $taxonomy );
    28642884                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    28652885                do_action( 'edited_term_taxonomy', $term, $taxonomy );
    28662886        }
    28672887}
    28682888
     2889/**
     2890 * Will update term count based on number of objects.
     2891 *
     2892 * Default callback for the link_category taxonomy.
     2893 *
     2894 * @package WordPress
     2895 * @subpackage Taxonomy
     2896 * @since 3.3.0
     2897 * @uses $wpdb
     2898 *
     2899 * @param array $terms List of Term taxonomy IDs
     2900 * @param object $taxonomy Current taxonomy object of terms
     2901 */
     2902function _update_generic_term_count( $terms, $taxonomy ) {
     2903        global $wpdb;
    28692904
     2905        foreach ( (array) $terms as $term ) {
     2906                $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
     2907
     2908                do_action( 'edit_term_taxonomy', $term, $taxonomy );
     2909                $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
     2910                do_action( 'edited_term_taxonomy', $term, $taxonomy );
     2911        }
     2912}
     2913
    28702914/**
    28712915 * Generates a permalink for a taxonomy term archive.
    28722916 *