From 77cdde3f877b5c2b7b92c5798d1711af7f1718d7 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 10 Apr 2007 19:52:15 +0000 Subject: [PATCH] Tag clouds from mdawaffe. fixes #4129 git-svn-id: http://svn.automattic.com/wordpress/trunk@5234 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/category-template.php | 84 +++++++++++++++++++++++++++++++ wp-includes/category.php | 25 ++++----- wp-includes/functions.php | 18 +++++++ 3 files changed, 112 insertions(+), 15 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 8432048f27..73889ebd22 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -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(' ', ' ', wp_specialchars( $tag )); + $a[] = "$tag"; + } + + switch ( $format ) : + case 'array' : + $return =& $a; + break; + case 'list' : + $return = "\n"; + break; + default : + $return = join("\n", $a); + break; + endswitch; + + return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); +} + // // Helper functions // diff --git a/wp-includes/category.php b/wp-includes/category.php index 713376f678..7de7c81dd8 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -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; } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index ffa7fe3861..08ccdce3b9 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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; +} + ?>