| 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 | |
| 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 | |