Make WordPress Core

Changeset 5234


Ignore:
Timestamp:
04/10/2007 07:52:15 PM (17 years ago)
Author:
ryan
Message:

Tag clouds from mdawaffe. fixes #4129

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r5231 r5234  
    310310}
    311311
     312function wp_tag_cloud( $args = '' ) {
     313    $defaults = array(
     314        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
     315        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
     316        'exclude' => '', 'include' => ''
     317    );
     318    $args = wp_parse_args( $args, $defaults );
     319
     320    $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
     321
     322    if ( empty($tags) )
     323        return;
     324
     325    $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
     326    echo apply_filters( 'wp_tag_cloud', $return, $args );
     327}
     328
     329// $tags = prefetched tag array ( get_tags() )
     330// $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
     331// $args['orderby'] = 'name', 'count'
     332function wp_generate_tag_cloud( $tags, $args = '' ) {
     333    global $wp_rewrite;
     334    $defaults = array(
     335        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
     336        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
     337    );
     338    $args = wp_parse_args( $args, $defaults );
     339    extract($args);
     340
     341    if ( !$tags )
     342        return;
     343    $counts = $tag_links = array();
     344    foreach ( (array) $tags as $tag ) {
     345        $counts[$tag->cat_name] = $tag->tag_count;
     346        $tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID );
     347    }
     348
     349    $min_count = min($counts);
     350    $spread = max($counts) - $min_count;
     351    if ( $spread <= 0 )
     352        $spread = 1;
     353    $font_spread = $largest - $smallest;
     354    if ( $font_spread <= 0 )
     355        $font_spread = 1;
     356    $font_step = $font_spread / $spread;
     357
     358    // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
     359    if ( 'name' == $orderby )
     360        uksort($counts, 'strnatcasecmp');
     361    else
     362        asort($counts);
     363
     364    if ( 'DESC' == $order )
     365        $counts = array_reverse( $tag_counts, true );
     366
     367    $a = array();
     368
     369    $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
     370
     371    foreach ( $counts as $tag => $count ) {
     372        $tag_link = clean_url($tag_links[$tag]);
     373        $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
     374        $a[] = "<a href='$tag_link' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
     375            ( $smallest + ( ( $count - $min_count ) * $font_step ) )
     376            . "$unit;'>$tag</a>";
     377    }
     378
     379    switch ( $format ) :
     380    case 'array' :
     381        $return =& $a;
     382        break;
     383    case 'list' :
     384        $return = "<ul class='wp-tag-cloud'>\n\t<li>";
     385        $return .= join("</li>\n\t<li>", $a);
     386        $return .= "</li>\n</ul>\n";
     387        break;
     388    default :
     389        $return = join("\n", $a);
     390        break;
     391    endswitch;
     392
     393    return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
     394}
     395
    312396//
    313397// Helper functions
  • trunk/wp-includes/category.php

    r5232 r5234  
    351351    global $wpdb, $category_links;
    352352
    353     if ( is_array($args) )
    354         $r = &$args;
    355     else
    356         parse_str($args, $r);
    357 
    358353    $defaults = array('orderby' => 'name', 'order' => 'ASC',
    359354        'hide_empty' => true, 'exclude' => '', 'include' => '',
    360355        'number' => '');
    361     $r = array_merge($defaults, $r);
    362     if ( 'count' == $r['orderby'] )
    363         $r['orderby'] = 'category_count';
     356    $args = wp_parse_args( $args, $defaults );
     357    if ( 'count' == $args['orderby'] )
     358        $args['orderby'] = 'tag_count';
    364359    else
    365         $r['orderby'] = "cat_" . $r['orderby'];  // restricts order by to cat_ID and cat_name fields
    366     $r['number'] = (int) $r['number'];
    367     extract($r);
    368 
    369     $key = md5( serialize( $r ) );
     360        $args['orderby'] = "cat_" . $args['orderby'];  // restricts order by to cat_ID and cat_name fields
     361    $args['number'] = (int) $args['number'];
     362    extract($args);
     363
     364    $key = md5( serialize( $args ) );
    370365    if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
    371366        if ( isset( $cache[ $key ] ) )
    372             return apply_filters('get_tags', $cache[$key], $r);
     367            return apply_filters('get_tags', $cache[$key], $args);
    373368
    374369    $where = 'cat_ID > 0';
     
    407402    if (!empty($exclusions))
    408403        $exclusions .= ')';
    409     $exclusions = apply_filters('list_tags_exclusions', $exclusions, $r );
     404    $exclusions = apply_filters('list_tags_exclusions', $exclusions, $args );
    410405    $where .= $exclusions;
    411406
     
    428423    wp_cache_set( 'get_tags', $cache, 'category' );
    429424
    430     $tags = apply_filters('get_tags', $tags, $r);
     425    $tags = apply_filters('get_tags', $tags, $args);
    431426    return $tags;
    432427}
  • trunk/wp-includes/functions.php

    r5200 r5234  
    14721472}
    14731473
     1474function wp_parse_args( $args, $defaults = '' ) {
     1475    if ( is_array($args) ) :
     1476        $r =& $args;
     1477    else :
     1478        parse_str( $args, $r );
     1479        if ( get_magic_quotes_gpc() )
     1480            $r = stripslashes_deep( $r );
     1481    endif;
     1482
     1483    if ( is_array($defaults) ) :
     1484        extract($defaults);
     1485        extract($r);
     1486        return compact(array_keys($defaults)); // only those options defined in $defaults
     1487    else :
     1488        return $r;
     1489    endif;
     1490}
     1491
    14741492?>
Note: See TracChangeset for help on using the changeset viewer.