<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39549] trunk: Taxonomy: Introduce `get_term_parents_list()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39549">39549</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39549","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-12-09 16:09:31 +0000 (Fri, 09 Dec 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Taxonomy: Introduce `get_term_parents_list()`.
This new function is a taxonomy-agnostic version of `get_category_parents()`.
Props keesiemeijer, SergeyBiryukov, rafaehlers.
Fixes <a href="https://core.trac.wordpress.org/ticket/17069">#17069</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescategorytemplatephp">trunk/src/wp-includes/category-template.php</a></li>
<li><a href="#trunktestsphpunittestscategorygetCategoryParentsphp">trunk/tests/phpunit/tests/category/getCategoryParents.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitteststermgetTermParentsListphp">trunk/tests/phpunit/tests/term/getTermParentsList.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescategorytemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/category-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/category-template.php 2016-12-09 06:15:15 UTC (rev 39548)
+++ trunk/src/wp-includes/category-template.php 2016-12-09 16:09:31 UTC (rev 39549)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,35 +32,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieve category parents with separator.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.2.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $id Category ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $link Optional, default is false. Whether to format with link.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $separator Optional, default is '/'. How to separate categories.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $nicename Optional, default is false. Whether to use nice name for display.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $visited Optional. Already linked to categories to prevent duplicates.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $deprecated Not used.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string|WP_Error A list of category parents on success, WP_Error on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
- $chain = '';
- $parent = get_term( $id, 'category' );
- if ( is_wp_error( $parent ) )
- return $parent;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $nicename )
- $name = $parent->slug;
- else
- $name = $parent->name;
-
- if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
- $visited[] = $parent->parent;
- $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $deprecated ) ) {
+ _deprecated_argument( __FUNCTION__, '4.8.0' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $link )
- $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
- else
- $chain .= $name.$separator;
- return $chain;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $format = $nicename ? 'slug' : 'name';
+
+ $args = array(
+ 'separator' => $separator,
+ 'link' => $link,
+ 'format' => $format,
+ );
+
+ return get_term_parents_list( $id, 'category', $args );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1233,6 +1228,71 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieve term parents with separator.
+ *
+ * @since 4.8.0
+ *
+ * @param int $term_id Term ID.
+ * @param string $taxonomy Taxonomy name.
+ * @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.
+ * }
+ * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
+ */
+function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {
+ $list = '';
+ $term = get_term( $term_id, $taxonomy );
+
+ if ( is_wp_error( $term ) ) {
+ return $term;
+ }
+
+ if ( ! $term ) {
+ return $list;
+ }
+
+ $term_id = $term->term_id;
+
+ $defaults = array(
+ 'format' => 'name',
+ 'separator' => '/',
+ 'link' => true,
+ 'inclusive' => true,
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+
+ foreach ( array( 'link', 'inclusive' ) as $bool ) {
+ $args[ $bool ] = wp_validate_boolean( $args[ $bool ] );
+ }
+
+ $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' );
+
+ if ( $args['inclusive'] ) {
+ array_unshift( $parents, $term_id );
+ }
+
+ foreach ( array_reverse( $parents ) as $term_id ) {
+ $parent = get_term( $term_id, $taxonomy );
+ $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;
+
+ if ( $args['link'] ) {
+ $list .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $name . '</a>' . $args['separator'];
+ } else {
+ $list .= $name . $args['separator'];
+ }
+ }
+
+ return $list;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Display the terms in a list.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span></span></pre></div>
<a id="trunktestsphpunittestscategorygetCategoryParentsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/category/getCategoryParents.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/category/getCategoryParents.php 2016-12-09 06:15:15 UTC (rev 39548)
+++ trunk/tests/phpunit/tests/category/getCategoryParents.php 2016-12-09 16:09:31 UTC (rev 39549)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,16 +50,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, $found );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_visited_should_also_exclude_children_of_visited_categories() {
- $c3 = self::factory()->category->create_and_get( array(
- 'parent' => $this->c2->term_id,
- ) );
- $c4 = self::factory()->category->create_and_get( array(
- 'parent' => $c3->term_id,
- ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_deprecated_argument_visited() {
+ $this->setExpectedDeprecated( 'get_category_parents' );
+ $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $this->c1->term_id ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = $this->c1->name . '/'. $this->c2->name . '/';
- $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $c3->term_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_category_without_parents() {
+ $expected = $this->c1->name . '/';
+ $found = get_category_parents( $this->c1->term_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, $found );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunitteststermgetTermParentsListphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/term/getTermParentsList.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/getTermParentsList.php (rev 0)
+++ trunk/tests/phpunit/tests/term/getTermParentsList.php 2016-12-09 16:09:31 UTC (rev 39549)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,113 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group taxonomy
+ */
+class Tests_Terms_GetTermsParentsList extends WP_UnitTestCase {
+ protected static $c1;
+ protected static $c2;
+
+ public static function wpSetUpBeforeClass( $factory ) {
+ register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+
+ self::$c1 = $factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );
+ self::$c2 = $factory->term->create_and_get( array(
+ 'taxonomy' => 'wptests_tax',
+ 'parent' => self::$c1->term_id,
+ ) );
+ }
+
+ public static function wpTearDownAfterClass() {
+ wp_delete_term( self::$c1->term_id, 'wptests_tax' );
+ wp_delete_term( self::$c2->term_id, 'wptests_tax' );
+ }
+
+ public function setUp() {
+ parent::setUp();
+ register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
+ }
+
+ public function test_should_return_wp_error_for_empty_id() {
+ $this->assertWPError( get_term_parents_list( '', 'wptests_tax' ) );
+ }
+
+ public function test_should_return_empty_for_invalid_id() {
+ $this->assertEquals( '', get_term_parents_list( 99999999, 'wptests_tax' ) );
+ }
+
+ public function test_should_return_wp_error_for_invalid_taxonomy() {
+ $this->assertWPError( get_term_parents_list( self::$c2->term_id, 'foo' ) );
+ }
+
+ public function test_with_default_parameters() {
+ $expected = '<a href="' . get_term_link( self::$c1->term_id ) . '">' . self::$c1->name . '</a>/<a href="' . get_term_link( self::$c2->term_id ) . '">'. self::$c2->name . '</a>/';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_array_parameters() {
+ $args = array(
+ 'separator' => ' --- ',
+ 'link' => false,
+ 'format' => 'slug',
+ 'inclusive' => false,
+ );
+
+ $expected = self::$c1->slug . ' --- ';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', $args );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_link_false() {
+ $expected = self::$c1->name . '/' . self::$c2->name . '/';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_separator() {
+ $expected = self::$c1->name . ' --- ' . self::$c2->name . ' --- ';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false&separator= --- ' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_format_name() {
+ $expected = self::$c1->name . '/'. self::$c2->name . '/';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false&format=name' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_format_slug() {
+ $expected = self::$c1->slug . '/'. self::$c2->slug . '/';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'link=false&format=slug' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_inclusive_false() {
+ $expected = '<a href="' . get_term_link( self::$c1->term_id ) . '">' . self::$c1->name . '</a>/';
+ $found = get_term_parents_list( self::$c2->term_id, 'wptests_tax', 'inclusive=false' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_term_without_parents() {
+ $expected = '<a href="' . get_term_link( self::$c1->term_id ) . '">' . self::$c1->name . '</a>/';
+ $found = get_term_parents_list( self::$c1->term_id, 'wptests_tax' );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_order_should_go_from_distant_to_nearest_ancestor() {
+ $c3 = self::factory()->term->create_and_get( array(
+ 'taxonomy' => 'wptests_tax',
+ 'parent' => self::$c2->term_id,
+ ) );
+
+ $expected = self::$c1->name . '/' . self::$c2->name . '/' . $c3->name . '/';
+ $found = get_term_parents_list( $c3->term_id, 'wptests_tax', array( 'link' => false ) );
+ $this->assertSame( $expected, $found );
+ }
+
+ public function test_should_accept_term_object() {
+ $expected = self::$c1->name . '/' . self::$c2->name . '/';
+ $found = get_term_parents_list( self::$c2, 'wptests_tax', array( 'link' => false ) );
+ $this->assertSame( $expected, $found );
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>