PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/layered_nav.php

https://github.com/ThemesWpFr/jigoshop
PHP | 295 lines | 160 code | 68 blank | 67 comment | 39 complexity | e766f6089a712169600bd2d720142386 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Layered Navigation 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_Layered_Nav extends WP_Widget {
  18. /**
  19. * Constructor
  20. *
  21. * Setup the widget with the available options
  22. */
  23. public function __construct() {
  24. $options = array(
  25. 'description' => __( "Shows a custom attribute in a widget which lets you narrow down the list of shown products in categories.", 'jigoshop'),
  26. );
  27. // Create the widget
  28. parent::__construct('layered_nav', __('Jigoshop: Layered Nav', 'jigoshop'), $options);
  29. }
  30. /**
  31. * Widget
  32. *
  33. * Display the widget in the sidebar
  34. *
  35. * @param array sidebar arguments
  36. * @param array instance
  37. */
  38. public function widget( $args, $instance ) {
  39. // TODO: Optimize this code
  40. // Extract the widget arguments
  41. extract($args);
  42. global $_chosen_attributes, $wpdb, $jigoshop_all_post_ids_in_view;
  43. // Hide widget if not product related
  44. if ( ! is_product_list() )
  45. return false;
  46. // Set the widget title
  47. $title = apply_filters(
  48. 'widget_title',
  49. ( $instance['title'] ) ? $instance['title'] : __( 'Filter by Attributes', 'jigoshop' ),
  50. $instance,
  51. $this->id_base
  52. );
  53. // Check if taxonomy exists
  54. $taxonomy = 'pa_'.sanitize_title($instance['attribute']);
  55. if ( ! taxonomy_exists($taxonomy) )
  56. return false;
  57. // Get all the terms that aren't empty
  58. $args = array(
  59. 'hide_empty' => true,
  60. );
  61. $terms = get_terms( $taxonomy, $args );
  62. $has_terms = (bool) $terms;
  63. // If has terms print layered navigation
  64. if($has_terms) {
  65. $found = false;
  66. ob_start();
  67. // Print the widget wrapper & title
  68. echo $before_widget;
  69. echo $before_title . $title . $after_title;
  70. //Remove param link
  71. $remove_link = remove_query_arg('filter_'.sanitize_title($instance['attribute']));
  72. echo "<a class=\"layerd_nav_clear\" href=\"{$remove_link}\">Clear</a>";
  73. // Open the list
  74. echo "<ul>";
  75. foreach ($terms as $term) {
  76. $_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );
  77. // Get product count & set flag
  78. $count = sizeof(array_intersect($_products_in_term, $jigoshop_all_post_ids_in_view ));
  79. $has_products = (bool) $count;
  80. if ($has_products) $found = true;
  81. $class = '';
  82. $arg = 'filter_'.sanitize_title($instance['attribute']);
  83. if (isset($_GET[ $arg ])) $current_filter = explode(',', $_GET[ $arg ]); else $current_filter = array();
  84. if (!is_array($current_filter)) $current_filter = array();
  85. if (!in_array($term->term_id, $current_filter)) $current_filter[] = $term->term_id;
  86. // Base Link decided by current page
  87. if (defined('SHOP_IS_ON_FRONT')) :
  88. $link = '';
  89. elseif ( is_shop() ) :
  90. $link = get_post_type_archive_link('product');
  91. else :
  92. $link = get_term_link( get_query_var('term'), get_query_var('taxonomy') );
  93. endif;
  94. // All current filters
  95. if ($_chosen_attributes) foreach ($_chosen_attributes as $name => $value) :
  96. if ($name!==$taxonomy) :
  97. $link = add_query_arg( sanitize_title(str_replace('pa_', 'filter_', $name)), implode(',', $value), $link );
  98. endif;
  99. endforeach;
  100. // Min/Max
  101. if (isset($_GET['min_price'])) :
  102. $link = add_query_arg( 'min_price', $_GET['min_price'], $link );
  103. endif;
  104. if (isset($_GET['max_price'])) :
  105. $link = add_query_arg( 'max_price', $_GET['max_price'], $link );
  106. endif;
  107. // Current Filter = this widget
  108. if (isset( $_chosen_attributes[$taxonomy] ) && is_array($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy])) :
  109. $class = 'class="chosen"';
  110. else :
  111. $link = add_query_arg( $arg, implode(',', $current_filter), $link );
  112. endif;
  113. // Search Arg
  114. if (get_search_query()) :
  115. $link = add_query_arg( 's', get_search_query(), $link );
  116. endif;
  117. // Post Type Arg
  118. if (isset($_GET['post_type'])) :
  119. $link = add_query_arg( 'post_type', $_GET['post_type'], $link );
  120. endif;
  121. echo '<li '.$class.'>';
  122. if ($has_products) echo '<a href="'.esc_url($link).'">'; else echo '<span>';
  123. echo $term->name;
  124. if ($has_products) echo '</a>'; else echo '</span>';
  125. echo ' <small class="count">'.$count.'</small></li>';
  126. }
  127. echo "</ul>"; // Close the list
  128. // Print closing widget wrapper
  129. echo $after_widget;
  130. if ( ! $found ) {
  131. ob_clean(); // clear the buffer
  132. return false; // display nothing
  133. } else {
  134. echo ob_get_clean(); // output the buffer
  135. }
  136. }
  137. }
  138. /**
  139. * Update
  140. *
  141. * Handles the processing of information entered in the wordpress admin
  142. *
  143. * @param array new instance
  144. * @param array old instance
  145. * @return array instance
  146. */
  147. public function update( $new_instance, $old_instance ) {
  148. $instance = $old_instance;
  149. // Save the new values
  150. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  151. $instance['attribute'] = stripslashes($new_instance['attribute']);
  152. return $instance;
  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. global $wpdb;
  163. // Get values from instance
  164. $title = (isset($instance['title'])) ? esc_attr($instance['title']) : null;
  165. $attr_tax = jigoshop_product::getAttributeTaxonomies();
  166. // Widget title
  167. echo '<p>';
  168. echo '<label for="' . esc_attr( $this->get_field_id('title') ) . '"> ' . _e('Title:', 'jigoshop') . '</label>';
  169. echo '<input type="text" class="widefat" id="' . esc_attr( $this->get_field_id('title') ) . '" name="' . esc_attr( $this->get_field_name('title') ) . '" value="' . esc_attr( $title ) . '" />';
  170. echo '</p>';
  171. // Print attribute selector
  172. if ( ! empty($attr_tax) ) {
  173. echo '<p>';
  174. echo '<label for="' . esc_attr( $this->get_field_id('attribute') ) . '">' . __('Attribute:', 'jigoshop') . '</label> ';
  175. echo '<select id="' . esc_attr( $this->get_field_id('attribute') ) . '" name="' . esc_attr( $this->get_field_name('attribute') ) . '">';
  176. foreach($attr_tax as $tax) {
  177. if (taxonomy_exists('pa_'.sanitize_title($tax->attribute_name))) {
  178. echo '<option value="' . esc_attr( $tax->attribute_name ) . '" ' . (isset($instance['attribute']) && $instance['attribute'] == $tax->attribute_name ? 'selected' : null) . '>';
  179. echo $tax->attribute_name;
  180. echo '</option>';
  181. }
  182. }
  183. echo '</select>';
  184. echo '</p>';
  185. }
  186. }
  187. } // class Jigoshop_Widget_Layered_Nav
  188. function jigoshop_layered_nav_query( $filtered_posts ) {
  189. global $_chosen_attributes;
  190. if (sizeof($_chosen_attributes)>0) :
  191. $matched_products = array();
  192. $filtered = false;
  193. foreach ($_chosen_attributes as $attribute => $values) :
  194. if (sizeof($values)>0) :
  195. foreach ($values as $value) :
  196. $posts = get_objects_in_term( $value, $attribute );
  197. if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) :
  198. $matched_products = array_intersect($posts, $matched_products);
  199. elseif (!is_wp_error($posts)) :
  200. $matched_products = $posts;
  201. endif;
  202. $filtered = true;
  203. endforeach;
  204. endif;
  205. endforeach;
  206. if ($filtered) :
  207. $matched_products[] = 0;
  208. $filtered_posts = array_intersect($filtered_posts, $matched_products);
  209. endif;
  210. endif;
  211. return $filtered_posts;
  212. }
  213. add_filter( 'loop-shop-posts-in', 'jigoshop_layered_nav_query' );
  214. function jigoshop_layered_nav_init() {
  215. global $_chosen_attributes;
  216. $_chosen_attributes = array();
  217. $attribute_taxonomies = jigoshop_product::getAttributeTaxonomies();
  218. if ( $attribute_taxonomies ) :
  219. foreach ($attribute_taxonomies as $tax) :
  220. $attribute = sanitize_title($tax->attribute_name);
  221. $taxonomy = 'pa_' . $attribute;
  222. $name = 'filter_' . $attribute;
  223. if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] );
  224. endforeach;
  225. endif;
  226. }
  227. add_action( 'init', 'jigoshop_layered_nav_init', 1 );