      1 <?php
      2 /**
      3  * Reviews 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_Reviews' ) ) :
     15
     16 /**
     17  * EDD_Reviews_Widget_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_Reviews 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 Reviews', 'edd-reviews' ),
     37             apply_filters( 'edd_reviews_widget_options', array(
     38                 'classname'   => 'widget_edd_reviews',
     39                 'description' => __( 'Display the latest reviews posted on your store.', 'edd-reviews' )
     40             ) )
     41         );
     42
     43         $this->alt_option_name = 'widget_edd_reviews';
     44
     45         add_action( 'comment_post',              array( $this, 'flush_widget_cache' ) );
     46         add_action( 'transition_comment_status', array( $this, 'flush_widget_cache' ) );
     47     }
     48
     49     /**
     50      * Flush Comment Cache
     51      *
     52      * @since 1.0
     53      * @access public
     54      * @uses wp_cache_delete()
     55      * @return void
     56      */
     57     public function flush_widget_cache() {
     58         wp_cache_delete( 'widget_edd_recent_reviews', 'widget' );
     59     }
     60
     61     /**
     62      * Widget API Function
     63      *
     64      * @since 1.0
     65      * @access public
     66      * @return void
     67      */
     68     public function widget( $args, $instance ) {
     69         extract( $args, EXTR_SKIP );
     70
     71         // Begin output
     72         $output = '';
     73
     74         // Get cached items if they exist
     75         $cache = wp_cache_get( 'widget_edd_recent_reviews', 'widget' );
     76
     77         // If cache doesn't exist, create an array for the cache
     78         if ( ! is_array( $cache ) )
     79             $cache = array();
     80
     81         $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Reviews', 'edd-reviews' );
     82         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
     83
     84         $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
     85
     86         if ( ! $number )
     87             $number = 5; // Sets default number of reviews to display as 5
     88
     89         $reviews = get_comments( apply_filters( 'widget_edd_reviews_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'download' ) ) );
     90
     91         $output .=  $before_widget;
     92
     93         if ( ! empty( $title ) )
     94             $output .= $before_title . $title . $after_title;
     95
     96         if ( $reviews ) {
     97             $output .= '<ul id="edd_recent_reviews" class="edd_reviews_list edd_widget_list">';
     98
     99             foreach ( (array) $reviews as $review ) {
    100                 $rating = get_comment_meta( $review->comment_ID, 'edd_rating', true );
    101
    102                 $output .= '<li class="edd_recent_review">';
    103                 $output .= sprintf( _x( '%1$s on %2$s', 'widgets', 'edd-reviews' ), get_comment_author_link(), '<a href="' . esc_url( get_comment_link( $review->comment_ID ) ) . '">' . get_the_title( $review->comment_post_ID ) . '</a>' );
    104                 $output .= '</li>';
    105             }
    106
    107             $output .= '</ul>';
    108         }
    109
    110         $output .= $after_widget;
    111
    112         echo $output;
    113
    114         // Stores the output in the $cache array
    115         $ouput = $cache[ $args['widget_id'] ];
    116
    117         // Puts the reviews data in the cache for performance enhancements
    118         wp_cache_set( 'widget_edd_recent_reviews', $cache, 'widget' );
    119     }
    120
    121     /**
    122      * Processes the widget's options to be saved.
    123      *
    124      * @since 1.0
    125      * @access public
    126      * @uses EDD_Reviews_Widget_Reviews::flush_widget_cache()
    127      * @return void
    128      */
    129     public function update( $new_instance, $old_instance ) {
    130         $instance = $old_instance;
    131
    132         $instance['title'] = strip_tags( $new_instance['title'] );
    133
    134         $instance['number'] = absint( $new_instance['number'] );
    135
    136         $this->flush_widget_cache();
    137
    138         $alloptions = wp_cache_get( 'alloptions', 'options' );
    139
    140         if ( isset( $alloptions['widget_edd_reviews'] ) )
    141             delete_option( 'widget_edd_reviews' );
    142
    143         return $instance;
    144     }
    145
    146     /**
    147      * Generates the administration form for the widget
    148      *
    149      * @since 1.0
    150      * @access public
    151      * @param array $instance The array of keys and values for the widget
    152      * @return void
    153      */
    154     public function form( $instance ) {
    155         $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    156         $number = isset( $instance['number'] ) ? esc_attr( $instance['number'] ) : 5;
    157         ?>
    158         <p>
    159             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'edd-reviews' ); ?></label>
    160             <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 ); ?>" />
    161         </p>
    162
    163         <p>
    164             <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of reviews to show:', 'edd-reviews' ); ?></label>
    165             <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
    166         </p>
    167         <?php
    168     }
    169 }
    170
    171 endif;