PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/featured-products.php

https://github.com/scottpoulin/jigoshop
PHP | 203 lines | 92 code | 35 blank | 76 comment | 3 complexity | 354dbe013d8c41444a6bfc54cc7c622f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Featured Products 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_Featured_Products 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_featured_products',
  27. 'description' => __( 'Featured products on your site', 'jigoshop' )
  28. );
  29. // Create the widget
  30. parent::__construct( 'jigoshop_featured_products', __( 'Jigoshop: Featured Products', '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. 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'] : __( 'Featured Products', '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' => 'featured',
  73. 'meta_value' => '1',
  74. 'meta_query' => array(
  75. array(
  76. 'key' => 'visibility',
  77. 'value' => array('catalog', 'visible'),
  78. 'compare'=> 'IN',
  79. ),
  80. )
  81. );
  82. // Run the query
  83. $q = new WP_Query( $query_args );
  84. if ( $q->have_posts() ) {
  85. // Print the widget wrapper & title
  86. echo $before_widget;
  87. echo $before_title . $title . $after_title;
  88. // Open the list
  89. echo '<ul class="product_list_widget">';
  90. // Print each product
  91. while( $q->have_posts() ) : $q->the_post();
  92. // Get a new jigoshop_product instance
  93. $_product = new jigoshop_product( get_the_ID() );
  94. echo '<li>';
  95. // Print the product image & title with a link to the permalink
  96. echo '<a href="' . esc_attr( get_permalink() ) . '" title="' . esc_attr( get_the_title() ) . '">';
  97. // Print the product image
  98. echo ( has_post_thumbnail() )
  99. ? the_post_thumbnail( 'shop_tiny' )
  100. : jigoshop_get_image_placeholder( 'shop_tiny' );
  101. echo '<span class="js_widget_product_title">' . get_the_title() . '</span>';
  102. echo '</a>';
  103. // Print the price with html wrappers
  104. echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>';
  105. echo '</li>';
  106. endwhile;
  107. echo '</ul>'; // Close the list
  108. // Print closing widget wrapper
  109. echo $after_widget;
  110. // Reset the global $the_post as this query will have stomped on it
  111. wp_reset_postdata();
  112. }
  113. // Flush output buffer and save to transient cache
  114. $cache[$args['widget_id']] = ob_get_flush();
  115. set_transient( 'jigoshop_widget_cache', $cache, 3600*3 ); // 3 hours ahead
  116. }
  117. /**
  118. * Update
  119. *
  120. * Handles the processing of information entered in the wordpress admin
  121. * Flushes the cache & removes entry from options array
  122. *
  123. * @param array new instance
  124. * @param array old instance
  125. * @return array instance
  126. */
  127. public function update( $new_instance, $old_instance ) {
  128. $instance = $old_instance;
  129. // Save the new values
  130. $instance['title'] = strip_tags( $new_instance['title'] );
  131. $instance['number'] = absint( $new_instance['number'] );
  132. // Flush the cache
  133. $this->flush_widget_cache();
  134. return $instance;
  135. }
  136. /**
  137. * Flush Widget Cache
  138. *
  139. * Flushes the cached output
  140. */
  141. public function flush_widget_cache() {
  142. delete_transient( 'jigoshop_widget_cache' );
  143. }
  144. /**
  145. * Form
  146. *
  147. * Displays the form for the wordpress admin
  148. *
  149. * @param array instance
  150. */
  151. function form( $instance ) {
  152. // Get instance data
  153. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  154. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  155. // Widget Title
  156. echo "
  157. <p>
  158. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  159. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  160. </p>";
  161. // Number of posts to fetch
  162. echo "
  163. <p>
  164. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  165. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' min='1' value='{$number}' />
  166. </p>";
  167. }
  168. } // class Jigoshop_Widget_Featured_Products