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.
Built from https://develop.svn.wordpress.org/trunk@32996


git-svn-id: http://core.svn.wordpress.org/trunk@32967 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-06-29 14:16:26 +00:00
parent c91c28d501
commit a3d9af584c
2 changed files with 33 additions and 11 deletions

View File

@ -808,24 +808,46 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
$font_spread = 1; $font_spread = 1;
$font_step = $font_spread / $spread; $font_step = $font_spread / $spread;
$a = array(); // Assemble the data that will be used to generate the tag cloud markup.
$tags_data = array();
foreach ( $tags as $key => $tag ) { foreach ( $tags as $key => $tag ) {
$tag_id = isset( $tag->id ) ? $tag->id : $key;
$count = $counts[ $key ]; $count = $counts[ $key ];
$real_count = $real_counts[ $key ]; $real_count = $real_counts[ $key ];
$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
$tag_name = $tags[ $key ]->name;
if ( $translate_nooped_plural ) { if ( $translate_nooped_plural ) {
$title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
} else { } else {
$title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
} }
$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " . $tags_data[] = array(
str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) ) 'id' => $tag_id,
. $args['unit'] . ";'>$tag_name</a>"; 'url' => '#' != $tag->link ? $tag->link : '#',
'name' => $tag->name,
'title' => $title,
'slug' => $tag->slug,
'real_count' => $real_count,
'class' => 'tag-link-' . $tag_id,
'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step,
);
}
/**
* Filter the data used to generate the tag cloud.
*
* @since 4.3.0
*
* @param array $tags_data An array of term data for term used to generate the tag cloud.
*/
$tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
$a = array();
// generate the output links array
foreach ( $tags_data as $key => $tag_data ) {
$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>";
} }
switch ( $args['format'] ) { switch ( $args['format'] ) {

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.3-alpha-32995'; $wp_version = '4.3-alpha-32996';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.