WordPress.org

Make WordPress Core

Ticket #27238: 27238.6.patch

File 27238.6.patch, 5.0 KB (added by dlh, 6 years ago)
  • src/wp-includes/taxonomy.php

     
    41074107 * post without specifying the Post ID. You can also use it outside the Loop to
    41084108 * display the taxonomies for a specific post.
    41094109 *
    4110  * The available defaults are:
    4111  * 'post' : default is 0. The post ID to get taxonomies of.
    4112  * 'before' : default is empty string. Display before taxonomies list.
    4113  * 'sep' : default is empty string. Separate every taxonomy with value in this.
    4114  * 'after' : default is empty string. Display this after the taxonomies list.
    4115  * 'template' : The template to use for displaying the taxonomy terms.
    4116  *
    41174110 * @since 2.5.0
    4118  * @uses get_the_taxonomies()
    41194111 *
    4120  * @param array $args Override the defaults.
     4112 * @param array $args {
     4113 *     Arguments about which post to use and how to format the output.
     4114 *
     4115 *     @type  int|WP_Post $post          Post ID or object to get taxonomies of. Default current post.
     4116 *     @type  string      $before        Displays before the taxonomies. Default empty string.
     4117 *     @type  string      $sep           Separates each taxonomy. Default is a space.
     4118 *     @type  string      $after         Displays after the taxonomies. Default empty string.
     4119 *     @type  string      $template      Template for displaying a taxonomy label and list of
     4120 *                                       terms. Default is "Label: Terms."
     4121 *     @type  string      $term_template Template for displaying a single term in the list.
     4122 *                                       Default is the term name linked to its archive.
     4123 * }
    41214124 */
    41224125function the_taxonomies( $args = array() ) {
    41234126        $defaults = array(
     
    41254128                'before' => '',
    41264129                'sep' => ' ',
    41274130                'after' => '',
    4128                 /* translators: %s: taxonomy label, %l: list of term links */
    4129                 'template' => __( '%s: %l.' )
     4131                /* translators: %s: taxonomy label, %l: list of terms formatted as $term_template */
     4132                'template' => __( '%s: %l.' ),
     4133                'term_template' => '<a href="%1$s">%2$s</a>',
    41304134        );
    41314135
    41324136        $r = wp_parse_args( $args, $defaults );
     
    41504154        $post = get_post( $post );
    41514155
    41524156        $args = wp_parse_args( $args, array(
    4153                 /* translators: %s: taxonomy label, %l: list of term links */
     4157                /* translators: %s: taxonomy label, %l: list of terms formatted as $term_template */
    41544158                'template' => __( '%s: %l.' ),
     4159                'term_template' => '<a href="%1$s">%2$s</a>',
    41554160        ) );
    41564161
    41574162        $taxonomies = array();
     
    41714176                if ( empty( $t['template'] ) ) {
    41724177                        $t['template'] = $args['template'];
    41734178                }
     4179                if ( empty( $t['term_template'] ) ) {
     4180                        $t['term_template'] = $args['term_template'];
     4181                }
    41744182
    41754183                $terms = get_object_term_cache( $post->ID, $taxonomy );
    41764184                if ( false === $terms ) {
     
    41794187                $links = array();
    41804188
    41814189                foreach ( $terms as $term ) {
    4182                         $links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>";
     4190                        $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link($term) ), $term->name );
    41834191                }
    41844192                if ( $links ) {
    41854193                        $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
  • tests/phpunit/tests/taxonomy.php

     
    4949                $this->assertEquals( array( 'category', 'post_tag' ), array_keys( $taxes ) );
    5050        }
    5151
     52        function test_get_the_taxonomies_term_template() {
     53                $post_id = $this->factory->post->create();
     54
     55                $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) );
     56                $this->assertEquals( 'Categories: Uncategorized.', $taxes['category'] );
     57
     58                $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '<span class="foo"><a href="%1$s">%2$s</a></span>' ) );
     59                $link = get_category_link( 1 );
     60                $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $taxes['category'] );
     61        }
     62
    5263        function test_the_taxonomies() {
    5364                $post_id = $this->factory->post->create();
    5465
     
    5768                $output = ob_get_clean();
    5869
    5970                $link = get_category_link( 1 );
    60                 $expected = "Categories: <a href='$link'>Uncategorized</a>.";
     71                $expected = 'Categories: <a href="' . $link . '">Uncategorized</a>.';
    6172                $this->assertEquals( $expected, $output );
    6273        }
    6374
     75        function test_the_taxonomies_term_template() {
     76                $post_id = $this->factory->post->create();
     77
     78                $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '%2$s' ) ) );
     79                $this->assertEquals( 'Categories: Uncategorized.', $output );
     80
     81                $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '<span class="foo"><a href="%1$s">%2$s</a></span>' ) ) );
     82                $link = get_category_link( 1 );
     83                $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $output );
     84        }
     85
    6486        function test_get_link_taxonomy() {
    6587                foreach ( get_object_taxonomies('link') as $taxonomy ) {
    6688                        $tax = get_taxonomy($taxonomy);