';
- $output = apply_filters( 'wp_list_categories', $output );
+ /**
+ * Filter the HTML output of a taxonomy list.
+ *
+ * @since 2.1.0
+ *
+ * @param string $output HTML output.
+ * @param array $args An array of taxonomy-listing arguments.
+ */
+ $output = apply_filters( 'wp_list_categories', $output, $args );
if ( $echo )
echo $output;
@@ -506,8 +565,14 @@ function wp_list_categories( $args = '' ) {
* The 'number' argument is how many tags to return. By default, the limit will
* be to return the top 45 tags in the tag cloud list.
*
- * The 'topic_count_text_callback' argument is a function, which, given the count
- * of the posts with that tag, returns a text for the tooltip of the tag link.
+ * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
+ * text for the tooltip of the tag link.
+ *
+ * The 'topic_count_text_callback' argument is a function, which given the count
+ * of the posts with that tag returns a text for the tooltip of the tag link.
+ *
+ * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
+ * passed to edit.php for the popular tags edit links.
*
* The 'exclude' and 'include' arguments are used for the {@link get_tags()}
* function. Only one should be used, because only one will be used and the
@@ -521,21 +586,21 @@ function wp_list_categories( $args = '' ) {
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
- 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
- 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
+ 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
+ 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
);
$args = wp_parse_args( $args, $defaults );
$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
- if ( empty( $tags ) )
+ if ( empty( $tags ) || is_wp_error( $tags ) )
return;
foreach ( $tags as $key => $tag ) {
if ( 'edit' == $args['link'] )
- $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
+ $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
else
- $link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
+ $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
if ( is_wp_error( $link ) )
return false;
@@ -545,6 +610,14 @@ function wp_tag_cloud( $args = '' ) {
$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
+ /**
+ * Filter the tag cloud output.
+ *
+ * @since 2.3.0
+ *
+ * @param string $return HTML output of the tag cloud.
+ * @param array $args An array of tag cloud arguments.
+ */
$return = apply_filters( 'wp_tag_cloud', $return, $args );
if ( 'array' == $args['format'] || empty($args['echo']) )
@@ -554,13 +627,13 @@ function wp_tag_cloud( $args = '' ) {
}
/**
- * Default text for tooltip for tag links
+ * Default topic count scaling for tag links
*
* @param integer $count number of posts with that tag
- * @return string text for the tooltip of a tag link.
+ * @return integer scaled count
*/
-function default_topic_count_text( $count ) {
- return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
+function default_topic_count_scale( $count ) {
+ return round(log10($count + 1) * 100);
}
/**
@@ -573,8 +646,9 @@ function default_topic_count_text( $count ) {
* 'format' argument will format the tags in a UL HTML list. The array value for
* the 'format' argument will return in PHP array type format.
*
- * The 'tag_cloud_sort' filter allows you to override the sorting done
- * by the 'orderby' argument; passed to the filter: $tags array and $args array.
+ * The 'tag_cloud_sort' filter allows you to override the sorting.
+ * Passed to the filter: $tags array and $args array, has to return the $tags array
+ * after sorting it.
*
* The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
* The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
@@ -583,66 +657,97 @@ function default_topic_count_text( $count ) {
* The 'number' argument is how many tags to return. By default, the limit will
* be to return the entire tag cloud list.
*
+ * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
+ * text for the tooltip of the tag link.
+ *
* The 'topic_count_text_callback' argument is a function, which given the count
- * of the posts with that tag returns a text for the tooltip of the tag link.
+ * of the posts with that tag returns a text for the tooltip of the tag link.
*
* @todo Complete functionality.
* @since 2.3.0
*
* @param array $tags List of tags.
* @param string|array $args Optional, override default arguments.
- * @return string
+ * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
*/
function wp_generate_tag_cloud( $tags, $args = '' ) {
- global $wp_rewrite;
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
- 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
- 'topic_count_text_callback' => 'default_topic_count_text',
- 'filter' => 1,
+ 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
+ 'topic_count_text' => null, 'topic_count_text_callback' => null,
+ 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
);
- if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
- $body = 'return sprintf (
- _n('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count),
- number_format_i18n( $count ));';
- $args['topic_count_text_callback'] = create_function('$count', $body);
- }
-
$args = wp_parse_args( $args, $defaults );
+ extract( $args, EXTR_SKIP );
- extract( $args );
+ $return = ( 'array' === $format ) ? array() : '';
- if ( empty( $tags ) )
- return;
+ if ( empty( $tags ) ) {
+ return $return;
+ }
- // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
- if ( 'name' == $orderby )
- uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
- else
- uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
+ // Juggle topic count tooltips:
+ if ( isset( $args['topic_count_text'] ) ) {
+ // First look for nooped plural support via topic_count_text.
+ $translate_nooped_plural = $args['topic_count_text'];
+ } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
+ // Look for the alternative callback style. Ignore the previous default.
+ if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
+ $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
+ } else {
+ $translate_nooped_plural = false;
+ }
+ } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
+ // If no callback exists, look for the old-style single_text and multiple_text arguments.
+ $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
+ } else {
+ // This is the default for when no callback, plural, or argument is passed in.
+ $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
+ }
- $tags = apply_filters( 'tag_cloud_sort', $tags, $args );
+ /**
+ * Filter how the items in a tag cloud are sorted.
+ *
+ * @since 2.8.0
+ *
+ * @param array $tags Ordered array of terms.
+ * @param array $args An array of tag cloud arguments.
+ */
+ $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
+ if ( empty( $tags_sorted ) ) {
+ return $return;
+ }
- if ( 'DESC' == $order )
- $tags = array_reverse( $tags, true );
- elseif ( 'RAND' == $order ) {
- $keys = (array) array_rand( $tags, count( $tags ) );
- $temp = array();
- foreach ( $keys as $key )
- $temp[$key] = $tags[$key];
+ if ( $tags_sorted !== $tags ) {
+ $tags = $tags_sorted;
+ unset( $tags_sorted );
+ } else {
+ if ( 'RAND' === $order ) {
+ shuffle( $tags );
+ } else {
+ // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
+ if ( 'name' === $orderby ) {
+ uasort( $tags, '_wp_object_name_sort_cb' );
+ } else {
+ uasort( $tags, '_wp_object_count_sort_cb' );
+ }
- $tags = $temp;
- $temp = null;
- unset( $temp );
+ if ( 'DESC' === $order ) {
+ $tags = array_reverse( $tags, true );
+ }
+ }
}
if ( $number > 0 )
$tags = array_slice($tags, 0, $number);
$counts = array();
- foreach ( (array) $tags as $key => $tag )
- $counts[ $key ] = $tag->count;
+ $real_counts = array(); // For the alt tag
+ foreach ( (array) $tags as $key => $tag ) {
+ $real_counts[ $key ] = $tag->count;
+ $counts[ $key ] = $topic_count_scale_callback($tag->count);
+ }
$min_count = min( $counts );
$spread = max( $counts ) - $min_count;
@@ -655,15 +760,21 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
$a = array();
- $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
-
foreach ( $tags as $key => $tag ) {
$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;
- $a[] = "$tag_name";
}
@@ -677,16 +788,54 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
$return .= "
\n\n";
break;
default :
- $return = join( "\n", $a );
+ $return = join( $separator, $a );
break;
endswitch;
- if ( $filter )
+ if ( $filter ) {
+ /**
+ * Filter the generated output of a tag cloud.
+ *
+ * The filter is only evaluated if a true value is passed
+ * to the $filter argument in wp_generate_tag_cloud().
+ *
+ * @since 2.3.0
+ *
+ * @see wp_generate_tag_cloud()
+ *
+ * @param array|string $return String containing the generated HTML tag cloud output
+ * or an array of tag links if the 'format' argument
+ * equals 'array'.
+ * @param array $tags An array of terms used in the tag cloud.
+ * @param array $args An array of wp_generate_tag_cloud() arguments.
+ */
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
- else
+ }
+
+ else
return $return;
}
+/**
+ * Callback for comparing objects based on name
+ *
+ * @since 3.1.0
+ * @access private
+ */
+function _wp_object_name_sort_cb( $a, $b ) {
+ return strnatcasecmp( $a->name, $b->name );
+}
+
+/**
+ * Callback for comparing objects based on count
+ *
+ * @since 3.1.0
+ * @access private
+ */
+function _wp_object_count_sort_cb( $a, $b ) {
+ return ( $a->count > $b->count );
+}
+
//
// Helper functions
//
@@ -727,6 +876,234 @@ function walk_category_dropdown_tree() {
return call_user_func_array(array( &$walker, 'walk' ), $args );
}
+/**
+ * Create HTML list of categories.
+ *
+ * @package WordPress
+ * @since 2.1.0
+ * @uses Walker
+ */
+class Walker_Category extends Walker {
+ /**
+ * What the class handles.
+ *
+ * @see Walker::$tree_type
+ * @since 2.1.0
+ * @var string
+ */
+ var $tree_type = 'category';
+
+ /**
+ * Database fields to use.
+ *
+ * @see Walker::$db_fields
+ * @since 2.1.0
+ * @todo Decouple this
+ * @var array
+ */
+ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
+
+ /**
+ * Starts the list before the elements are added.
+ *
+ * @see Walker::start_lvl()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
+ * @see wp_list_categories()
+ */
+ function start_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent
\n";
+ }
+
+ /**
+ * Ends the list of after the elements are added.
+ *
+ * @see Walker::end_lvl()
+ *
+ * @since 2.1.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
+ * @wsee wp_list_categories()
+ */
+ function end_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent