Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/wp-includes/category-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1402,11 +1402,12 @@ function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after
* @param string|array $args {
* Array of optional arguments.
*
* @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'.
* Default 'name'.
* @type string $separator Separator for between the terms. Default '/'.
* @type bool $link Whether to format as a link. Default true.
* @type bool $inclusive Include the term to get the parents for. Default true.
* @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'.
* Default 'name'.
* @type string $separator Separator for between the terms. Default '/'.
* @type bool $link Whether to format as a link. Default true.
* @type bool $inclusive Include the term to get the parents for. Default true.
* @type bool $parents_first Whether to reverse the hierarchical order of the Terms on output. Default true
* }
* @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
*/
Expand All @@ -1425,15 +1426,16 @@ function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {
$term_id = $term->term_id;

$defaults = array(
'format' => 'name',
'separator' => '/',
'link' => true,
'inclusive' => true,
'format' => 'name',
'separator' => '/',
'link' => true,
'inclusive' => true,
'parents_first' => true,
);

$args = wp_parse_args( $args, $defaults );

foreach ( array( 'link', 'inclusive' ) as $bool ) {
foreach ( array( 'link', 'inclusive', 'parents_first' ) as $bool ) {
$args[ $bool ] = wp_validate_boolean( $args[ $bool ] );
}

Expand All @@ -1443,7 +1445,11 @@ function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {
array_unshift( $parents, $term_id );
}

foreach ( array_reverse( $parents ) as $term_id ) {
if ( $args['parents_first'] ) {
$parents = array_reverse( $parents );
}

foreach ( $parents as $term_id ) {
$parent = get_term( $term_id, $taxonomy );
$name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;

Expand Down
130 changes: 130 additions & 0 deletions tests/phpunit/tests/term/getTermParentsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,134 @@ public function test_should_accept_term_object() {
$found = get_term_parents_list( self::$c2, 'wptests_tax', array( 'link' => false ) );
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_true_should_match_default_order() {
$args = array( 'link' => false );

$expected = get_term_parents_list( self::$c2->term_id, 'wptests_tax', $args );
$found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', $args + array( 'parents_first' => true ) );
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_false() {
$expected = self::$c2->name . '/' . self::$c1->name . '/';
$found = get_term_parents_list(
self::$c2->term_id,
'wptests_tax',
array(
'link' => false,
'parents_first' => false,
)
);
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_false_should_go_from_nearest_to_distant_ancestor() {
$c3 = self::factory()->term->create_and_get(
array(
'taxonomy' => 'wptests_tax',
'parent' => self::$c2->term_id,
)
);

$expected = $c3->name . '/' . self::$c2->name . '/' . self::$c1->name . '/';
$found = get_term_parents_list(
$c3->term_id,
'wptests_tax',
array(
'link' => false,
'parents_first' => false,
)
);
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_false_with_links() {
$expected = '<a href="' . get_term_link( self::$c2->term_id ) . '">' . self::$c2->name . '</a>/<a href="' . get_term_link( self::$c1->term_id ) . '">' . self::$c1->name . '</a>/';
$found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', array( 'parents_first' => false ) );
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_false_with_inclusive_false() {
$c3 = self::factory()->term->create_and_get(
array(
'taxonomy' => 'wptests_tax',
'parent' => self::$c2->term_id,
)
);

$expected = self::$c2->name . '/' . self::$c1->name . '/';
$found = get_term_parents_list(
$c3->term_id,
'wptests_tax',
array(
'link' => false,
'inclusive' => false,
'parents_first' => false,
)
);
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*/
public function test_parents_first_false_term_without_parents() {
$expected = self::$c1->name . '/';
$found = get_term_parents_list(
self::$c1->term_id,
'wptests_tax',
array(
'link' => false,
'parents_first' => false,
)
);
$this->assertSame( $expected, $found );
}

/**
* @ticket 53319
*
* @dataProvider data_parents_first_should_be_validated_as_boolean
*
* @param string $value Query string value for the 'reverse' argument.
* @param bool $parents_first Whether the parent term is expected first.
*/
public function test_parents_first_should_be_validated_as_boolean( $value, $parents_first ) {
$expected = $parents_first
? self::$c1->name . '/' . self::$c2->name . '/'
: self::$c2->name . '/' . self::$c1->name . '/';

$found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false&parents_first=' . $value );
$this->assertSame( $expected, $found );
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_parents_first_should_be_validated_as_boolean() {
return array(
'string true' => array( 'true', true ),
'string 1' => array( '1', true ),
'string false' => array( 'false', false ),
'string 0' => array( '0', false ),
);
}
}
Loading