Skip to content

Commit e6a0b75

Browse files
committed
html from MRW: checkboxes, search button
1 parent d816749 commit e6a0b75

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

a11y-add-on-for-facetwp.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,87 @@ function a11y_addon_add_facet_class( $output, $params ){
6565
return $output;
6666
}
6767

68-
add_filter( 'facetwp_facet_html', 'a11y_addon_add_facet_class', 10, 2);
68+
add_filter( 'facetwp_facet_html', 'a11y_addon_add_facet_class', 20, 2);
69+
70+
/**
71+
* Adjusts markup for specific facets so they use real input elements
72+
*
73+
* @param string $output HTML
74+
* @param array $params FacetWP field parameters
75+
*
76+
* @return string Updated HTML output for a facet
77+
*
78+
* @todo consider whether a combination of totally custom output and str_replace make sense or whether doing something with the WP HTML API might make more sense in the long term
79+
*
80+
* courtesy of Mark Root-Wiley
81+
* @link https://github.com/mrwweb/accessibility-addon-for-facetwp
82+
*/
83+
function a11y_addon_transform_facet_markup( $output, $params ) {
84+
85+
$facet_type = $params['facet']['type'];
86+
87+
switch ($facet_type) {
88+
89+
case 'checkboxes':
90+
// Note: The trick to this working was moving the facetwp-checkbox class and data-value attribute to the `input`. Clicking the label works because the input element still emits a click event when the label is clicked. Leaving that class and attribute on the wrapping list item resulted in two events being fired when the label was clicked.
91+
$output = '';
92+
$output .= '<ul>';
93+
foreach( $params['values'] as $value ) {
94+
if( $value['counter'] > 0 || ! $params['facet']['preserve_ghosts'] === 'no' ) {
95+
$output .= sprintf(
96+
'<li>
97+
<input type="checkbox" id="%3$s"%1$s value="%2$s" class="facetwp-checkbox%1$s" data-value="%2$s">
98+
<label for="%3$s">
99+
<span class="facetwp-display-value">%4$s</span>
100+
<span class="facetwp-counter">(%5$d)</span>
101+
</label>
102+
</li>',
103+
in_array( $value['facet_value'], $params['selected_values'] ) ? ' checked' : '',
104+
esc_attr( $value['facet_value'] ),
105+
'checkbox-' . esc_attr( $value['term_id'] ),
106+
esc_html( $value['facet_display_value'] ),
107+
$value['counter']
108+
);
109+
}
110+
}
111+
$output .= '</ul>';
112+
break;
113+
114+
case 'search':
115+
// use search landmark and insert a real button
116+
$output = '<search>' . $output . '</search>';
117+
// remove the fake button
118+
$output = str_replace( '<i class="facetwp-icon"></i>', '', $output );
119+
// add label to search input
120+
$id = $params['facet']['name'];
121+
$output = str_replace(
122+
'<input', '<div class="trec-facetwp-search-wrapper"><input id="' . esc_attr( $id ) . '"', $output );
123+
// facetwp-icon class is important to retain event handling
124+
$output = str_replace( '</span>', '<button class="facetwp-icon"><span class="ally-addon-search-submit-text">Submit</span></button></div></span>', $output );
125+
// placeholders are bad for UX
126+
$output = str_replace( 'placeholder="Enter keywords"', '', $output );
127+
break;
128+
129+
/*
130+
//do we want this?
131+
case 'pager':
132+
// put links in a list with nav element
133+
$output = str_replace( '<div', '<nav aria-labelledby="resource-paging-heading"><h3 class="screen-reader-text" id="resource-paging-heading">' . esc_html__( 'Results Pages', 'afwp' ) . '</h3><ul', $output );
134+
$output = str_replace( '</div>', '</ul></nav>', $output );
135+
$output = str_replace( '<a', '<li><a', $output );
136+
$output = str_replace( '</a>', '</a></li>', $output );
137+
// add tabindex to valid links only for keyboard accessibility
138+
$output = str_replace( 'data-page', 'tabindex="0" data-page', $output );
139+
break;
140+
*/
141+
142+
}
143+
144+
return $output;
145+
}
146+
147+
add_filter( 'facetwp_facet_html', 'a11y_addon_transform_facet_markup', 10, 2);
148+
69149

70150
/**
71151
* Programatically add labels above filters

0 commit comments

Comments
 (0)