WordPress.org

Make WordPress Core

Ticket #40351: 40351-1.diff

File 40351-1.diff, 7.2 KB (added by rebasaurus, 5 weeks ago)

Add $taxonomy arg to countable_status filter

  • wp-includes/default-filters.php

    diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php
    index 2fd1d33b69..5598add424 100644
    a b add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); 
    357357add_action( 'publish_post', '_publish_post_hook', 5, 1 );
    358358add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
    359359add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 );
     360add_action( 'added_term_relationship', 'wp_increment_term_relationship', 10, 3 );
     361add_action( 'deleted_term_relationships', 'wp_decrement_term_relationship', 10, 3 );
     362add_action( 'edit_term', 'maybe_recount_posts_for_term', 10, 3 );
    360363add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
    361364add_action( 'admin_init', 'send_frame_options_header', 10, 0 );
    362365add_action( 'welcome_panel', 'wp_welcome_panel' );
  • wp-includes/post.php

    diff --git a/wp-includes/post.php b/wp-includes/post.php
    index c76381ce95..05f8dc686f 100644
    a b function _update_term_count_on_transition_post_status( $new_status, $old_status, 
    71897189        // Update counts for the post's terms.
    71907190        foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
    71917191                $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
    7192                 wp_update_term_count( $tt_ids, $taxonomy );
     7192
     7193                $countable_status = apply_filters( 'countable_status', [ 'publish' ], $taxonomy );
     7194
     7195                $count_new = in_array( $new_status, $countable_status, true );
     7196                $count_old = in_array( $old_status, $countable_status, true );
     7197                if ( $count_new && ! $count_old ) {
     7198                        $transition = 'increment';
     7199                } elseif ( $count_old && ! $count_new ) {
     7200                        $transition = 'decrement';
     7201                }
     7202
     7203                if ( $transition ) {
     7204                        wp_quick_update_term_count( $tt_ids, $taxonomy, $transition );
     7205                }
     7206
     7207                // For non-attachments, let's check if there are any attachment children
     7208                // with 'inherited' post status -- if so those will need to be re-counted.
     7209                if ( 'attachment' !== $post->post_type ) {
     7210                        $attachments = new WP_Query( array(
     7211                                'post_type'           => 'attachment',
     7212                                'post_parent'         => $post->ID,
     7213                                'post_status'         => 'inherit',
     7214                                'ignore_sticky_posts' => true,
     7215                                'no_found_rows'       => true,
     7216                                'posts_per_page'      => -1,
     7217                                'fields'              => 'ids',
     7218                                'orderby'             => 'ID',
     7219                                'order'               => 'ASC',
     7220                        ) );
     7221
     7222                        if ( $attachments->have_posts() ) {
     7223                                foreach ( $attachments->posts as $attachment_id ) {
     7224                                        _update_term_count_on_transition_post_status( $new_status, $old_status, (object) [
     7225                                                'ID' => $attachment_id,
     7226                                                'post_type' => 'attachment',
     7227                                        ] );
     7228                                }
     7229                        }
     7230                }
    71937231        }
    71947232}
    71957233
  • wp-includes/taxonomy.php

    diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php
    index d02aa9b9e4..88f26ee684 100644
    a b function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) { 
    31513151        return wp_update_term_count_now( $terms, $taxonomy );
    31523152}
    31533153
     3154/**
     3155 * Uses an increment/decrement system to perform term count.
     3156 *
     3157 * @since 5.5
     3158 *
     3159 * @param array  $terms           The term_taxonomy_id of terms to update.
     3160 * @param string $taxonomy        The context of the term.
     3161 * @param string $transition_type Accepts either 'increment' or 'decrement' value.
     3162 * @return true Always true when complete.
     3163 */
     3164function wp_quick_update_term_count( $terms, $taxonomy, $transition_type ) {
     3165        $terms = array_map( 'intval', $terms );
     3166
     3167        $taxonomy = get_taxonomy( $taxonomy );
     3168        if ( ! empty( $taxonomy->update_count_callback ) ) {
     3169                call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
     3170        } elseif ( ! empty( $terms )) {
     3171                $tt_ids_string = '(' . implode( ',', $terms ) . ')';
     3172
     3173                if ( 'increment' === $transition_type ) {
     3174                        // Incrementing.
     3175                        $update_query = $wpdb->prepare( "UPDATE {$wpdb->term_taxonomy} AS tt SET tt.count = tt.count + 1 WHERE tt.term_taxonomy_id IN %s", $tt_ids_string );
     3176                } else {
     3177                        // Decrementing.
     3178                        $update_query = $wpdb->prepare( "UPDATE {$wpdb->term_taxonomy} AS tt SET tt.count = tt.count - 1 WHERE tt.term_taxonomy_id IN %s AND tt.count > 0", $tt_ids_string );
     3179                }
     3180
     3181                foreach ( $terms as $term ) {
     3182                        /** This action is documented in wp-includes/taxonomy.php */
     3183                        do_action( 'edit_term_taxonomy', $term, $taxonomy );
     3184                }
     3185
     3186                $wpdb->query( $update_query ); // WPCS: unprepared SQL ok.
     3187                foreach ( $terms as $term ) {
     3188                        /** This action is documented in wp-includes/taxonomy.php */
     3189                        do_action( 'edited_term_taxonomy', $term, $taxonomy );
     3190                }
     3191        }
     3192
     3193        clean_term_cache( $terms, '', false );
     3194
     3195        return true;
     3196}
     3197
     3198/**
     3199 * When a term relationship is added, increment the term count.
     3200 *
     3201 * @since 5.5
     3202 *
     3203* @param int    $object_id Object ID.
     3204* @param int    $tt_id     Single term taxonomy ID.
     3205* @param string $taxonomy  Taxonomy slug.
     3206 */
     3207function wp_increment_term_relationship( $object_id, $tt_id, $taxonomy ) {
     3208        _handle_term_relationship_change( $object_id, (array) $tt_id, $taxonomy, 'increment' );
     3209}
     3210
     3211/**
     3212 * When a term relationship is added, decrement the term count.
     3213 *
     3214 * @since 5.5
     3215 *
     3216* @param int    $object_id Object ID.
     3217* @param int    $tt_id     Single term taxonomy ID.
     3218* @param string $taxonomy  Taxonomy slug.
     3219 */
     3220function wp_decrement_term_relationship( $object_id, $tt_id, $taxonomy ) {
     3221        _handle_term_relationship_change( $object_id, (array) $tt_id, $taxonomy, 'decrement' );
     3222}
     3223
     3224/**
     3225 * Force-recount posts for a term.  Do this only when the update originates from the edit term screen.
     3226 *
     3227 * @since 5.5
     3228 *
     3229 * @param  int    $term_id the term id.
     3230 * @param  int    $tt_id the term taxonomy id.
     3231 * @param  string $taxonomy the taxonomy.
     3232 *
     3233 * @return bool false if the screen check fails, true otherwise
     3234 */
     3235function maybe_recount_posts_for_term( $term_id, $tt_id, $taxonomy ) {
     3236        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : '';
     3237        if ( ! ( $screen instanceof WP_Screen ) ) {
     3238                return false;
     3239        }
     3240        if ( "edit-$taxonomy" === $screen->id ) {
     3241                wp_update_term_count_now( [ $tt_id ], $taxonomy );
     3242        }
     3243        return true;
     3244}
     3245
     3246
    31543247/**
    31553248 * Perform term count update immediately.
    31563249 *
    function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() 
    36023695        return $term_list;
    36033696}
    36043697
     3698/**
     3699 * Update term counts when term relationships are added or deleted.
     3700 *
     3701 * @access private
     3702 * @since 5.5
     3703 *
     3704 * @param int    $object_id  Object ID.
     3705 * @param array  $tt_ids     Array of term taxonomy IDs.
     3706 * @param string $taxonomy   Taxonomy slug.
     3707 * @param string $transition Transition (increment or decrement).
     3708 */
     3709function _handle_term_relationship_change( $object_id, $tt_ids, $taxonomy, $transition ) {
     3710        $post = get_post( $object_id );
     3711
     3712        if ( ( ! $post || ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) ||
     3713                in_array( get_post_status( $post ), apply_filters( 'countable_status', [ 'publish' ], $taxonomy ), true ) ) {
     3714                // We use `get_post_status()` to check if parent status is 'inherit'.
     3715                wp_quick_update_term_count( $object_id, $tt_ids, $taxonomy, $transition );
     3716        } else {
     3717                clean_term_cache( $tt_ids, $taxonomy, false );
     3718        }
     3719}
     3720
     3721
    36053722/**
    36063723 * Add count of children to parent count.
    36073724 *