Enforce ORDER BY and LIMIT clauses in term_exists()
queries.
In case of edge cases where a duplicate term might exist in a replicated database for a split second, these explicit query clauses ensure that `term_exists()` will always recognize the oldest matched term as the canonical one. See [30238] for background. Props pento. See #22023, #5809. git-svn-id: https://develop.svn.wordpress.org/trunk@30239 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
3f7051bf81
commit
8096af102a
@ -2054,6 +2054,8 @@ function term_exists( $term, $taxonomy = '', $parent = null ) {
|
||||
$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 ( is_numeric( $parent ) ) {
|
||||
$parent = (int) $parent;
|
||||
@ -2066,16 +2068,16 @@ function term_exists( $term, $taxonomy = '', $parent = null ) {
|
||||
$where_fields[] = $taxonomy;
|
||||
$else_where_fields[] = $taxonomy;
|
||||
|
||||
if ( $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", $where_fields), ARRAY_A) )
|
||||
if ( $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) )
|
||||
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", $else_where_fields), ARRAY_A);
|
||||
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);
|
||||
}
|
||||
|
||||
if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )
|
||||
if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
|
||||
return $result;
|
||||
|
||||
return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
|
||||
return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user