Taxonomy: Use `get_terms` instead of a database lookup in `term_exists()`.

Replace raw SQL queries to the terms table, with a call to the `get_terms` function. Using `get_terms` means that `term_exists` is now cached. For developers using `term_exists` where cache invalidation is disabled, such as importing, a workaround was added to ensure that queries are uncached. 

Props Spacedmonkey, boonebgorges, flixos90, peterwilsoncc. 
Fixes #36949. 

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


git-svn-id: http://core.svn.wordpress.org/trunk@52510 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2022-03-11 11:07:05 +00:00
parent 28b6093061
commit 4c23c2f079
2 changed files with 51 additions and 44 deletions

View File

@ -1519,8 +1519,9 @@ function unregister_term_meta( $taxonomy, $meta_key ) {
* Conditional Tags} article in the Theme Developer Handbook. * Conditional Tags} article in the Theme Developer Handbook.
* *
* @since 3.0.0 * @since 3.0.0
* @since 6.0.0 Converted to use `get_terms()`.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global bool $_wp_suspend_cache_invalidation
* *
* @param int|string $term The term to check. Accepts term ID, slug, or name. * @param int|string $term The term to check. Accepts term ID, slug, or name.
* @param string $taxonomy Optional. The taxonomy name to use. * @param string $taxonomy Optional. The taxonomy name to use.
@ -1531,65 +1532,71 @@ function unregister_term_meta( $taxonomy, $meta_key ) {
* Returns 0 if term ID 0 is passed to the function. * Returns 0 if term ID 0 is passed to the function.
*/ */
function term_exists( $term, $taxonomy = '', $parent = null ) { function term_exists( $term, $taxonomy = '', $parent = null ) {
global $wpdb; global $_wp_suspend_cache_invalidation;
if ( null === $term ) { if ( null === $term ) {
return null; return null;
} }
$select = "SELECT term_id FROM $wpdb->terms as t WHERE "; $defaults = array(
$tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE "; 'get' => 'all',
'fields' => 'ids',
'number' => 1,
'update_term_meta_cache' => false,
'order' => 'ASC',
'orderby' => 'term_id',
'suppress_filter' => true,
);
// Ensure that while importing, queries are not cached.
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
// @todo Disable caching once #52710 is merged.
$defaults['cache_domain'] = microtime();
}
if ( ! empty( $taxonomy ) ) {
$defaults['taxonomy'] = $taxonomy;
$defaults['fields'] = 'all';
}
if ( is_int( $term ) ) { if ( is_int( $term ) ) {
if ( 0 === $term ) { if ( 0 === $term ) {
return 0; return 0;
} }
$where = 't.term_id = %d'; $args = wp_parse_args( array( 'include' => array( $term ) ), $defaults );
if ( ! empty( $taxonomy ) ) { $terms = get_terms( $args );
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber } else {
return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A ); $term = trim( wp_unslash( $term ) );
} else { if ( '' === $term ) {
return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); return null;
}
if ( ! empty( $taxonomy ) && is_numeric( $parent ) ) {
$defaults['parent'] = (int) $parent;
}
$args = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );
$terms = get_terms( $args );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
$args = wp_parse_args( array( 'name' => $term ), $defaults );
$terms = get_terms( $args );
} }
} }
$term = trim( wp_unslash( $term ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) {
$slug = sanitize_title( $term ); return null;
}
$_term = array_shift( $terms );
$where = 't.slug = %s';
$else_where = 't.name = %s';
$where_fields = array( $slug );
$else_where_fields = array( $term );
$orderby = 'ORDER BY t.term_id ASC';
$limit = 'LIMIT 1';
if ( ! empty( $taxonomy ) ) { if ( ! empty( $taxonomy ) ) {
if ( is_numeric( $parent ) ) { return array(
$parent = (int) $parent; 'term_id' => (string) $_term->term_id,
$where_fields[] = $parent; 'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
$else_where_fields[] = $parent; );
$where .= ' AND tt.parent = %d';
$else_where .= ' AND tt.parent = %d';
}
$where_fields[] = $taxonomy;
$else_where_fields[] = $taxonomy;
$result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields ), ARRAY_A );
if ( $result ) {
return $result;
}
return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields ), ARRAY_A );
} }
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare return (string) $_term;
$result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) );
if ( $result ) {
return $result;
}
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.0-alpha-52895'; $wp_version = '6.0-alpha-52921';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.