@@ -10,11 +10,11 @@ public function register( Service_Container $container ): void {
1010 }
1111
1212 public function boot ( Service_Container $ container ): void {
13- add_filter ( 'facetwp_load_assets ' , '__return_true ' );
1413 add_filter ( 'facetwp_load_a11y ' , '__return_true ' );
1514 add_filter ( 'facetwp_facets ' , [ $ this , 'register_facets ' ], 40 );
1615 add_filter ( 'facetwp_pager_html ' , [ $ this , 'accessible_facetwp_pager_html ' ], 10 , 2 );
1716 add_filter ( 'facetwp_facet_pager_link ' , [ $ this , 'facetwp_facet_pager_link ' ], 10 , 2 );
17+ add_filter ( 'facetwp_facet_html ' , [ $ this , 'facetwp_add_labels ' ], 10 , 2 );
1818 }
1919
2020 /**
@@ -180,4 +180,32 @@ public function facetwp_facet_pager_link( $html, $params ): string {
180180
181181 return $ html ;
182182 }
183+
184+ /**
185+ * Add fake labels to all facets
186+ * Put in $add_label_if_not_empty the facets types for which you want to display the label *only* if the facet is not empty.
187+ *
188+ * @param string $html
189+ * @param array $args
190+ *
191+ * @return string
192+ */
193+ public function facetwp_add_labels ( string $ html , array $ args ): string {
194+ $ add_label_if_not_empty = [
195+ 'checkboxes ' ,
196+ 'pager ' ,
197+ 'reset ' ,
198+ ];
199+
200+ if ( ( true === in_array ( $ args ['facet ' ]['type ' ], $ add_label_if_not_empty , true ) && ! empty ( $ args ['values ' ] ) ) || false === in_array ( $ args ['facet ' ]['type ' ], $ add_label_if_not_empty , true ) ) {
201+ $ label = $ args ['facet ' ]['label ' ];
202+ if ( function_exists ( 'facetwp_i18n ' ) ) {
203+ $ label = facetwp_i18n ( $ label );
204+ }
205+
206+ $ html = sprintf ( '<p aria-hidden="true" class="facetwp-label">%s</p>%s ' , esc_html ( $ label ), $ html );
207+ }
208+
209+ return $ html ;
210+ }
183211}
0 commit comments