Ticket #15242: 15242.4.diff
| File 15242.4.diff, 25.4 KB (added by , 16 years ago) |
|---|
-
wp-includes/taxonomy.php
1056 1059 $where .= ' AND tt.count > 0'; 1057 1060 1058 1061 // don't limit the query results when we have to descend the family tree 1059 if ( ! empty( $number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {1062 if ( ! empty( $number ) && ! $hierarchical && empty( $child_of ) && '' === $parent ) { 1060 1063 if ( $offset ) 1061 1064 $limit = 'LIMIT ' . $offset . ',' . $number; 1062 1065 else … … 1136 1139 1137 1140 $_terms = array(); 1138 1141 if ( 'id=>parent' == $fields ) { 1139 while ( $term = array_shift( $terms) )1142 while ( $term = array_shift( $terms ) ) 1140 1143 $_terms[$term->term_id] = $term->parent; 1141 1144 $terms = $_terms; 1142 1145 } elseif ( 'ids' == $fields ) { 1143 while ( $term = array_shift( $terms) )1146 while ( $term = array_shift( $terms ) ) 1144 1147 $_terms[] = $term->term_id; 1145 1148 $terms = $_terms; 1146 1149 } elseif ( 'names' == $fields ) { 1147 while ( $term = array_shift( $terms) )1150 while ( $term = array_shift( $terms ) ) 1148 1151 $_terms[] = $term->name; 1149 1152 $terms = $_terms; 1150 1153 } 1151 1154 1152 if ( 0 < $number && intval( @count($terms)) > $number ) {1153 $terms = array_slice( $terms, $offset, $number);1155 if ( 0 < $number && intval( @count( $terms ) ) > $number ) { 1156 $terms = array_slice( $terms, $offset, $number ); 1154 1157 } 1155 1158 1156 1159 wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day 1157 1160 1158 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args);1161 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1159 1162 return $terms; 1160 1163 } 1161 1164 … … 1177 1180 * @param int $parent ID of parent term under which to confine the exists search. 1178 1181 * @return mixed Get the term id or Term Object, if exists. 1179 1182 */ 1180 function term_exists( $term, $taxonomy = '', $parent = 0) {1183 function term_exists( $term, $taxonomy = '', $parent = 0 ) { 1181 1184 global $wpdb; 1182 1185 1183 1186 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1184 1187 $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 "; 1185 1188 1186 if ( is_int( $term) ) {1189 if ( is_int( $term ) ) { 1187 1190 if ( 0 == $term ) 1188 1191 return 0; 1189 1192 $where = 't.term_id = %d'; 1190 if ( !empty( $taxonomy) )1193 if ( !empty( $taxonomy ) ) 1191 1194 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); 1192 1195 else 1193 1196 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); … … 1195 1198 1196 1199 $term = trim( stripslashes( $term ) ); 1197 1200 1198 if ( '' === $slug = sanitize_title( $term) )1201 if ( '' === $slug = sanitize_title( $term ) ) 1199 1202 return 0; 1200 1203 1201 1204 $where = 't.slug = %s'; 1202 1205 $else_where = 't.name = %s'; 1203 $where_fields = array( $slug);1204 $else_where_fields = array( $term);1205 if ( !empty( $taxonomy) ) {1206 $where_fields = array( $slug ); 1207 $else_where_fields = array( $term ); 1208 if ( !empty( $taxonomy ) ) { 1206 1209 $parent = (int) $parent; 1207 1210 if ( $parent > 0 ) { 1208 1211 $where_fields[] = $parent; … … 1214 1217 $where_fields[] = $taxonomy; 1215 1218 $else_where_fields[] = $taxonomy; 1216 1219 1217 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) )1220 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 ) ) 1218 1221 return $result; 1219 1222 1220 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);1223 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 ); 1221 1224 } 1222 1225 1223 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )1226 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields ) ) ) 1224 1227 return $result; 1225 1228 1226 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );1229 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields ) ); 1227 1230 } 1228 1231 1229 1232 /** … … 1246 1249 * @param string $context Default is 'display'. 1247 1250 * @return array|object Term with all fields sanitized 1248 1251 */ 1249 function sanitize_term( $term, $taxonomy, $context = 'display') {1252 function sanitize_term( $term, $taxonomy, $context = 'display' ) { 1250 1253 1251 1254 if ( 'raw' == $context ) 1252 1255 return $term; 1253 1256 1254 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');1257 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group' ); 1255 1258 1256 1259 $do_object = false; 1257 if ( is_object( $term) )1260 if ( is_object( $term ) ) 1258 1261 $do_object = true; 1259 1262 1260 $term_id = $do_object ? $term->term_id : ( isset($term['term_id']) ? $term['term_id'] : 0);1263 $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 ); 1261 1264 1262 1265 foreach ( (array) $fields as $field ) { 1263 1266 if ( $do_object ) { 1264 if ( isset( $term->$field) )1265 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context);1267 if ( isset( $term->$field ) ) 1268 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); 1266 1269 } else { 1267 if ( isset( $term[$field]) )1268 $term[$field] = sanitize_term_field( $field, $term[$field], $term_id, $taxonomy, $context);1270 if ( isset( $term[$field] ) ) 1271 $term[$field] = sanitize_term_field( $field, $term[$field], $term_id, $taxonomy, $context ); 1269 1272 } 1270 1273 } 1271 1274 … … 1303 1306 * @param string $context Either edit, db, display, attribute, or js. 1304 1307 * @return mixed sanitized field 1305 1308 */ 1306 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context) {1309 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { 1307 1310 if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) { 1308 1311 $value = (int) $value; 1309 1312 if ( $value < 0 ) … … 1314 1317 return $value; 1315 1318 1316 1319 if ( 'edit' == $context ) { 1317 $value = apply_filters( "edit_term_$field", $value, $term_id, $taxonomy);1318 $value = apply_filters( "edit_${taxonomy}_$field", $value, $term_id);1320 $value = apply_filters( "edit_term_$field", $value, $term_id, $taxonomy ); 1321 $value = apply_filters( "edit_${taxonomy}_$field", $value, $term_id ); 1319 1322 if ( 'description' == $field ) 1320 $value = format_to_edit( $value);1323 $value = format_to_edit( $value ); 1321 1324 else 1322 $value = esc_attr( $value);1325 $value = esc_attr( $value ); 1323 1326 } else if ( 'db' == $context ) { 1324 $value = apply_filters( "pre_term_$field", $value, $taxonomy);1325 $value = apply_filters( "pre_${taxonomy}_$field", $value);1327 $value = apply_filters( "pre_term_$field", $value, $taxonomy ); 1328 $value = apply_filters( "pre_${taxonomy}_$field", $value ); 1326 1329 // Back compat filters 1327 1330 if ( 'slug' == $field ) 1328 $value = apply_filters( 'pre_category_nicename', $value);1331 $value = apply_filters( 'pre_category_nicename', $value ); 1329 1332 1330 1333 } else if ( 'rss' == $context ) { 1331 $value = apply_filters( "term_${field}_rss", $value, $taxonomy);1332 $value = apply_filters( "${taxonomy}_${field}_rss", $value);1334 $value = apply_filters( "term_${field}_rss", $value, $taxonomy ); 1335 $value = apply_filters( "${taxonomy}_${field}_rss", $value ); 1333 1336 } else { 1334 1337 // Use display filters by default. 1335 $value = apply_filters( "term_$field", $value, $term_id, $taxonomy, $context);1336 $value = apply_filters( "${taxonomy}_$field", $value, $term_id, $context);1338 $value = apply_filters( "term_$field", $value, $term_id, $taxonomy, $context ); 1339 $value = apply_filters( "${taxonomy}_$field", $value, $term_id, $context ); 1337 1340 } 1338 1341 1339 1342 if ( 'attribute' == $context ) 1340 $value = esc_attr( $value);1343 $value = esc_attr( $value ); 1341 1344 else if ( 'js' == $context ) 1342 $value = esc_js( $value);1345 $value = esc_js( $value ); 1343 1346 1344 1347 return $value; 1345 1348 } … … 1361 1364 * @return int How many terms are in $taxonomy 1362 1365 */ 1363 1366 function wp_count_terms( $taxonomy, $args = array() ) { 1364 $defaults = array( 'hide_empty' => false);1365 $args = wp_parse_args( $args, $defaults);1367 $defaults = array( 'hide_empty' => false ); 1368 $args = wp_parse_args( $args, $defaults ); 1366 1369 1367 1370 // backwards compatibility 1368 if ( isset( $args['ignore_empty']) ) {1371 if ( isset( $args['ignore_empty'] ) ) { 1369 1372 $args['hide_empty'] = $args['ignore_empty']; 1370 unset( $args['ignore_empty']);1373 unset( $args['ignore_empty'] ); 1371 1374 } 1372 1375 1373 1376 $args['fields'] = 'count'; 1374 1377 1375 return get_terms( $taxonomy, $args);1378 return get_terms( $taxonomy, $args ); 1376 1379 } 1377 1380 1378 1381 /** … … 1395 1398 1396 1399 $object_id = (int) $object_id; 1397 1400 1398 if ( !is_array( $taxonomies) )1399 $taxonomies = array( $taxonomies);1401 if ( !is_array( $taxonomies ) ) 1402 $taxonomies = array( $taxonomies ); 1400 1403 1401 1404 foreach ( (array) $taxonomies as $taxonomy ) { 1402 $tt_ids = wp_get_object_terms( $object_id, $taxonomy, array('fields' => 'tt_ids'));1403 $in_tt_ids = "'" . implode( "', '", $tt_ids) . "'";1405 $tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) ); 1406 $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; 1404 1407 do_action( 'delete_term_relationships', $object_id, $tt_ids ); 1405 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );1408 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); 1406 1409 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 1407 wp_update_term_count( $tt_ids, $taxonomy);1410 wp_update_term_count( $tt_ids, $taxonomy ); 1408 1411 } 1409 1412 } 1410 1413 … … 1438 1441 1439 1442 $term = (int) $term; 1440 1443 1441 if ( ! $ids = term_exists( $term, $taxonomy) )1444 if ( ! $ids = term_exists( $term, $taxonomy ) ) 1442 1445 return false; 1443 1446 if ( is_wp_error( $ids ) ) 1444 1447 return $ids; … … 1453 1456 return 0; // Don't delete the default category 1454 1457 } 1455 1458 1456 $args = wp_parse_args( $args, $defaults);1457 extract( $args, EXTR_SKIP);1459 $args = wp_parse_args( $args, $defaults ); 1460 extract( $args, EXTR_SKIP ); 1458 1461 1459 1462 if ( isset( $default ) ) { 1460 1463 $default = (int) $default; 1461 if ( ! term_exists( $default, $taxonomy) )1462 unset( $default);1464 if ( ! term_exists( $default, $taxonomy ) ) 1465 unset( $default ); 1463 1466 } 1464 1467 1465 1468 // Update children to point to new parent 1466 if ( is_taxonomy_hierarchical( $taxonomy) ) {1467 $term_obj = get_term( $term, $taxonomy);1469 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 1470 $term_obj = get_term( $term, $taxonomy ); 1468 1471 if ( is_wp_error( $term_obj ) ) 1469 1472 return $term_obj; 1470 1473 $parent = $term_obj->parent; 1471 1474 1472 1475 $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); 1473 1476 do_action( 'edit_term_taxonomies', $edit_tt_ids ); 1474 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );1477 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); 1475 1478 do_action( 'edited_term_taxonomies', $edit_tt_ids ); 1476 1479 } 1477 1480 1478 1481 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); 1479 1482 1480 1483 foreach ( (array) $objects as $object ) { 1481 $terms = wp_get_object_terms( $object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));1482 if ( 1 == count( $terms) && isset($default) ) {1483 $terms = array( $default);1484 $terms = wp_get_object_terms( $object, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 1485 if ( 1 == count( $terms ) && isset( $default ) ) { 1486 $terms = array( $default ); 1484 1487 } else { 1485 $terms = array_diff( $terms, array($term));1486 if ( isset($default) && isset($force_default) && $force_default)1487 $terms = array_merge( $terms, array($default));1488 $terms = array_diff( $terms, array( $term ) ); 1489 if ( isset( $default ) && isset( $force_default ) && $force_default ) 1490 $terms = array_merge( $terms, array( $default ) ); 1488 1491 } 1489 $terms = array_map( 'intval', $terms);1490 wp_set_object_terms( $object, $terms, $taxonomy);1492 $terms = array_map( 'intval', $terms ); 1493 wp_set_object_terms( $object, $terms, $taxonomy ); 1491 1494 } 1492 1495 1493 1496 do_action( 'delete_term_taxonomy', $tt_id ); … … 1495 1498 do_action( 'deleted_term_taxonomy', $tt_id ); 1496 1499 1497 1500 // Delete the term if no taxonomies use it. 1498 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) )1499 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term ) );1501 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) 1502 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term ) ); 1500 1503 1501 clean_term_cache( $term, $taxonomy);1504 clean_term_cache( $term, $taxonomy ); 1502 1505 1503 do_action( 'delete_term', $term, $tt_id, $taxonomy);1504 do_action( "delete_$taxonomy", $term, $tt_id);1506 do_action( 'delete_term', $term, $tt_id, $taxonomy ); 1507 do_action( "delete_$taxonomy", $term, $tt_id ); 1505 1508 1506 1509 return true; 1507 1510 } … … 1552 1555 * @param array|string $args Change what is returned 1553 1556 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist. 1554 1557 */ 1555 function wp_get_object_terms( $object_ids, $taxonomies, $args = array()) {1558 function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 1556 1559 global $wpdb; 1557 1560 1558 if ( !is_array( $taxonomies) )1559 $taxonomies = array( $taxonomies);1561 if ( !is_array( $taxonomies ) ) 1562 $taxonomies = array( $taxonomies ); 1560 1563 1561 1564 foreach ( (array) $taxonomies as $taxonomy ) { 1562 if ( ! taxonomy_exists( $taxonomy) )1563 return new WP_Error( 'invalid_taxonomy', __('Invalid Taxonomy'));1565 if ( ! taxonomy_exists( $taxonomy ) ) 1566 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 1564 1567 } 1565 1568 1566 if ( !is_array( $object_ids) )1567 $object_ids = array( $object_ids);1568 $object_ids = array_map( 'intval', $object_ids);1569 if ( !is_array( $object_ids ) ) 1570 $object_ids = array( $object_ids ); 1571 $object_ids = array_map( 'intval', $object_ids ); 1569 1572 1570 $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');1573 $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all' ); 1571 1574 $args = wp_parse_args( $args, $defaults ); 1572 1575 1573 1576 $terms = array(); 1574 if ( count( $taxonomies) > 1 ) {1577 if ( count( $taxonomies ) > 1 ) { 1575 1578 foreach ( $taxonomies as $index => $taxonomy ) { 1576 $t = get_taxonomy( $taxonomy);1577 if ( isset( $t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {1578 unset( $taxonomies[$index]);1579 $terms = array_merge( $terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));1579 $t = get_taxonomy( $taxonomy ); 1580 if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) { 1581 unset( $taxonomies[$index] ); 1582 $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) ); 1580 1583 } 1581 1584 } 1582 1585 } else { 1583 $t = get_taxonomy( $taxonomies[0]);1584 if ( isset( $t->args) && is_array($t->args) )1585 $args = array_merge( $args, $t->args);1586 $t = get_taxonomy( $taxonomies[0] ); 1587 if ( isset( $t->args ) && is_array( $t->args ) ) 1588 $args = array_merge( $args, $t->args ); 1586 1589 } 1587 1590 1588 extract( $args, EXTR_SKIP);1591 extract( $args, EXTR_SKIP ); 1589 1592 1590 1593 if ( 'count' == $orderby ) 1591 1594 $orderby = 'tt.count'; … … 1605 1608 } 1606 1609 1607 1610 // tt_ids queries can only be none or tr.term_taxonomy_id 1608 if ( ( 'tt_ids' == $fields) && !empty($orderby) )1611 if ( ( 'tt_ids' == $fields ) && !empty( $orderby ) ) 1609 1612 $orderby = 'tr.term_taxonomy_id'; 1610 1613 1611 if ( !empty( $orderby) )1614 if ( !empty( $orderby ) ) 1612 1615 $orderby = "ORDER BY $orderby"; 1613 1616 1614 $taxonomies = "'" . implode( "', '", $taxonomies) . "'";1615 $object_ids = implode( ', ', $object_ids);1617 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; 1618 $object_ids = implode( ', ', $object_ids ); 1616 1619 1617 1620 $select_this = ''; 1618 1621 if ( 'all' == $fields ) … … 1627 1630 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order"; 1628 1631 1629 1632 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 1630 $terms = array_merge( $terms, $wpdb->get_results($query));1631 update_term_cache( $terms);1633 $terms = array_merge( $terms, $wpdb->get_results( $query ) ); 1634 update_term_cache( $terms ); 1632 1635 } else if ( 'ids' == $fields || 'names' == $fields ) { 1633 $terms = array_merge( $terms, $wpdb->get_col($query));1636 $terms = array_merge( $terms, $wpdb->get_col( $query ) ); 1634 1637 } else if ( 'tt_ids' == $fields ) { 1635 $terms = $wpdb->get_col( "SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");1638 $terms = $wpdb->get_col( "SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order" ); 1636 1639 } 1637 1640 1638 1641 if ( ! $terms ) 1639 1642 $terms = array(); 1640 1643 1641 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);1644 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); 1642 1645 } 1643 1646 1644 1647 /** … … 1697 1700 function wp_insert_term( $term, $taxonomy, $args = array() ) { 1698 1701 global $wpdb; 1699 1702 1700 if ( ! taxonomy_exists( $taxonomy) )1701 return new WP_Error( 'invalid_taxonomy', __('Invalid taxonomy'));1703 if ( ! taxonomy_exists( $taxonomy ) ) 1704 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 1702 1705 1703 1706 $term = apply_filters( 'pre_insert_term', $term, $taxonomy ); 1704 1707 if ( is_wp_error( $term ) ) 1705 1708 return $term; 1706 1709 1707 if ( is_int( $term) && 0 == $term )1708 return new WP_Error( 'invalid_term_id', __('Invalid term ID'));1710 if ( is_int( $term ) && 0 == $term ) 1711 return new WP_Error( 'invalid_term_id', __( 'Invalid term ID' ) ); 1709 1712 1710 if ( '' == trim( $term) )1711 return new WP_Error( 'empty_term_name', __('A name is required for this term'));1713 if ( '' == trim( $term ) ) 1714 return new WP_Error( 'empty_term_name', __( 'A name is required for this term' ) ); 1712 1715 1713 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' );1714 $args = wp_parse_args( $args, $defaults);1716 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' ); 1717 $args = wp_parse_args( $args, $defaults ); 1715 1718 $args['name'] = $term; 1716 1719 $args['taxonomy'] = $taxonomy; 1717 $args = sanitize_term( $args, $taxonomy, 'db');1718 extract( $args, EXTR_SKIP);1720 $args = sanitize_term( $args, $taxonomy, 'db' ); 1721 extract( $args, EXTR_SKIP ); 1719 1722 1720 1723 // expected_slashed ($name) 1721 $name = stripslashes( $name);1722 $description = stripslashes( $description);1724 $name = stripslashes( $name ); 1725 $description = stripslashes( $description ); 1723 1726 1724 if ( empty( $slug) )1725 $slug = sanitize_title( $name);1727 if ( empty( $slug ) ) 1728 $slug = sanitize_title( $name ); 1726 1729 1727 1730 $term_group = 0; 1728 1731 if ( $alias_of ) { 1729 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of ) );1732 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of ) ); 1730 1733 if ( $alias->term_group ) { 1731 1734 // The alias we want is already in a group, so let's use that one. 1732 1735 $term_group = $alias->term_group; 1733 1736 } else { 1734 1737 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. 1735 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms") + 1;1738 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 1736 1739 do_action( 'edit_terms', $alias->term_id ); 1737 $wpdb->update( $wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );1740 $wpdb->update( $wpdb->terms, compact( 'term_group' ), array( 'term_id' => $alias->term_id ) ); 1738 1741 do_action( 'edited_terms', $alias->term_id ); 1739 1742 } 1740 1743 } 1741 1744 1742 if ( $term_id = term_exists( $slug) ) {1743 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id ), ARRAY_A );1745 if ( $term_id = term_exists( $slug ) ) { 1746 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id ), ARRAY_A ); 1744 1747 // We've got an existing term in the same taxonomy, which matches the name of the new term: 1745 if ( is_taxonomy_hierarchical( $taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {1748 if ( is_taxonomy_hierarchical( $taxonomy ) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) { 1746 1749 // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level. 1747 $siblings = get_terms( $taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );1748 if ( in_array( $name, $siblings) ) {1749 return new WP_Error( 'term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);1750 $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => (int)$parent ) ); 1751 if ( in_array( $name, $siblings ) ) { 1752 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] ); 1750 1753 } else { 1751 $slug = wp_unique_term_slug( $slug, (object) $args);1754 $slug = wp_unique_term_slug( $slug, (object) $args ); 1752 1755 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1753 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1756 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1754 1757 $term_id = (int) $wpdb->insert_id; 1755 1758 } 1756 1759 } elseif ( $existing_term['name'] != $name ) { 1757 1760 // We've got an existing term, with a different name, Create the new term. 1758 $slug = wp_unique_term_slug( $slug, (object) $args);1761 $slug = wp_unique_term_slug( $slug, (object) $args ); 1759 1762 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1760 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1763 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1761 1764 $term_id = (int) $wpdb->insert_id; 1762 1765 } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) { 1763 1766 // Same name, same slug. 1764 return new WP_Error( 'term_exists', __('A term with the name provided already exists.'), $exists['term_id']);1767 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists.' ), $exists['term_id'] ); 1765 1768 } 1766 1769 } else { 1767 1770 // This term does not exist at all in the database, Create it. 1768 $slug = wp_unique_term_slug( $slug, (object) $args);1771 $slug = wp_unique_term_slug( $slug, (object) $args ); 1769 1772 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1770 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1773 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1771 1774 $term_id = (int) $wpdb->insert_id; 1772 1775 } 1773 1776 1774 1777 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. 1775 if ( empty( $slug) ) {1776 $slug = sanitize_title( $slug, $term_id);1778 if ( empty( $slug ) ) { 1779 $slug = sanitize_title( $slug, $term_id ); 1777 1780 do_action( 'edit_terms', $term_id ); 1778 1781 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 1779 1782 do_action( 'edited_terms', $term_id ); … … 1781 1784 1782 1785 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) ); 1783 1786 1784 if ( !empty( $tt_id) )1785 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);1787 if ( !empty( $tt_id ) ) 1788 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 1786 1789 1787 1790 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); 1788 1791 $tt_id = (int) $wpdb->insert_id; 1789 1792 1790 do_action( "create_term", $term_id, $tt_id, $taxonomy);1791 do_action( "create_$taxonomy", $term_id, $tt_id);1793 do_action( "create_term", $term_id, $tt_id, $taxonomy ); 1794 do_action( "create_$taxonomy", $term_id, $tt_id ); 1792 1795 1793 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id);1796 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 1794 1797 1795 clean_term_cache( $term_id, $taxonomy);1798 clean_term_cache( $term_id, $taxonomy ); 1796 1799 1797 do_action( "created_term", $term_id, $tt_id, $taxonomy);1798 do_action( "created_$taxonomy", $term_id, $tt_id);1800 do_action( "created_term", $term_id, $tt_id, $taxonomy ); 1801 do_action( "created_$taxonomy", $term_id, $tt_id ); 1799 1802 1800 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);1803 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 1801 1804 } 1802 1805 1803 1806 /**
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)