diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
index f1c1bce307..9880368d64 100644
|
|
function update_termmeta_cache( $term_ids ) { |
1258 | 1258 | * is specified and the pairing exists. |
1259 | 1259 | */ |
1260 | 1260 | function term_exists( $term, $taxonomy = '', $parent = null ) { |
1261 | | global $wpdb; |
| 1261 | if ( is_int( $term ) ) { |
| 1262 | if ( 0 == $term ) { |
| 1263 | return 0; |
| 1264 | } |
1262 | 1265 | |
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 "; |
| 1266 | $_term = get_term( $term, $taxonomy ); |
| 1267 | if ( is_wp_error( $_term ) || is_null( $_term ) ) { |
| 1268 | return null; |
| 1269 | } |
| 1270 | if ( ! empty( $taxonomy ) ) { |
| 1271 | return array( |
| 1272 | 'term_id' => (string) $_term->term_id, |
| 1273 | 'term_taxonomy_id' => (string) $_term->term_taxonomy_id, |
| 1274 | ); |
| 1275 | } |
1265 | 1276 | |
1266 | | if ( is_int($term) ) { |
1267 | | if ( 0 == $term ) |
1268 | | 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 ) ); |
| 1277 | return (string) $_term->term_id; |
1274 | 1278 | } |
1275 | 1279 | |
1276 | 1280 | $term = trim( wp_unslash( $term ) ); |
1277 | | $slug = sanitize_title( $term ); |
1278 | 1281 | |
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 | | } |
| 1282 | if ( empty( $term ) ) { |
| 1283 | return null; |
| 1284 | } |
1293 | 1285 | |
1294 | | $where_fields[] = $taxonomy; |
1295 | | $else_where_fields[] = $taxonomy; |
| 1286 | $slug = sanitize_title( $term ); |
1296 | 1287 | |
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; |
| 1288 | $defaults = array( |
| 1289 | 'get' => 'all', |
| 1290 | 'hide_empty' => false, |
| 1291 | 'number' => 1, |
| 1292 | 'update_term_meta_cache' => false, |
| 1293 | 'orderby' => 'term_id', |
| 1294 | 'suppress_filter' => true, |
| 1295 | ); |
| 1296 | |
| 1297 | if ( is_numeric( $parent ) ) { |
| 1298 | $defaults['parent'] = (int) $parent; |
| 1299 | } |
| 1300 | if ( ! empty( $taxonomy ) ) { |
| 1301 | $defaults['taxonomy'] = $taxonomy; |
| 1302 | } |
| 1303 | $args = wp_parse_args( array( 'slug' => $term ), $defaults ); |
| 1304 | $terms = get_terms( $args ); |
| 1305 | if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 1306 | $args = wp_parse_args( array( 'name' => $term ), $defaults ); |
1299 | 1307 | |
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); |
| 1308 | $terms = get_terms( $args ); |
| 1309 | if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 1310 | return null; |
| 1311 | } |
1301 | 1312 | } |
| 1313 | $_term = array_shift( $terms ); |
1302 | 1314 | |
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; |
| 1315 | if ( ! empty( $taxonomy ) ) { |
| 1316 | return array( |
| 1317 | 'term_id' => (string) $_term->term_id, |
| 1318 | 'term_taxonomy_id' => (string) $_term->term_taxonomy_id, |
| 1319 | ); |
| 1320 | } |
1305 | 1321 | |
1306 | | return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) ); |
| 1322 | return (string) $_term->term_id; |
1307 | 1323 | } |
1308 | 1324 | |
1309 | 1325 | /** |
… |
… |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
2227 | 2243 | foreach ( (array) $terms as $term) { |
2228 | 2244 | if ( !strlen(trim($term)) ) |
2229 | 2245 | continue; |
2230 | | |
2231 | 2246 | if ( !$term_info = term_exists($term, $taxonomy) ) { |
| 2247 | |
2232 | 2248 | // Skip if a non-existent term ID is passed. |
2233 | 2249 | if ( is_int($term) ) |
2234 | 2250 | continue; |
diff --git tests/phpunit/tests/term/getTerm.php tests/phpunit/tests/term/getTerm.php
index 7aeb3f3a5b..62931616f9 100644
|
|
class Tests_Term_GetTerm extends WP_UnitTestCase { |
30 | 30 | array( '%d' ) |
31 | 31 | ); |
32 | 32 | |
| 33 | clean_term_cache( $t1['term_id'], 'wptests_tax' ); |
| 34 | |
33 | 35 | return array( |
34 | 36 | array( |
35 | 37 | 'term_id' => $t1['term_id'], |
diff --git tests/phpunit/tests/term/wpGetObjectTerms.php tests/phpunit/tests/term/wpGetObjectTerms.php
index f68e4f674d..03d3daec2d 100644
|
|
class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { |
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 | |
| 341 | clean_term_cache( array( $term_1->term_id, $term_2->term_id, $term_3->term_id ), $this->taxonomy ); |
| 342 | |
341 | 343 | $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy ); |
342 | 344 | |
343 | 345 | $found = wp_get_object_terms( $p, $this->taxonomy, array( |