WordPress.org

Making WordPress.org

Changeset 3039


Ignore:
Timestamp:
04/28/2016 08:30:43 PM (4 years ago)
Author:
obenland
Message:

Plugin Directory: Make ratings widget compatible with meta env.

Accounts for cases where ratings post meta and wporg_get_user_rating()
are not available.

See #1575.

File:
1 edited

Legend:

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

    r3020 r3039  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory\Widgets;
    3 
    43use WordPressdotorg\Plugin_Directory\Template;
    54
     
    3029        $post = get_post();
    3130
    32         $rating = get_post_meta( $post->ID, 'rating', true );
    33         $ratings = get_post_meta( $post->ID, 'ratings', true );
     31        $rating      = get_post_meta( $post->ID, 'rating', true );
     32        $ratings     = get_post_meta( $post->ID, 'ratings', true ) ?: array();
    3433        $num_ratings = array_sum( $ratings );
    3534
    3635        $user_rating = 0;
    37         if ( is_user_logged_in() ) {
     36        if ( is_user_logged_in() && function_exists( 'wporg_get_user_rating' ) ) {
    3837            $user_rating = wporg_get_user_rating( 'plugin', $post->post_name, get_current_user_id() );
    3938            if ( ! $user_rating ) {
     
    4746        <h4><?php _e( 'Ratings', 'wporg-plugins' ); ?></h4>
    4847
    49         <?php if ( $rating ) { ?>
    50             <div class="rating">
    51                 <?php echo Template::dashicons_stars( $rating ); ?>
    52                 <p class="description"><?php printf( __( '%s out of 5 stars.', 'wporg-plugins' ), '<span itemprop="ratingValue">' . $rating . '</span>' ); ?></p>
    53             </div>
    54         <?php } else { ?>
    55             <div class="rating">
    56                 <div class="ratings"><?php _e( 'This plugin has not been rated yet.', 'wporg-plugins' ); ?></div>
    57             </div>
    58         <?php } ?>
     48        <?php if ( $rating ) : ?>
     49        <div class="rating">
     50            <?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>
     52        </div>
     53        <?php else : ?>
     54        <div class="rating">
     55            <div class="ratings"><?php _e( 'This plugin has not been rated yet.', 'wporg-plugins' ); ?></div>
     56        </div>
     57        <?php endif; // $rating
    5958
    60         <?php if ( $ratings ) { ?>
     59        if ( $ratings ) : ?>
    6160        <ul class="ratings-list">
    6261            <?php foreach ( range( 5, 1 ) as $stars ) :
    63                 $rating_bar_width = $num_ratings ? 100 * $ratings[$stars] / $num_ratings : 0;
     62                $rating_bar_width = $num_ratings ? 100 * $ratings[ $stars ] / $num_ratings : 0;
    6463            ?>
    6564            <li class="counter-container">
     
    6968                        <span class="counter-bar" style="width: <?php echo $rating_bar_width; ?>%;"></span>
    7069                    </span>
    71                     <span class="counter-count"><?php echo $ratings[$stars]; ?></span>
     70                    <span class="counter-count"><?php echo $ratings[ $stars ]; ?></span>
    7271                </a>
    7372            </li>
     
    7574        </ul>
    7675        <?php
    77         }
     76        endif; // $ratings
    7877
    7978        if ( is_user_logged_in() ) {
     
    134133            </script>
    135134            <?php
    136 
    137135        }
    138136
    139137        echo $args['after_widget'];
    140138    }
    141 
    142139}
    143 
Note: See TracChangeset for help on using the changeset viewer.