Tag clouds from mdawaffe. fixes #4129

git-svn-id: http://svn.automattic.com/wordpress/trunk@5234 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-04-10 19:52:15 +00:00
parent f32f812fa9
commit 77cdde3f87
3 changed files with 112 additions and 15 deletions

View File

@ -309,6 +309,90 @@ function wp_list_categories($args = '') {
echo apply_filters('wp_list_categories', $output);
}
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => ''
);
$args = wp_parse_args( $args, $defaults );
$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
if ( empty($tags) )
return;
$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
echo apply_filters( 'wp_tag_cloud', $return, $args );
}
// $tags = prefetched tag array ( get_tags() )
// $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
// $args['orderby'] = 'name', 'count'
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'
);
$args = wp_parse_args( $args, $defaults );
extract($args);
if ( !$tags )
return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) {
$counts[$tag->cat_name] = $tag->tag_count;
$tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID );
}
$min_count = min($counts);
$spread = max($counts) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
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( $tag_counts, true );
$a = array();
$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $counts as $tag => $count ) {
$tag_link = clean_url($tag_links[$tag]);
$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
$a[] = "<a href='$tag_link' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
( $smallest + ( ( $count - $min_count ) * $font_step ) )
. "$unit;'>$tag</a>";
}
switch ( $format ) :
case 'array' :
$return =& $a;
break;
case 'list' :
$return = "<ul class='wp-tag-cloud'>\n\t<li>";
$return .= join("</li>\n\t<li>", $a);
$return .= "</li>\n</ul>\n";
break;
default :
$return = join("\n", $a);
break;
endswitch;
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
}
//
// Helper functions
//

View File

@ -350,26 +350,21 @@ function _get_category_hierarchy() {
function &get_tags($args = '') {
global $wpdb, $category_links;
if ( is_array($args) )
$r = &$args;
else
parse_str($args, $r);
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => '', 'include' => '',
'number' => '');
$r = array_merge($defaults, $r);
if ( 'count' == $r['orderby'] )
$r['orderby'] = 'category_count';
$args = wp_parse_args( $args, $defaults );
if ( 'count' == $args['orderby'] )
$args['orderby'] = 'tag_count';
else
$r['orderby'] = "cat_" . $r['orderby']; // restricts order by to cat_ID and cat_name fields
$r['number'] = (int) $r['number'];
extract($r);
$args['orderby'] = "cat_" . $args['orderby']; // restricts order by to cat_ID and cat_name fields
$args['number'] = (int) $args['number'];
extract($args);
$key = md5( serialize( $r ) );
$key = md5( serialize( $args ) );
if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
if ( isset( $cache[ $key ] ) )
return apply_filters('get_tags', $cache[$key], $r);
return apply_filters('get_tags', $cache[$key], $args);
$where = 'cat_ID > 0';
$inclusions = '';
@ -406,7 +401,7 @@ function &get_tags($args = '') {
if (!empty($exclusions))
$exclusions .= ')';
$exclusions = apply_filters('list_tags_exclusions', $exclusions, $r );
$exclusions = apply_filters('list_tags_exclusions', $exclusions, $args );
$where .= $exclusions;
if ( $hide_empty )
@ -427,7 +422,7 @@ function &get_tags($args = '') {
$cache[ $key ] = $tags;
wp_cache_set( 'get_tags', $cache, 'category' );
$tags = apply_filters('get_tags', $tags, $r);
$tags = apply_filters('get_tags', $tags, $args);
return $tags;
}

View File

@ -1471,4 +1471,22 @@ function smilies_init() {
}
}
function wp_parse_args( $args, $defaults = '' ) {
if ( is_array($args) ) :
$r =& $args;
else :
parse_str( $args, $r );
if ( get_magic_quotes_gpc() )
$r = stripslashes_deep( $r );
endif;
if ( is_array($defaults) ) :
extract($defaults);
extract($r);
return compact(array_keys($defaults)); // only those options defined in $defaults
else :
return $r;
endif;
}
?>