PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/recent_reviews.php

https://github.com/ThemesWpFr/jigoshop
PHP | 239 lines | 104 code | 46 blank | 89 comment | 7 complexity | ec4ab4e4fdd52642d54ba0f966824e01 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Recent Reviews Widget
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Widgets
  13. * @author Jigowatt
  14. * @copyright Copyright Š 2011-2012 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class Jigoshop_Widget_Recent_Reviews extends WP_Widget {
  18. /**
  19. * Constructor
  20. *
  21. * Setup the widget with the available options
  22. * Add actions to clear the cache whenever a post is saved|deleted or a theme is switched
  23. */
  24. public function __construct() {
  25. $options = array(
  26. 'classname' => 'widget_recent_reviews',
  27. 'description' => __( 'Display a list of your most recent product reviews', 'jigoshop' )
  28. );
  29. parent::__construct( 'recent-reviews', __( 'Jigoshop: Recent Reviews', 'jigoshop' ), $options );
  30. // Flush cache after every save
  31. add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
  32. add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
  33. add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
  34. }
  35. /**
  36. * Widget
  37. *
  38. * Display the widget in the sidebar
  39. * Save output to the cache if empty
  40. *
  41. * @param array sidebar arguments
  42. * @param array instance
  43. */
  44. public function widget( $args, $instance ) {
  45. // Get the most recent products from the cache
  46. $cache = wp_cache_get( 'widget_recent_reviews', 'widget' );
  47. // If no entry exists use array
  48. if ( ! is_array( $cache ) ) {
  49. $cache = array();
  50. }
  51. // If cached get from the cache
  52. if ( isset( $cache[$args['widget_id']] ) ) {
  53. echo $cache[$args['widget_id']];
  54. return false;
  55. }
  56. // Start buffering
  57. ob_start();
  58. extract($args);
  59. // Set the widget title
  60. $title = apply_filters(
  61. 'widget_title',
  62. ($instance['title']) ? $instance['title'] : __( 'Recent Reviews', 'jigoshop' ),
  63. $instance,
  64. $this->id_base
  65. );
  66. // Set number of products to fetch
  67. if ( ! $number = absint( $instance['number'] ) ) {
  68. $number = 5;
  69. }
  70. // Modify get_comments query to only include products which are visible
  71. add_filter( 'comments_clauses', array( &$this, 'where_product_is_visible' ) );
  72. // Get the latest reviews
  73. $comments = get_comments(array(
  74. 'number' => $number,
  75. 'status' => 'approve',
  76. 'post_status'=> 'publish',
  77. 'post_type' => 'product',
  78. ));
  79. // If there are products
  80. if( $comments ) {
  81. // Print the widget wrapper & title
  82. echo $before_widget;
  83. echo $before_title . $title . $after_title;
  84. // Open the list
  85. echo '<ul class="product_list_widget">';
  86. // Print out each product
  87. foreach( $comments as $comment ) {
  88. // Get new jigoshop_product instance
  89. $_product = new jigoshop_product( $comment->comment_post_ID );
  90. // Skip products that are invisible
  91. if( $_product->visibility == 'hidden' )
  92. continue;
  93. // TODO: Refactor this
  94. // Apply star size
  95. $star_size = apply_filters('jigoshop_star_rating_size_recent_reviews', 16);
  96. $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
  97. echo '<li>';
  98. // Print the product image & title with a link to the permalink
  99. echo '<a href="'.esc_url( get_comment_link($comment->comment_ID) ).'">';
  100. // Print the product image
  101. echo ( has_post_thumbnail( $_product->id ) )
  102. ? get_the_post_thumbnail( $_product->id,'shop_tiny' )
  103. : jigoshop_get_image_placeholder( 'shop_tiny' );
  104. echo '<span class="js_widget_product_title">' . $_product->get_title() . '</span>';
  105. echo '</a>';
  106. // Print the star rating
  107. echo "<div class='star-rating' title='{$rating}'>
  108. <span style='width:".($rating*$star_size)."px;'>{$rating} ".__( 'out of 5', 'jigoshop' )."</span>
  109. </div>";
  110. // Print the author
  111. printf( _x('by %1$s', 'author', 'jigoshop' ), get_comment_author() );
  112. echo '</li>';
  113. }
  114. echo '</ul>'; // Close the list
  115. // Print closing widget wrapper
  116. echo $after_widget;
  117. // Remove the filter on comments to stop other queries from being manipulated
  118. remove_filter( 'comments_clauses', array(&$this, 'where_product_is_visible') );
  119. }
  120. // Flush output buffer and save to cache
  121. $cache[$args['widget_id']] = ob_get_flush();
  122. wp_cache_set( 'widget_recent_reviews', $cache, 'widget' );
  123. }
  124. /**
  125. * Update
  126. *
  127. * Handles the processing of information entered in the wordpress admin
  128. * Flushes the cache & removes entry from options array
  129. *
  130. * @param array new instance
  131. * @param array old instance
  132. * @return array instance
  133. */
  134. public function update( $new_instance, $old_instance ) {
  135. $instance = $old_instance;
  136. // Save the new values
  137. $instance['title'] = strip_tags( $new_instance['title'] );
  138. $instance['number'] = absint( $new_instance['number'] );
  139. // Flush the cache
  140. $this->flush_widget_cache();
  141. // Remove the cache entry from the options array
  142. $alloptions = wp_cache_get( 'alloptions', 'options' );
  143. if ( isset( $alloptions['widget_recent_reviews'] ) ) {
  144. delete_option( 'widget_recent_reviews' );
  145. }
  146. return $instance;
  147. }
  148. /**
  149. * Modifies get_comments query to only grab comments whose products are visible
  150. *
  151. * @param array Query Arguments
  152. * @return array
  153. */
  154. public function where_product_is_visible( $clauses ) {
  155. global $wpdb;
  156. // Only fetch comments whose products are visible
  157. $clauses['where'] .= " AND $wpdb->postmeta.meta_value = 'visible'";
  158. $clauses['join'] .= " LEFT JOIN $wpdb->postmeta ON($wpdb->comments.comment_post_ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'visibility')";;
  159. return $clauses;
  160. }
  161. /**
  162. * Flush Widget Cache
  163. *
  164. * Flushes the cached output
  165. */
  166. public function flush_widget_cache() {
  167. wp_cache_delete( 'widget_recent_reviews', 'widget' );
  168. }
  169. /**
  170. * Form
  171. *
  172. * Displays the form for the wordpress admin
  173. *
  174. * @param array instance
  175. */
  176. public function form( $instance ) {
  177. // Get instance data
  178. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  179. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  180. // Widget Title
  181. echo "
  182. <p>
  183. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  184. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  185. </p>";
  186. // Number of posts to fetch
  187. echo "
  188. <p>
  189. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  190. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' value='{$number}' />
  191. </p>";
  192. }
  193. } // class Jigoshop_Widget_Recent_Products