| 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 | */ |
| 3161 | function 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 | */ |
| 3204 | function 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 | */ |
| 3217 | function 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 | */ |
| 3232 | function 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 | |
| 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 | */ |
| 3706 | function _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 | |