PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/recent_products.php

https://github.com/ThemesWpFr/jigoshop
PHP | 231 lines | 100 code | 39 blank | 92 comment | 5 complexity | 5d008c19324d89b49ae796fa89e8666e MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Recent 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 Jigowatt
  14. * @copyright Copyright Š 2011-2012 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class Jigoshop_Widget_Recent_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' => 'widget_recent_entries',
  27. 'description' => __( "The most recent products on your site", 'jigoshop')
  28. );
  29. parent::__construct('recent-products', __('Jigoshop: New Products', '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. function widget($args, $instance) {
  45. // Get the most recent products from the cache
  46. $cache = wp_cache_get('widget_recent_products', '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 = ($instance['title']) ? $instance['title'] : __('New Products', 'jigoshop');
  61. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  62. // Set number of products to fetch
  63. if ( ! $number = $instance['number'] ) {
  64. $number = 10;
  65. }
  66. $number = apply_filters('jigoshop_widget_recent_default_number', $number, $instance, $this->id_base);
  67. // Set up query
  68. $query_args = array(
  69. 'posts_per_page' => $number,
  70. 'post_type' => 'product',
  71. 'post_status' => 'publish',
  72. 'orderby' => 'date',
  73. 'order' => 'desc',
  74. 'meta_query' => array(
  75. array(
  76. 'key' => 'visibility',
  77. 'value' => array('catalog', 'visible'),
  78. 'compare' => 'IN',
  79. ),
  80. )
  81. );
  82. // Show variations of products? TODO: fix this -JAP-
  83. /*
  84. if( ! $instance['show_variations']) {
  85. $query_args['meta_query'] = array(
  86. array(
  87. 'key' => 'visibility',
  88. 'value' => array('catalog', 'visible'),
  89. 'compare' => 'IN',
  90. ),
  91. );
  92. $query_args['parent'] = false;
  93. }
  94. */
  95. // Run the query
  96. $q = new WP_Query($query_args);
  97. // If there are products
  98. if($q->have_posts()) {
  99. // Print the widget wrapper & title
  100. echo $before_widget;
  101. echo $before_title . $title . $after_title;
  102. // Open the list
  103. echo '<ul class="product_list_widget">';
  104. // Print out each product
  105. while($q->have_posts()) : $q->the_post();
  106. // Get new jigoshop_product instance
  107. $_product = new jigoshop_product(get_the_ID());
  108. echo '<li>';
  109. // Print the product image & title with a link to the permalink
  110. echo '<a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">';
  111. echo (has_post_thumbnail()) ? the_post_thumbnail('shop_tiny') : jigoshop_get_image_placeholder('shop_tiny');
  112. echo '<span class="js_widget_product_title">' . get_the_title() . '</span>';
  113. echo '</a>';
  114. // Print the price with html wrappers
  115. echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>';
  116. echo '</li>';
  117. endwhile;
  118. echo '</ul>'; // Close the list
  119. // Print closing widget wrapper
  120. echo $after_widget;
  121. // Reset the global $the_post as this query will have stomped on it
  122. wp_reset_postdata();
  123. }
  124. // Flush output buffer and save to cache
  125. $cache[$args['widget_id']] = ob_get_flush();
  126. wp_cache_set('widget_recent_products', $cache, 'widget');
  127. }
  128. /**
  129. * Update
  130. *
  131. * Handles the processing of information entered in the wordpress admin
  132. * Flushes the cache & removes entry from options array
  133. *
  134. * @param array new instance
  135. * @param array old instance
  136. * @return array instance
  137. */
  138. public function update( $new_instance, $old_instance ) {
  139. $instance = $old_instance;
  140. // Save the new values
  141. $instance['title'] = strip_tags($new_instance['title']);
  142. $instance['number'] = abs($new_instance['number']);
  143. $instance['show_variations'] = (bool) isset($new_instance['show_variations']) ? $new_instance['show_variations'] : false;
  144. // Flush the cache
  145. $this->flush_widget_cache();
  146. // Remove the cache entry from the options array
  147. $alloptions = wp_cache_get( 'alloptions', 'options' );
  148. if ( isset($alloptions['widget_recent_products']) ) {
  149. delete_option('widget_recent_products');
  150. }
  151. return $instance;
  152. }
  153. /**
  154. * Flush Widget Cache
  155. *
  156. * Flushes the cached output
  157. */
  158. public function flush_widget_cache() {
  159. wp_cache_delete('widget_recent_products', 'widget');
  160. }
  161. /**
  162. * Form
  163. *
  164. * Displays the form for the wordpress admin
  165. *
  166. * @param array instance
  167. */
  168. public function form( $instance ) {
  169. // Get instance data
  170. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  171. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  172. $show_variations = (bool) isset($instance['show_variations']) ? $instance['show_variations'] : false;
  173. // Widget Title
  174. echo "
  175. <p>
  176. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  177. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  178. </p>";
  179. // Number of posts to fetch
  180. echo "
  181. <p>
  182. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  183. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' value='{$number}' size='3' />
  184. </p>";
  185. // Show variations?
  186. echo '<p>';
  187. echo '<input type="checkbox" class="checkbox" id="' . esc_attr( $this->get_field_id('show_variations') ) . '" name="' . esc_attr( $this->get_field_name('show_variations') ) . '"' . checked( $show_variations ) . '/>';
  188. echo '<label for="' . esc_attr( $this->get_field_id('show_variations') ) . '"> ' . __( 'Show hidden product variations', 'jigoshop' ) . '</label>';
  189. echo '</p>';
  190. }
  191. } // class Jigoshop_Widget_Recent_Products