Changeset 28465
- Timestamp:
- 05/17/2014 02:46:07 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r28464 r28465 1280 1280 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. 1281 1281 */ 1282 function get_terms( $taxonomies, $args = '') {1282 function get_terms( $taxonomies, $args = '' ) { 1283 1283 global $wpdb; 1284 1284 $empty_array = array(); 1285 1285 1286 1286 $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies ); 1287 if ( ! is_array( $taxonomies ) ) 1287 if ( ! is_array( $taxonomies ) ) { 1288 1288 $taxonomies = array( $taxonomies ); 1289 } 1289 1290 1290 1291 foreach ( $taxonomies as $taxonomy ) { … … 1327 1328 $args = apply_filters( 'get_terms_args', $args, $taxonomies ); 1328 1329 1329 extract($args, EXTR_SKIP); 1330 1330 $child_of = $args['child_of']; 1331 1331 if ( $child_of ) { 1332 1332 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); 1333 if ( ! isset( $hierarchy[ $child_of ] ) ) 1333 if ( ! isset( $hierarchy[ $child_of ] ) ) { 1334 1334 return $empty_array; 1335 } 1336 1335 } 1336 } 1337 1338 $parent = $args['parent']; 1337 1339 if ( $parent ) { 1338 1340 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); 1339 if ( ! isset( $hierarchy[ $parent ] ) ) 1341 if ( ! isset( $hierarchy[ $parent ] ) ) { 1340 1342 return $empty_array; 1343 } 1341 1344 } 1342 1345 1343 1346 // $args can be whatever, only use the args defined in defaults to compute the key 1344 1347 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; 1345 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );1348 $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key ); 1346 1349 $last_changed = wp_cache_get( 'last_changed', 'terms' ); 1347 1350 if ( ! $last_changed ) { … … 1366 1369 } 1367 1370 1368 $_orderby = strtolower( $orderby);1369 if ( 'count' == $_orderby ) 1371 $_orderby = strtolower( $args['orderby'] ); 1372 if ( 'count' == $_orderby ) { 1370 1373 $orderby = 'tt.count'; 1371 else if ( 'name' == $_orderby )1374 } else if ( 'name' == $_orderby ) { 1372 1375 $orderby = 't.name'; 1373 else if ( 'slug' == $_orderby )1376 } else if ( 'slug' == $_orderby ) { 1374 1377 $orderby = 't.slug'; 1375 else if ( 'term_group' == $_orderby )1378 } else if ( 'term_group' == $_orderby ) { 1376 1379 $orderby = 't.term_group'; 1377 else if ( 'none' == $_orderby )1380 } else if ( 'none' == $_orderby ) { 1378 1381 $orderby = ''; 1379 elseif ( empty($_orderby) || 'id' == $_orderby )1382 } elseif ( empty($_orderby) || 'id' == $_orderby ) { 1380 1383 $orderby = 't.term_id'; 1381 else1384 } else { 1382 1385 $orderby = 't.name'; 1383 1386 } 1384 1387 /** 1385 1388 * Filter the ORDERBY clause of the terms query. … … 1393 1396 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies ); 1394 1397 1395 if ( !empty($orderby) ) 1398 $order = strtoupper( $args['order'] ); 1399 if ( ! empty( $orderby ) ) { 1396 1400 $orderby = "ORDER BY $orderby"; 1397 else1401 } else { 1398 1402 $order = ''; 1399 1400 $order = strtoupper( $order ); 1401 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )1403 } 1404 1405 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) { 1402 1406 $order = 'ASC'; 1407 } 1403 1408 1404 1409 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; 1410 1411 $exclude = $args['exclude']; 1412 $exclude_tree = $args['exclude_tree']; 1413 $include = $args['include']; 1414 1405 1415 $inclusions = ''; 1406 1416 if ( ! empty( $include ) ) { … … 1415 1425 } 1416 1426 1417 $exclusions = '';1418 1427 if ( ! empty( $exclude_tree ) ) { 1419 1428 $exclude_tree = wp_parse_id_list( $exclude_tree ); … … 1426 1435 } 1427 1436 $exclusions = implode( ',', array_map( 'intval', $excluded_children ) ); 1437 } else { 1438 $exclusions = ''; 1428 1439 } 1429 1440 1430 1441 if ( ! empty( $exclude ) ) { 1431 1442 $exterms = wp_parse_id_list( $exclude ); 1432 if ( empty( $exclusions ) ) 1443 if ( empty( $exclusions ) ) { 1433 1444 $exclusions = implode( ',', $exterms ); 1434 else1445 } else { 1435 1446 $exclusions .= ', ' . implode( ',', $exterms ); 1436 } 1437 1438 if ( ! empty( $exclusions ) ) 1447 } 1448 } 1449 1450 if ( ! empty( $exclusions ) ) { 1439 1451 $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')'; 1452 } 1440 1453 1441 1454 /** … … 1450 1463 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); 1451 1464 1452 if ( ! empty( $exclusions ) ) 1465 if ( ! empty( $exclusions ) ) { 1453 1466 $where .= $exclusions; 1454 1455 if ( !empty($slug) ) { 1456 $slug = sanitize_title($slug); 1467 } 1468 1469 if ( ! empty( $args['slug'] ) ) { 1470 $slug = sanitize_title( $args['slug'] ); 1457 1471 $where .= " AND t.slug = '$slug'"; 1458 1472 } 1459 1473 1460 if ( ! empty($name__like) ) {1461 $name__like = like_escape( $ name__like);1474 if ( ! empty( $args['name__like'] ) ) { 1475 $name__like = like_escape( $args['name__like'] ); 1462 1476 $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' ); 1463 1477 } 1464 1478 1465 if ( ! empty( $ description__like) ) {1466 $description__like = like_escape( $ description__like);1479 if ( ! empty( $args['description__like'] ) ) { 1480 $description__like = like_escape( $args['description__like'] ); 1467 1481 $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' ); 1468 1482 } … … 1473 1487 } 1474 1488 1475 if ( 'count' == $fields ) 1489 $hierarchical = $args['hierarchical']; 1490 if ( 'count' == $args['fields'] ) { 1476 1491 $hierarchical = false; 1477 1478 if ( $ hide_empty && !$hierarchical )1492 } 1493 if ( $args['hide_empty'] && !$hierarchical ) { 1479 1494 $where .= ' AND tt.count > 0'; 1495 } 1496 1497 $number = $args['number']; 1498 $offset = $args['offset']; 1480 1499 1481 1500 // don't limit the query results when we have to descend the family tree 1482 1501 if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { 1483 if ( $offset ) 1502 if ( $offset ) { 1484 1503 $limits = 'LIMIT ' . $offset . ',' . $number; 1485 else1504 } else { 1486 1505 $limits = 'LIMIT ' . $number; 1506 } 1487 1507 } else { 1488 1508 $limits = ''; 1489 1509 } 1490 1510 1491 if ( ! empty( $ search) ) {1492 $search = like_escape( $ search);1511 if ( ! empty( $args['search'] ) ) { 1512 $search = like_escape( $args['search'] ); 1493 1513 $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' ); 1494 1514 } 1495 1515 1496 1516 $selects = array(); 1497 switch ( $ fields) {1517 switch ( $args['fields'] ) { 1498 1518 case 'all': 1499 1519 $selects = array( 't.*', 'tt.*' ); … … 1519 1539 } 1520 1540 1521 $_fields = $ fields;1541 $_fields = $args['fields']; 1522 1542 1523 1543 /** … … 1546 1566 */ 1547 1567 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); 1548 foreach ( $pieces as $piece ) 1568 foreach ( $pieces as $piece ) { 1549 1569 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 1550 1570 } 1551 1571 $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; 1552 1572 1553 $fields = $_fields; 1554 1555 if ( 'count' == $fields ) { 1573 if ( 'count' == $_fields ) { 1556 1574 $term_count = $wpdb->get_var($query); 1557 1575 return $term_count; … … 1559 1577 1560 1578 $terms = $wpdb->get_results($query); 1561 if ( 'all' == $ fields ) {1579 if ( 'all' == $_fields ) { 1562 1580 update_term_cache($terms); 1563 1581 } … … 1573 1591 if ( $child_of ) { 1574 1592 $children = _get_term_hierarchy( reset( $taxonomies ) ); 1575 if ( ! empty( $children ) ) 1593 if ( ! empty( $children ) ) { 1576 1594 $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) ); 1595 } 1577 1596 } 1578 1597 1579 1598 // Update term counts to include children. 1580 if ( $ pad_counts && 'all' == $fields )1599 if ( $args['pad_counts'] && 'all' == $_fields ) { 1581 1600 _pad_term_counts( $terms, reset( $taxonomies ) ); 1582 1601 } 1583 1602 // Make sure we show empty categories that have children. 1584 if ( $hierarchical && $ hide_empty&& is_array( $terms ) ) {1603 if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { 1585 1604 foreach ( $terms as $k => $term ) { 1586 1605 if ( ! $term->count ) { … … 1603 1622 1604 1623 $_terms = array(); 1605 if ( 'id=>parent' == $ fields ) {1606 while ( $term = array_shift( $terms ) ) 1624 if ( 'id=>parent' == $_fields ) { 1625 while ( $term = array_shift( $terms ) ) { 1607 1626 $_terms[$term->term_id] = $term->parent; 1608 } elseif ( 'ids' == $fields ) { 1609 while ( $term = array_shift( $terms ) ) 1627 } 1628 } elseif ( 'ids' == $_fields ) { 1629 while ( $term = array_shift( $terms ) ) { 1610 1630 $_terms[] = $term->term_id; 1611 } elseif ( 'names' == $fields ) { 1612 while ( $term = array_shift( $terms ) ) 1631 } 1632 } elseif ( 'names' == $_fields ) { 1633 while ( $term = array_shift( $terms ) ) { 1613 1634 $_terms[] = $term->name; 1614 } elseif ( 'id=>name' == $fields ) { 1615 while ( $term = array_shift( $terms ) ) 1635 } 1636 } elseif ( 'id=>name' == $_fields ) { 1637 while ( $term = array_shift( $terms ) ) { 1616 1638 $_terms[$term->term_id] = $term->name; 1617 } elseif ( 'id=>slug' == $fields ) { 1618 while ( $term = array_shift( $terms ) ) 1639 } 1640 } elseif ( 'id=>slug' == $_fields ) { 1641 while ( $term = array_shift( $terms ) ) { 1619 1642 $_terms[$term->term_id] = $term->slug; 1620 } 1621 1622 if ( ! empty( $_terms ) ) 1643 } 1644 } 1645 1646 if ( ! empty( $_terms ) ) { 1623 1647 $terms = $_terms; 1624 1625 if ( $number && is_array( $terms ) && count( $terms ) > $number ) 1648 } 1649 1650 if ( $number && is_array( $terms ) && count( $terms ) > $number ) { 1626 1651 $terms = array_slice( $terms, $offset, $number ); 1652 } 1627 1653 1628 1654 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
Note: See TracChangeset
for help on using the changeset viewer.