WordPress.org

Make WordPress Core

Ticket #40351: 40351.diff

File 40351.diff, 7.1 KB (added by rebasaurus, 5 weeks ago)

Porting of the LTCU plugin into core

  • 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 e196d09726..905f11e4dd 100644
    a b function _update_term_count_on_transition_post_status( $new_status, $old_status, 
    71787178        // Update counts for the post's terms.
    71797179        foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
    71807180                $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
    7181                 wp_update_term_count( $tt_ids, $taxonomy );
     7181
     7182                $countable_status = apply_filters( 'countable_status', [ 'publish' ] );
     7183
     7184                $count_new = in_array( $new_status, $countable_status, true );
     7185                $count_old = in_array( $old_status, $countable_status, true );
     7186                if ( $count_new && ! $count_old ) {
     7187                        $transition = 'increment';
     7188                } elseif ( $count_old && ! $count_new ) {
     7189                        $transition = 'decrement';
     7190                }
     7191
     7192                if ( $transition ) {
     7193                        wp_quick_update_term_count( $tt_ids, $taxonomy, $transition );
     7194                }
     7195
     7196                // For non-attachments, let's check if there are any attachment children
     7197                // with 'inherited' post status -- if so those will need to be re-counted.
     7198                if ( 'attachment' !== $post->post_type ) {
     7199                        $attachments = new WP_Query( array(
     7200                                'post_type'           => 'attachment',
     7201                                'post_parent'         => $post->ID,
     7202                                'post_status'         => 'inherit',
     7203                                'ignore_sticky_posts' => true,
     7204                                'no_found_rows'       => true,
     7205                                'posts_per_page'      => -1,
     7206                                'fields'              => 'ids',
     7207                                'orderby'             => 'ID',
     7208                                'order'               => 'ASC',
     7209                        ) );
     7210
     7211                        if ( $attachments->have_posts() ) {
     7212                                foreach ( $attachments->posts as $attachment_id ) {
     7213                                        _update_term_count_on_transition_post_status( $new_status, $old_status, (object) [
     7214                                                'ID' => $attachment_id,
     7215                                                'post_type' => 'attachment',
     7216                                        ] );
     7217                                }
     7218                        }
     7219                }
    71827220        }
    71837221}
    71847222
  • wp-includes/taxonomy.php

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