From ef13be6f27ecbcba712ab003d2ced3ae58977d33 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 5 Nov 2014 01:09:21 +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. Built from https://develop.svn.wordpress.org/trunk@30239 git-svn-id: http://core.svn.wordpress.org/trunk@30239 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 10 ++++++---- wp-includes/version.php | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index d5f5f33ade..3a9b7c985b 100644 --- a/wp-includes/taxonomy.php +++ b/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) ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 9cea8be0d7..bf77663155 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.