diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 459876f509..9f4e1ac74a 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -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[] = "$tag_name"; + $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[] = "" . esc_html( $tag_data['name'] ) . ""; } switch ( $args['format'] ) { diff --git a/tests/phpunit/tests/term/wpGenerateTagCloud.php b/tests/phpunit/tests/term/wpGenerateTagCloud.php index c7c79011b4..4a931ec4e2 100644 --- a/tests/phpunit/tests/term/wpGenerateTagCloud.php +++ b/tests/phpunit/tests/term/wpGenerateTagCloud.php @@ -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 ); + } }