Make WordPress Core


Ignore:
Timestamp:
11/26/2013 02:24:25 AM (10 years ago)
Author:
dd32
Message:

Replace the Star ratings in the dashboard with dashicons ratings.
This also moves the ratings to only displaying full, half, or empty stars (ie. 3.0, 3.5, 4.0, no 3.7 ratings - which are rounded).
Fixes #26170

File:
1 edited

Legend:

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

    r26247 r26380  
    19671967    <?php
    19681968}
     1969
     1970/**
     1971 * Output a HTML element with a star rating for a given rating.
     1972 *
     1973 * Outputs a HTML element with the star rating exposed on a 0..5 scale in
     1974 * half star increments (ie. 1, 1.5, 2 stars). Optionally, if specified, the
     1975 * number of ratings may also be displayed by passing the $number parameter.
     1976 *
     1977 * @since 3.8.0
     1978 * @param array $args {
     1979 *     @type int    $rating The Rating to display, Expressed in a 0.5 rating increment, or a percentage.
     1980 *     @type string $type   The format that the $rating is in. Valid values are 'rating' (default), or, 'percent'.
     1981 *     @type int    $number The number of ratings which makes up this rating.
     1982 * }
     1983 */
     1984function wp_star_rating( $args = array() ) {
     1985    $defaults = array(
     1986        'rating' => 0,
     1987        'type' => 'rating',
     1988        'number' => 0,
     1989    );
     1990    $r = wp_parse_args( $args, $defaults );
     1991    extract( $r, EXTR_SKIP );
     1992
     1993    // Non-english decimal places when the $rating is coming from a string
     1994    $rating = str_replace( ',', '.', $rating );
     1995
     1996    // Convert Percentage to star rating, 0..5 in .5 increments
     1997    if ( 'percent' == $type ) {
     1998        $rating = round( $rating / 10, 0 ) / 2;
     1999    }
     2000
     2001    // Calculate the number of each type of star needed
     2002    $full_stars = floor( $rating );
     2003    $half_stars = ceil( $rating - $full_stars );
     2004    $empty_stars = 5 - $full_stars - $half_stars;
     2005
     2006    if ( $number ) {
     2007        /* translators: 1: The rating, 2: The number of ratings */
     2008        $title = _n( '%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $number );
     2009        $title = sprintf( $title, number_format_i18n( $rating, 1 ), number_format_i18n( $number ) );
     2010    } else {
     2011        /* translators: 1: The rating */
     2012        $title = sprintf( __( '%s rating' ), number_format_i18n( $rating, 1 ) );
     2013    }
     2014
     2015    echo '<div class="star-rating" title="' . esc_attr( $title ) . '">';
     2016    echo str_repeat( '<div class="star star-full"></div>', $full_stars );
     2017    echo str_repeat( '<div class="star star-half"></div>', $half_stars );
     2018    echo str_repeat( '<div class="star star-empty"></div>', $empty_stars);
     2019    echo '</div>';
     2020}
Note: See TracChangeset for help on using the changeset viewer.