Make WordPress Core

Ticket #27238: 27238.5.patch

File 27238.5.patch, 5.6 KB (added by dlh, 10 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()
     4111 * @see get_the_taxonomies() for detail about the $template and $term_template arguments.
    41194112 *
    4120  * @param array $args Override the defaults.
     4113 * @param array $args {
     4114 *     Arguments about which post to use and how to format the output.
     4115 *
     4116 *     @type  int|WP_Post $post          Post ID or object to get taxonomies of. Default current post.
     4117 *     @type  string      $before        Displays before the taxonomies. Default empty string.
     4118 *     @type  string      $sep           Separates each taxonomy. Default is a space.
     4119 *     @type  string      $after         Displays after the taxonomies. Default empty string.
     4120 *     @type  string      $template      Template for a taxonomy label and its terms.
     4121 *     @type  string      $term_template Template for a term.
     4122 * }
    41214123 */
    41224124function the_taxonomies( $args = array() ) {
    41234125        $defaults = array(
     
    41254127                'before' => '',
    41264128                'sep' => ' ',
    41274129                'after' => '',
    4128                 /* translators: %s: taxonomy label, %l: list of term links */
    4129                 'template' => __( '%s: %l.' )
     4130                /* translators: %s: taxonomy label, %l: list of terms formatted as $term_template */
     4131                'template' => __( '%s: %l.' ),
     4132                'term_template' => '<a href="%1$s">%2$s</a>',
    41304133        );
    41314134
    41324135        $r = wp_parse_args( $args, $defaults );
     
    41434146 * @since 2.5.0
    41444147 *
    41454148 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
    4146  * @param array $args Override the defaults.
     4149 * @param array $args {
     4150 *     Arguments about how to format the taxonomy name and its terms.
     4151 *
     4152 *     @type  string      $template      Template for displaying a taxonomy label and list of
     4153 *                                       terms. Default is "Label: Terms."
     4154 *     @type  string      $term_template Template for displaying a single term in the list.
     4155 *                                       Default is the term name linked to its archive.
     4156 * }
    41474157 * @return array List of taxonomies.
    41484158 */
    41494159function get_the_taxonomies( $post = 0, $args = array() ) {
    41504160        $post = get_post( $post );
    41514161
    41524162        $args = wp_parse_args( $args, array(
    4153                 /* translators: %s: taxonomy label, %l: list of term links */
     4163                /* translators: %s: taxonomy label, %l: list of terms formatted as $term_template */
    41544164                'template' => __( '%s: %l.' ),
     4165                'term_template' => '<a href="%1$s">%2$s</a>',
    41554166        ) );
    41564167
    41574168        $taxonomies = array();
     
    41714182                if ( empty( $t['template'] ) ) {
    41724183                        $t['template'] = $args['template'];
    41734184                }
     4185                if ( empty( $t['term_template'] ) ) {
     4186                        $t['term_template'] = $args['term_template'];
     4187                }
    41744188
    41754189                $terms = get_object_term_cache( $post->ID, $taxonomy );
    41764190                if ( false === $terms ) {
     
    41794193                $links = array();
    41804194
    41814195                foreach ( $terms as $term ) {
    4182                         $links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>";
     4196                        $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link($term) ), $term->name );
    41834197                }
    41844198                if ( $links ) {
    41854199                        $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);