PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/products_on_sale.php

https://github.com/ThemesWpFr/jigoshop
PHP | 213 lines | 98 code | 35 blank | 80 comment | 9 complexity | e62f8c135095ad173dba8e662e83c65f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Widget which lists products on sale
  4. *
  5. * Displays shopping cart widget
  6. *
  7. * DISCLAIMER
  8. *
  9. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  10. * versions in the future. If you wish to customise Jigoshop core for your needs,
  11. * please use our GitHub repository to publish essential changes for consideration.
  12. *
  13. * @package Jigoshop
  14. * @category Widgets
  15. * @author Jigowatt
  16. * @copyright Copyright Š 2011-2012 Jigowatt Ltd.
  17. * @license http://jigoshop.com/license/commercial-edition
  18. */
  19. class Jigoshop_Widget_Products_On_Sale extends WP_Widget {
  20. /**
  21. * Constructor
  22. *
  23. * Setup the widget with the available options
  24. * Add actions to clear the cache whenever a post is saved|deleted or a theme is switched
  25. */
  26. public function __construct() {
  27. $options = array(
  28. 'classname' => 'widget_products_on_sale',
  29. 'description' => __( 'Display a list of products currently onsale', 'jigoshop' )
  30. );
  31. // Create the widget
  32. parent::__construct( 'products-onsale', __( 'Jigoshop: Products On-Sale', 'jigoshop' ), $options );
  33. // Flush cache after every save
  34. add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
  35. add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
  36. add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
  37. }
  38. /**
  39. * Widget
  40. *
  41. * Display the widget in the sidebar
  42. * Save output to the cache if empty
  43. *
  44. * @param array sidebar arguments
  45. * @param array instance
  46. */
  47. public function widget( $args, $instance ) {
  48. // Get the most recent products from the cache
  49. $cache = wp_cache_get( 'widget_recent_products', 'widget' );
  50. // If no entry exists use array
  51. if ( ! is_array($cache) ) {
  52. $cache = array();
  53. }
  54. // If cached get from the cache
  55. if ( isset($cache[$args['widget_id']]) ) {
  56. echo $cache[$args['widget_id']];
  57. return false;
  58. }
  59. // Start buffering
  60. ob_start();
  61. extract( $args );
  62. // Set the widget title
  63. $title = apply_filters(
  64. 'widget_title',
  65. ( $instance['title'] ) ? $instance['title'] : __( 'Special Offers', 'jigoshop' ),
  66. $instance,
  67. $this->id_base
  68. );
  69. // Set number of products to fetch
  70. if( ! $number = absint( $instance['number'] ) ) {
  71. $number = 5;
  72. }
  73. // Set up query
  74. $query_args = array(
  75. 'posts_per_page' => -1,
  76. 'post_type' => 'product',
  77. 'post_status' => 'publish',
  78. 'orderby' => 'rand',
  79. 'meta_query' => array(
  80. array(
  81. 'key' => 'visibility',
  82. 'value' => array( 'catalog', 'visible' ),
  83. 'compare' => 'IN',
  84. ),
  85. )
  86. );
  87. $q = new WP_Query( $query_args );
  88. // If there are products
  89. if( $q->have_posts() ) {
  90. // Print the widget wrapper & title
  91. echo $before_widget;
  92. echo $before_title . $title . $after_title;
  93. // Open the list
  94. echo '<ul class="product_list_widget">';
  95. // Print out each product
  96. for($i = 0; $q->have_posts() && $i < $number;) : $q->the_post();
  97. // Get new jigoshop_product instance
  98. $_product = new jigoshop_product( get_the_ID() );
  99. // Skip if not on sale
  100. if( ! $_product->is_on_sale() ) continue; else $i++;
  101. echo '<li>';
  102. // Print the product image & title with a link to the permalink
  103. echo '<a href="'.get_permalink().'" title="'.esc_attr( get_the_title() ).'">';
  104. // Print the product image
  105. echo ( has_post_thumbnail() )
  106. ? the_post_thumbnail( 'shop_tiny' )
  107. : jigoshop_get_image_placeholder( 'shop_tiny' );
  108. echo '<span class="js_widget_product_title">' . get_the_title() . '</span>';
  109. echo '</a>';
  110. // Print the price with html wrappers
  111. echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>';
  112. echo '</li>';
  113. endfor;
  114. echo '</ul>'; // Close the list
  115. // Print closing widget wrapper
  116. echo $after_widget;
  117. // Reset the global $the_post as this query will have stomped on it
  118. wp_reset_postdata();
  119. }
  120. ob_get_flush();
  121. }
  122. /**
  123. * Update
  124. *
  125. * Handles the processing of information entered in the wordpress admin
  126. * Flushes the cache & removes entry from options array
  127. *
  128. * @param array new instance
  129. * @param array old instance
  130. * @return array instance
  131. */
  132. public function update( $new_instance, $old_instance ) {
  133. $instance = $old_instance;
  134. // Save the new values
  135. $instance['title'] = strip_tags( $new_instance['title'] );
  136. $instance['number'] = absint( $new_instance['number'] );
  137. // Flush the cache
  138. $this->flush_widget_cache();
  139. // Remove the cache entry from the options array
  140. $alloptions = wp_cache_get( 'alloptions', 'options' );
  141. if ( isset( $alloptions['widget_recent_products'] ) ) {
  142. delete_option( 'widget_recent_products' );
  143. }
  144. return $instance;
  145. }
  146. /**
  147. * Flush Widget Cache
  148. *
  149. * Flushes the cached output
  150. */
  151. public function flush_widget_cache() {
  152. wp_cache_delete( 'widget_recent_products', 'widget' );
  153. }
  154. /**
  155. * Form
  156. *
  157. * Displays the form for the wordpress admin
  158. *
  159. * @param array instance
  160. */
  161. public function form( $instance ) {
  162. // Get instance data
  163. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  164. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  165. // Widget Title
  166. echo "
  167. <p>
  168. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  169. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  170. </p>";
  171. // Number of posts to fetch
  172. echo "
  173. <p>
  174. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  175. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' min='1' value='{$number}' />
  176. </p>";
  177. }
  178. }