Ticket #36949: 36949.2.patch
File 36949.2.patch, 5.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/taxonomy.php
1258 1258 * is specified and the pairing exists. 1259 1259 */ 1260 1260 function term_exists( $term, $taxonomy = '', $parent = null ) { 1261 global $wpdb;1262 1261 1263 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1264 $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 "; 1262 1265 1263 1266 if ( is_int( $term) ) {1267 if ( 0 == $term )1264 if ( is_int( $term ) ) { 1265 if( 0 == $term ){ 1268 1266 return 0; 1269 $where = 't.term_id = %d'; 1270 if ( !empty($taxonomy) ) 1271 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); 1272 else 1273 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); 1267 } 1268 $_term = get_term( $term, $taxonomy ); 1269 if ( is_wp_error( $_term ) || is_null( $_term ) ) { 1270 return null; 1271 } 1272 if ( !empty( $taxonomy ) ) { 1273 return array( 'term_id' => (string)$_term->term_id, 'term_taxonomy_id' => (string)$_term->term_taxonomy_id ); 1274 } 1275 return (string)$_term->term_id; 1274 1276 } 1275 1277 1276 1278 $term = trim( wp_unslash( $term ) ); 1279 1280 if ( empty( $term )) { 1281 1282 return null; 1283 } 1284 1277 1285 $slug = sanitize_title( $term ); 1278 1286 1279 $where = 't.slug = %s'; 1280 $else_where = 't.name = %s'; 1281 $where_fields = array($slug); 1282 $else_where_fields = array($term); 1283 $orderby = 'ORDER BY t.term_id ASC'; 1284 $limit = 'LIMIT 1'; 1285 if ( !empty($taxonomy) ) { 1286 if ( is_numeric( $parent ) ) { 1287 $parent = (int) $parent; 1288 $where_fields[] = $parent; 1289 $else_where_fields[] = $parent; 1290 $where .= ' AND tt.parent = %d'; 1291 $else_where .= ' AND tt.parent = %d'; 1292 } 1293 1294 $where_fields[] = $taxonomy; 1295 $else_where_fields[] = $taxonomy; 1296 1297 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) ) 1298 return $result; 1287 $defaults = array( 1288 'get' => 'all', 1289 'hide_empty' => false, 1290 'number' => 1, 1291 'update_term_meta_cache' => false, 1292 'orderby' => 'term_id', 1293 'suppress_filter' => true, 1294 ); 1299 1295 1300 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); 1296 if ( is_numeric( $parent ) ) { 1297 $defaults['parent'] = (int) $parent; 1301 1298 } 1299 if ( !empty( $taxonomy ) ) { 1300 $defaults['taxonomy'] = $taxonomy; 1301 } 1302 $args = wp_parse_args( array( 'slug' => $term ), $defaults ); 1303 $terms = get_terms( $args ); 1304 if ( empty( $terms ) || is_wp_error( $terms ) ) { 1305 $args = wp_parse_args( array( 'name' => $term ), $defaults ); 1306 1307 $terms = get_terms( $args ); 1308 if ( empty( $terms ) || is_wp_error( $terms ) ) { 1309 return null; 1310 } 1311 } 1312 $_term = array_shift( $terms ); 1302 1313 1303 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) ) 1304 return $result; 1314 if ( !empty( $taxonomy ) ) { 1315 return array( 'term_id' => (string)$_term->term_id, 'term_taxonomy_id' => (string)$_term->term_taxonomy_id ); 1316 } 1305 1317 1306 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );1318 return (string)$_term->term_id; 1307 1319 } 1308 1320 1309 1321 /** … … 2207 2219 foreach ( (array) $terms as $term) { 2208 2220 if ( !strlen(trim($term)) ) 2209 2221 continue; 2210 2211 2222 if ( !$term_info = term_exists($term, $taxonomy) ) { 2223 2212 2224 // Skip if a non-existent term ID is passed. 2213 2225 if ( is_int($term) ) 2214 2226 continue; -
tests/phpunit/tests/term/getTerm.php
29 29 array( '%d' ), 30 30 array( '%d' ) 31 31 ); 32 32 clean_term_cache($t1['term_id'], 'wptests_tax'); 33 33 return array( 34 34 array( 35 35 'term_id' => $t1['term_id'], -
tests/phpunit/tests/term/wpGetObjectTerms.php
337 337 $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100004 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) ); 338 338 $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) ); 339 339 $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) ); 340 340 clean_term_cache( array( $term_1->term_id, $term_2->term_id, $term_3->term_id ), $this->taxonomy); 341 341 $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy ); 342 342 343 343 $found = wp_get_object_terms( $p, $this->taxonomy, array( 344 344 'orderby' => 'term_taxonomy_id', 345 345 'fields' => 'ids',