From 8096af102a638d29d019fbcef2dc85e02fca5b83 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 5 Nov 2014 01:08:44 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index d5f5f33ade..3a9b7c985b 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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) ); } /**