mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@49141 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
+290
-54
@@ -336,6 +336,7 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* @since 5.1.0 Introduced `meta_box_sanitize_cb` argument.
|
||||
* @since 5.4.0 Added the registered taxonomy object as a return value.
|
||||
* @since 5.5.0 Introduced `default_term` argument.
|
||||
* @since 5.6.0 Introduced `update_count_by_callback` argument.
|
||||
*
|
||||
* @global array $wp_taxonomies Registered taxonomies.
|
||||
*
|
||||
@@ -344,44 +345,44 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* @param array|string $args {
|
||||
* Optional. Array or query string of arguments for registering a taxonomy.
|
||||
*
|
||||
* @type array $labels An array of labels for this taxonomy. By default, Tag labels are
|
||||
* used for non-hierarchical taxonomies, and Category labels are used
|
||||
* for hierarchical taxonomies. See accepted values in
|
||||
* get_taxonomy_labels(). Default empty array.
|
||||
* @type string $description A short descriptive summary of what the taxonomy is for. Default empty.
|
||||
* @type bool $public Whether a taxonomy is intended for use publicly either via
|
||||
* the admin interface or by front-end users. The default settings
|
||||
* of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus`
|
||||
* are inherited from `$public`.
|
||||
* @type bool $publicly_queryable Whether the taxonomy is publicly queryable.
|
||||
* If not set, the default is inherited from `$public`
|
||||
* @type bool $hierarchical Whether the taxonomy is hierarchical. Default false.
|
||||
* @type bool $show_ui Whether to generate and allow a UI for managing terms in this taxonomy in
|
||||
* the admin. If not set, the default is inherited from `$public`
|
||||
* (default true).
|
||||
* @type bool $show_in_menu Whether to show the taxonomy in the admin menu. If true, the taxonomy is
|
||||
* shown as a submenu of the object type menu. If false, no menu is shown.
|
||||
* `$show_ui` must be true. If not set, default is inherited from `$show_ui`
|
||||
* (default true).
|
||||
* @type bool $show_in_nav_menus Makes this taxonomy available for selection in navigation menus. If not
|
||||
* set, the default is inherited from `$public` (default true).
|
||||
* @type bool $show_in_rest Whether to include the taxonomy in the REST API. Set this to true
|
||||
* for the taxonomy to be available in the block editor.
|
||||
* @type string $rest_base To change the base url of REST API route. Default is $taxonomy.
|
||||
* @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
|
||||
* @type bool $show_tagcloud Whether to list the taxonomy in the Tag Cloud Widget controls. If not set,
|
||||
* the default is inherited from `$show_ui` (default true).
|
||||
* @type bool $show_in_quick_edit Whether to show the taxonomy in the quick/bulk edit panel. It not set,
|
||||
* the default is inherited from `$show_ui` (default true).
|
||||
* @type bool $show_admin_column Whether to display a column for the taxonomy on its post type listing
|
||||
* screens. Default false.
|
||||
* @type bool|callable $meta_box_cb Provide a callback function for the meta box display. If not set,
|
||||
* post_categories_meta_box() is used for hierarchical taxonomies, and
|
||||
* post_tags_meta_box() is used for non-hierarchical. If false, no meta
|
||||
* box is shown.
|
||||
* @type callable $meta_box_sanitize_cb Callback function for sanitizing taxonomy data saved from a meta
|
||||
* box. If no callback is defined, an appropriate one is determined
|
||||
* based on the value of `$meta_box_cb`.
|
||||
* @type array $labels An array of labels for this taxonomy. By default, Tag labels are
|
||||
* used for non-hierarchical taxonomies, and Category labels are used
|
||||
* for hierarchical taxonomies. See accepted values in
|
||||
* get_taxonomy_labels(). Default empty array.
|
||||
* @type string $description A short descriptive summary of what the taxonomy is for. Default empty.
|
||||
* @type bool $public Whether a taxonomy is intended for use publicly either via
|
||||
* the admin interface or by front-end users. The default settings
|
||||
* of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus`
|
||||
* are inherited from `$public`.
|
||||
* @type bool $publicly_queryable Whether the taxonomy is publicly queryable.
|
||||
* If not set, the default is inherited from `$public`
|
||||
* @type bool $hierarchical Whether the taxonomy is hierarchical. Default false.
|
||||
* @type bool $show_ui Whether to generate and allow a UI for managing terms in this taxonomy in
|
||||
* the admin. If not set, the default is inherited from `$public`
|
||||
* (default true).
|
||||
* @type bool $show_in_menu Whether to show the taxonomy in the admin menu. If true, the taxonomy is
|
||||
* shown as a submenu of the object type menu. If false, no menu is shown.
|
||||
* `$show_ui` must be true. If not set, default is inherited from `$show_ui`
|
||||
* (default true).
|
||||
* @type bool $show_in_nav_menus Makes this taxonomy available for selection in navigation menus. If not
|
||||
* set, the default is inherited from `$public` (default true).
|
||||
* @type bool $show_in_rest Whether to include the taxonomy in the REST API. Set this to true
|
||||
* for the taxonomy to be available in the block editor.
|
||||
* @type string $rest_base To change the base url of REST API route. Default is $taxonomy.
|
||||
* @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
|
||||
* @type bool $show_tagcloud Whether to list the taxonomy in the Tag Cloud Widget controls. If not set,
|
||||
* the default is inherited from `$show_ui` (default true).
|
||||
* @type bool $show_in_quick_edit Whether to show the taxonomy in the quick/bulk edit panel. It not set,
|
||||
* the default is inherited from `$show_ui` (default true).
|
||||
* @type bool $show_admin_column Whether to display a column for the taxonomy on its post type listing
|
||||
* screens. Default false.
|
||||
* @type bool|callable $meta_box_cb Provide a callback function for the meta box display. If not set,
|
||||
* post_categories_meta_box() is used for hierarchical taxonomies, and
|
||||
* post_tags_meta_box() is used for non-hierarchical. If false, no meta
|
||||
* box is shown.
|
||||
* @type callable $meta_box_sanitize_cb Callback function for sanitizing taxonomy data saved from a meta
|
||||
* box. If no callback is defined, an appropriate one is determined
|
||||
* based on the value of `$meta_box_cb`.
|
||||
* @type array $capabilities {
|
||||
* Array of capabilities for this taxonomy.
|
||||
*
|
||||
@@ -399,14 +400,17 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* @type bool $hierarchical Either hierarchical rewrite tag or not. Default false.
|
||||
* @type int $ep_mask Assign an endpoint mask. Default `EP_NONE`.
|
||||
* }
|
||||
* @type string|bool $query_var Sets the query var key for this taxonomy. Default `$taxonomy` key. If
|
||||
* false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a
|
||||
* string, the query `?{query_var}={term_slug}` will be valid.
|
||||
* @type callable $update_count_callback Works much like a hook, in that it will be called when the count is
|
||||
* updated. Default _update_post_term_count() for taxonomies attached
|
||||
* to post types, which confirms that the objects are published before
|
||||
* counting them. Default _update_generic_term_count() for taxonomies
|
||||
* attached to other object types, such as users.
|
||||
* @type string|bool $query_var Sets the query var key for this taxonomy. Default `$taxonomy` key. If
|
||||
* false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a
|
||||
* string, the query `?{query_var}={term_slug}` will be valid.
|
||||
* @type callable $update_count_callback Works much like a hook, in that it will be called when the count is
|
||||
* updated. Default _update_post_term_count() for taxonomies attached
|
||||
* to post types, which confirms that the objects are published before
|
||||
* counting them. Default _update_generic_term_count() for taxonomies
|
||||
* attached to other object types, such as users.
|
||||
* @type callable $update_count_by_callback Works much like a hook, in that it will be called when the count is
|
||||
* incremented or decremented. Defaults to the value of `$update_count_callback` if
|
||||
* a custom callack is defined, otherwise uses wp_modify_term_count_by().
|
||||
* @type string|array $default_term {
|
||||
* Default term to be used for the taxonomy.
|
||||
*
|
||||
@@ -414,8 +418,8 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* @type string $slug Slug for default term. Default empty.
|
||||
* @type string $description Description for default term. Default empty.
|
||||
* }
|
||||
* @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!
|
||||
* Default false.
|
||||
* @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!
|
||||
* Default false.
|
||||
* }
|
||||
* @return WP_Taxonomy|WP_Error The registered taxonomy object on success, WP_Error object on failure.
|
||||
*/
|
||||
@@ -2561,6 +2565,25 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
|
||||
}
|
||||
|
||||
$taxonomy_object = get_taxonomy( $taxonomy );
|
||||
|
||||
$object_types = (array) $taxonomy_object->object_type;
|
||||
foreach ( $object_types as &$object_type ) {
|
||||
if ( 0 === strpos( $object_type, 'attachment:' ) ) {
|
||||
list( $object_type ) = explode( ':', $object_type );
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_filter( $object_types, 'post_type_exists' ) !== $object_types ) {
|
||||
// This taxonomy applies to non-posts, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} elseif ( 'publish' === get_post_status( $object_id ) ) {
|
||||
// Published post, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} else {
|
||||
$do_recount = false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $terms ) ) {
|
||||
$terms = array( $terms );
|
||||
}
|
||||
@@ -2646,8 +2669,8 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
$new_tt_ids[] = $tt_id;
|
||||
}
|
||||
|
||||
if ( $new_tt_ids ) {
|
||||
wp_update_term_count( $new_tt_ids, $taxonomy );
|
||||
if ( $new_tt_ids && $do_recount ) {
|
||||
wp_increment_term_count( $new_tt_ids, $taxonomy );
|
||||
}
|
||||
|
||||
if ( ! $append ) {
|
||||
@@ -2665,9 +2688,7 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
}
|
||||
}
|
||||
|
||||
$t = get_taxonomy( $taxonomy );
|
||||
|
||||
if ( ! $append && isset( $t->sort ) && $t->sort ) {
|
||||
if ( ! $append && isset( $taxonomy_object->sort ) && $taxonomy_object->sort ) {
|
||||
$values = array();
|
||||
$term_order = 0;
|
||||
|
||||
@@ -2748,6 +2769,31 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
|
||||
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
|
||||
}
|
||||
|
||||
$taxonomy_object = get_taxonomy( $taxonomy );
|
||||
|
||||
$object_types = (array) $taxonomy_object->object_type;
|
||||
foreach ( $object_types as &$object_type ) {
|
||||
if ( 0 === strpos( $object_type, 'attachment:' ) ) {
|
||||
list( $object_type ) = explode( ':', $object_type );
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_filter( $object_types, 'post_type_exists' ) !== $object_types ) {
|
||||
// This taxonomy applies to non-posts, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} elseif (
|
||||
'publish' === get_post_status( $object_id ) ||
|
||||
(
|
||||
'inherit' === get_post_status( $object_id ) &&
|
||||
'publish' === get_post_status( wp_get_post_parent_id( $object_id ) )
|
||||
)
|
||||
) {
|
||||
// Published post, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} else {
|
||||
$do_recount = false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $terms ) ) {
|
||||
$terms = array( $terms );
|
||||
}
|
||||
@@ -2806,7 +2852,9 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
|
||||
*/
|
||||
do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy );
|
||||
|
||||
wp_update_term_count( $tt_ids, $taxonomy );
|
||||
if ( $do_recount ) {
|
||||
wp_decrement_term_count( $tt_ids, $taxonomy );
|
||||
}
|
||||
|
||||
return (bool) $deleted;
|
||||
}
|
||||
@@ -3226,6 +3274,7 @@ function wp_defer_term_counting( $defer = null ) {
|
||||
$_defer = $defer;
|
||||
// Flush any deferred counts.
|
||||
if ( ! $defer ) {
|
||||
wp_modify_term_count_by( null, null, null, true );
|
||||
wp_update_term_count( null, null, true );
|
||||
}
|
||||
}
|
||||
@@ -3233,6 +3282,193 @@ function wp_defer_term_counting( $defer = null ) {
|
||||
return $_defer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents add/removing a term from modifying a term count.
|
||||
*
|
||||
* This is used by functions calling wp_transition_post_status() to indicate the
|
||||
* term count will be handled during the post's transition.
|
||||
*
|
||||
* @private
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param bool $new_setting The new setting for preventing term counts.
|
||||
* @return bool Whether term count prevention is enabled or disabled.
|
||||
*/
|
||||
function _wp_prevent_term_counting( $new_setting = null ) {
|
||||
static $prevent = false;
|
||||
|
||||
if ( is_bool( $new_setting ) ) {
|
||||
$prevent = $new_setting;
|
||||
}
|
||||
|
||||
return $prevent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to increment the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $increment_by By how many the term count is to be incremented. Default 1.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_increment_term_count( $tt_ids, $taxonomy, $increment_by = 1, $do_deferred = false ) {
|
||||
return wp_modify_term_count_by( $tt_ids, $taxonomy, $increment_by, $do_deferred );
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $decrement_by By how many the term count is to be decremented. Default 1.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_decrement_term_count( $tt_ids, $taxonomy, $decrement_by = 1, $do_deferred = false ) {
|
||||
return wp_modify_term_count_by( $tt_ids, $taxonomy, $decrement_by * -1, $do_deferred );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $modify_by By how many the term count is to be modified.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by, $do_deferred = false ) {
|
||||
static $_deferred = array();
|
||||
|
||||
if ( $do_deferred ) {
|
||||
foreach ( (array) $_deferred as $taxonomy_name => $modifications ) {
|
||||
$tax_by_count = array_reduce(
|
||||
array_keys( $modifications ),
|
||||
function( $by_count, $tt_id ) use ( $modifications ) {
|
||||
if ( ! isset( $by_count[ $modifications[ $tt_id ] ] ) ) {
|
||||
$by_count[ $modifications[ $tt_id ] ] = array();
|
||||
}
|
||||
$by_count[ $modifications[ $tt_id ] ][] = $tt_id;
|
||||
return $by_count;
|
||||
},
|
||||
array()
|
||||
);
|
||||
|
||||
foreach ( $tax_by_count as $_modify_by => $_tt_ids ) {
|
||||
wp_modify_term_count_by_now( $_tt_ids, $taxonomy_name, $_modify_by );
|
||||
}
|
||||
unset( $_deferred[ $taxonomy_name ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $tt_ids ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $tt_ids ) ) {
|
||||
$tt_ids = array( $tt_ids );
|
||||
}
|
||||
|
||||
if ( wp_defer_term_counting() ) {
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
if ( ! isset( $_deferred[ $taxonomy ][ $tt_id ] ) ) {
|
||||
$_deferred[ $taxonomy ][ $tt_id ] = 0;
|
||||
}
|
||||
$_deferred[ $taxonomy ][ $tt_id ] += $modify_by;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return wp_modify_term_count_by_now( $tt_ids, $taxonomy, $modify_by );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the amount of terms in taxonomy immediately
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $modify_by By how many the term count is to be modified.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_modify_term_count_by_now( $tt_ids, $taxonomy, $modify_by ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( 0 === $modify_by ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tt_ids = array_filter( array_map( 'intval', (array) $tt_ids ) );
|
||||
|
||||
if ( empty( $tt_ids ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$taxonomy = get_taxonomy( $taxonomy );
|
||||
if ( ! empty( $taxonomy->update_count_by_callback ) ) {
|
||||
call_user_func( $taxonomy->update_count_by_callback, $tt_ids, $taxonomy, $modify_by );
|
||||
clean_term_cache( $tt_ids, '', false );
|
||||
return true;
|
||||
}
|
||||
|
||||
$tt_ids_string = '(' . implode( ',', $tt_ids ) . ')';
|
||||
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
/** This action is documented in wp-includes/taxonomy.php */
|
||||
do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
|
||||
}
|
||||
|
||||
$result = $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
"UPDATE {$wpdb->term_taxonomy} AS tt SET tt.count = GREATEST( 0, tt.count + %d ) WHERE tt.term_taxonomy_id IN $tt_ids_string",
|
||||
$modify_by
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $result ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
/** This action is documented in wp-includes/taxonomy.php */
|
||||
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
|
||||
}
|
||||
|
||||
clean_term_cache( $tt_ids, '', false );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the amount of terms in taxonomy.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user