From 94a9896b05932afcdd5fd170cb170edffa908dff Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 23 May 2015 18:28:22 +0000 Subject: [PATCH] Introduced `$field` argument to `get_term_link()`. This new argument allows developers to specify which term field should be matched by the value of the `$term` parameter (in particular, 'name' and 'term_taxonomy_id' are now supported). Props sudar, mordauk. Fixes #14156. git-svn-id: https://develop.svn.wordpress.org/trunk@32553 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 15 +++++--- tests/phpunit/tests/term/getTermLink.php | 47 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 75873c3ea5..0651e057c4 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -4381,20 +4381,25 @@ function wp_get_split_term( $old_term_id, $taxonomy ) { * Generate a permalink for a taxonomy term archive. * * @since 2.5.0 + * @since 4.3.0 Introduced `$field` argument. * * @param object|int|string $term The term object, ID, or slug whose link will be retrieved. * @param string $taxonomy Optional. Taxonomy. Default empty. + * @param string $field Optional. The term field that should be matched by the `$term` argument. Accepts + * any `$field` values accepted by `get_term_by()`: 'slug', 'name', + * 'term_taxonomy_id', or 'id'. Default is 'slug', unless `$term` is an integer, in + * which case it's asssumed to be an ID. * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist. */ -function get_term_link( $term, $taxonomy = '' ) { +function get_term_link( $term, $taxonomy = '', $field = null ) { global $wp_rewrite; if ( !is_object($term) ) { - if ( is_int($term) ) { - $term = get_term($term, $taxonomy); - } else { - $term = get_term_by('slug', $term, $taxonomy); + if ( is_null( $field ) ) { + $field = is_int( $term ) ? 'id' : 'slug'; } + + $term = get_term_by( $field, $term, $taxonomy ); } if ( !is_object($term) ) diff --git a/tests/phpunit/tests/term/getTermLink.php b/tests/phpunit/tests/term/getTermLink.php index 748b8e48c9..728a7cc5b5 100644 --- a/tests/phpunit/tests/term/getTermLink.php +++ b/tests/phpunit/tests/term/getTermLink.php @@ -54,6 +54,53 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { $term = (string) $t1; + $actual = get_term_link( $term, 'wptests_tax', 'id' ); + $this->assertContains( 'wptests_tax=foo', $actual ); + } + + /** + * @ticket 14156 + */ + public function test_should_match_field_by_name() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'foo', + 'name' => 'Bar Term', + ) ); + + $actual = get_term_link( 'Bar Term', 'wptests_tax', 'name' ); + $this->assertContains( 'wptests_tax=foo', $actual ); + } + + /** + * @ticket 14156 + */ + public function test_should_match_field_by_tt_id() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'foo', + 'name' => 'Bar Term', + ) ); + + $actual = get_term_link( 'Bar Term', 'wptests_tax', 'name' ); + $this->assertContains( 'wptests_tax=foo', $actual ); + } + + /** + * @ticket 14156 + */ + public function test_numeric_string_should_be_interpreted_as_term_id_if_id_field_is_specified() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => $t1, + ) ); + + $term = (string) $t1; + $actual = get_term_link( $term, 'wptests_tax' ); $this->assertContains( 'wptests_tax=' . $term, $actual ); }