From 6c4caf33a122e4e3938f0ce2b3af790774541c19 Mon Sep 17 00:00:00 2001 From: Marie Comet Date: Fri, 5 Jul 2024 11:23:22 +0200 Subject: [PATCH] FacetWP Service : remove load_assets filter // Simplify labels --- inc/Services/Facet_WP.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/inc/Services/Facet_WP.php b/inc/Services/Facet_WP.php index 9180e424..7a0771dc 100644 --- a/inc/Services/Facet_WP.php +++ b/inc/Services/Facet_WP.php @@ -10,11 +10,11 @@ public function register( Service_Container $container ): void { } public function boot( Service_Container $container ): void { - add_filter( 'facetwp_load_assets', '__return_true' ); add_filter( 'facetwp_load_a11y', '__return_true' ); add_filter( 'facetwp_facets', [ $this, 'register_facets' ], 40 ); add_filter( 'facetwp_pager_html', [ $this, 'accessible_facetwp_pager_html' ], 10, 2 ); add_filter( 'facetwp_facet_pager_link', [ $this, 'facetwp_facet_pager_link' ], 10, 2 ); + add_filter( 'facetwp_facet_html', [ $this, 'facetwp_add_labels' ], 10, 2 ); } /** @@ -180,4 +180,32 @@ public function facetwp_facet_pager_link( $html, $params ): string { return $html; } + + /** + * Add fake labels to all facets + * 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. + * + * @param string $html + * @param array $args + * + * @return string + */ + public function facetwp_add_labels( string $html, array $args ): string { + $add_label_if_not_empty = [ + 'checkboxes', + 'pager', + 'reset', + ]; + + 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 ) ) { + $label = $args['facet']['label']; + if ( function_exists( 'facetwp_i18n' ) ) { + $label = facetwp_i18n( $label ); + } + + $html = sprintf( '%s', esc_html( $label ), $html ); + } + + return $html; + } }