      1 <?php
      2 /**
      3  * Featured Review Widget
      4  *
      5  * @package EDD_Reviews
      6  * @subpackage Widgets
      7  * @copyright Copyright (c) 2013, Sunny Ratilal
      8  * @since 1.0
      9  */
     10
     11 // Exit if accessed directly
     12 if ( ! defined( 'ABSPATH' ) ) exit;
     13
     14 if ( ! class_exists( 'EDD_Reviews_Widget_Featured_Review' ) ) :
     15
     16 /**
     17  * EDD_Reviews_Widget_Featured_Reviews Class
     18  *
     19  * @package EDD_Reviews
     20  * @since 1.0
     21  * @version 1.0
     22  * @author Sunny Ratilal
     23  * @see WP_Widget
     24  */
     25 final class EDD_Reviews_Widget_Featured_Review extends WP_Widget {
     26     /**
     27      * Constructor Function
     28      *
     29      * @since 1.0
     30      * @access protected
     31      * @see WP_Widget::__construct()
     32      */
     33     public function __construct() {
     34         parent::__construct(
     35             false,
     36             __( 'EDD Featured Review', 'edd-reviews' ),
     37             apply_filters( 'edd_reviews_widget_featured_review_options', array(
     38                 'classname'   => 'widget_edd_reviews_featured_review',
     39                 'description' => __( 'Display a featured review.', 'edd-reviews' )
     40             ) )
     41         );
     42
     43         $this->alt_option_name = 'widget_edd_reviews_featured_review';
     44     }
     45
     46     /**
     47      * Widget API Function
     48      *
     49      * @since 1.0
     50      * @access public
     51      * @return void
     52      */
     53     public function widget( $args, $instance ) {
     54         extract( $args, EXTR_SKIP );
     55
     56         // Begin output
     57         $output = '';
     58
     59         $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Featured Review ', 'edd-reviews' );
     60         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
     61
     62         $review_id = ( ! empty( $instance['review'] ) ) ? absint( $instance['review'] ) : '';
     63
     64         if ( ! $review_id )
     65             return _e( 'No review was selected. Please configure it from within the widget settings.', 'edd-reviews' );
     66
     67         $review = get_comment( $review_id );
     68
     69         $output .=  $before_widget;
     70
     71         if ( ! empty( $title ) )
     72             $output .= $before_title . $title . $after_title;
     73
     74         if ( $review ) {
     75             $output .= '<div class="edd_featured_reviews">';
     76             $output .= '<div class="edd-featured-review-h-card">';
     77
     78             if ( get_option( 'show_avatars' ) ) $output .= get_avatar( $review, apply_filters( 'edd_reviews_widget_avatar_size', 40 ) );
     79
     80             $output .= '<p>' . get_comment_meta( $review->comment_ID, 'edd_review_title', true ) . ' ' . __( 'by', 'edd-reviews' ) . ' ' . get_comment_author_link( $review->comment_ID ) . '</p>';
     81             $output .= '<p><a href="' . get_permalink( $review->comment_post_ID ) . '">' . get_the_title( $review->comment_post_ID ) . '</a>' . '</p>';
     82
     83             $rating = get_comment_meta( $review->comment_ID, 'edd_rating', true );
     84
     85             $output .= '<div class="edd_reviews_rating_box" role="img" aria-label="'. $rating . ' ' . __( 'stars', 'edd-reviews' ) .'">';
     86             $output .= '<div class="edd_star_rating" style="width: ' . 19 * $rating . 'px"></div>';
     87             $output .= '</div>';
     88             $output .= '<div class="clear"></div>';
     89             $output .= '</div>';
     90             $output .= '<div class="edd-review-content">';
     91             $output .= $review->comment_content;
     92             $output .= '</div>';
     93             $output .= '<div class="edd-review-dateline">';
     94             $output .= get_comment_date( apply_filters( 'edd_reviews_widget_date_format', get_option( 'date_format' ) ), $review->comment_ID );
     95             $output .= '</div>';
     96             $output .= '</div>';
     97         }
     98
     99         $output .= $after_widget;
    100
    101         echo $output;
    102     }
    103
    104     /**
    105      * Processes the widget's options to be saved.
    106      *
    107      * @since 1.0
    108      * @access public
    109      * @return void
    110      */
    111     public function update( $new_instance, $old_instance ) {
    112         $instance = $old_instance;
    113
    114         $instance['title'] = strip_tags( $new_instance['title'] );
    115
    116         $instance['review'] = absint( $new_instance['review'] );
    117
    118         if ( isset( $alloptions['widget_edd_reviews_featured_review'] ) )
    119             delete_option( 'widget_edd_reviews_featured_review' );
    120
    121         return $instance;
    122     }
    123
    124     /**
    125      * Generates the administration form for the widget
    126      *
    127      * @since 1.0
    128      * @access public
    129      * @param array $instance The array of keys and values for the widget
    130      * @return void
    131      */
    132     public function form( $instance ) {
    133         global $wpdb;
    134
    135         $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    136         $review_id = isset( $instance['review'] ) ? absint( $instance['review'] ) : '';
    137         ?>
    138         <p>
    139             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'edd-reviews' ); ?></label>
    140             <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    141         </p>
    142
    143         <p>
    144             <label for="<?php echo $this->get_field_id( 'review' ); ?>"><?php _e( 'Review to display:', 'edd-reviews' ); ?></label>
    145             <select id="<?php echo $this->get_field_id( 'review' ); ?>" name="<?php echo $this->get_field_name( 'review' ); ?>">
    146                 <?php
    147                 $reviews = $wpdb->get_results( $wpdb->prepare( "SELECT meta_value, comment_id FROM {$wpdb->commentmeta} WHERE meta_key = %s", 'edd_review_title' ) );
    148
    149                 if ( $reviews ) :
    150                     foreach ( $reviews as $review ) :
    151                         echo '<option value="' . $review->comment_id . '"' . selected( $review_id, $review->comment_id ) .'>' . esc_html( $review->meta_value ) . '</option>';
    152                     endforeach;
    153                 endif;
    154                 ?>
    155             </select>
    156         </p>
    157         <?php
    158     }
    159 }
    160
    161 endif;