Make WordPress Core


Ignore:
Timestamp:
06/29/2015 02:15:59 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce 'wp_generate_tag_cloud_data' filter.

This filter allows developers to modify the data that is used to create tag
clouds, without having to manipulate the tag cloud markup directly.

As part of the refactor, this changeset also adds a few unit tests for the way
wp_generate_tag_cloud() generates the 'title' attribute, as well as
improvements to output escaping.

Props flixos90, ysalame.
Fixes #24656.

File:
1 edited

Legend:

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

    r32568 r32996  
    809809    $font_step = $font_spread / $spread;
    810810
    811     $a = array();
    812 
     811    // Assemble the data that will be used to generate the tag cloud markup.
     812    $tags_data = array();
    813813    foreach ( $tags as $key => $tag ) {
     814        $tag_id = isset( $tag->id ) ? $tag->id : $key;
     815
    814816        $count = $counts[ $key ];
    815817        $real_count = $real_counts[ $key ];
    816         $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
    817         $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
    818         $tag_name = $tags[ $key ]->name;
    819818
    820819        if ( $translate_nooped_plural ) {
    821             $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
     820            $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
    822821        } else {
    823             $title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
    824         }
    825 
    826         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " .
    827             str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) )
    828             . $args['unit'] . ";'>$tag_name</a>";
     822            $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
     823        }
     824
     825        $tags_data[] = array(
     826            'id'         => $tag_id,
     827            'url'        => '#' != $tag->link ? $tag->link : '#',
     828            'name'       => $tag->name,
     829            'title'      => $title,
     830            'slug'       => $tag->slug,
     831            'real_count' => $real_count,
     832            'class'      => 'tag-link-' . $tag_id,
     833            'font_size'  => $args['smallest'] + ( $count - $min_count ) * $font_step,
     834        );
     835    }
     836
     837    /**
     838     * Filter the data used to generate the tag cloud.
     839     *
     840     * @since 4.3.0
     841     *
     842     * @param array $tags_data An array of term data for term used to generate the tag cloud.
     843     */
     844    $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
     845
     846    $a = array();
     847
     848    // generate the output links array
     849    foreach ( $tags_data as $key => $tag_data ) {
     850        $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "' class='" . esc_attr( $tag_data['class'] ) . "' title='" . esc_attr( $tag_data['title'] ) . "' style='font-size: " . esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ) . ";'>" . esc_html( $tag_data['name'] ) . "</a>";
    829851    }
    830852
Note: See TracChangeset for help on using the changeset viewer.