jigoshop /widgets/layered_nav.php

Language PHP Lines 296
MD5 Hash a4b3a81a5d57af980b60f2deaf5d4b21
Repository https://github.com/scottpoulin/jigoshop.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
 * Layered Navigation Widget
 *
 * DISCLAIMER
 *
 * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
 * versions in the future. If you wish to customise Jigoshop core for your needs,
 * please use our GitHub repository to publish essential changes for consideration.
 *
 * @package             Jigoshop
 * @category            Widgets
 * @author              Jigoshop
 * @copyright           Copyright Š 2011-2013 Jigoshop.
 * @license             http://jigoshop.com/license/commercial-edition
 */

class Jigoshop_Widget_Layered_Nav extends WP_Widget {

	/**
	 * Constructor
	 *
	 * Setup the widget with the available options
	 */
	public function __construct() {
		$options = array(
			'description' => __( "Shows a custom attribute in a widget which lets you narrow down the list of shown products in categories.", 'jigoshop'),
		);

		// Create the widget
		parent::__construct('layered_nav', __('Jigoshop: Layered Nav', 'jigoshop'), $options);
	}

	/**
	 * Widget
	 *
	 * Display the widget in the sidebar
	 *
	 * @param	array	sidebar arguments
	 * @param	array	instance
	 */
	public function widget( $args, $instance ) {

		// TODO: Optimize this code

		// Extract the widget arguments
		extract($args);
		global $_chosen_attributes, $wpdb, $jigoshop_all_post_ids_in_view;

		// Hide widget if not product related
		if ( ! is_product_list() )
			return false;

		// Set the widget title
		$title = apply_filters(
			'widget_title',
			( $instance['title'] ) ? $instance['title'] : __( 'Filter by Attributes', 'jigoshop' ),
			$instance,
			$this->id_base
		);

		// Check if taxonomy exists
		$taxonomy = 'pa_'.sanitize_title($instance['attribute']);
		if ( ! taxonomy_exists($taxonomy) )
			return false;

		// Get all the terms that aren't empty
		$args = array(
			'hide_empty' => true,
		);
		$terms = get_terms( $taxonomy, $args );
		$has_terms = (bool) $terms;

		// If has terms print layered navigation
		if($has_terms) {

			$found = false;
			ob_start();

			// Print the widget wrapper & title
			echo $before_widget;
			echo $before_title . $title . $after_title;

			//Remove param link
			$remove_link = remove_query_arg('filter_'.sanitize_title($instance['attribute']));
			echo "<a class=\"layerd_nav_clear\" href=\"{$remove_link}\">Clear</a>";

			// Open the list
			echo "<ul>";

			foreach ($terms as $term) {

				$_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );

				// Get product count & set flag
				$count = sizeof(array_intersect($_products_in_term, $jigoshop_all_post_ids_in_view ));
				$has_products = (bool) $count;

				if ($has_products) $found = true;

				$class = '';

				$arg = 'filter_'.sanitize_title($instance['attribute']);

				if (isset($_GET[ $arg ])) $current_filter = explode(',', $_GET[ $arg ]); else $current_filter = array();

				if (!is_array($current_filter)) $current_filter = array();

				if (!in_array($term->term_id, $current_filter)) $current_filter[] = $term->term_id;

				// Base Link decided by current page
				if (defined('SHOP_IS_ON_FRONT')) :
					$link = '';
				elseif ( is_shop() ) :
					$link = get_post_type_archive_link('product');
				else :
					$link = get_term_link( get_query_var('term'), get_query_var('taxonomy') );
				endif;

				// All current filters
				if ($_chosen_attributes) foreach ($_chosen_attributes as $name => $value) :
					if ($name!==$taxonomy) :
						$link = add_query_arg( sanitize_title(str_replace('pa_', 'filter_', $name)), implode(',', $value), $link );
					endif;
				endforeach;

				// Min/Max
				if (isset($_GET['min_price'])) :
					$link = add_query_arg( 'min_price', $_GET['min_price'], $link );
				endif;
				if (isset($_GET['max_price'])) :
					$link = add_query_arg( 'max_price', $_GET['max_price'], $link );
				endif;

				// Current Filter = this widget
				if (isset( $_chosen_attributes[$taxonomy] ) && is_array($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy])) :
					$class = 'class="chosen"';
				else :
					$link = add_query_arg( $arg, implode(',', $current_filter), $link );
				endif;

				// Search Arg
				if (get_search_query()) :
					$link = add_query_arg( 's', get_search_query(), $link );
				endif;

				// Post Type Arg
				if (isset($_GET['post_type'])) :
					$link = add_query_arg( 'post_type', $_GET['post_type'], $link );
				endif;

				echo '<li '.$class.'>';

				if ($has_products) echo '<a href="'.esc_url($link).'">'; else echo '<span>';

				echo $term->name;

				if ($has_products) echo '</a>'; else echo '</span>';

				echo ' <small class="count">'.$count.'</small></li>';

			}

