';
- $output = apply_filters('wp_list_categories', $output);
+ $output = apply_filters( 'wp_list_categories', $output, $args );
if ( $echo )
echo $output;
@@ -337,90 +490,190 @@ function wp_list_categories($args = '') {
return $output;
}
+/**
+ * Display tag cloud.
+ *
+ * The text size is set by the 'smallest' and 'largest' arguments, which will
+ * use the 'unit' argument value for the CSS text size unit. The 'format'
+ * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
+ * 'format' argument will separate tags with spaces. The list value for the
+ * '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 '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'.
+ *
+ * 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 '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
+ * other ignored, if they are both set.
+ *
+ * @since 2.3.0
+ *
+ * @param array|string $args Optional. Override default arguments.
+ * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
+ */
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
- 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
- 'exclude' => '', 'include' => ''
+ 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
+ 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
);
$args = wp_parse_args( $args, $defaults );
- $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
+ $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;
- $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
+ foreach ( $tags as $key => $tag ) {
+ if ( 'edit' == $args['link'] )
+ $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
+ else
+ $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
+ if ( is_wp_error( $link ) )
+ return false;
- if ( is_wp_error( $return ) )
- return false;
+ $tags[ $key ]->link = $link;
+ $tags[ $key ]->id = $tag->term_id;
+ }
+
+ $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
$return = apply_filters( 'wp_tag_cloud', $return, $args );
- if ( 'array' == $args['format'] )
+ if ( 'array' == $args['format'] || empty($args['echo']) )
return $return;
echo $return;
}
-// $tags = prefetched tag array ( get_tags() )
-// $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
-// $args['orderby'] = 'name', 'count'
+/**
+ * Default text for tooltip for tag links
+ *
+ * @param integer $count number of posts with that tag
+ * @return string text for the tooltip of a tag link.
+ */
+function default_topic_count_text( $count ) {
+ return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
+}
+
+/**
+ * Default topic count scaling for tag links
+ *
+ * @param integer $count number of posts with that tag
+ * @return integer scaled count
+ */
+function default_topic_count_scale( $count ) {
+ return round(log10($count + 1) * 100);
+}
+
+/**
+ * Generates a tag cloud (heatmap) from provided data.
+ *
+ * The text size is set by the 'smallest' and 'largest' arguments, which will
+ * use the 'unit' argument value for the CSS text size unit. The 'format'
+ * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
+ * 'format' argument will separate tags with spaces. The list value for the
+ * '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.
+ * 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
+ * 'RAND'.
+ *
+ * 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_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.
+ *
+ * @todo Complete functionality.
+ * @since 2.3.0
+ *
+ * @param array $tags List of tags.
+ * @param string|array $args Optional, override default arguments.
+ * @return string
+ */
function wp_generate_tag_cloud( $tags, $args = '' ) {
- global $wp_rewrite;
$defaults = array(
- 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
- 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
+ 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
+ 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
+ 'topic_count_text_callback' => 'default_topic_count_text',
+ '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);
+ extract( $args );
- if ( !$tags )
+ if ( empty( $tags ) )
return;
- $counts = $tag_links = array();
- foreach ( (array) $tags as $tag ) {
- $counts[$tag->name] = $tag->count;
- $tag_links[$tag->name] = get_tag_link( $tag->term_id );
- if ( is_wp_error( $tag_links[$tag->name] ) )
- return $tag_links[$tag->name];
- $tag_ids[$tag->name] = $tag->term_id;
+
+ $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
+ if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
+ $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' );
+
+ if ( 'DESC' == $order )
+ $tags = array_reverse( $tags, true );
+ }
}
- $min_count = min($counts);
- $spread = max($counts) - $min_count;
+ if ( $number > 0 )
+ $tags = array_slice($tags, 0, $number);
+
+ $counts = array();
+ $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;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
- if ( $font_spread <= 0 )
+ if ( $font_spread < 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
- // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
- if ( 'name' == $orderby )
- uksort($counts, 'strnatcasecmp');
- else
- asort($counts);
-
- if ( 'DESC' == $order )
- $counts = array_reverse( $counts, true );
- elseif ( 'RAND' == $order ) {
- $keys = array_rand( $counts, count($counts) );
- foreach ( $keys as $key )
- $temp[$key] = $counts[$key];
- $counts = $temp;
- unset($temp);
- }
-
$a = array();
- $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
-
- foreach ( $counts as $tag => $count ) {
- $tag_id = $tag_ids[$tag];
- $tag_link = clean_url($tag_links[$tag]);
- $a[] = "$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";
}
switch ( $format ) :
@@ -429,82 +682,401 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
break;
case 'list' :
$return = "
\n\t
";
- $return .= join("
\n\t
", $a);
+ $return .= join( "
\n\t
", $a );
$return .= "
\n
\n";
break;
default :
- $return = join("\n", $a);
+ $return = join( $separator, $a );
break;
endswitch;
- return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
+ if ( $filter )
+ return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
+ 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
//
+/**
+ * Retrieve HTML list content for category list.
+ *
+ * @uses Walker_Category to create HTML list content.
+ * @since 2.1.0
+ * @see Walker_Category::walk() for parameters and return description.
+ */
function walk_category_tree() {
- $walker = new Walker_Category;
$args = func_get_args();
- return call_user_func_array(array(&$walker, 'walk'), $args);
+ // the user's options are the third parameter
+ if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
+ $walker = new Walker_Category;
+ else
+ $walker = $args[2]['walker'];
+
+ return call_user_func_array(array( &$walker, 'walk' ), $args );
}
+/**
+ * Retrieve HTML dropdown (select) content for category list.
+ *
+ * @uses Walker_CategoryDropdown to create HTML dropdown content.
+ * @since 2.1.0
+ * @see Walker_CategoryDropdown::walk() for parameters and return description.
+ */
function walk_category_dropdown_tree() {
- $walker = new Walker_CategoryDropdown;
$args = func_get_args();
- return call_user_func_array(array(&$walker, 'walk'), $args);
+ // the user's options are the third parameter
+ if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
+ $walker = new Walker_CategoryDropdown;
+ else
+ $walker = $args[2]['walker'];
+
+ 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 {
+ /**
+ * @see Walker::$tree_type
+ * @since 2.1.0
+ * @var string
+ */
+ var $tree_type = 'category';
+
+ /**
+ * @see Walker::$db_fields
+ * @since 2.1.0
+ * @todo Decouple this
+ * @var array
+ */
+ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
+
+ /**
+ * @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 Will only append content if style argument value is 'list'.
+ */
+ function start_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent
\n";
+ }
+
+ /**
+ * @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 Will only append content if style argument value is 'list'.
+ */
+ function end_lvl( &$output, $depth = 0, $args = array() ) {
+ if ( 'list' != $args['style'] )
+ return;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent