PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/best-sellers.php

https://github.com/scottpoulin/jigoshop
PHP | 204 lines | 93 code | 34 blank | 77 comment | 3 complexity | 14277e81ac41f8d27457c062463823af MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Best Sellers 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 Jigoshop
  14. * @copyright Copyright Š 2011-2013 Jigoshop.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class Jigoshop_Widget_Best_Sellers 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' => 'jigoshop_best_sellers',
  27. 'description' => __( 'Lists the best selling products', 'jigoshop' )
  28. );
  29. // Create the widget
  30. parent::__construct( 'jigoshop_best_sellers', __( 'Jigoshop: Best Sellers', 'jigoshop' ), $options );
  31. // Flush cache after every save
  32. add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
  33. add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
  34. add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
  35. }
  36. /**
  37. * Widget
  38. *
  39. * Display the widget in the sidebar
  40. * Save output to the cache if empty
  41. *
  42. * @param array sidebar arguments
  43. * @param array instance
  44. */
  45. public function widget( $args, $instance ) {
  46. // Get the best selling products from the transient
  47. $cache = get_transient( 'jigoshop_widget_cache' );
  48. // If cached get from the cache
  49. if ( isset( $cache[$args['widget_id']] ) ) {
  50. echo $cache[$args['widget_id']];
  51. return false;
  52. }
  53. // Start buffering
  54. ob_start();
  55. extract( $args );
  56. // Set the widget title
  57. $title = apply_filters(
  58. 'widget_title',
  59. ( $instance['title'] ) ? $instance['title'] : __( 'Best Sellers', 'jigoshop' ),
  60. $instance,
  61. $this->id_base
  62. );
  63. // Set number of products to fetch
  64. if ( ! $number = absint( $instance['number'] ) ) {
  65. $number = 5;
  66. }
  67. // Set up query
  68. $query_args = array(
  69. 'posts_per_page' => $number,
  70. 'post_type' => 'product',
  71. 'post_status' => 'publish',
  72. 'meta_key' => '_js_total_sales',
  73. 'orderby' => 'meta_value_num',
  74. 'nopaging' => false,
  75. 'meta_query' => array(
  76. array(
  77. 'key' => 'visibility',
  78. 'value' => array('catalog', 'visible'),
  79. 'compare' => 'IN',
  80. ),
  81. )
  82. );
  83. // Run the query
  84. $q = new WP_Query( $query_args );
  85. // If there are products
  86. if( $q->have_posts() ) {
  87. // Print the widget wrapper & title
  88. echo $before_widget;
  89. echo $before_title . $title . $after_title;
  90. // Open the list
  91. echo '<ul class="product_list_widget">';
  92. // Print out each product
  93. while( $q->have_posts() ) : $q->the_post();
  94. // Get a new jigoshop_product instance
  95. $_product = new jigoshop_product( get_the_ID() );
  96. echo '<li>';
  97. // Print the product image & title with a link to the permalink
  98. echo '<a href="' . esc_attr( get_permalink() ) . '" title="' . esc_attr( get_the_title() ) . '">';
  99. // Print the product image
  100. echo ( has_post_thumbnail() )
  101. ? the_post_thumbnail( 'shop_tiny' )
  102. : jigoshop_get_image_placeholder( 'shop_tiny' );
  103. echo '<span class="js_widget_product_title">' . get_the_title() . '</span>';
  104. echo '</a>';
  105. // Print the price with html wrappers
  106. echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>';
  107. echo '</li>';
  108. endwhile;
  109. echo '</ul>'; // Close the list
  110. // Print closing widget wrapper
  111. echo $after_widget;
  112. // Reset the global $the_post as this query will have stomped on it
  113. wp_reset_postdata();
  114. }
  115. // Flush output buffer and save to transient cache
  116. $cache[$args['widget_id']] = ob_get_flush();
  117. set_transient( 'jigoshop_widget_cache', $cache, 3600*3 ); // 3 hours ahead
  118. }
  119. /**
  120. * Update
  121. *
  122. * Handles the processing of information entered in the wordpress admin
  123. * Flushes the cache & removes entry from options array
  124. *
  125. * @param array new instance
  126. * @param array old instance
  127. * @return array instance
  128. */
  129. public function update( $new_instance, $old_instance ) {
  130. $instance = $old_instance;
  131. // Save the new values
  132. $instance['title'] = strip_tags( $new_instance['title'] );
  133. $instance['number'] = absint( $new_instance['number'] );
  134. // Flush the cache
  135. $this->flush_widget_cache();
  136. return $instance;
  137. }
  138. /**
  139. * Flush Widget Cache
  140. *
  141. * Flushes the cached output
  142. */
  143. public function flush_widget_cache() {
  144. delete_transient( 'jigoshop_widget_cache' );
  145. }
  146. /**
  147. * Form
  148. *
  149. * Displays the form for the wordpress admin
  150. *
  151. * @param array instance
  152. */
  153. public function form( $instance ) {
  154. // Get instance data
  155. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  156. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  157. // Widget Title
  158. echo "
  159. <p>
  160. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  161. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  162. </p>";
  163. // Number of posts to fetch
  164. echo "
  165. <p>
  166. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  167. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' min='1' value='{$number}' />
  168. </p>";
  169. }
  170. } // class Jigoshop_Widget_Best_Sellers