Make WordPress Core


Ignore:
Timestamp:
03/03/2014 05:28:07 PM (10 years ago)
Author:
nacin
Message:

Accept nooped plurals in wp_generate_tag_cloud() / wp_tag_cloud().

Renders topic_count_text_callback more or less obsolete. It can still be used, but passing a plural is easier.

fixes #27262. see #7989, #14424.

File:
1 edited

Legend:

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

    r27373 r27376  
    516516 * be to return the top 45 tags in the tag cloud list.
    517517 *
    518  * The 'topic_count_text_callback' argument is a function, which, given the count
    519  * of the posts  with that tag, returns a text for the tooltip of the tag link.
     518 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
     519 * text for the tooltip of the tag link.
     520 *
     521 * The 'topic_count_text_callback' argument is a function, which given the count
     522 * of the posts with that tag returns a text for the tooltip of the tag link.
    520523 *
    521524 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
     
    564567
    565568/**
    566  * Default text for tooltip for tag links
    567  *
    568  * @param integer $count number of posts with that tag
    569  * @return string text for the tooltip of a tag link.
    570  */
    571 function default_topic_count_text( $count ) {
    572     return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
    573 }
    574 
    575 /**
    576569 * Default topic count scaling for tag links
    577570 *
     
    604597 * be to return the entire tag cloud list.
    605598 *
     599 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
     600 * text for the tooltip of the tag link.
     601 *
    606602 * The 'topic_count_text_callback' argument is a function, which given the count
    607  * of the posts  with that tag returns a text for the tooltip of the tag link.
     603 * of the posts with that tag returns a text for the tooltip of the tag link.
    608604 *
    609605 * @todo Complete functionality.
     
    618614        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
    619615        'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
    620         'topic_count_text_callback' => 'default_topic_count_text',
     616        'topic_count_text' => null, 'topic_count_text_callback' => null,
    621617        'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
    622618    );
    623619
    624     if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
    625         $args['topic_count_text_callback'] = '_plugin_topic_count';
    626     }
    627 
    628620    $args = wp_parse_args( $args, $defaults );
    629     extract( $args );
     621    extract( $args, EXTR_SKIP );
    630622
    631623    if ( empty( $tags ) )
    632624        return;
     625
     626    // Juggle topic count tooltips:
     627    if ( isset( $args['topic_count_text'] ) ) {
     628        // First look for nooped plural support via topic_count_text.
     629        $translate_nooped_plural = $args['topic_count_text'];
     630    } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
     631        // Look for the alternative callback style. Ignore the previous default.
     632        if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
     633            $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
     634        } else {
     635            $translate_nooped_plural = false;
     636        }
     637    } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
     638        // If no callback exists, look for the old-style single_text and multiple_text arguments.
     639        $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
     640    } else {
     641        // This is the default for when no callback, plural, or argument is passed in.
     642        $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
     643    }
    633644
    634645    $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
     
    678689        $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
    679690        $tag_name = $tags[ $key ]->name;
    680         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count, $tag, $args ) ) . "' style='font-size: " .
     691
     692        if ( $translate_nooped_plural ) {
     693            $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
     694        } else {
     695            $title_attribute = call_user_func( $topic_count_text_callback, $real_count, $tag, $args );
     696        }
     697
     698        $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " .
    681699            str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
    682700            . "$unit;'>$tag_name</a>";
     
    721739function _wp_object_count_sort_cb( $a, $b ) {
    722740    return ( $a->count > $b->count );
    723 }
    724 
    725 /**
    726  * Default text for tooltip for tag links
    727  *
    728  * @since 3.9.0
    729  * @access private
    730  *
    731  * @param integer $count Number of posts with that tag.
    732  * @param object  $tag   Tag object.
    733  * @param array   $args  Arguments for the tag cloud.
    734  * @return string Text for the tooltip of a tag link.
    735  */
    736 function _plugin_topic_count( $count, $tag, $args ) {
    737     return sprintf( 1 == $count? $args['single_text'] : $args['multiple_text'], number_format_i18n( $count ) );
    738741}
    739742
Note: See TracChangeset for help on using the changeset viewer.