1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LSX_Search_FacetWP_Hierarchy Frontend Main Class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace lsx\search\classes\facetwp; |
7
|
|
|
|
8
|
|
|
class Hierarchy { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Holds class instance |
12
|
|
|
* |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* |
15
|
|
|
* @var object \lsx\search\classes\facetwp\Hierarchy() |
16
|
|
|
*/ |
17
|
|
|
protected static $instance = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor |
21
|
|
|
*/ |
22
|
|
|
public function __construct() { |
|
|
|
|
23
|
|
|
add_filter( 'facetwp_facet_html', array( $this, 'checkbox_facet_html' ), 100, 2 ); |
24
|
|
|
} |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Return an instance of this class. |
28
|
|
|
* |
29
|
|
|
* @since 1.0.0 |
30
|
|
|
* |
31
|
|
|
* @return object \lsx\search\classes\facetwp\Hierarchy() A single instance of this class. |
32
|
|
|
*/ |
33
|
|
|
public static function get_instance() { |
|
|
|
|
34
|
|
|
// If the single instance hasn't been set, set it now. |
35
|
|
|
if ( null === self::$instance ) { |
|
|
|
|
36
|
|
|
self::$instance = new self(); |
37
|
|
|
} |
38
|
|
|
return self::$instance; |
39
|
|
|
} |
|
|
|
|
40
|
|
|
|
41
|
|
|
public function checkbox_facet_html( $output, $params ) { |
|
|
|
|
42
|
|
|
if ( 'checkboxes' === $params['facet']['type'] && 'yes' === $params['facet']['hierarchical'] ) { |
|
|
|
|
43
|
|
|
$output = $this->render_hierarchy( $params ); |
44
|
|
|
} |
45
|
|
|
return $output; |
46
|
|
|
} |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generate the facet HTML (hierarchical taxonomies) |
50
|
|
|
*/ |
51
|
|
|
function render_hierarchy( $params ) { |
|
|
|
|
52
|
|
|
|
53
|
|
|
$output = ''; |
|
|
|
|
54
|
|
|
$facet = $params['facet']; |
|
|
|
|
55
|
|
|
$selected_values = (array) $params['selected_values']; |
56
|
|
|
$values = FWP()->helper->sort_taxonomy_values( $params['values'], $facet['orderby'] ); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$init_depth = -1; |
59
|
|
|
$last_depth = -1; |
60
|
|
|
|
61
|
|
|
foreach ( $values as $result ) { |
|
|
|
|
62
|
|
|
$depth = (int) $result['depth']; |
63
|
|
|
|
64
|
|
|
/*if ( -1 == $last_depth ) { |
|
|
|
|
65
|
|
|
$init_depth = $depth; |
66
|
|
|
} |
67
|
|
|
elseif ( $depth > $last_depth ) { |
68
|
|
|
$output .= '<div class="facetwp-depth">'; |
69
|
|
|
} |
70
|
|
|
elseif ( $depth < $last_depth ) { |
71
|
|
|
for ( $i = $last_depth; $i > $depth; $i-- ) { |
72
|
|
|
$output .= '</div>'; |
73
|
|
|
} |
74
|
|
|
}*/ |
75
|
|
|
|
76
|
|
|
$selected = in_array( $result['facet_value'], $selected_values ) ? ' checked' : ''; |
|
|
|
|
77
|
|
|
$selected .= ( 0 == $result['counter'] && '' == $selected ) ? ' disabled' : ''; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$is_child = ( 0 == $result['parent_id'] && '0' == $result['parent_id'] ) ? ' is-child' : ''; |
|
|
|
|
80
|
|
|
$depth_css = ' depth-' . $result['depth']; |
81
|
|
|
|
82
|
|
|
$output .= '<div class="facetwp-checkbox' . $selected . $is_child . $depth_css . '" data-parent-id="' . esc_attr( $result['parent_id'] ) . '" data-value="' . esc_attr( $result['facet_value'] ) . '">'; |
83
|
|
|
$output .= esc_html( $result['facet_display_value'] ) . ' <span class="facetwp-counter">(' . $result['counter'] . ')</span>'; |
84
|
|
|
$output .= '</div>'; |
85
|
|
|
|
86
|
|
|
$last_depth = $depth; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
for ( $i = $last_depth; $i > $init_depth; $i-- ) { |
|
|
|
|
90
|
|
|
$output .= '</div>'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $output; |
94
|
|
|
} |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|