Making WordPress.org

Changeset 3203


Ignore:
Timestamp:
05/20/2016 10:08:16 PM (9 years ago)
Author:
obenland
Message:

Plugin Directory: Make Ratings widget setup available to Widgets API.

Allows for the title to be overwritten and adheres to widget args for heading
markup. Also replaces the rating dingus with a simpler button for now.

See #1719.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/widgets/class-ratings.php

    r3185 r3203  
    1010class Ratings extends \WP_Widget {
    1111
     12    /**
     13     * Ratings constructor.
     14     */
    1215    public function __construct() {
    13         $widget_ops = array(
    14             'classname' => 'plugin-ratings',
    15             'description' => 'Displays the plugin ratings.',
    16         );
    17         parent::__construct( 'plugin_ratings', 'Plugin Ratings', $widget_ops );
     16        parent::__construct( 'plugin_ratings', __( 'Plugin Ratings', 'wporg-plugins' ), array(
     17            'classname'   => 'plugin-ratings',
     18            'description' => __( 'Displays the plugin ratings.', 'wporg-plugins' ),
     19        ) );
    1820    }
    1921
     
    2527     */
    2628    public function widget( $args, $instance ) {
    27         echo $args['before_widget'];
     29        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ratings', 'wporg-plugins' ) : $instance['title'], $instance, $this->id_base );
    2830
    29         $post = get_post();
    30 
     31        $post        = get_post();
    3132        $rating      = get_post_meta( $post->ID, 'rating', true );
    3233        $ratings     = get_post_meta( $post->ID, 'ratings', true ) ?: array();
    3334        $num_ratings = array_sum( $ratings );
    3435
    35         $user_rating = 0;
    36         if ( is_user_logged_in() && function_exists( 'wporg_get_user_rating' ) ) {
    37             $user_rating = wporg_get_user_rating( 'plugin', $post->post_name, get_current_user_id() );
    38             if ( ! $user_rating ) {
    39                 $user_rating = 0;
    40             }
    41         }
    42 
     36        echo $args['before_widget'];
     37        echo $args['before_title'] . $title . $args['after_title'];
    4338        ?>
    44 
    4539        <meta itemprop="ratingCount" content="<?php echo esc_attr( $num_ratings ) ?>"/>
    46         <h4><?php _e( 'Ratings', 'wporg-plugins' ); ?></h4>
    4740
    4841        <?php if ( $rating ) : ?>
    4942        <div class="rating">
    5043            <?php echo Template::dashicons_stars( $rating ); ?>
    51             <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-plugins' ), '<span itemprop="ratingValue">' . $rating . '</span>' ); ?></p>
     44            <meta itemprop="ratingValue" content="<?php echo esc_attr( $rating ) ?>">
    5245        </div>
    5346        <?php else : ?>
    5447        <div class="rating">
    55             <p class="description"><?php _e( 'This plugin has not been rated yet.', 'wporg-plugins' ); ?></p>
     48            <p><?php _e( 'This plugin has not been rated yet.', 'wporg-plugins' ); ?></p>
    5649        </div>
    5750        <?php endif; // $rating
     
    6356            ?>
    6457            <li class="counter-container">
    65                 <a href="https://wordpress.org/support/view/plugin-reviews/<?php echo $post->post_name; ?>?filter=<?php echo $stars; ?>" title="<?php echo esc_attr( sprintf( _n( 'Click to see reviews that provided a rating of %d star', 'Click to see reviews that provided a rating of %d stars', $stars, 'wporg-plugins' ), $stars ) ); ?>">
     58                <a href="<?php echo esc_url( 'https://wordpress.org/support/view/plugin-reviews/' . $post->post_name . '?filter=' . $stars ); ?>">
    6659                    <span class="counter-label"><?php printf( _n( '%d star', '%d stars', $stars, 'wporg-plugin' ), $stars ); ?></span>
    6760                    <span class="counter-back">
     
    7669        endif; // $ratings
    7770
    78         if ( is_user_logged_in() ) {
    79             echo '<div class="user-rating">';
    80             echo Template::dashicons_stars( array(
    81                 'rating' => $user_rating,
    82                 'template' => '<a class="%1$s" href="https://wordpress.org/support/view/plugin-reviews/' . $post->post_name . '?rate=%2$d#postform"></a>'
    83             ) );
    84             echo '</div>';
    85             ?>
    86             <script>
    87             jQuery(document).ready( function($) {
    88                 var $rating = $( '.user-rating div' ),
    89                     $stars = $rating.find( '.dashicons' ),
    90                     current_rating = $rating.data( 'rating' ),
    91                     rating_clear_timer = 0;
    92 
    93                 $stars.mouseover( function() {
    94                     var $this = $(this),
    95                         $prev_items = $this.prevAll(),
    96                         $next_items = $this.nextAll(),
    97                         rating = $prev_items.length + 1;
    98 
    99                     if ( rating_clear_timer ) {
    100                         clearTimeout( rating_clear_timer );
    101                         rating_clear_timer = 0;
    102                     }
    103 
    104                     if ( rating == current_rating ) {
    105                         return;
    106                     }
    107 
    108                     $this.removeClass( 'dashicons-star-empty' ).addClass( 'dashicons-star-filled' );
    109                     $prev_items.removeClass( 'dashicons-star-empty' ).addClass( 'dashicons-star-filled' );
    110                     $next_items.removeClass( 'dashicons-star-filled' ).addClass( 'dashicons-star-empty' );
    111 
    112                     $rating.prop( 'title', $rating.data( 'title-template' ).replace( '%s', rating ).replace( '%d', rating ) );
    113                     current_rating = rating;
    114                 } );
    115                 $rating.mouseout( function() {
    116                     var clear_callback = function() {
    117                         var rating = $rating.data( 'rating' );
    118                         if ( rating == current_rating ) {
    119                             return;
    120                         }
    121                         if ( rating ) {
    122                             $( $stars.get( rating-1 ) ).mouseover();
    123                         } else {
    124                             $stars.removeClass( 'dashicons-star-filled' ).addClass( 'dashicons-star-empty' );
    125                         }
    126                     };
    127 
    128                     if ( ! rating_clear_timer ) {
    129                         rating_clear_timer = setTimeout( clear_callback, 2000 );
    130                     }
    131                 } );
    132             } );
    133             </script>
    134             <?php
    135         }
     71        if ( is_user_logged_in() ) : ?>
     72            <div class="user-rating">
     73                <a class="button button-secondary" href="<?php echo esc_url( 'https://wordpress.org/support/view/plugin-reviews/' . $post->post_name . '#postform' ); ?>"><?php _e( 'Add your review', 'wporg-plugins' ); ?></a>
     74            </div>
     75        <?php
     76        endif;
    13677
    13778        echo $args['after_widget'];
Note: See TracChangeset for help on using the changeset viewer.