WordPress.org

Make WordPress Core

Changeset 32996


Ignore:
Timestamp:
06/29/2015 02:15:59 PM (4 years ago)
Author:
boonebgorges
Message:

Introduce 'wp_generate_tag_cloud_data' filter.

This filter allows developers to modify the data that is used to create tag
clouds, without having to manipulate the tag cloud markup directly.

As part of the refactor, this changeset also adds a few unit tests for the way
wp_generate_tag_cloud() generates the 'title' attribute, as well as
improvements to output escaping.

Props flixos90, ysalame.
Fixes #24656.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r32568 r32996  
    809809    $font_step = $font_spread / $spread;
    810810
    811     $a = array();
    812 
     811    // Assemble the data that will be used to generate the tag cloud markup.
     812    $tags_data = array();
    813813    foreach ( $tags as $key => $tag ) {
     814        $tag_id = isset( $tag->id ) ? $tag->id : $key;
     815
    814816        $count = $counts[ $key ];
    815817        $real_count = $real_counts[ $key ];
    816         $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
    817         $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
    818         $tag_name = $tags[ $key ]->name;
    819818
    820819        if ( $translate_nooped_plural ) {
    821             $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
     820            $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
    822821        } else {
    823             $title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
    824         }
    825 
    826         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " .
    827             str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) )
    828             . $args['unit'] . ";'>$tag_name</a>";
     822            $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
     823        }
     824
     825        $tags_data[] = array(
     826            'id'         => $tag_id,
     827            'url'        => '#' != $tag->link ? $tag->link : '#',
     828            'name'       => $tag->name,
     829            'title'      => $title,
     830            'slug'       => $tag->slug,
     831            'real_count' => $real_count,
     832            'class'      => 'tag-link-' . $tag_id,
     833            'font_size'  => $args['smallest'] + ( $count - $min_count ) * $font_step,
     834        );
     835    }
     836
     837    /**
     838     * Filter the data used to generate the tag cloud.
     839     *
     840     * @since 4.3.0
     841     *
     842     * @param array $tags_data An array of term data for term used to generate the tag cloud.
     843     */
     844    $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
     845
     846    $a = array();
     847
     848    // generate the output links array
     849    foreach ( $tags_data as $key => $tag_data ) {
     850        $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "' class='" . esc_attr( $tag_data['class'] ) . "' title='" . esc_attr( $tag_data['title'] ) . "' style='font-size: " . esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ) . ";'>" . esc_html( $tag_data['name'] ) . "</a>";
    829851    }
    830852
  • trunk/tests/phpunit/tests/term/wpGenerateTagCloud.php

    r32995 r32996  
    165165    }
    166166
    167 
     167    public function test_topic_count_text() {
     168        register_taxonomy( 'wptests_tax', 'post' );
     169        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     170        $posts = $this->factory->post->create_many( 2 );
     171
     172        wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
     173        wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     174
     175        $term_objects = $this->retrieve_terms( array(
     176            'include' => $terms,
     177        ), 'wptests_tax' );
     178
     179        $actual = wp_generate_tag_cloud( $term_objects, array(
     180            'format' => 'array',
     181            'topic_count_text' => array(
     182                'singular' => 'Term has %s post',
     183                'plural' => 'Term has %s posts',
     184                'domain' => 'foo',
     185                'context' => 'bar',
     186            ),
     187        ) );
     188
     189        $this->assertContains( "title='Term has 1 post'", $actual[0] );
     190        $this->assertContains( "title='Term has 2 posts'", $actual[1] );
     191    }
     192
     193    public function test_topic_count_text_callback() {
     194        register_taxonomy( 'wptests_tax', 'post' );
     195        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     196        $posts = $this->factory->post->create_many( 2 );
     197
     198        wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
     199        wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     200
     201        $term_objects = $this->retrieve_terms( array(
     202            'include' => $terms,
     203        ), 'wptests_tax' );
     204
     205        $actual = wp_generate_tag_cloud( $term_objects, array(
     206            'format' => 'array',
     207            'topic_count_text_callback' => array( $this, 'topic_count_text_callback' ),
     208        ) );
     209
     210        $this->assertContains( "title='1 foo'", $actual[0] );
     211        $this->assertContains( "title='2 foo'", $actual[1] );
     212    }
    168213
    169214    /**
     
    176221     * @return array
    177222     */
    178     protected function retrieve_terms( $get_terms_args ) {
    179 
    180         $terms = get_terms( array( 'post_tag' ), $get_terms_args );
     223    protected function retrieve_terms( $get_terms_args, $taxonomy = 'post_tag' ) {
     224        $terms = get_terms( array( $taxonomy ), $get_terms_args );
    181225
    182226        $tags = array();
     
    190234        return $tags;
    191235    }
     236
     237    public function topic_count_text_callback( $real_count, $tag, $args ) {
     238        return sprintf( '%s foo', $real_count );
     239    }
    192240}
Note: See TracChangeset for help on using the changeset viewer.