Ticket #17069: 17069.2.patch
File 17069.2.patch, 8.4 KB (added by , 8 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 $format = $nicename ? 'slug' : 'name'; 51 52 $args = array( 53 'separator' => $separator, 54 'link' => $link, 55 'format' => $format, 56 ); 57 58 return get_term_parents_list( $id, 'category', $args ); 64 59 } 65 60 66 61 /** … … 1233 1228 } 1234 1229 1235 1230 /** 1231 * Retrieve term parents with separator. 1232 * 1233 * @since 4.8.0 1234 * 1235 * @param int $term_id Term ID. 1236 * @param string $taxonomy Taxonomy name. 1237 * @param string|array $args { 1238 * Array of optional arguments. 1239 * 1240 * @type string $format Use term names or slugs, Accepts 'name' or 'slug'. Default 'name'. 1241 * @type string $separator Separator for between the terms. default '/'. 1242 * @type bool $link Whether to format as a link. Default true. 1243 * @type bool $inclusive Include the term to get the parents for. Default true. 1244 * } 1245 * @return string|WP_Error A list of category parents on success, WP_Error or empty on failure. 1246 */ 1247 function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { 1248 $list = ''; 1249 $term = get_term( $term_id, $taxonomy ); 1250 1251 if ( is_wp_error( $term ) ) { 1252 return $term; 1253 } 1254 1255 if ( ! $term ) { 1256 return ''; 1257 } 1258 1259 $defaults = array( 1260 'format' => 'name', 1261 'separator' => '/', 1262 'link' => true, 1263 'inclusive' => true, 1264 ); 1265 1266 $args = wp_parse_args( $args, $defaults ); 1267 1268 foreach ( array( 'link', 'inclusive' ) as $bool ) { 1269 $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); 1270 } 1271 1272 $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); 1273 1274 if ( $args['inclusive'] ) { 1275 array_unshift( $parents, $term_id ); 1276 } 1277 1278 foreach ( array_reverse( $parents ) as $term_id ) { 1279 $parent = get_term( $term_id, $taxonomy ); 1280 $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; 1281 1282 if ( $args['link'] ) { 1283 $list .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $name . '</a>' . $args['separator']; 1284 } else { 1285 $list .= $name . $args['separator']; 1286 } 1287 } 1288 1289 return $list; 1290 } 1291 1292 /** 1236 1293 * Display the terms in a list. 1237 1294 * 1238 1295 * @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 ) ); 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 ) ); 56 } 60 57 61 $expected = $this->c1->name . '/'. $this->c2->name . '/'; 62 $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $c3->term_id ) ); 58 public function test_category_without_parents() { 59 $expected = $this->c1->name . '/'; 60 $found = get_category_parents( $this->c1->term_id ); 63 61 $this->assertSame( $expected, $found ); 64 62 } 65 63 } -
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 'format' => 'slug', 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_format_name() { 66 $expected = $this->c1->name . '/'. $this->c2->name . '/'; 67 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false&format=name' ); 68 $this->assertSame( $expected, $found ); 69 } 70 71 public function test_format_slug() { 72 $expected = $this->c1->slug . '/'. $this->c2->slug . '/'; 73 $found = get_term_parents_list( $this->c2->term_id, 'wptests_tax', 'link=false&format=slug' ); 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 83 public function test_term_without_parents() { 84 $expected = '<a href="' . get_term_link( $this->c1->term_id ) . '">' . $this->c1->name . '</a>/'; 85 $found = get_term_parents_list( $this->c1->term_id, 'wptests_tax' ); 86 $this->assertSame( $expected, $found ); 87 } 88 }