';
- $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;
@@ -337,90 +549,233 @@ 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' 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
+ * 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', 'post_type' => '', '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_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
+ 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
+
+ /**
+ * 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'] )
+ 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 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' 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.
+ *
+ * @todo Complete functionality.
+ * @since 2.3.0
+ *
+ * @param array $tags List of tags.
+ * @param string|array $args Optional, override default arguments.
+ * @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' => 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' => null, 'topic_count_text_callback' => null,
+ 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
);
+
$args = wp_parse_args( $args, $defaults );
- extract($args);
+ extract( $args, EXTR_SKIP );
- if ( !$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;
+ $return = ( 'array' === $format ) ? array() : '';
+
+ if ( empty( $tags ) ) {
+ return $return;
}
- $min_count = min($counts);
- $spread = max($counts) - $min_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' );
+ }
+
+ /**
+ * 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 ( $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' );
+ }
+
+ if ( 'DESC' === $order ) {
+ $tags = array_reverse( $tags, true );
+ }
+ }
+ }
+
+ 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 ( $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;
+
+ if ( $translate_nooped_plural ) {
+ $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
+ } else {
+ $title_attribute = call_user_func( $topic_count_text_callback, $real_count, $tag, $args );
+ }
- foreach ( $counts as $tag => $count ) {
- $tag_id = $tag_ids[$tag];
- $tag_link = clean_url($tag_links[$tag]);
- $a[] = "$tag";
+ $a[] = "$tag_name";
}
switch ( $format ) :
@@ -429,82 +784,477 @@ 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 ) {
+ /**
+ * 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
+ 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 {
+ /**
+ * 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