Geen omschrijving

class-wc-widget-layered-nav.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * Layered nav widget
  4. *
  5. * @package WooCommerce\Widgets
  6. * @version 2.6.0
  7. */
  8. use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Widget layered nav class.
  12. */
  13. class WC_Widget_Layered_Nav extends WC_Widget {
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct() {
  18. $this->widget_cssclass = 'woocommerce widget_layered_nav woocommerce-widget-layered-nav';
  19. $this->widget_description = __( 'Display a list of attributes to filter products in your store.', 'woocommerce' );
  20. $this->widget_id = 'woocommerce_layered_nav';
  21. $this->widget_name = __( 'Filter Products by Attribute', 'woocommerce' );
  22. parent::__construct();
  23. }
  24. /**
  25. * Updates a particular instance of a widget.
  26. *
  27. * @see WP_Widget->update
  28. *
  29. * @param array $new_instance New Instance.
  30. * @param array $old_instance Old Instance.
  31. *
  32. * @return array
  33. */
  34. public function update( $new_instance, $old_instance ) {
  35. $this->init_settings();
  36. return parent::update( $new_instance, $old_instance );
  37. }
  38. /**
  39. * Outputs the settings update form.
  40. *
  41. * @see WP_Widget->form
  42. *
  43. * @param array $instance Instance.
  44. */
  45. public function form( $instance ) {
  46. $this->init_settings();
  47. parent::form( $instance );
  48. }
  49. /**
  50. * Init settings after post types are registered.
  51. */
  52. public function init_settings() {
  53. $attribute_array = array();
  54. $std_attribute = '';
  55. $attribute_taxonomies = wc_get_attribute_taxonomies();
  56. if ( ! empty( $attribute_taxonomies ) ) {
  57. foreach ( $attribute_taxonomies as $tax ) {
  58. if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
  59. $attribute_array[ $tax->attribute_name ] = $tax->attribute_name;
  60. }
  61. }
  62. $std_attribute = current( $attribute_array );
  63. }
  64. $this->settings = array(
  65. 'title' => array(
  66. 'type' => 'text',
  67. 'std' => __( 'Filter by', 'woocommerce' ),
  68. 'label' => __( 'Title', 'woocommerce' ),
  69. ),
  70. 'attribute' => array(
  71. 'type' => 'select',
  72. 'std' => $std_attribute,
  73. 'label' => __( 'Attribute', 'woocommerce' ),
  74. 'options' => $attribute_array,
  75. ),
  76. 'display_type' => array(
  77. 'type' => 'select',
  78. 'std' => 'list',
  79. 'label' => __( 'Display type', 'woocommerce' ),
  80. 'options' => array(
  81. 'list' => __( 'List', 'woocommerce' ),
  82. 'dropdown' => __( 'Dropdown', 'woocommerce' ),
  83. ),
  84. ),
  85. 'query_type' => array(
  86. 'type' => 'select',
  87. 'std' => 'and',
  88. 'label' => __( 'Query type', 'woocommerce' ),
  89. 'options' => array(
  90. 'and' => __( 'AND', 'woocommerce' ),
  91. 'or' => __( 'OR', 'woocommerce' ),
  92. ),
  93. ),
  94. );
  95. }
  96. /**
  97. * Get this widgets taxonomy.
  98. *
  99. * @param array $instance Array of instance options.
  100. * @return string
  101. */
  102. protected function get_instance_taxonomy( $instance ) {
  103. if ( isset( $instance['attribute'] ) ) {
  104. return wc_attribute_taxonomy_name( $instance['attribute'] );
  105. }
  106. $attribute_taxonomies = wc_get_attribute_taxonomies();
  107. if ( ! empty( $attribute_taxonomies ) ) {
  108. foreach ( $attribute_taxonomies as $tax ) {
  109. if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
  110. return wc_attribute_taxonomy_name( $tax->attribute_name );
  111. }
  112. }
  113. }
  114. return '';
  115. }
  116. /**
  117. * Get this widgets query type.
  118. *
  119. * @param array $instance Array of instance options.
  120. * @return string
  121. */
  122. protected function get_instance_query_type( $instance ) {
  123. return isset( $instance['query_type'] ) ? $instance['query_type'] : 'and';
  124. }
  125. /**
  126. * Get this widgets display type.
  127. *
  128. * @param array $instance Array of instance options.
  129. * @return string
  130. */
  131. protected function get_instance_display_type( $instance ) {
  132. return isset( $instance['display_type'] ) ? $instance['display_type'] : 'list';
  133. }
  134. /**
  135. * Output widget.
  136. *
  137. * @see WP_Widget
  138. *
  139. * @param array $args Arguments.
  140. * @param array $instance Instance.
  141. */
  142. public function widget( $args, $instance ) {
  143. if ( ! is_shop() && ! is_product_taxonomy() ) {
  144. return;
  145. }
  146. $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
  147. $taxonomy = $this->get_instance_taxonomy( $instance );
  148. $query_type = $this->get_instance_query_type( $instance );
  149. $display_type = $this->get_instance_display_type( $instance );
  150. if ( ! taxonomy_exists( $taxonomy ) ) {
  151. return;
  152. }
  153. $terms = get_terms( $taxonomy, array( 'hide_empty' => '1' ) );
  154. if ( 0 === count( $terms ) ) {
  155. return;
  156. }
  157. ob_start();
  158. $this->widget_start( $args, $instance );
  159. if ( 'dropdown' === $display_type ) {
  160. wp_enqueue_script( 'selectWoo' );
  161. wp_enqueue_style( 'select2' );
  162. $found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type );
  163. } else {
  164. $found = $this->layered_nav_list( $terms, $taxonomy, $query_type );
  165. }
  166. $this->widget_end( $args );
  167. // Force found when option is selected - do not force found on taxonomy attributes.
  168. if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
  169. $found = true;
  170. }
  171. if ( ! $found ) {
  172. ob_end_clean();
  173. } else {
  174. echo ob_get_clean(); // @codingStandardsIgnoreLine
  175. }
  176. }
  177. /**
  178. * Return the currently viewed taxonomy name.
  179. *
  180. * @return string
  181. */
  182. protected function get_current_taxonomy() {
  183. return is_tax() ? get_queried_object()->taxonomy : '';
  184. }
  185. /**
  186. * Return the currently viewed term ID.
  187. *
  188. * @return int
  189. */
  190. protected function get_current_term_id() {
  191. return absint( is_tax() ? get_queried_object()->term_id : 0 );
  192. }
  193. /**
  194. * Return the currently viewed term slug.
  195. *
  196. * @return int
  197. */
  198. protected function get_current_term_slug() {
  199. return absint( is_tax() ? get_queried_object()->slug : 0 );
  200. }
  201. /**
  202. * Show dropdown layered nav.
  203. *
  204. * @param array $terms Terms.
  205. * @param string $taxonomy Taxonomy.
  206. * @param string $query_type Query Type.
  207. * @return bool Will nav display?
  208. */
  209. protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
  210. global $wp;
  211. $found = false;
  212. if ( $taxonomy !== $this->get_current_taxonomy() ) {
  213. $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
  214. $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
  215. $taxonomy_filter_name = wc_attribute_taxonomy_slug( $taxonomy );
  216. $taxonomy_label = wc_attribute_label( $taxonomy );
  217. /* translators: %s: taxonomy name */
  218. $any_label = apply_filters( 'woocommerce_layered_nav_any_label', sprintf( __( 'Any %s', 'woocommerce' ), $taxonomy_label ), $taxonomy_label, $taxonomy );
  219. $multiple = 'or' === $query_type;
  220. $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
  221. if ( '' === get_option( 'permalink_structure' ) ) {
  222. $form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
  223. } else {
  224. $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( user_trailingslashit( $wp->request ) ) );
  225. }
  226. echo '<form method="get" action="' . esc_url( $form_action ) . '" class="woocommerce-widget-layered-nav-dropdown">';
  227. echo '<select class="woocommerce-widget-layered-nav-dropdown dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '"' . ( $multiple ? 'multiple="multiple"' : '' ) . '>';
  228. echo '<option value="">' . esc_html( $any_label ) . '</option>';
  229. foreach ( $terms as $term ) {
  230. // If on a term page, skip that term in widget list.
  231. if ( $term->term_id === $this->get_current_term_id() ) {
  232. continue;
  233. }
  234. // Get count based on current view.
  235. $option_is_set = in_array( $term->slug, $current_values, true );
  236. $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
  237. // Only show options with count > 0.
  238. if ( 0 < $count ) {
  239. $found = true;
  240. } elseif ( 0 === $count && ! $option_is_set ) {
  241. continue;
  242. }
  243. echo '<option value="' . esc_attr( urldecode( $term->slug ) ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
  244. }
  245. echo '</select>';
  246. if ( $multiple ) {
  247. echo '<button class="woocommerce-widget-layered-nav-dropdown__submit" type="submit" value="' . esc_attr__( 'Apply', 'woocommerce' ) . '">' . esc_html__( 'Apply', 'woocommerce' ) . '</button>';
  248. }
  249. if ( 'or' === $query_type ) {
  250. echo '<input type="hidden" name="query_type_' . esc_attr( $taxonomy_filter_name ) . '" value="or" />';
  251. }
  252. echo '<input type="hidden" name="filter_' . esc_attr( $taxonomy_filter_name ) . '" value="' . esc_attr( implode( ',', $current_values ) ) . '" />';
  253. echo wc_query_string_form_fields( null, array( 'filter_' . $taxonomy_filter_name, 'query_type_' . $taxonomy_filter_name ), '', true ); // @codingStandardsIgnoreLine
  254. echo '</form>';
  255. wc_enqueue_js(
  256. "
  257. // Update value on change.
  258. jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).on( 'change', function() {
  259. var slug = jQuery( this ).val();
  260. jQuery( ':input[name=\"filter_" . esc_js( $taxonomy_filter_name ) . "\"]' ).val( slug );
  261. // Submit form on change if standard dropdown.
  262. if ( ! jQuery( this ).attr( 'multiple' ) ) {
  263. jQuery( this ).closest( 'form' ).trigger( 'submit' );
  264. }
  265. });
  266. // Use Select2 enhancement if possible
  267. if ( jQuery().selectWoo ) {
  268. var wc_layered_nav_select = function() {
  269. jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).selectWoo( {
  270. placeholder: decodeURIComponent('" . rawurlencode( (string) wp_specialchars_decode( $any_label ) ) . "'),
  271. minimumResultsForSearch: 5,
  272. width: '100%',
  273. allowClear: " . ( $multiple ? 'false' : 'true' ) . ",
  274. language: {
  275. noResults: function() {
  276. return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "';
  277. }
  278. }
  279. } );
  280. };
  281. wc_layered_nav_select();
  282. }
  283. "
  284. );
  285. }
  286. return $found;
  287. }
  288. /**
  289. * Count products within certain terms, taking the main WP query into consideration.
  290. *
  291. * This query allows counts to be generated based on the viewed products, not all products.
  292. *
  293. * @param array $term_ids Term IDs.
  294. * @param string $taxonomy Taxonomy.
  295. * @param string $query_type Query Type.
  296. * @return array
  297. */
  298. protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) {
  299. return wc_get_container()->get( Filterer::class )->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type );
  300. }
  301. /**
  302. * Wrapper for WC_Query::get_main_tax_query() to ease unit testing.
  303. *
  304. * @since 4.4.0
  305. * @return array
  306. */
  307. protected function get_main_tax_query() {
  308. return WC_Query::get_main_tax_query();
  309. }
  310. /**
  311. * Wrapper for WC_Query::get_main_search_query_sql() to ease unit testing.
  312. *
  313. * @since 4.4.0
  314. * @return string
  315. */
  316. protected function get_main_search_query_sql() {
  317. return WC_Query::get_main_search_query_sql();
  318. }
  319. /**
  320. * Wrapper for WC_Query::get_main_search_queryget_main_meta_query to ease unit testing.
  321. *
  322. * @since 4.4.0
  323. * @return array
  324. */
  325. protected function get_main_meta_query() {
  326. return WC_Query::get_main_meta_query();
  327. }
  328. /**
  329. * Show list based layered nav.
  330. *
  331. * @param array $terms Terms.
  332. * @param string $taxonomy Taxonomy.
  333. * @param string $query_type Query Type.
  334. * @return bool Will nav display?
  335. */
  336. protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
  337. // List display.
  338. echo '<ul class="woocommerce-widget-layered-nav-list">';
  339. $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
  340. $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
  341. $found = false;
  342. $base_link = $this->get_current_page_url();
  343. foreach ( $terms as $term ) {
  344. $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
  345. $option_is_set = in_array( $term->slug, $current_values, true );
  346. $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
  347. // Skip the term for the current archive.
  348. if ( $this->get_current_term_id() === $term->term_id ) {
  349. continue;
  350. }
  351. // Only show options with count > 0.
  352. if ( 0 < $count ) {
  353. $found = true;
  354. } elseif ( 0 === $count && ! $option_is_set ) {
  355. continue;
  356. }
  357. $filter_name = 'filter_' . wc_attribute_taxonomy_slug( $taxonomy );
  358. // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  359. $current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array();
  360. $current_filter = array_map( 'sanitize_title', $current_filter );
  361. if ( ! in_array( $term->slug, $current_filter, true ) ) {
  362. $current_filter[] = $term->slug;
  363. }
  364. $link = remove_query_arg( $filter_name, $base_link );
  365. // Add current filters to URL.
  366. foreach ( $current_filter as $key => $value ) {
  367. // Exclude query arg for current term archive term.
  368. if ( $value === $this->get_current_term_slug() ) {
  369. unset( $current_filter[ $key ] );
  370. }
  371. // Exclude self so filter can be unset on click.
  372. if ( $option_is_set && $value === $term->slug ) {
  373. unset( $current_filter[ $key ] );
  374. }
  375. }
  376. if ( ! empty( $current_filter ) ) {
  377. asort( $current_filter );
  378. $link = add_query_arg( $filter_name, implode( ',', $current_filter ), $link );
  379. // Add Query type Arg to URL.
  380. if ( 'or' === $query_type && ! ( 1 === count( $current_filter ) && $option_is_set ) ) {
  381. $link = add_query_arg( 'query_type_' . wc_attribute_taxonomy_slug( $taxonomy ), 'or', $link );
  382. }
  383. $link = str_replace( '%2C', ',', $link );
  384. }
  385. if ( $count > 0 || $option_is_set ) {
  386. $link = apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy );
  387. $term_html = '<a rel="nofollow" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a>';
  388. } else {
  389. $link = false;
  390. $term_html = '<span>' . esc_html( $term->name ) . '</span>';
  391. }
  392. $term_html .= ' ' . apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term );
  393. echo '<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term ' . ( $option_is_set ? 'woocommerce-widget-layered-nav-list__item--chosen chosen' : '' ) . '">';
  394. // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.EscapeOutput.OutputNotEscaped
  395. echo apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count );
  396. echo '</li>';
  397. }
  398. echo '</ul>';
  399. return $found;
  400. }
  401. }