Taxonomy: Improve performance of term recounting database queries.

When modifying terms assigned to an object, replace full term recounts with incrementing/decrementing the count as appropriate. This provides a significant performance boost on sites with a high number of term/object relationships and/or posts.

Introduces the functions `wp_increment_term_count()`, `wp_decrement_term_count()`, `wp_modify_term_count_by()` and `wp_modify_term_count_by_now()` for updating the term count.

Introduces the function `_wp_prevent_term_counting()` for preventing double counting on posts that are about to transition.

Adds the parameter `update_count_by_callback` to `register_taxonomy()` to allow developers to use a custom callback for incrementing or decrementing a term count.

Props boonebgorges, davidbaumwald, hellofromTonya, johnbillion, lcyh78, mattoperry, peterwilsoncc, rebasaurus, whyisjake.
Fixes #40351.


Built from https://develop.svn.wordpress.org/trunk@49141


git-svn-id: http://core.svn.wordpress.org/trunk@48903 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson
2020-10-14 00:51:06 +00:00
parent 8859aea3e2
commit fa5a6c8622
4 changed files with 421 additions and 79 deletions

View File

@@ -180,6 +180,14 @@ final class WP_Taxonomy {
*/
public $update_count_callback;
/**
* Function that will be called when the count is modified by an amount.
*
* @since 5.6.0
* @var callable
*/
public $update_count_by_callback;
/**
* Whether this taxonomy should appear in the REST API.
*
@@ -277,28 +285,29 @@ final class WP_Taxonomy {
$args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'publicly_queryable' => null,
'hierarchical' => false,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_tagcloud' => null,
'show_in_quick_edit' => null,
'show_admin_column' => false,
'meta_box_cb' => null,
'meta_box_sanitize_cb' => null,
'capabilities' => array(),
'rewrite' => true,
'query_var' => $this->name,
'update_count_callback' => '',
'show_in_rest' => false,
'rest_base' => false,
'rest_controller_class' => false,
'default_term' => null,
'_builtin' => false,
'labels' => array(),
'description' => '',
'public' => true,
'publicly_queryable' => null,
'hierarchical' => false,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_tagcloud' => null,
'show_in_quick_edit' => null,
'show_admin_column' => false,
'meta_box_cb' => null,
'meta_box_sanitize_cb' => null,
'capabilities' => array(),
'rewrite' => true,
'query_var' => $this->name,
'update_count_callback' => '',
'update_count_by_callback' => '',
'show_in_rest' => false,
'rest_base' => false,
'rest_controller_class' => false,
'default_term' => null,
'_builtin' => false,
);
$args = array_merge( $defaults, $args );
@@ -411,6 +420,17 @@ final class WP_Taxonomy {
);
}
// If generic update callback is defined but increment/decrement callback is not.
if (
! empty( $args['update_count_callback'] ) &&
is_callable( $args['update_count_callback'] ) &&
empty( $args['update_count_by_callback'] )
) {
$args['update_count_by_callback'] = function( $tt_ids, $taxonomy, $modify_by ) {
return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy );
};
}
foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}