1 <?php
2 3 4 5 6 7 8 9
10
11
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 if ( ! class_exists( 'EDD_Reviews_Widget_Featured_Review' ) ) :
15
16 17 18 19 20 21 22 23 24
25 final class EDD_Reviews_Widget_Featured_Review extends WP_Widget {
26 27 28 29 30 31 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 48 49 50 51 52
53 public function widget( $args, $instance ) {
54 extract( $args, EXTR_SKIP );
55
56
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 106 107 108 109 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 126 127 128 129 130 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;