Ticket #40351: 40351.3.patch
File 40351.3.patch, 9.7 KB (added by , 4 years ago) |
---|
-
src/wp-includes/default-filters.php
358 358 add_action( 'publish_post', '_publish_post_hook', 5, 1 ); 359 359 add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); 360 360 add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 ); 361 add_action( 'added_term_relationship', 'wp_increment_term_relationship', 10, 3 ); 362 add_action( 'deleted_term_relationships', 'wp_decrement_term_relationship', 10, 3 ); 363 add_action( 'edit_term', 'maybe_recount_posts_for_term', 10, 3 ); 361 364 add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ); 362 365 add_action( 'admin_init', 'send_frame_options_header', 10, 0 ); 363 366 add_action( 'welcome_panel', 'wp_welcome_panel' ); -
src/wp-includes/post.php
7239 7239 // Update counts for the post's terms. 7240 7240 foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { 7241 7241 $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 } 7243 7281 } 7244 7282 } 7245 7283 -
src/wp-includes/taxonomy.php
3248 3248 } 3249 3249 3250 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 */ 3260 function 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 */ 3303 function 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 */ 3316 function 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 */ 3331 function 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 3343 /** 3251 3344 * Perform term count update immediately. 3252 3345 * 3253 3346 * @since 2.5.0 … … 3699 3792 } 3700 3793 3701 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 */ 3805 function _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 3818 /** 3702 3819 * Add count of children to parent count. 3703 3820 * 3704 3821 * Recalculates term counts by including items from child terms. Assumes all … … 3840 3957 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 3841 3958 } 3842 3959 3960 // create ( status_list ) text fragment for SQL. 3961 $in_countable_status = "( '" . implode( "', '", apply_filters( 'countable_status', array( 'publish' ), $taxonomy->name ) ) . "' )"; 3962 3843 3963 foreach ( (array) $terms as $term ) { 3844 3964 $count = 0; 3845 3965 3846 3966 // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 3847 3967 if ( $check_attachments ) { 3848 $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 ) ); 3968 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration 3969 $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 IN " . $in_countable_status . " OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN " . $in_countable_status . " ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); 3849 3970 } 3850 3971 3851 3972 if ( $object_types ) { 3852 // phpcs:ignore WordPress.DB. PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration3853 $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 ) );3973 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration 3974 $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 IN " . $in_countable_status . " AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 3854 3975 } 3855 3976 3856 3977 /** This action is documented in wp-includes/taxonomy.php */