From 9c135465183de25360368a3f87e7614a826528f3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 3 Nov 2014 18:48:42 +0000 Subject: [PATCH] Introduce `term_template` param to `get_the_taxonomies()`. This parameter allows theme and plugin authors to specify the formatting they would like on the term links as they are parsed into the taxonomy list. Props hereswhatidid, dlh, davidjlaietta. See #27238. git-svn-id: https://develop.svn.wordpress.org/trunk@30209 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 35 +++++++++++++++++++++----------- tests/phpunit/tests/taxonomy.php | 30 ++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 65ab7ba0d7..c9feb504da 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -4131,16 +4131,20 @@ function get_term_link( $term, $taxonomy = '') { * post without specifying the Post ID. You can also use it outside the Loop to * display the taxonomies for a specific post. * - * The available defaults are: - * 'post' : default is 0. The post ID to get taxonomies of. - * 'before' : default is empty string. Display before taxonomies list. - * 'sep' : default is empty string. Separate every taxonomy with value in this. - * 'after' : default is empty string. Display this after the taxonomies list. - * 'template' : The template to use for displaying the taxonomy terms. - * * @since 2.5.0 * - * @param array $args Override the defaults. + * @param array $args { + * Arguments about which post to use and how to format the output. + * + * @type int|WP_Post $post Post ID or object to get taxonomies of. Default current post. + * @type string $before Displays before the taxonomies. Default empty string. + * @type string $sep Separates each taxonomy. Default is a space. + * @type string $after Displays after the taxonomies. Default empty string. + * @type string $template Template for displaying a taxonomy label and list of + * terms. Default is "Label: Terms." + * @type string $term_template Template for displaying a single term in the list. + * Default is the term name linked to its archive. + * } */ function the_taxonomies( $args = array() ) { $defaults = array( @@ -4148,8 +4152,10 @@ function the_taxonomies( $args = array() ) { 'before' => '', 'sep' => ' ', 'after' => '', - /* translators: %s: taxonomy label, %l: list of term links */ - 'template' => __( '%s: %l.' ) + /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */ + 'template' => __( '%s: %l.' ), + /* translators: %1$s: term link, %2$s: term name */ + 'term_template' => '%2$s', ); $r = wp_parse_args( $args, $defaults ); @@ -4173,8 +4179,10 @@ function get_the_taxonomies( $post = 0, $args = array() ) { $post = get_post( $post ); $args = wp_parse_args( $args, array( - /* translators: %s: taxonomy label, %l: list of term links */ + /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */ 'template' => __( '%s: %l.' ), + /* translators: %1$s: term link, %2$s: term name */ + 'term_template' => '%2$s', ) ); $taxonomies = array(); @@ -4194,6 +4202,9 @@ function get_the_taxonomies( $post = 0, $args = array() ) { if ( empty( $t['template'] ) ) { $t['template'] = $args['template']; } + if ( empty( $t['term_template'] ) ) { + $t['term_template'] = $args['term_template']; + } $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { @@ -4202,7 +4213,7 @@ function get_the_taxonomies( $post = 0, $args = array() ) { $links = array(); foreach ( $terms as $term ) { - $links[] = "$term->name"; + $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name ); } if ( $links ) { $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 1fd3cd356c..e7da80a5f7 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -49,6 +49,20 @@ class Tests_Taxonomy extends WP_UnitTestCase { $this->assertEquals( array( 'category', 'post_tag' ), array_keys( $taxes ) ); } + /** + * @group 27238 + */ + public function test_get_the_taxonomies_term_template() { + $post_id = $this->factory->post->create(); + + $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) ); + $this->assertEquals( 'Categories: Uncategorized.', $taxes['category'] ); + + $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) ); + $link = get_category_link( 1 ); + $this->assertEquals( 'Categories: Uncategorized.', $taxes['category'] ); + } + function test_the_taxonomies() { $post_id = $this->factory->post->create(); @@ -57,10 +71,24 @@ class Tests_Taxonomy extends WP_UnitTestCase { $output = ob_get_clean(); $link = get_category_link( 1 ); - $expected = "Categories: Uncategorized."; + $expected = 'Categories: Uncategorized.'; $this->assertEquals( $expected, $output ); } + /** + * @group 27238 + */ + function test_the_taxonomies_term_template() { + $post_id = $this->factory->post->create(); + + $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '%2$s' ) ) ); + $this->assertEquals( 'Categories: Uncategorized.', $output ); + + $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '%2$s' ) ) ); + $link = get_category_link( 1 ); + $this->assertEquals( 'Categories: Uncategorized.', $output ); + } + function test_get_link_taxonomy() { foreach ( get_object_taxonomies('link') as $taxonomy ) { $tax = get_taxonomy($taxonomy);