-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-combobox.php
99 lines (75 loc) · 3.15 KB
/
class-combobox.php
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
<?php
class FacetWP_Facet_Combobox extends FacetWP_Facet
{
function __construct() {
$this->label = __( 'Combobox', 'fwp' );
$this->fields = [ 'label_any', 'parent_term', 'modifiers', 'hierarchical', 'multiple',
'ghosts', 'operator', 'orderby', 'count' ];
if ( !is_admin() ) {
// Enqueue Semantic UI CSS and JavaScript files
wp_enqueue_style( 'semantic-ui-css', 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.5.0/semantic.min.css', array(), '2.5.0' );
wp_enqueue_script( 'semantic-ui-js', 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.5.0/semantic.min.js', array('jquery'), '2.5.0', true );
}
}
/**
* Load the available choices
*/
function load_values( $params ) {
$values = FWP()->helper->facet_types['checkboxes']->load_values( $params );
// Remove empty options
$values = array_filter($values, function($value) {
return !empty($value['facet_display_value']);
});
return $values;
}
/**
* Generate the facet HTML
*/
function render( $params ) {
$output = '';
$facet = $params['facet'];
$values = (array) $params['values'];
$selected_values = (array) $params['selected_values'];
$is_hierarchical = FWP()->helper->facet_is( $facet, 'hierarchical', 'yes' );
$multiple = FWP()->helper->facet_is( $facet, 'multiple', 'yes' ) ? 'multiple' : '';
$search = FWP()->helper->facet_is( $facet, 'multiple', 'yes' ) ? 'multiple search selection' : 'search selection';
$class = 'ui fluid ' . $search . ' dropdown';
if ( $is_hierarchical ) {
$class .= ' hierarchical';
}
$output .= '<div class="' . $class . '">';
$output .= '<input class="facetwp-combobox" type="hidden" name="' . $facet['name'] . '" value="' . esc_attr(implode(',', $selected_values)) . '">';
$output .= '<i class="dropdown icon"></i>';
$output .= '<div class="default text">' . esc_html( $facet['label_any'] ) . '</div>';
$output .= '<div class="menu">';
foreach ( $values as $row ) {
$selected = in_array( $row['facet_value'], $selected_values ) ? ' selected' : '';
$indent = $is_hierarchical ? str_repeat( ' ', (int) $row['depth'] ) : '';
$label = esc_html( $row['facet_display_value'] );
$label = apply_filters( 'facetwp_facet_display_value', $label, [
'selected' => ( '' !== $selected ),
'facet' => $facet,
'row' => $row
]);
$show_counts = apply_filters( 'facetwp_facet_dropdown_show_counts', true, [ 'facet' => $facet ] );
if ( $show_counts ) {
$label .= ' (' . $row['counter'] . ')';
}
$output .= '<div class="item" data-value="' . esc_attr( $row['facet_value'] ) . '" '.$selected.'>' . $indent . $label . '</div>';
}
$output .= '</div></div>';
return $output;
}
/**
* Load the necessary front-end script(s) for handling user interactions
*/
function front_scripts() {
FWP()->display->assets['combobox-front.js'] = plugins_url( '', __FILE__ ) . '/assets/js/front.js';
}
/**
* Filter the query based on selected values
*/
function filter_posts( $params ) {
return FWP()->helper->facet_types['checkboxes']->filter_posts( $params );
}
}