PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/widget-recent_reviews.php

https://github.com/CammoKing/woocommerce
PHP | 169 lines | 80 code | 39 blank | 50 comment | 10 complexity | 3ae1351d0e4e1c77985363da47246963 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Recent Reviews Widget
  4. *
  5. * @author WooThemes
  6. * @category Widgets
  7. * @package WooCommerce/Widgets
  8. * @version 1.6.4
  9. * @extends WP_Widget
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12. class WooCommerce_Widget_Recent_Reviews extends WP_Widget {
  13. var $woo_widget_cssclass;
  14. var $woo_widget_description;
  15. var $woo_widget_idbase;
  16. var $woo_widget_name;
  17. /**
  18. * constructor
  19. *
  20. * @access public
  21. * @return void
  22. */
  23. function WooCommerce_Widget_Recent_Reviews() {
  24. /* Widget variable settings. */
  25. $this->woo_widget_cssclass = 'woocommerce widget_recent_reviews';
  26. $this->woo_widget_description = __( 'Display a list of your most recent reviews on your site.', 'woocommerce' );
  27. $this->woo_widget_idbase = 'woocommerce_recent_reviews';
  28. $this->woo_widget_name = __( 'WooCommerce Recent Reviews', 'woocommerce' );
  29. /* Widget settings. */
  30. $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
  31. /* Create the widget. */
  32. $this->WP_Widget('recent_reviews', $this->woo_widget_name, $widget_ops);
  33. add_action( 'save_post', array(&$this, 'flush_widget_cache') );
  34. add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
  35. add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
  36. }
  37. /**
  38. * widget function.
  39. *
  40. * @see WP_Widget
  41. * @access public
  42. * @param array $args
  43. * @param array $instance
  44. * @return void
  45. */
  46. function widget( $args, $instance ) {
  47. global $comments, $comment, $woocommerce;
  48. $cache = wp_cache_get('widget_recent_reviews', 'widget');
  49. if ( ! is_array( $cache ) )
  50. $cache = array();
  51. if ( isset( $cache[$args['widget_id']] ) ) {
  52. echo $cache[$args['widget_id']];
  53. return;
  54. }
  55. ob_start();
  56. extract($args);
  57. $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Reviews', 'woocommerce' ) : $instance['title'], $instance, $this->id_base);
  58. if ( ! $number = absint( $instance['number'] ) ) $number = 5;
  59. $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
  60. if ( $comments ) {
  61. echo $before_widget;
  62. if ( $title ) echo $before_title . $title . $after_title;
  63. echo '<ul class="product_list_widget">';
  64. foreach ( (array) $comments as $comment) {
  65. $_product = get_product( $comment->comment_post_ID );
  66. $star_size = intval( apply_filters( 'woocommerce_star_rating_size_recent_reviews', 16 ) );
  67. $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
  68. $rating_html = '<div class="star-rating" title="' . $rating . '">
  69. <span style="width:' . ( $rating * $star_size ) . 'px">' . $rating . ' ' . __( 'out of 5', 'woocommerce' ) . '</span>
  70. </div>';
  71. echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
  72. echo $_product->get_image();
  73. echo $_product->get_title().'</a>';
  74. echo $rating_html;
  75. printf( _x( 'by %1$s', 'by comment author', 'woocommerce' ), get_comment_author() ) . '</li>';
  76. }
  77. echo '</ul>';
  78. echo $after_widget;
  79. }
  80. $content = ob_get_clean();
  81. if ( isset( $args['widget_id'] ) ) $cache[$args['widget_id']] = $content;
  82. echo $content;
  83. wp_cache_set('widget_recent_reviews', $cache, 'widget');
  84. }
  85. /**
  86. * update function.
  87. *
  88. * @see WP_Widget->update
  89. * @access public
  90. * @param array $new_instance
  91. * @param array $old_instance
  92. * @return array
  93. */
  94. function update( $new_instance, $old_instance ) {
  95. $instance = $old_instance;
  96. $instance['title'] = strip_tags($new_instance['title']);
  97. $instance['number'] = (int) $new_instance['number'];
  98. $this->flush_widget_cache();
  99. $alloptions = wp_cache_get( 'alloptions', 'options' );
  100. if ( isset($alloptions['widget_recent_reviews']) ) delete_option('widget_recent_reviews');
  101. return $instance;
  102. }
  103. /**
  104. * flush_widget_cache function.
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. function flush_widget_cache() {
  110. wp_cache_delete('widget_recent_reviews', 'widget');
  111. }
  112. /**
  113. * form function.
  114. *
  115. * @see WP_Widget->form
  116. * @access public
  117. * @param array $instance
  118. * @return void
  119. */
  120. function form( $instance ) {
  121. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  122. if ( !isset($instance['number']) || !$number = (int) $instance['number'] ) $number = 5;
  123. ?>
  124. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'woocommerce' ); ?></label>
  125. <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 ); ?>" /></p>
  126. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number of products to show:', 'woocommerce' ); ?></label>
  127. <input id="<?php echo esc_attr( $this->get_field_id('number') ); ?>" name="<?php echo esc_attr( $this->get_field_name('number') ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
  128. <?php
  129. }
  130. }