diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 130f55e3fe043..db2274711e5ba 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -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. */ @@ -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 ] ); } @@ -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; diff --git a/tests/phpunit/tests/term/getTermParentsList.php b/tests/phpunit/tests/term/getTermParentsList.php index 772bc79daaa7f..cca0edea02222 100644 --- a/tests/phpunit/tests/term/getTermParentsList.php +++ b/tests/phpunit/tests/term/getTermParentsList.php @@ -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 = '' . self::$c2->name . '/' . self::$c1->name . '/'; + $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 ), + ); + } }