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. git-svn-id: https://develop.svn.wordpress.org/trunk@32996 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
85ce3f7eba
commit
10d81a3fe5
|
@ -808,24 +808,46 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
|
|||
$font_spread = 1;
|
||||
$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 ) {
|
||||
$tag_id = isset( $tag->id ) ? $tag->id : $key;
|
||||
|
||||
$count = $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 ) {
|
||||
$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 {
|
||||
$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: " .
|
||||
str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) )
|
||||
. $args['unit'] . ";'>$tag_name</a>";
|
||||
$tags_data[] = array(
|
||||
'id' => $tag_id,
|
||||
'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'] ) {
|
||||
|
|
|
@ -164,7 +164,52 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
|
|||
);
|
||||
}
|
||||
|
||||
public function test_topic_count_text() {
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
$posts = $this->factory->post->create_many( 2 );
|
||||
|
||||
wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
|
||||
wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
|
||||
|
||||
$term_objects = $this->retrieve_terms( array(
|
||||
'include' => $terms,
|
||||
), 'wptests_tax' );
|
||||
|
||||
$actual = wp_generate_tag_cloud( $term_objects, array(
|
||||
'format' => 'array',
|
||||
'topic_count_text' => array(
|
||||
'singular' => 'Term has %s post',
|
||||
'plural' => 'Term has %s posts',
|
||||
'domain' => 'foo',
|
||||
'context' => 'bar',
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertContains( "title='Term has 1 post'", $actual[0] );
|
||||
$this->assertContains( "title='Term has 2 posts'", $actual[1] );
|
||||
}
|
||||
|
||||
public function test_topic_count_text_callback() {
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
$posts = $this->factory->post->create_many( 2 );
|
||||
|
||||
wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
|
||||
wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
|
||||
|
||||
$term_objects = $this->retrieve_terms( array(
|
||||
'include' => $terms,
|
||||
), 'wptests_tax' );
|
||||
|
||||
$actual = wp_generate_tag_cloud( $term_objects, array(
|
||||
'format' => 'array',
|
||||
'topic_count_text_callback' => array( $this, 'topic_count_text_callback' ),
|
||||
) );
|
||||
|
||||
$this->assertContains( "title='1 foo'", $actual[0] );
|
||||
$this->assertContains( "title='2 foo'", $actual[1] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method retrieve the created terms.
|
||||
|
@ -175,9 +220,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function retrieve_terms( $get_terms_args ) {
|
||||
|
||||
$terms = get_terms( array( 'post_tag' ), $get_terms_args );
|
||||
protected function retrieve_terms( $get_terms_args, $taxonomy = 'post_tag' ) {
|
||||
$terms = get_terms( array( $taxonomy ), $get_terms_args );
|
||||
|
||||
$tags = array();
|
||||
foreach ( $terms as $term ) {
|
||||
|
@ -189,4 +233,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
|
|||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
public function topic_count_text_callback( $real_count, $tag, $args ) {
|
||||
return sprintf( '%s foo', $real_count );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue