WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 4 years ago

Last modified 4 years ago

#24656 closed enhancement (fixed)

Tag Cloud generation return object for manipulation

Reported by: ysalame Owned by: boonebgorges
Milestone: 4.3 Priority: normal
Severity: normal Version: 3.5
Component: Taxonomy Keywords: needs-patch good-first-bug dev-feedback
Focuses: Cc:
PR Number:

Description

"wp_generate_tag_cloud" and "wp_tag_cloud" functions in "/wp-includes/category-template.php" only return html or an array of html links as a result.

To manipulate the result you must use filters which on certain cases are not the most efficient way to do it.

I´ve attached a patch to include a new format in the args array as "object". This will create an object with enough data on it so that it can be manipulated accordingly.

ps. first time submiter.

Attachments (3)

24656#category-template.php.patch (3.1 KB) - added by ysalame 6 years ago.
wp_generate_tag_cloud.tests (5.5 KB) - added by welcher 4 years ago.
First pass of unit tests for wp_generate_tag_cloud
24656.diff (2.6 KB) - added by flixos90 4 years ago.
Updated patch according to suggestions by @boonebgorges

Download all attachments as: .zip

Change History (12)

#1 @SergeyBiryukov
6 years ago

  • Keywords has-patch added
  • Version changed from trunk to 3.5

#2 @boonebgorges
5 years ago

  • Keywords needs-refresh added; has-patch removed

#3 follow-ups: @boonebgorges
4 years ago

  • Keywords needs-patch needs-unit-tests good-first-bug added; needs-refresh removed
  • Milestone changed from Awaiting Review to Future Release

ysalame - Thanks for the patch, and sorry for the lack of feedback. Having better control over the customizability of tag cloud markup is a good idea.

If we're going to pass structured data around, let's do it in arrays rather than objects. This is more in keeping with practice throughout WP.

I'm not sure I see the value in *returning* structured data from the function. The purpose of the function is to generate markup; changing it to return structured data in an arbitrary format suggests the need for another function. That being said, I don't think another function is really necessary. How about this: Break the foreach ( $tags as $key => $tag ) loop into two loops. The first one would assemble an array of structured data:

$tag_data = array();
foreach ( $tags as $key => $tag ) {
    $tag_data[] = array(
        'count' => $counts[ $key ],
        'real_count' => $real_counts[ $key ],
        // etc
    );
}

Then pass it through a filter before creating the anchor tags:

$tag_data = apply_filters( 'wp_generate_tag_cloud_data', $tag_data );

$a = array();
foreach ( $tag_data as $tag_d ) {
    $a[] = "<a href='" // ...
}

This way, we don't have to maintain a new function return value, but you get full customizability.

I'd like basic test coverage for wp_generate_tag_cloud() before making these changes.

@welcher
4 years ago

First pass of unit tests for wp_generate_tag_cloud

#4 in reply to: ↑ 3 @welcher
4 years ago

  • Keywords dev-feedback added; needs-unit-tests removed

Replying to boonebgorges:

I'd like basic test coverage for wp_generate_tag_cloud() before making these changes.

I've added some initial tests and would love any feedback or comments!

@flixos90
4 years ago

Updated patch according to suggestions by @boonebgorges

#5 in reply to: ↑ 3 @flixos90
4 years ago

@boonebgorges:
In this patch I implemented your suggestions regarding using arrays for the tag data as well as splitting the foreach loop.

If we're going to pass structured data around, let's do it in arrays rather than objects. This is more in keeping with practice throughout WP.

I'm not sure I see the value in *returning* structured data from the function. The purpose of the function is to generate markup; changing it to return structured data in an arbitrary format suggests the need for another function. That being said, I don't think another function is really necessary. How about this: Break the foreach ( $tags as $key => $tag ) loop into two loops.

#6 @boonebgorges
4 years ago

welcher - Thanks for the tests!

#7 @boonebgorges
4 years ago

In 32995:

Add some tests for wp_generate_tag_cloud().

Props welcher.
See #24656.

#8 @boonebgorges
4 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 32996:

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.

#9 @boonebgorges
4 years ago

  • Milestone changed from Future Release to 4.3
Note: See TracTickets for help on using tickets.