			echo "</ul>"; // Close the list

			// Print closing widget wrapper
			echo $after_widget;

			if ( ! $found ) {
				ob_clean(); // clear the buffer
				return false; // display nothing
			} else {
				echo ob_get_clean(); // output the buffer
			}

		}
	}

	/**
	 * Update
	 *
	 * Handles the processing of information entered in the wordpress admin
	 *
	 * @param	array	new instance
	 * @param	array	old instance
	 * @return	array	instance
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		// Save the new values
		$instance['title'] = strip_tags(stripslashes($new_instance['title']));
		$instance['attribute'] = stripslashes($new_instance['attribute']);

		return $instance;
	}

	/**
	 * Form
	 *
	 * Displays the form for the wordpress admin
	 *
	 * @param	array	instance
	 */
	public function form( $instance ) {
		global $wpdb;

		// Get values from instance
		$title = (isset($instance['title'])) ? esc_attr($instance['title']) : null;
		$attr_tax = jigoshop_product::getAttributeTaxonomies();

		// Widget title
		echo '<p>';
		echo '<label for="' . esc_attr( $this->get_field_id('title')  ) . '"> ' . _e('Title:', 'jigoshop') . '</label>';
		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 ) . '" />';
		echo '</p>';

		// Print attribute selector
		if ( ! empty($attr_tax) ) {
			echo '<p>';
			echo '<label for="' . esc_attr( $this->get_field_id('attribute')  ) . '">' . __('Attribute:', 'jigoshop') . '</label> ';
			echo '<select id="' . esc_attr( $this->get_field_id('attribute')  ) . '" name="' . esc_attr( $this->get_field_name('attribute')  ) . '">';
			foreach($attr_tax as $tax) {

				if (taxonomy_exists('pa_'.sanitize_title($tax->attribute_name))) {
					echo '<option value="' . esc_attr( $tax->attribute_name  ) . '" ' . (isset($instance['attribute']) && $instance['attribute'] == $tax->attribute_name ? 'selected' : null) . '>';
					echo $tax->attribute_name;
					echo '</option>';
				}
			}

			echo '</select>';
			echo '</p>';
		}
	}
} // class Jigoshop_Widget_Layered_Nav

function jigoshop_layered_nav_query( $filtered_posts ) {

	global $_chosen_attributes;

	if (sizeof($_chosen_attributes)>0) :

		$matched_products = array();
		$filtered = false;

		foreach ($_chosen_attributes as $attribute => $values) :
			if (sizeof($values)>0) :
				foreach ($values as $value) :

					$posts = get_objects_in_term( $value, $attribute );
					if (!is_wp_error($posts) && (sizeof($matched_products)>0 || $filtered)) :
						$matched_products = array_intersect($posts, $matched_products);
					elseif (!is_wp_error($posts)) :
						$matched_products = $posts;
					endif;

					$filtered = true;

				endforeach;
			endif;
		endforeach;

		if ($filtered) :
			$matched_products[] = 0;
			$filtered_posts = array_intersect($filtered_posts, $matched_products);
		endif;

	endif;

	return $filtered_posts;
}
add_filter( 'loop-shop-posts-in', 'jigoshop_layered_nav_query' );

function jigoshop_layered_nav_init() {

	global $_chosen_attributes;

	$_chosen_attributes = array();

	$attribute_taxonomies = jigoshop_product::getAttributeTaxonomies();
	if ( $attribute_taxonomies ) :
		foreach ($attribute_taxonomies as $tax) :

	    	$attribute = sanitize_title($tax->attribute_name);
	    	$taxonomy = 'pa_' . $attribute;
	    	$name = 'filter_' . $attribute;

	    	if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) $_chosen_attributes[$taxonomy] = explode(',', $_GET[$name] );

	    endforeach;
    endif;

}
add_action( 'init', 'jigoshop_layered_nav_init', 1 );
Back to Top