Ticket #17069: 17069.1.patch
| File 17069.1.patch, 7.9 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/category-template.php
32 32 * Retrieve category parents with separator. 33 33 * 34 34 * @since 1.2.0 35 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. 35 36 * 36 37 * @param int $id Category ID. 37 38 * @param bool $link Optional, default is false. Whether to format with link. 38 39 * @param string $separator Optional, default is '/'. How to separate categories. 39 40 * @param bool $nicename Optional, default is false. Whether to use nice name for display. 40 * @param array $ visited Optional. Already linked to categories to prevent duplicates.41 * @param array $deprecated Not used. 41 42 * @return string|WP_Error A list of category parents on success, WP_Error on failure. 42 43 */ 43 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { 44 $chain = ''; 45 $parent = get_term( $id, 'category' ); 46 if ( is_wp_error( $parent ) ) 47 return $parent; 44 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { 48 45 49 if ( $nicename ) 50 $name = $parent->slug; 51 else 52 $name = $parent->name; 53 54 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { 55 $visited[] = $parent->parent; 56 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited ); 46 if ( !empty( $deprecated ) ) { 47 _deprecated_argument( __FUNCTION__, '4.8.0' ); 57 48 } 58 49 59 if ( $link ) 60 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator; 61 else 62 $chain .= $name.$separator; 63 return $chain; 50 $args = array( 51 'separator' => $separator, 52 'link' => $link, 53 'nicename' => !$nicename, 54 ); 55 56 return get_term_parents_list( $id, 'category', $args ); 64 57 } 65 58 66 59 /** … … 1233 1226 } 1234 1227 1235 1228 /** 1229 * Retrieve term parents with separator. 1230 * 1231 * @since 4.8.0 1232 * 1233 * @param int $term_id Term ID. 1234 * @param string $taxonomy Taxonomy name. 1235 * @param string|array $args { 1236 * Array of optional arguments. 1237 * 1238 * @type bool $link Whether to format as a link. Default false. 1239 * @type string $separator Separator for between the terms. default '/'. 1240 * @type bool nicename Use term names if true, terms slugs if false. Default true. 1241 * @type bool inclusive Include the term to get the parents for. Default true. 1242 * } 1243 * @return string|WP_Error A list of category parents on success, WP_Error or empty on failure. 1244 */ 1245 function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { 1246 $list = ''; 1247 $term = get_term( $term_id, $taxonomy ); 1248 1249 if ( is_wp_error( $term ) ) { 1250 return $term; 1251 } 1252 1253 if ( !$term ) { 1254 return ''; 1255 } 1256 1257 $defaults = array( 1258 'separator' => '/', 1259 'link' => true, 1260 'nicename' => true, 1261 'inclusive' => true 1262 ); 1263 1264 $args = wp_parse_args( $args, $defaults ); 1265 1266 foreach ( array( 'link', 'nicename', 'inclusive' ) as $bool ) { 1267 $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); 1268 } 1269 1270 $parents = get_ancestors( $term_id, $taxonomy ); 1271 1272 if ( $args['inclusive'] ) { 1273 array_unshift( $parents, $term_id ); 1274 } 1275 1276 foreach ( array_reverse( $parents ) as $term_id ) { 1277 $parent = get_term( $term_id, $taxonomy ); 1278 $name = ( $args['nicename'] ) ? $parent->name : $parent->slug; 1279 1280 if ( $args['link'] ) { 1281 $list .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $name . '</a>' . $args['separator']; 1282 } else { 1283 $list .= $name . $args['separator']; 1284 } 1285 } 1286 1287 return $list; 1288 } 1289 1290 /** 1236 1291 * Display the terms in a list. 1237 1292 * 1238 1293 * @since 2.5.0 -
tests/phpunit/tests/category/getCategoryParents.php
50 50 $this->assertSame( $expected, $found ); 51 51 } 52 52 53 public function test_visited_should_also_exclude_children_of_visited_categories() { 54 $c3 = self::factory()->category->create_and_get( array( 55 'parent' => $this->c2->term_id, 56 ) ); 57 $c4 = self::factory()->category->create_and_get( array( 58 'parent' => $c3->term_id, 59 ) ); 60 61 $expected = $this->c1->name . '/'. $this->c2->name . '/'; 62 $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $c3->term_id ) ); 63 $this->assertSame( $expected, $found ); 53 public function test_deprecated_argument_visited() { 54 $this->setExpectedDeprecated( 'get_category_parents' ); 55 $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $this->c1->term_id ) ); 64 56 } 65 57 } -
tests/phpunit/tests/term/getTermParentsList.php
1 <?php 2 3 /** 4 * @group taxonomy 5 */ 6 class Tests_Terms_GetTermsParentsList extends WP_UnitTestCase { 7 protected $c1; 8 protected $c2; 9 10 public function setUp() { 11 parent::setUp(); 12 13 register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) ); 14 15 $this->c1 = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) ); 16 $this->c2 = self::factory()->term->create_and_get( array( 17 'taxonomy' => 'wptests_tax', 18 'parent' => $this->c1->term_id, 19 ) ); 20 } 21 22 public function test_should_return_wp_error_for_empty_id() { 23 $this->assertWPError( get_term_parents_list( '', 'wptests_tax' ) ); 24 } 25 26 public function test_should_return_empty_for_invalid_id() { 27 $this->assertEquals( '', get_term_parents_list( 99999999, 'wptests_tax' ) ); 28 } 29 30 public function test_should_return_wp_error_for_invalid_taxonomy() { 31 $this->assertWPError( get_term_parents_list( $this->c2->term_id, 'foo' ) ); 32 } 33 34 public function test_with_default_parameters() { 35 $expected = '<a href="' . get_term_link( $this->c1->term_id ) . '">' . $this->c1->name . '</a>/<a href="' . get_term_link( $this->c2->term_id ) . '">'. $this->c2->name . '</a>/'; 36 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax' ); 37 $this->assertSame( $expected, $found ); 38 } 39 40 public function test_array_parameters() { 41 $args = array( 42 'separator' => ' --- ', 43 'link' => false, 44 'nicename' => false, 45 'inclusive' => false, 46 ); 47 48 $expected = $this->c1->slug . ' --- '; 49 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', $args ); 50 $this->assertSame( $expected, $found ); 51 } 52 53 public function test_link_false() { 54 $expected = $this->c1->name . '/' . $this->c2->name . '/'; 55 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false' ); 56 $this->assertSame( $expected, $found ); 57 } 58 59 public function test_separator() { 60 $expected = $this->c1->name . ' --- ' . $this->c2->name . ' --- '; 61 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false&separator= --- ' ); 62 $this->assertSame( $expected, $found ); 63 } 64 65 public function test_nicename_true() { 66 $expected = $this->c1->name . '/'. $this->c2->name . '/'; 67 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false&nicename=true' ); 68 $this->assertSame( $expected, $found ); 69 } 70 71 public function test_nicename_false() { 72 $expected = $this->c1->slug . '/'. $this->c2->slug . '/'; 73 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false&nicename=false' ); 74 $this->assertSame( $expected, $found ); 75 } 76 77 public function test_inclusive_false() { 78 $expected = '<a href="' . get_term_link( $this->c1->term_id ) . '">' . $this->c1->name . '</a>/'; 79 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'inclusive=false' ); 80 $this->assertSame( $expected, $found ); 81 } 82 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)