From 62bf3d437f30638a686853e02a99a411948e4565 Mon Sep 17 00:00:00 2001 From: sawfly Date: Wed, 25 Nov 2026 16:56:04 +0000 Subject: [PATCH 1/2] additional parameter for get term parents list and tests --- src/wp-includes/category-template.php | 10 +- .../phpunit/tests/term/getTermParentsList.php | 130 ++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 130f55e3fe043..fa08ae2d540b6 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -1407,6 +1407,7 @@ function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after * @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 $reverse 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. */ @@ -1429,11 +1430,12 @@ function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { 'separator' => '/', 'link' => true, 'inclusive' => true, + 'reverse' => true, ); $args = wp_parse_args( $args, $defaults ); - foreach ( array( 'link', 'inclusive' ) as $bool ) { + foreach ( array( 'link', 'inclusive', 'reverse' ) 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['reverse'] ) { + $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..b1a5b61a605fe 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_reverse_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( 'reverse' => true ) ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + */ + public function test_reverse_false() { + $expected = self::$c2->name . '/' . self::$c1->name . '/'; + $found = get_term_parents_list( + self::$c2->term_id, + 'wptests_tax', + array( + 'link' => false, + 'reverse' => false, + ) + ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + */ + public function test_reverse_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, + 'reverse' => false, + ) + ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + */ + public function test_reverse_false_with_links() { + $expected = '' . self::$c2->name . '/' . self::$c1->name . '/'; + $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', array( 'reverse' => false ) ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + */ + public function test_reverse_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, + 'reverse' => false, + ) + ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + */ + public function test_reverse_false_term_without_parents() { + $expected = self::$c1->name . '/'; + $found = get_term_parents_list( + self::$c1->term_id, + 'wptests_tax', + array( + 'link' => false, + 'reverse' => false, + ) + ); + $this->assertSame( $expected, $found ); + } + + /** + * @ticket 53319 + * + * @dataProvider data_reverse_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_reverse_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&reverse=' . $value ); + $this->assertSame( $expected, $found ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_reverse_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 ), + ); + } } From 09fa401f3d54b050e167ad9f9d2639d643febcba Mon Sep 17 00:00:00 2001 From: sawfly Date: Wed, 25 Nov 2026 17:04:41 +0000 Subject: [PATCH 2/2] parameter name changed --- src/wp-includes/category-template.php | 26 ++++++------ .../phpunit/tests/term/getTermParentsList.php | 42 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index fa08ae2d540b6..db2274711e5ba 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -1402,12 +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 bool $reverse Whether to reverse the hierarchical order of the Terms on output. 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. */ @@ -1426,16 +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, - 'reverse' => true, + 'format' => 'name', + 'separator' => '/', + 'link' => true, + 'inclusive' => true, + 'parents_first' => true, ); $args = wp_parse_args( $args, $defaults ); - foreach ( array( 'link', 'inclusive', 'reverse' ) as $bool ) { + foreach ( array( 'link', 'inclusive', 'parents_first' ) as $bool ) { $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); } @@ -1445,7 +1445,7 @@ function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { array_unshift( $parents, $term_id ); } - if ( $args['reverse'] ) { + if ( $args['parents_first'] ) { $parents = array_reverse( $parents ); } diff --git a/tests/phpunit/tests/term/getTermParentsList.php b/tests/phpunit/tests/term/getTermParentsList.php index b1a5b61a605fe..cca0edea02222 100644 --- a/tests/phpunit/tests/term/getTermParentsList.php +++ b/tests/phpunit/tests/term/getTermParentsList.php @@ -119,25 +119,25 @@ public function test_should_accept_term_object() { /** * @ticket 53319 */ - public function test_reverse_true_should_match_default_order() { + 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( 'reverse' => true ) ); + $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_reverse_false() { + 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, - 'reverse' => false, + 'link' => false, + 'parents_first' => false, ) ); $this->assertSame( $expected, $found ); @@ -146,7 +146,7 @@ public function test_reverse_false() { /** * @ticket 53319 */ - public function test_reverse_false_should_go_from_nearest_to_distant_ancestor() { + public function test_parents_first_false_should_go_from_nearest_to_distant_ancestor() { $c3 = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax', @@ -159,8 +159,8 @@ public function test_reverse_false_should_go_from_nearest_to_distant_ancestor() $c3->term_id, 'wptests_tax', array( - 'link' => false, - 'reverse' => false, + 'link' => false, + 'parents_first' => false, ) ); $this->assertSame( $expected, $found ); @@ -169,16 +169,16 @@ public function test_reverse_false_should_go_from_nearest_to_distant_ancestor() /** * @ticket 53319 */ - public function test_reverse_false_with_links() { + 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( 'reverse' => false ) ); + $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', array( 'parents_first' => false ) ); $this->assertSame( $expected, $found ); } /** * @ticket 53319 */ - public function test_reverse_false_with_inclusive_false() { + public function test_parents_first_false_with_inclusive_false() { $c3 = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax', @@ -191,9 +191,9 @@ public function test_reverse_false_with_inclusive_false() { $c3->term_id, 'wptests_tax', array( - 'link' => false, - 'inclusive' => false, - 'reverse' => false, + 'link' => false, + 'inclusive' => false, + 'parents_first' => false, ) ); $this->assertSame( $expected, $found ); @@ -202,14 +202,14 @@ public function test_reverse_false_with_inclusive_false() { /** * @ticket 53319 */ - public function test_reverse_false_term_without_parents() { + 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, - 'reverse' => false, + 'link' => false, + 'parents_first' => false, ) ); $this->assertSame( $expected, $found ); @@ -218,17 +218,17 @@ public function test_reverse_false_term_without_parents() { /** * @ticket 53319 * - * @dataProvider data_reverse_should_be_validated_as_boolean + * @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_reverse_should_be_validated_as_boolean( $value, $parents_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&reverse=' . $value ); + $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false&parents_first=' . $value ); $this->assertSame( $expected, $found ); } @@ -237,7 +237,7 @@ public function test_reverse_should_be_validated_as_boolean( $value, $parents_fi * * @return array[] */ - public static function data_reverse_should_be_validated_as_boolean() { + public static function data_parents_first_should_be_validated_as_boolean() { return array( 'string true' => array( 'true', true ), 'string 1' => array( '1', true ),