WordPress.org

Make WordPress Core

Ticket #40351: 40351.2.patch

File 40351.2.patch, 7.2 KB (added by davidbaumwald, 5 months ago)

Updated patch for 5.6

  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index 18c3224466..ff81c11ad5 100644
    a b add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); 
    358358add_action( 'publish_post', '_publish_post_hook', 5, 1 );
    359359add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
    360360add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 );
     361add_action( 'added_term_relationship', 'wp_increment_term_relationship', 10, 3 );
     362add_action( 'deleted_term_relationships', 'wp_decrement_term_relationship', 10, 3 );
     363add_action( 'edit_term', 'maybe_recount_posts_for_term', 10, 3 );
    361364add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
    362365add_action( 'admin_init', 'send_frame_options_header', 10, 0 );
    363366add_action( 'welcome_panel', 'wp_welcome_panel' );
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 6b3d9ae42d..3f0febb535 100644
    a b function _update_term_count_on_transition_post_status( $new_status, $old_status, 
    72397239        // Update counts for the post's terms.
    72407240        foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
    72417241                $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
    7242                 wp_update_term_count( $tt_ids, $taxonomy );
     7242
     7243                $countable_status = apply_filters( 'countable_status', [ 'publish' ], $taxonomy );
     7244
     7245                $count_new = in_array( $new_status, $countable_status, true );
     7246                $count_old = in_array( $old_status, $countable_status, true );
     7247                if ( $count_new && ! $count_old ) {
     7248                        $transition = 'increment';
     7249                } elseif ( $count_old && ! $count_new ) {
     7250                        $transition = 'decrement';
     7251                }
     7252
     7253                if ( $transition ) {
     7254                        wp_quick_update_term_count( $tt_ids, $taxonomy, $transition );
     7255                }
     7256
     7257                // For non-attachments, let's check if there are any attachment children
     7258                // with 'inherited' post status -- if so those will need to be re-counted.
     7259                if ( 'attachment' !== $post->post_type ) {
     7260                        $attachments = new WP_Query( array(
     7261                                'post_type'           => 'attachment',
     7262                                'post_parent'         => $post->ID,
     7263                                'post_status'         => 'inherit',
     7264                                'ignore_sticky_posts' => true,
     7265                                'no_found_rows'       => true,
     7266                                'posts_per_page'      => -1,
     7267                                'fields'              => 'ids',
     7268                                'orderby'             => 'ID',
     7269                                'order'               => 'ASC',
     7270                        ) );
     7271
     7272                        if ( $attachments->have_posts() ) {
     7273                                foreach ( $attachments->posts as $attachment_id ) {
     7274                                        _update_term_count_on_transition_post_status( $new_status, $old_status, (object) array(
     7275                                                'ID'        => $attachment_id,
     7276                                                'post_type' => 'attachment',
     7277                                         ) );
     7278                                }
     7279                        }
     7280                }
    72437281        }
    72447282}
    72457283
  • src/wp-includes/taxonomy.php

    diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
    index af5a20a443..c560706268 100644
    a b function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) { 
    32473247        return wp_update_term_count_now( $terms, $taxonomy );
    32483248}
    32493249
     3250/**
     3251 * Uses an increment/decrement system to perform term count.
     3252 *
     3253 * @since 5.6
     3254 *
     3255 * @param array  $terms           The term_taxonomy_id of terms to update.
     3256 * @param string $taxonomy        The context of the term.
     3257 * @param string $transition_type Accepts either 'increment' or 'decrement' value.
     3258 * @return true Always true when complete.
     3259 */
     3260function wp_quick_update_term_count( $terms, $taxonomy, $transition_type ) {
     3261        $terms = array_map( 'intval', $terms );
     3262
     3263        $taxonomy = get_taxonomy( $taxonomy );
     3264        if ( ! empty( $taxonomy->update_count_callback ) ) {
     3265                call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
     3266        } elseif ( ! empty( $terms ) ) {
     3267                $tt_ids_string = '(' . implode( ',', $terms ) . ')';
     3268
     3269                if ( 'increment' === $transition_type ) {
     3270                        // Incrementing.
     3271                        $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 );
     3272                } else {
     3273                        // Decrementing.
     3274                        $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 );
     3275                }
     3276
     3277                foreach ( $terms as $term ) {
     3278                        /** This action is documented in wp-includes/taxonomy.php */
     3279                        do_action( 'edit_term_taxonomy', $term, $taxonomy );
     3280                }
     3281
     3282                $wpdb->query( $update_query ); // WPCS: unprepared SQL ok.
     3283                foreach ( $terms as $term ) {
     3284                        /** This action is documented in wp-includes/taxonomy.php */
     3285                        do_action( 'edited_term_taxonomy', $term, $taxonomy );
     3286                }
     3287        }
     3288
     3289        clean_term_cache( $terms, '', false );
     3290
     3291        return true;
     3292}
     3293
     3294/**
     3295 * When a term relationship is added, increment the term count.
     3296 *
     3297 * @since 5.6
     3298 *
     3299* @param int    $object_id Object ID.
     3300* @param int    $tt_id     Single term taxonomy ID.
     3301* @param string $taxonomy  Taxonomy slug.
     3302 */
     3303function wp_increment_term_relationship( $object_id, $tt_id, $taxonomy ) {
     3304        _handle_term_relationship_change( $object_id, (array) $tt_id, $taxonomy, 'increment' );
     3305}
     3306
     3307/**
     3308 * When a term relationship is added, decrement the term count.
     3309 *
     3310 * @since 5.6
     3311 *
     3312* @param int    $object_id Object ID.
     3313* @param int    $tt_id     Single term taxonomy ID.
     3314* @param string $taxonomy  Taxonomy slug.
     3315 */
     3316function wp_decrement_term_relationship( $object_id, $tt_id, $taxonomy ) {
     3317        _handle_term_relationship_change( $object_id, (array) $tt_id, $taxonomy, 'decrement' );
     3318}
     3319
     3320/**
     3321 * Force-recount posts for a term.  Do this only when the update originates from the edit term screen.
     3322 *
     3323 * @since 5.6
     3324 *
     3325 * @param  int    $term_id the term id.
     3326 * @param  int    $tt_id the term taxonomy id.
     3327 * @param  string $taxonomy the taxonomy.
     3328 *
     3329 * @return bool false if the screen check fails, true otherwise
     3330 */
     3331function maybe_recount_posts_for_term( $term_id, $tt_id, $taxonomy ) {
     3332        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : '';
     3333        if ( ! ( $screen instanceof WP_Screen ) ) {
     3334                return false;
     3335        }
     3336        if ( "edit-{$taxonomy}" === $screen->id ) {
     3337                wp_update_term_count_now( array( $tt_id ), $taxonomy );
     3338        }
     3339        return true;
     3340}
     3341
     3342
    32503343/**
    32513344 * Perform term count update immediately.
    32523345 *
    function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() 
    36983791        return $term_list;
    36993792}
    37003793
     3794/**
     3795 * Update term counts when term relationships are added or deleted.
     3796 *
     3797 * @access private
     3798 * @since 5.6
     3799 *
     3800 * @param int    $object_id  Object ID.
     3801 * @param array  $tt_ids     Array of term taxonomy IDs.
     3802 * @param string $taxonomy   Taxonomy slug.
     3803 * @param string $transition Transition (increment or decrement).
     3804 */
     3805function _handle_term_relationship_change( $object_id, $tt_ids, $taxonomy, $transition ) {
     3806        $post = get_post( $object_id );
     3807
     3808        if ( ( ! $post || ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) ||
     3809                in_array( get_post_status( $post ), apply_filters( 'countable_status', array( 'publish' ), $taxonomy ), true ) ) {
     3810                // We use `get_post_status()` to check if parent status is 'inherit'.
     3811                wp_quick_update_term_count( $object_id, $tt_ids, $taxonomy, $transition );
     3812        } else {
     3813                clean_term_cache( $tt_ids, $taxonomy, false );
     3814        }
     3815}
     3816
     3817
    37013818/**
    37023819 * Add count of children to parent count.
    37033820 *