PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/random-products.php

https://github.com/scottpoulin/jigoshop
PHP | 168 lines | 76 code | 26 blank | 66 comment | 2 complexity | 677950610961fe20cc7648c081228e97 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Random Products Widget
  4. *
  5. * Displays a list of random products, inspired by WebFactory's Jigoshop Random Product Widget
  6. * http://codecanyon.net/item/jigoshop-random-product-widget/1371096?WT?ref=jigowatt
  7. *
  8. * DISCLAIMER
  9. *
  10. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  11. * versions in the future. If you wish to customise Jigoshop core for your needs,
  12. * please use our GitHub repository to publish essential changes for consideration.
  13. *
  14. * @package Jigoshop
  15. * @category Widgets
  16. * @author Jigoshop
  17. * @copyright Copyright Š 2011-2013 Jigoshop.
  18. * @license http://jigoshop.com/license/commercial-edition
  19. */
  20. class Jigoshop_Widget_Random_Products extends WP_Widget {
  21. /**
  22. * Constructor
  23. *
  24. * Setup the widget with the available options
  25. * Add actions to clear the cache whenever a post is saved|deleted or a theme is switched
  26. */
  27. public function __construct() {
  28. $options = array(
  29. 'classname' => 'jigoshop_random_products',
  30. 'description' => __( 'Lists a random selection of products on your site.', 'jigoshop' ),
  31. );
  32. // Create the widget
  33. parent::__construct( 'jigoshop_random_products', __('Jigoshop: Random Products', 'jigoshop' ), $options );
  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. public function widget( $args, $instance ) {
  45. // Start buffering
  46. ob_start();
  47. extract($args);
  48. // Set the widget title
  49. $title = apply_filters(
  50. 'widget_title',
  51. ( $instance['title'] ) ? $instance['title'] : __( 'Random Products', 'jigoshop' ),
  52. $instance,
  53. $this->id_base
  54. );
  55. // Set number of products to fetch
  56. if ( ! $number = absint( $instance['number'] ) ) {
  57. $number = 5;
  58. }
  59. // Set up query
  60. $query_args = array(
  61. 'posts_per_page' => $number,
  62. 'post_type' => 'product',
  63. 'post_status' => 'publish',
  64. 'orderby' => 'rand',
  65. 'meta_query' => array(
  66. array(
  67. 'key' => 'visibility',
  68. 'value' => array('catalog', 'visible'),
  69. 'compare' => 'IN',
  70. ),
  71. )
  72. );
  73. // Run the query
  74. $q = new WP_Query($query_args);
  75. // If there are products
  76. if ( $q->have_posts() ) {
  77. // Print the widget wrapper & title
  78. echo $before_widget;
  79. echo $before_title . $title . $after_title;
  80. // Open the list
  81. echo '<ul class="product_list_widget">';
  82. // Print out each product
  83. while($q->have_posts()) : $q->the_post();
  84. // Get new jigoshop_product instance
  85. $_product = new jigoshop_product(get_the_ID());
  86. echo '<li>';
  87. // Print the product image & title with a link to the permalink
  88. echo '<a href="'.get_permalink().'" title="'.esc_attr(get_the_title()).'">';
  89. echo (has_post_thumbnail()) ? the_post_thumbnail('shop_tiny') : jigoshop_get_image_placeholder('shop_tiny');
  90. echo '<span class="js_widget_product_title">' . get_the_title() . '</span>';
  91. echo '</a>';
  92. // Print the price with html wrappers
  93. echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>';
  94. echo '</li>';
  95. endwhile;
  96. echo '</ul>'; // Close the list
  97. // Print closing widget wrapper
  98. echo $after_widget;
  99. // Reset the global $the_post as this query will have stomped on it
  100. wp_reset_postdata();
  101. }
  102. ob_get_flush();
  103. }
  104. /**
  105. * Update
  106. *
  107. * @param array new instance
  108. * @param array old instance
  109. * @return array instance
  110. */
  111. public function update( $new_instance, $old_instance ) {
  112. $instance = $old_instance;
  113. // Save the new values
  114. $instance['title'] = strip_tags($new_instance['title']);
  115. $instance['number'] = abs($new_instance['number']);
  116. return $instance;
  117. }
  118. /**
  119. * Form
  120. *
  121. * Displays the form for the wordpress admin
  122. *
  123. * @param array instance
  124. */
  125. function form( $instance ) {
  126. // Get instance data
  127. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null;
  128. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  129. // Widget Title
  130. echo "
  131. <p>
  132. <label for='{$this->get_field_id( 'title' )}'>" . __( 'Title:', 'jigoshop' ) . "</label>
  133. <input class='widefat' id='{$this->get_field_id( 'title' )}' name='{$this->get_field_name( 'title' )}' type='text' value='{$title}' />
  134. </p>";
  135. // Number of posts to fetch
  136. echo "
  137. <p>
  138. <label for='{$this->get_field_id( 'number' )}'>" . __( 'Number of products to show:', 'jigoshop' ) . "</label>
  139. <input id='{$this->get_field_id( 'number' )}' name='{$this->get_field_name( 'number' )}' type='number' value='{$number}' size='3' />
  140. </p>";
  141. }
  142. } // class Jigoshop_Widget_Random_Products