Ticket #29894: taxonomy.php.update4.patch
File taxonomy.php.update4.patch, 6.7 KB (added by , 10 years ago) |
---|
-
wp-includes/taxonomy.php
1326 1326 } 1327 1327 } 1328 1328 1329 // $args can be whatever, only use the args defined in defaults to compute the key1330 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';1331 $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );1332 $last_changed = wp_cache_get( 'last_changed', 'terms' );1333 if ( ! $last_changed ) {1334 $last_changed = microtime();1335 wp_cache_set( 'last_changed', $last_changed, 'terms' );1336 }1337 $cache_key = "get_terms:$key:$last_changed";1338 $cache = wp_cache_get( $cache_key, 'terms' );1339 if ( false !== $cache ) {1340 1341 /**1342 * Filter the given taxonomy's terms cache.1343 *1344 * @since 2.3.01345 *1346 * @param array $cache Cached array of terms for the given taxonomy.1347 * @param string|array $taxonomies A taxonomy or array of taxonomies.1348 * @param array $args An array of arguments to get terms.1349 */1350 $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args );1351 return $cache;1352 }1353 1354 1329 $_orderby = strtolower( $args['orderby'] ); 1355 1330 if ( 'count' == $_orderby ) { 1356 1331 $orderby = 'tt.count'; … … 1494 1469 $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); 1495 1470 } 1496 1471 1497 $selects = array();1498 switch ( $args['fields'] ) {1499 case 'all':1500 $selects = array( 't.*', 'tt.*' );1501 break;1502 case 'ids':1503 case 'id=>parent':1504 $selects = array( 't.term_id', 'tt.parent', 'tt.count' );1505 break;1506 case 'names':1507 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );1508 break;1509 case 'count':1510 $orderby = '';1511 $order = '';1512 $selects = array( 'COUNT(*)' );1513 break;1514 case 'id=>name':1515 $selects = array( 't.term_id', 't.name' );1516 break;1517 case 'id=>slug':1518 $selects = array( 't.term_id', 't.slug' );1519 break;1520 }1521 1522 $_fields = $args['fields'];1523 1524 /**1525 * Filter the fields to select in the terms query.1526 *1527 * @since 2.8.01528 *1529 * @param array $selects An array of fields to select for the terms query.1530 * @param array $args An array of term query arguments.1531 * @param string|array $taxonomies A taxonomy or array of taxonomies.1532 */1533 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );1534 1535 1472 $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 1473 $pieces = array( 'join', 'where', 'orderby', 'order', 'limits' ); 1536 1474 1537 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );1538 1539 1475 /** 1540 1476 * Filter the terms query SQL clauses. 1541 1477 * … … 1546 1482 * @param array $args An array of terms query arguments. 1547 1483 */ 1548 1484 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); 1549 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';1550 1485 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 1551 1486 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 1552 1487 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 1553 1488 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; 1554 1489 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 1555 1490 1556 $query = "SELECT $fieldsFROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";1491 $query = "SELECT * FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; 1557 1492 1558 if ( 'count' == $_fields ) { 1559 $term_count = $wpdb->get_var($query); 1560 return $term_count; 1493 // Attempt to use cached query 1494 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; 1495 $key = md5( $query . $filter_key ); 1496 $last_changed = wp_cache_get( 'last_changed', 'terms' ); 1497 if ( ! $last_changed ) { 1498 $last_changed = microtime(); 1499 wp_cache_set( 'last_changed', $last_changed, 'terms' ); 1561 1500 } 1562 1563 $terms = $wpdb->get_results($query); 1564 if ( 'all' == $_fields ) { 1501 $cache_key = "get_terms:$key:$last_changed"; 1502 $cache = wp_cache_get( $cache_key, 'terms' ); 1503 if ( false !== $cache ) { 1504 $terms = $cache; 1505 } else { 1506 $terms = $wpdb->get_results( $query ); 1507 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS); 1508 } 1509 1510 if ( 'count' == $args['fields'] ) { 1511 /** 1512 * Filter the given taxonomy's terms cache. 1513 * 1514 * @since 2.3.0 1515 * 1516 * @param array $cache Cached array of terms for the given taxonomy. 1517 * @param string|array $taxonomies A taxonomy or array of taxonomies. 1518 * @param array $args An array of arguments to get terms. 1519 */ 1520 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1521 return count($terms); 1522 } 1523 1524 if ( 'all' == $args['fields'] ) { 1565 1525 update_term_cache($terms); 1566 1526 } 1567 1527 1568 1528 if ( empty($terms) ) { 1569 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );1570 1571 1529 /** This filter is documented in wp-includes/taxonomy.php */ 1572 $terms = apply_filters( 'get_terms', array(), $taxonomies, $args ); 1573 return $terms; 1530 return apply_filters( 'get_terms', array(), $taxonomies, $args ); 1574 1531 } 1575 1532 1576 1533 if ( $child_of ) { … … 1581 1538 } 1582 1539 1583 1540 // Update term counts to include children. 1584 if ( $args['pad_counts'] && 'all' == $ _fields) {1541 if ( $args['pad_counts'] && 'all' == $args['fields'] ) { 1585 1542 _pad_term_counts( $terms, reset( $taxonomies ) ); 1586 1543 } 1587 1544 // Make sure we show empty categories that have children. … … 1606 1563 reset( $terms ); 1607 1564 1608 1565 $_terms = array(); 1609 if ( 'id=>parent' == $ _fields) {1566 if ( 'id=>parent' == $args['fields'] ) { 1610 1567 while ( $term = array_shift( $terms ) ) { 1611 1568 $_terms[$term->term_id] = $term->parent; 1612 1569 } 1613 } elseif ( 'ids' == $ _fields) {1570 } elseif ( 'ids' == $args['fields'] ) { 1614 1571 while ( $term = array_shift( $terms ) ) { 1615 1572 $_terms[] = $term->term_id; 1616 1573 } 1617 } elseif ( 'names' == $ _fields) {1574 } elseif ( 'names' == $args['fields'] ) { 1618 1575 while ( $term = array_shift( $terms ) ) { 1619 1576 $_terms[] = $term->name; 1620 1577 } 1621 } elseif ( 'id=>name' == $ _fields) {1578 } elseif ( 'id=>name' == $args['fields'] ) { 1622 1579 while ( $term = array_shift( $terms ) ) { 1623 1580 $_terms[$term->term_id] = $term->name; 1624 1581 } 1625 } elseif ( 'id=>slug' == $ _fields) {1582 } elseif ( 'id=>slug' == $args['fields'] ) { 1626 1583 while ( $term = array_shift( $terms ) ) { 1627 1584 $_terms[$term->term_id] = $term->slug; 1628 1585 } … … 1636 1593 $terms = array_slice( $terms, $offset, $number ); 1637 1594 } 1638 1595 1639 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );1640 1641 1596 /** This filter is documented in wp-includes/taxonomy */ 1642 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1643 return $terms; 1597 return apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1644 1598 } 1645 1599 1646 1600 /**