Changeset 31275
- Timestamp:
- 01/24/2015 06:47:30 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r31270 r31275 1548 1548 * 1549 1549 * @since 2.3.0 1550 * @since 4.2.0 Introduced 'name' parameter.1550 * @since 4.2.0 Introduced 'name' and 'childless' parameters. 1551 1551 * 1552 1552 * @global wpdb $wpdb WordPress database abstraction object. … … 1596 1596 * are passed, $child_of is ignored. Default 0. 1597 1597 * @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty. 1598 * @type bool $childless True to limit results to terms that have no children. This parameter has 1599 * no effect on non-hierarchical taxonomies. Default false. 1598 1600 * @type string $cache_domain Unique cache key to be produced when this query is stored in an 1599 1601 * object cache. Default is 'core'. … … 1620 1622 $defaults = array('orderby' => 'name', 'order' => 'ASC', 1621 1623 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 1622 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 1624 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false, 1623 1625 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 1624 1626 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); … … 1639 1641 1640 1642 if ( 'all' == $args['get'] ) { 1643 $args['childless'] = false; 1641 1644 $args['child_of'] = 0; 1642 1645 $args['hide_empty'] = 0; … … 1755 1758 } 1756 1759 1760 $exclusions = array(); 1757 1761 if ( ! empty( $exclude_tree ) ) { 1758 1762 $exclude_tree = wp_parse_id_list( $exclude_tree ); … … 1764 1768 ); 1765 1769 } 1766 $exclusions = implode( ',', array_map( 'intval', $excluded_children ) ); 1767 } else { 1768 $exclusions = ''; 1770 $exclusions = array_merge( $excluded_children, $exclusions ); 1769 1771 } 1770 1772 1771 1773 if ( ! empty( $exclude ) ) { 1772 $exterms = wp_parse_id_list( $exclude ); 1773 if ( empty( $exclusions ) ) { 1774 $exclusions = implode( ',', $exterms ); 1775 } else { 1776 $exclusions .= ', ' . implode( ',', $exterms ); 1774 $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions ); 1775 } 1776 1777 // 'childless' terms are those without an entry in the flattened term hierarchy. 1778 $childless = (bool) $args['childless']; 1779 if ( $childless ) { 1780 foreach ( $taxonomies as $_tax ) { 1781 $term_hierarchy = _get_term_hierarchy( $_tax ); 1782 $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions ); 1777 1783 } 1778 1784 } 1779 1785 1780 1786 if ( ! empty( $exclusions ) ) { 1781 $exclusions = ' AND t.term_id NOT IN (' . $exclusions. ')';1787 $exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')'; 1782 1788 } 1783 1789 -
trunk/tests/phpunit/tests/term/getTerms.php
r31248 r31275 494 494 495 495 $this->assertEqualSets( array( $t3, $t1 ), $found ); 496 } 497 498 /** 499 * @ticket 29839 500 */ 501 public function test_childless_should_return_all_terms_for_flat_hierarchy() { 502 // If run on a flat hierarchy it should return everything. 503 $flat_tax = 'countries'; 504 register_taxonomy( $flat_tax, 'post', array( 'hierarchical' => false ) ); 505 $australia = $this->factory->term->create( array( 'name' => 'Australia', 'taxonomy' => $flat_tax ) ); 506 $china = $this->factory->term->create( array( 'name' => 'China', 'taxonomy' => $flat_tax ) ); 507 $tanzania = $this->factory->term->create( array( 'name' => 'Tanzania', 'taxonomy' => $flat_tax ) ); 508 509 $terms = get_terms( $flat_tax, array( 510 'childless' => true, 511 'hide_empty' => false, 512 'fields' => 'ids', 513 ) ); 514 515 $expected = array( $australia, $china, $tanzania ); 516 $this->assertEqualSets( $expected, $terms ); 517 } 518 519 520 /** 521 * @ticket 29839 522 */ 523 public function test_childless_hierarchical_taxonomy() { 524 $tax = 'location'; 525 register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); 526 /* 527 Canada 528 Ontario 529 Ottawa 530 Nepean 531 Toronto 532 Quebec 533 Montreal 534 PEI 535 */ 536 // Level 1 537 $canada = $this->factory->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) ); 538 539 // Level 2 540 $ontario = $this->factory->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) ); 541 $quebec = $this->factory->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) ); 542 $pei = $this->factory->term->create( array( 'name' => 'PEI', 'parent' => $canada, 'taxonomy' => $tax ) ); 543 544 // Level 3 545 $toronto = $this->factory->term->create( array( 'name' => 'Toronto', 'parent' => $ontario, 'taxonomy' => $tax ) ); 546 $ottawa = $this->factory->term->create( array( 'name' => 'Ottawa', 'parent' => $ontario, 'taxonomy' => $tax ) ); 547 $montreal = $this->factory->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) ); 548 549 // Level 4 550 $nepean = $this->factory->term->create( array( 'name' => 'Nepean', 'parent' => $ottawa, 'taxonomy' => $tax ) ); 551 552 $terms = get_terms( $tax, array( 553 'childless' => true, 554 'hide_empty' => false, 555 'fields' => 'ids', 556 ) ); 557 558 $this->assertEqualSets( array( $montreal, $nepean, $toronto, $pei ), $terms ); 559 } 560 561 /** 562 * @ticket 29839 563 */ 564 public function test_childless_hierarchical_taxonomy_used_with_child_of() { 565 $tax = 'location'; 566 register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) ); 567 568 // Level 1 569 $canada = $this->factory->term->create( array( 'name' => 'Canada', 'taxonomy' => $tax ) ); 570 571 // Level 2 572 $ontario = $this->factory->term->create( array( 'name' => 'Ontario', 'parent' => $canada, 'taxonomy' => $tax ) ); 573 $quebec = $this->factory->term->create( array( 'name' => 'Quebec', 'parent' => $canada, 'taxonomy' => $tax ) ); 574 575 // Level 3 576 $laval = $this->factory->term->create( array( 'name' => 'Laval', 'parent' => $quebec, 'taxonomy' => $tax ) ); 577 $montreal = $this->factory->term->create( array( 'name' => 'Montreal', 'parent' => $quebec, 'taxonomy' => $tax ) ); 578 579 // Level 4 580 $dorval = $this->factory->term->create( array( 'name' => 'Dorval', 'parent' => $montreal, 'taxonomy' => $tax ) ); 581 582 $terms = get_terms( $tax, array( 583 'childless' => true, 584 'child_of' => $quebec, 585 'hide_empty' => false, 586 'fields' => 'ids', 587 ) ); 588 589 $this->assertEqualSets( array( $laval ), $terms ); 590 } 591 592 /** 593 * @ticket 29839 594 */ 595 public function test_childless_should_enforce_childless_status_for_all_queried_taxonomies() { 596 register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) ); 597 register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) ); 598 599 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 600 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ) ); 601 $t3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 602 $t4 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t3 ) ); 603 604 $found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array( 605 'fields' => 'ids', 606 'hide_empty' => false, 607 'childless' => true, 608 ) ); 609 610 $this->assertEqualSets( array( $t2, $t4 ), $found ); 496 611 } 497 612
Note: See TracChangeset
for help on using the changeset viewer.