Changeset 37572
- Timestamp:
- 05/26/2016 04:32:30 AM (9 years ago)
- Location:
- trunk/src
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r37567 r37572 1175 1175 global $wpdb; 1176 1176 1177 $defaults = array( 1178 'taxonomy' => null, 1179 'orderby' => 'name', 1180 'order' => 'ASC', 1181 'hide_empty' => true, 1182 'include' => array(), 1183 'exclude' => array(), 1184 'exclude_tree' => array(), 1185 'number' => '', 1186 'offset' => '', 1187 'fields' => 'all', 1188 'name' => '', 1189 'slug' => '', 1190 'hierarchical' => true, 1191 'search' => '', 1192 'name__like' => '', 1193 'description__like' => '', 1194 'pad_counts' => false, 1195 'get' => '', 1196 'child_of' => 0, 1197 'parent' => '', 1198 'childless' => false, 1199 'cache_domain' => 'core', 1200 'update_term_meta_cache' => true, 1201 'meta_query' => '' 1202 ); 1177 $term_query = new WP_Term_Query(); 1203 1178 1204 1179 /* … … 1209 1184 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies) 1210 1185 */ 1211 $key_intersect = array_intersect_key( $ defaults, (array) $args );1186 $key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $args ); 1212 1187 $do_legacy_args = $deprecated || empty( $key_intersect ); 1213 1188 1214 $taxonomies = null;1215 1189 if ( $do_legacy_args ) { 1216 1190 $taxonomies = (array) $args; 1217 1191 $args = $deprecated; 1192 $args['taxonomy'] = $taxonomies; 1218 1193 } elseif ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) { 1219 $taxonomies = (array) $args['taxonomy']; 1220 unset( $args['taxonomy'] ); 1194 $args['taxonomy'] = (array) $args['taxonomy']; 1221 1195 } 1222 1196 1223 1197 $empty_array = array(); 1224 1198 1225 if ( $taxonomies) {1226 foreach ( $ taxonomiesas $taxonomy ) {1227 if ( ! taxonomy_exists( $taxonomy) ) {1199 if ( ! empty( $args['taxonomy'] ) ) { 1200 foreach ( $args['taxonomy'] as $taxonomy ) { 1201 if ( ! taxonomy_exists( $taxonomy ) ) { 1228 1202 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 1229 1203 } … … 1231 1205 } 1232 1206 1207 $terms = $term_query->query( $args ); 1208 1209 $taxonomies = isset( $args['taxonomy'] ) ? (array) $args['taxonomy'] : null; 1210 1233 1211 /** 1234 * Filters the terms query default arguments. 1235 * 1236 * Use {@see 'get_terms_args'} to filter the passed arguments. 1237 * 1238 * @since 4.4.0 1239 * 1240 * @param array $defaults An array of default get_terms() arguments. 1241 * @param array $taxonomies An array of taxonomies. 1212 * Filters the found terms. 1213 * 1214 * @since 2.3.0 1215 * @since 4.6.0 Added `$term_query`. 1216 * 1217 * @param array $terms Array of found terms. 1218 * @param array $taxonomies An array of taxonomies. 1219 * @param array $args An array of get_terms() arguments. 1220 * @param WP_Term_Query $term_query The WP_Term_Query object. 1242 1221 */ 1243 $args = wp_parse_args( $args, apply_filters( 'get_terms_defaults', $defaults, $taxonomies ) );1244 1245 $args['number'] = absint( $args['number'] );1246 $args['offset'] = absint( $args['offset'] );1247 1248 // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.1249 $has_hierarchical_tax = false;1250 if ( $taxonomies ) {1251 foreach ( $taxonomies as $_tax ) {1252 if ( is_taxonomy_hierarchical( $_tax ) ) {1253 $has_hierarchical_tax = true;1254 }1255 }1256 }1257 1258 if ( ! $has_hierarchical_tax ) {1259 $args['hierarchical'] = false;1260 $args['pad_counts'] = false;1261 }1262 1263 // 'parent' overrides 'child_of'.1264 if ( 0 < intval( $args['parent'] ) ) {1265 $args['child_of'] = false;1266 }1267 1268 if ( 'all' == $args['get'] ) {1269 $args['childless'] = false;1270 $args['child_of'] = 0;1271 $args['hide_empty'] = 0;1272 $args['hierarchical'] = false;1273 $args['pad_counts'] = false;1274 }1275 1276 /**1277 * Filters the terms query arguments.1278 *1279 * @since 3.1.01280 *1281 * @param array $args An array of get_terms() arguments.1282 * @param array $taxonomies An array of taxonomies.1283 */1284 $args = apply_filters( 'get_terms_args', $args, $taxonomies );1285 1286 // Avoid the query if the queried parent/child_of term has no descendants.1287 $child_of = $args['child_of'];1288 $parent = $args['parent'];1289 1290 if ( $child_of ) {1291 $_parent = $child_of;1292 } elseif ( $parent ) {1293 $_parent = $parent;1294 } else {1295 $_parent = false;1296 }1297 1298 if ( $_parent ) {1299 $in_hierarchy = false;1300 foreach ( $taxonomies as $_tax ) {1301 $hierarchy = _get_term_hierarchy( $_tax );1302 1303 if ( isset( $hierarchy[ $_parent ] ) ) {1304 $in_hierarchy = true;1305 }1306 }1307 1308 if ( ! $in_hierarchy ) {1309 return $empty_array;1310 }1311 }1312 1313 $_orderby = strtolower( $args['orderby'] );1314 if ( 'count' == $_orderby ) {1315 $orderby = 'tt.count';1316 } elseif ( 'name' == $_orderby ) {1317 $orderby = 't.name';1318 } elseif ( 'slug' == $_orderby ) {1319 $orderby = 't.slug';1320 } elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {1321 $include = implode( ',', array_map( 'absint', $args['include'] ) );1322 $orderby = "FIELD( t.term_id, $include )";1323 } elseif ( 'term_group' == $_orderby ) {1324 $orderby = 't.term_group';1325 } elseif ( 'description' == $_orderby ) {1326 $orderby = 'tt.description';1327 } elseif ( 'none' == $_orderby ) {1328 $orderby = '';1329 } elseif ( empty( $_orderby ) || 'id' == $_orderby || 'term_id' === $_orderby ) {1330 $orderby = 't.term_id';1331 } else {1332 $orderby = 't.name';1333 }1334 1335 /**1336 * Filters the ORDERBY clause of the terms query.1337 *1338 * @since 2.8.01339 *1340 * @param string $orderby `ORDERBY` clause of the terms query.1341 * @param array $args An array of terms query arguments.1342 * @param array $taxonomies An array of taxonomies.1343 */1344 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );1345 1346 $order = strtoupper( $args['order'] );1347 if ( ! empty( $orderby ) ) {1348 $orderby = "ORDER BY $orderby";1349 } else {1350 $order = '';1351 }1352 1353 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {1354 $order = 'ASC';1355 }1356 1357 $where_conditions = array();1358 1359 if ( $taxonomies ) {1360 $where_conditions[] = "tt.taxonomy IN ('" . implode("', '", array_map( 'esc_sql', $taxonomies ) ) . "')";1361 }1362 1363 $exclude = $args['exclude'];1364 $exclude_tree = $args['exclude_tree'];1365 $include = $args['include'];1366 1367 $inclusions = '';1368 if ( ! empty( $include ) ) {1369 $exclude = '';1370 $exclude_tree = '';1371 $inclusions = implode( ',', wp_parse_id_list( $include ) );1372 }1373 1374 if ( ! empty( $inclusions ) ) {1375 $where_conditions[] = 't.term_id IN ( ' . $inclusions . ' )';1376 }1377 1378 $exclusions = array();1379 if ( ! empty( $exclude_tree ) ) {1380 $exclude_tree = wp_parse_id_list( $exclude_tree );1381 $excluded_children = $exclude_tree;1382 foreach ( $exclude_tree as $extrunk ) {1383 $excluded_children = array_merge(1384 $excluded_children,1385 (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )1386 );1387 }1388 $exclusions = array_merge( $excluded_children, $exclusions );1389 }1390 1391 if ( ! empty( $exclude ) ) {1392 $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );1393 }1394 1395 // 'childless' terms are those without an entry in the flattened term hierarchy.1396 $childless = (bool) $args['childless'];1397 if ( $childless ) {1398 foreach ( $taxonomies as $_tax ) {1399 $term_hierarchy = _get_term_hierarchy( $_tax );1400 $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );1401 }1402 }1403 1404 if ( ! empty( $exclusions ) ) {1405 $exclusions = 't.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';1406 } else {1407 $exclusions = '';1408 }1409 1410 /**1411 * Filters the terms to exclude from the terms query.1412 *1413 * @since 2.3.01414 *1415 * @param string $exclusions `NOT IN` clause of the terms query.1416 * @param array $args An array of terms query arguments.1417 * @param array $taxonomies An array of taxonomies.1418 */1419 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );1420 1421 if ( ! empty( $exclusions ) ) {1422 // Must do string manipulation here for backward compatibility with filter.1423 $where_conditions[] = preg_replace( '/^\s*AND\s*/', '', $exclusions );1424 }1425 1426 if ( ! empty( $args['name'] ) ) {1427 $names = (array) $args['name'];1428 foreach ( $names as &$_name ) {1429 // `sanitize_term_field()` returns slashed data.1430 $_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) );1431 }1432 1433 $where_conditions[] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";1434 }1435 1436 if ( ! empty( $args['slug'] ) ) {1437 if ( is_array( $args['slug'] ) ) {1438 $slug = array_map( 'sanitize_title', $args['slug'] );1439 $where_conditions[] = "t.slug IN ('" . implode( "', '", $slug ) . "')";1440 } else {1441 $slug = sanitize_title( $args['slug'] );1442 $where_conditions[] = "t.slug = '$slug'";1443 }1444 }1445 1446 if ( ! empty( $args['name__like'] ) ) {1447 $where_conditions[] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );1448 }1449 1450 if ( ! empty( $args['description__like'] ) ) {1451 $where_conditions[] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );1452 }1453 1454 if ( '' !== $parent ) {1455 $parent = (int) $parent;1456 $where_conditions[] = "tt.parent = '$parent'";1457 }1458 1459 $hierarchical = $args['hierarchical'];1460 if ( 'count' == $args['fields'] ) {1461 $hierarchical = false;1462 }1463 if ( $args['hide_empty'] && !$hierarchical ) {1464 $where_conditions[] = 'tt.count > 0';1465 }1466 1467 $number = $args['number'];1468 $offset = $args['offset'];1469 1470 // Don't limit the query results when we have to descend the family tree.1471 if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {1472 if ( $offset ) {1473 $limits = 'LIMIT ' . $offset . ',' . $number;1474 } else {1475 $limits = 'LIMIT ' . $number;1476 }1477 } else {1478 $limits = '';1479 }1480 1481 if ( ! empty( $args['search'] ) ) {1482 $like = '%' . $wpdb->esc_like( $args['search'] ) . '%';1483 $where_conditions[] = $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );1484 }1485 1486 // Meta query support.1487 $join = '';1488 $distinct = '';1489 1490 $mquery = new WP_Meta_Query();1491 $mquery->parse_query_vars( $args );1492 $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );1493 $meta_clauses = $mquery->get_clauses();1494 1495 if ( ! empty( $meta_clauses ) ) {1496 $join .= $mq_sql['join'];1497 $where_conditions[] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] );1498 $distinct .= "DISTINCT";1499 1500 // 'orderby' support.1501 $allowed_keys = array();1502 $primary_meta_key = null;1503 $primary_meta_query = reset( $meta_clauses );1504 if ( ! empty( $primary_meta_query['key'] ) ) {1505 $primary_meta_key = $primary_meta_query['key'];1506 $allowed_keys[] = $primary_meta_key;1507 }1508 $allowed_keys[] = 'meta_value';1509 $allowed_keys[] = 'meta_value_num';1510 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );1511 1512 if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], $allowed_keys ) ) {1513 switch( $args['orderby'] ) {1514 case $primary_meta_key:1515 case 'meta_value':1516 if ( ! empty( $primary_meta_query['type'] ) ) {1517 $orderby = "ORDER BY CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";1518 } else {1519 $orderby = "ORDER BY {$primary_meta_query['alias']}.meta_value";1520 }1521 break;1522 1523 case 'meta_value_num':1524 $orderby = "ORDER BY {$primary_meta_query['alias']}.meta_value+0";1525 break;1526 1527 default:1528 if ( array_key_exists( $args['orderby'], $meta_clauses ) ) {1529 // $orderby corresponds to a meta_query clause.1530 $meta_clause = $meta_clauses[ $args['orderby'] ];1531 $orderby = "ORDER BY CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";1532 }1533 break;1534 }1535 }1536 }1537 1538 $selects = array();1539 switch ( $args['fields'] ) {1540 case 'all':1541 $selects = array( 't.*', 'tt.*' );1542 break;1543 case 'ids':1544 case 'id=>parent':1545 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );1546 break;1547 case 'names':1548 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );1549 break;1550 case 'count':1551 $orderby = '';1552 $order = '';1553 $selects = array( 'COUNT(*)' );1554 break;1555 case 'id=>name':1556 $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );1557 break;1558 case 'id=>slug':1559 $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );1560 break;1561 }1562 1563 $_fields = $args['fields'];1564 1565 /**1566 * Filters the fields to select in the terms query.1567 *1568 * Field lists modified using this filter will only modify the term fields returned1569 * by the function when the `$fields` parameter set to 'count' or 'all'. In all other1570 * cases, the term fields in the results array will be determined by the `$fields`1571 * parameter alone.1572 *1573 * Use of this filter can result in unpredictable behavior, and is not recommended.1574 *1575 * @since 2.8.01576 *1577 * @param array $selects An array of fields to select for the terms query.1578 * @param array $args An array of term query arguments.1579 * @param array $taxonomies An array of taxonomies.1580 */1581 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );1582 1583 $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";1584 1585 $where = implode( ' AND ', $where_conditions );1586 1587 $pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' );1588 1589 /**1590 * Filters the terms query SQL clauses.1591 *1592 * @since 3.1.01593 *1594 * @param array $pieces Terms query SQL clauses.1595 * @param array $taxonomies An array of taxonomies.1596 * @param array $args An array of terms query arguments.1597 */1598 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );1599 1600 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';1601 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';1602 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';1603 $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';1604 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';1605 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';1606 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';1607 1608 if ( $where ) {1609 $where = "WHERE $where";1610 }1611 1612 $query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join $where $orderby $order $limits";1613 1614 // $args can be anything. Only use the args defined in defaults to compute the key.1615 $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $query );1616 $last_changed = wp_cache_get( 'last_changed', 'terms' );1617 if ( ! $last_changed ) {1618 $last_changed = microtime();1619 wp_cache_set( 'last_changed', $last_changed, 'terms' );1620 }1621 $cache_key = "get_terms:$key:$last_changed";1622 $cache = wp_cache_get( $cache_key, 'terms' );1623 if ( false !== $cache ) {1624 if ( 'all' === $_fields ) {1625 $cache = array_map( 'get_term', $cache );1626 }1627 1628 /**1629 * Filters the given taxonomy's terms cache.1630 *1631 * @since 2.3.01632 *1633 * @param array $cache Cached array of terms for the given taxonomy.1634 * @param array $taxonomies An array of taxonomies.1635 * @param array $args An array of get_terms() arguments.1636 */1637 return apply_filters( 'get_terms', $cache, $taxonomies, $args );1638 }1639 1640 if ( 'count' == $_fields ) {1641 return $wpdb->get_var( $query );1642 }1643 1644 $terms = $wpdb->get_results($query);1645 if ( 'all' == $_fields ) {1646 update_term_cache( $terms );1647 }1648 1649 // Prime termmeta cache.1650 if ( $args['update_term_meta_cache'] ) {1651 $term_ids = wp_list_pluck( $terms, 'term_id' );1652 update_termmeta_cache( $term_ids );1653 }1654 1655 if ( empty($terms) ) {1656 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );1657 1658 /** This filter is documented in wp-includes/taxonomy.php */1659 return apply_filters( 'get_terms', array(), $taxonomies, $args );1660 }1661 1662 if ( $child_of ) {1663 foreach ( $taxonomies as $_tax ) {1664 $children = _get_term_hierarchy( $_tax );1665 if ( ! empty( $children ) ) {1666 $terms = _get_term_children( $child_of, $terms, $_tax );1667 }1668 }1669 }1670 1671 // Update term counts to include children.1672 if ( $args['pad_counts'] && 'all' == $_fields ) {1673 foreach ( $taxonomies as $_tax ) {1674 _pad_term_counts( $terms, $_tax );1675 }1676 }1677 1678 // Make sure we show empty categories that have children.1679 if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {1680 foreach ( $terms as $k => $term ) {1681 if ( ! $term->count ) {1682 $children = get_term_children( $term->term_id, $term->taxonomy );1683 if ( is_array( $children ) ) {1684 foreach ( $children as $child_id ) {1685 $child = get_term( $child_id, $term->taxonomy );1686 if ( $child->count ) {1687 continue 2;1688 }1689 }1690 }1691 1692 // It really is empty.1693 unset($terms[$k]);1694 }1695 }1696 }1697 1698 $_terms = array();1699 if ( 'id=>parent' == $_fields ) {1700 foreach ( $terms as $term ) {1701 $_terms[ $term->term_id ] = $term->parent;1702 }1703 } elseif ( 'ids' == $_fields ) {1704 foreach ( $terms as $term ) {1705 $_terms[] = $term->term_id;1706 }1707 } elseif ( 'names' == $_fields ) {1708 foreach ( $terms as $term ) {1709 $_terms[] = $term->name;1710 }1711 } elseif ( 'id=>name' == $_fields ) {1712 foreach ( $terms as $term ) {1713 $_terms[ $term->term_id ] = $term->name;1714 }1715 } elseif ( 'id=>slug' == $_fields ) {1716 foreach ( $terms as $term ) {1717 $_terms[ $term->term_id ] = $term->slug;1718 }1719 }1720 1721 if ( ! empty( $_terms ) ) {1722 $terms = $_terms;1723 }1724 1725 // Hierarchical queries are not limited, so 'offset' and 'number' must be handled now.1726 if ( $hierarchical && $number && is_array( $terms ) ) {1727 if ( $offset >= count( $terms ) ) {1728 $terms = array();1729 } else {1730 $terms = array_slice( $terms, $offset, $number, true );1731 }1732 }1733 1734 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );1735 1736 if ( 'all' === $_fields ) {1737 $terms = array_map( 'get_term', $terms );1738 }1739 1740 /** This filter is documented in wp-includes/taxonomy.php */1741 1222 return apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1742 1223 } -
trunk/src/wp-settings.php
r37536 r37572 169 169 require( ABSPATH . WPINC . '/taxonomy.php' ); 170 170 require( ABSPATH . WPINC . '/class-wp-term.php' ); 171 require( ABSPATH . WPINC . '/class-wp-term-query.php' ); 171 172 require( ABSPATH . WPINC . '/class-wp-tax-query.php' ); 172 173 require( ABSPATH . WPINC . '/update.php' );
Note: See TracChangeset
for help on using the changeset viewer.