Make WordPress Core

Changeset 11744


Ignore:
Timestamp:
07/26/2009 01:23:46 AM (17 years ago)
Author:
azaozz
Message:

Fix wp_tag_cloud('order=RAND'), don't pre-sort if filter 'tag_cloud_sort' is used to sort the tags, props Joern_W, fixes #10393

File:
1 edited

Legend:

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

    r11450 r11744  
    574574 * the 'format' argument will return in PHP array type format.
    575575 *
    576  * The 'tag_cloud_sort' filter allows you to override the sorting done
    577  * by the 'orderby' argument; passed to the filter: $tags array and $args array.
     576 * The 'tag_cloud_sort' filter allows you to override the sorting.
     577 * Passed to the filter: $tags array and $args array, has to return the $tags array
     578 * after sorting it.
    578579 *
    579580 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
    … …  
    605606        if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
    606607                $body = 'return sprintf (
    607                         _n('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count),
     608                        _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
    608609                        number_format_i18n( $count ));';
    609610                $args['topic_count_text_callback'] = create_function('$count', $body);
    … …  
    611612
    612613        $args = wp_parse_args( $args, $defaults );
    613 
    614614        extract( $args );
    615615
    … …  
    617617                return;
    618618
    619         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
    620         if ( 'name' == $orderby )
    621                 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
    622         else
    623                 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
    624 
    625         $tags = apply_filters( 'tag_cloud_sort', $tags, $args );
    626 
    627         if ( 'DESC' == $order )
    628                 $tags = array_reverse( $tags, true );
    629         elseif ( 'RAND' == $order ) {
    630                 $keys = (array) array_rand( $tags, count( $tags ) );
    631                 $temp = array();
    632                 foreach ( $keys as $key )
    633                         $temp[$key] = $tags[$key];
    634 
    635                 $tags = $temp;
    636                 $temp = null;
    637                 unset( $temp );
     619        $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
     620        if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
     621                $tags = $tags_sorted;
     622                unset($tags_sorted);
     623        } else {
     624                if ( 'RAND' == $order ) {
     625                        shuffle($tags);
     626                } else {
     627                        // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
     628                        if ( 'name' == $orderby )
     629                                uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
     630                        else
     631                                uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
     632
     633                        if ( 'DESC' == $order )
     634                                $tags = array_reverse( $tags, true );
     635                }
    638636        }
    639637
Note: See TracChangeset for help on using the changeset viewer.