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.
Built from https://develop.svn.wordpress.org/trunk@30239


git-svn-id: http://core.svn.wordpress.org/trunk@30239 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2014-11-05 01:09:21 +00:00
parent 4ce9db9ce8
commit ef13be6f27
2 changed files with 7 additions and 5 deletions

View File

@ -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) );
}
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.1-alpha-30238';
$wp_version = '4.1-alpha-30239';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.